aboutsummaryrefslogtreecommitdiff
path: root/src/Rendering/Renderer.h
blob: d6c41542da412ef759192f34e7ff94897d5d9f1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#pragma once

#include "Core/Memory.h"
#include "VertexArray.h"
#include "Shader.h"

#include <glm/glm.hpp>

namespace Donut
{
    class RendererAPI 
    {
    public:
        enum class API 
        {
            None   = 0, 
            OpenGL = 1,
            Vulkan = 2,
        };
    public:
        virtual ~RendererAPI() = default;

        virtual void Init()                                               = 0;
        virtual void SetViewport(uint32_t x, uint32_t y, 
                                 uint32_t width, uint32_t height)         = 0;
        virtual void SetClearColor(const glm::vec4& color)                = 0;
        virtual void Clear()                                              = 0;
        virtual void EnableDepthTest()                                    = 0;
        virtual void DisableDepthTest()                                   = 0;
        virtual void SetFaceCulling(bool enabled)                         = 0;
        virtual void EnableBlending()                                     = 0;
        virtual void DisableBlending()                                    = 0;

        virtual void DrawIndexed(const Ref<VertexArray>& vertexArray, 
                                 uint32_t indexCount = 0)                 = 0;
        
        virtual void DrawArrays(uint32_t vertexCount, uint32_t first = 0) = 0;
        virtual void DrawLines(const Ref<VertexArray>& vertexArray, 
                               uint32_t indexCount = 0)                   = 0;
        virtual void BindTexture(uint32_t textureID, uint32_t slot = 0)   = 0;
        virtual void BindImageTexture(uint32_t textureID, 
                                      uint32_t slot = 0, 
                                      bool readOnly = false)              = 0;

        inline static API GetAPI()         { return s_API; }
        inline static void SetAPI(API api) { s_API = api;  }
        static Scope<RendererAPI> Create();
    private:
        static API s_API;
    };

    class RenderCommand 
    {
    public:
        inline static void Init() 
        {
            s_RendererAPI->Init();
        }

        inline static void SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height)
        {
            s_RendererAPI->SetViewport(x, y, width, height);
        }

        inline static void SetClearColor(const glm::vec4& color)
        {
            s_RendererAPI->SetClearColor(color);
        }

        inline static void Clear()
        {
            s_RendererAPI->Clear();
        }

        inline static void EnableDepthTest()
        {
            s_RendererAPI->EnableDepthTest();
        }

        inline static void DisableDepthTest()
        {
            s_RendererAPI->DisableDepthTest();
        }

        inline static void SetFaceCulling(bool enabled)
        {
            s_RendererAPI->SetFaceCulling(enabled);
        }

        inline static void EnableBlending()
        {
            s_RendererAPI->EnableBlending();
        }

        inline static void DisableBlending()
        {
            s_RendererAPI->DisableBlending();
        }

        inline static void DrawIndexed(const Ref<VertexArray>& vertexArray, uint32_t indexCount = 0)
        {
            s_RendererAPI->DrawIndexed(vertexArray, indexCount);
        }

        inline static void DrawArrays(uint32_t vertexCount, uint32_t first = 0)
        {
            s_RendererAPI->DrawArrays(vertexCount, first);
        }

        inline static void DrawLines(const Ref<VertexArray>& vertexArray, uint32_t indexCount = 0)
        {
            s_RendererAPI->DrawLines(vertexArray, indexCount);
        }

        inline static void BindTexture(uint32_t textureID, uint32_t slot = 0)
        {
            s_RendererAPI->BindTexture(textureID, slot);
        }

        inline static void BindImageTexture(uint32_t textureID, uint32_t slot = 0, bool readOnly = false)
        {
            s_RendererAPI->BindImageTexture(textureID, slot, readOnly);
        }

    private:
        static Scope<RendererAPI> s_RendererAPI;
    };

    class Renderer 
    {
    public:
        static void Init();
        static void Shutdown();

        static void OnWindowResize(uint32_t width, uint32_t height);

        static void Submit(const Ref<Shader>& shader, 
                           const Ref<VertexArray>& vertexArray,
                           const glm::mat4& transform,
                           const glm::mat4& viewProjection);

        static void SetClearColor(const glm::vec4& color);
        static void Clear();

        inline static RendererAPI::API GetAPI() { return RendererAPI::GetAPI(); }
    };
};