-
Notifications
You must be signed in to change notification settings - Fork 0
/
Device.h
257 lines (168 loc) · 6.21 KB
/
Device.h
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//
// Created by Marrony Neris on 11/7/15.
//
#ifndef DEVICE_H_H
#define DEVICE_H_H
#include <GL/gl3w.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void check_error(const char* file, int line);
#define CHECK_ERROR check_error(__FILE__, __LINE__)
struct Sampler {
GLuint id;
};
struct Texture2D {
GLuint id;
};
struct TextureCube {
GLuint id;
};
union DepthTexture {
GLuint id;
Texture2D texture;
};
union DepthStencilTexture {
GLuint id;
Texture2D texture;
};
struct VertexBuffer {
GLuint id;
};
struct IndexBuffer {
GLuint id;
};
struct ConstantBuffer {
GLuint id;
};
struct VertexArray {
GLuint id;
};
struct Program {
GLuint id;
};
struct SeparateProgram {
GLuint id;
};
struct Framebuffer {
GLuint id;
};
struct Renderbuffer {
GLuint id;
};
struct Rect {
float x;
float y;
float width;
float height;
};
struct VertexFormat {
uint32_t size : 16;
uint32_t type : 16;
};
const VertexFormat VertexFloat1 = {1, GL_FLOAT};
const VertexFormat VertexFloat2 = {2, GL_FLOAT};
const VertexFormat VertexFloat3 = {3, GL_FLOAT};
const VertexFormat VertexFloat4 = {4, GL_FLOAT};
const int POSITIVE_X = 0;
const int NEGATIVE_X = 1;
const int POSITIVE_Y = 2;
const int NEGATIVE_Y = 3;
const int POSITIVE_Z = 4;
const int NEGATIVE_Z = 5;
struct VertexDeclaration {
VertexFormat format;
uint32_t stride;
void* offset;
VertexBuffer buffer;
};
struct Image {
int width;
int height;
int format;
uint8_t* pixels;
};
struct ImageCube {
Image faces[6];
};
class Device {
public:
Device();
~Device();
VertexBuffer createDynamicVertexBuffer(size_t size, const void* data);
VertexBuffer createStaticVertexBuffer(size_t size, const void* data);
IndexBuffer createIndexBuffer(size_t size, const void* data);
ConstantBuffer createConstantBuffer(size_t size);
void setConstantBufferBindingPoint(Program program, const char* blockName, int bindingPoint);
void setTextureBindingPoint(Program program, const char* name, int bindingPoint);
VertexArray createVertexArray(const VertexDeclaration* vertexDeclarations, int size, IndexBuffer indexBuffer);
Sampler createSampler(int minFilter, int magFilter, int mipFilter = GL_NONE);
Texture2D createRGB16FTexture(int width, int height, const void* pixels);
Texture2D createRGBA16FTexture(int width, int height, const void* pixels);
Texture2D createRGB32FTexture(int width, int height, const void* pixels);
Texture2D createRGBA32FTexture(int width, int height, const void* pixels);
Texture2D createRGBATexture(int width, int height, const void* pixels);
Texture2D createRGBTexture(int width, int height, const void* pixels);
Texture2D createRGBAFTexture(int width, int height, const void* pixels);
Texture2D createRGBFTexture(int width, int height, const void* pixels);
Texture2D createRTexture(int width, int height, const void* pixels);
Texture2D createRG32FTexture(int width, int height, const void* pixels);
TextureCube createRGBCubeTexture(const ImageCube cube[], int mipLevels);
TextureCube createRGBCubeTexture(const ImageCube& cube);
DepthTexture createDepth32FTexture(int width, int height);
DepthStencilTexture createDepth24Stencil8Texture(int width, int height);
SeparateProgram createVertexProgram(const char* commonSource, const char* source);
SeparateProgram createFragmentProgram(const char* commonSource, const char* source);
Program createProgram(const char* commonSource, const char* vertexSource, const char* fragmentSource, const char* geometrySource = nullptr);
Framebuffer createFramebuffer();
Renderbuffer createRenderbuffer(int width, int height);
//////////////////////////////////////////////////
void destroyTexture(Texture2D texture);
void destroyTexture(TextureCube texture);
void destroyTexture(DepthStencilTexture texture);
void destroySampler(Sampler sampler);
void destroyVertexBuffer(VertexBuffer vertexBuffer);
void destroyIndexBuffer(IndexBuffer indexBuffer);
void destroyConstantBuffer(ConstantBuffer constantBuffer);
void destroyVertexArray(VertexArray vertexArray);
void destroyProgram(Program program);
void destroyRenderbuffer(Renderbuffer renderbuffer);
void destroyFramebuffer(Framebuffer framebuffer);
//////////////////////////////////////////////////
void bindTextureToFramebuffer(Framebuffer framebuffer, Texture2D texture, int index);
void bindDepthTextureToFramebuffer(Framebuffer framebuffer, DepthTexture texture);
void bindDepthStencilTextureToFramebuffer(Framebuffer framebuffer, DepthStencilTexture texture);
void bindRenderbufferToFramebuffer(Framebuffer framebuffer, Renderbuffer renderbuffer);
void bindFramebuffer(Framebuffer framebuffer);
void bindReadFramebuffer(Framebuffer framebuffer);
void bindDrawFramebuffer(Framebuffer framebuffer);
void setRenderTarget(Framebuffer framebuffer, int targets[], int count);
bool isFramebufferComplete(Framebuffer framebuffer);
void bindVertexArray(VertexArray vertexArray);
void bindProgram(Program program);
void copyConstantBuffer(ConstantBuffer constantBuffer, const void* data, size_t size);
void bindTexture(Texture2D texture, int unit);
void bindTexture(TextureCube texture, int unit);
//TODO remove this method
void setValue(Program program, const char* name, float x, float y, float z);
void bindSampler(Sampler sampler, int unit);
void drawTriangles(int offset, int count);
void drawTrianglesInstanced(int offset, int count, int instance);
void drawArrays(int type, int first, int count);
void drawArraysInstanced(int type, int first, int count, int instance);
void updateVertexBuffer(VertexBuffer vertexBuffer, size_t offset, size_t size, const void* data);
private:
uint32_t vertexBufferCount;
uint32_t indexBufferCount;
uint32_t constantBufferCount;
uint32_t vertexArrayCount;
uint32_t textureCount;
uint32_t samplerCount;
uint32_t programCount;
uint32_t vertexProgramCount;
uint32_t fragmentProgramCount;
uint32_t framebufferCount;
uint32_t renderbufferCount;
};
#endif //DEVICE_H_H