-
Notifications
You must be signed in to change notification settings - Fork 0
/
gbuffer.h
429 lines (380 loc) · 10.1 KB
/
gbuffer.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#ifndef GBUFFER_H
#define GBUFFER_H
#include "glog.h"
#include <vector>
#include <tuple>
#include <functional>
#include "ggraphiclibdefine.h"
#include "gmath.h"
#include "tgaimage.h"
class GRenderBuffer;
class GColorBuffer;
class GDepthStencilBuffer;
struct GColor
{
GColor() = default;
GColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
this->r = r;
this->g = g;
this->b = b;
this->a = a;
}
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
bool IsBlack()
{
return (r==0 && g==0 && b==0);
}
static GColor white;
static GColor black;
static GColor red;
static GColor green;
static GColor blue;
static GColor gray;
static GColor pink;
static GColor normal;
static GMath::vec4 FastTonemap(GMath::vec4 color)
{
GMath::vec4 ret;
ret.SetX((color[0] * 1.0/(color[0]+1.0)));
ret.SetY((color[1] * 1.0/(color[1]+1.0)));
ret.SetZ((color[2] * 1.0/(color[2]+1.0)));
ret.SetW(std::max(0.0, std::min(color[3], 1.0)));
return ret;
}
static GMath::vec4f ToFloat01Color(const GColor& color)
{
GMath::vec4f ret;
ret.SetX(((float)color.r)/255.0);
ret.SetY(((float)color.g)/255.0);
ret.SetZ(((float)color.b)/255.0);
ret.SetW(((float)color.a)/255.0);
return ret;
}
static GMath::vec4f ToFloatColor(const GColor& color)
{
GMath::vec4f ret;
ret.SetX((float)color.r);
ret.SetY((float)color.g);
ret.SetZ((float)color.b);
ret.SetW((float)color.a);
return ret;
}
static GColor FromFloat01Color(GMath::vec4 color)
{
GColor ret;
ret.r = (unsigned char)(color[0] * 255);
ret.g = (unsigned char)(color[1] * 255);
ret.b = (unsigned char)(color[2] * 255);
ret.a = (unsigned char)(color[3] * 255);
return ret;
}
static GColor FromFloatColor(GMath::vec4 color)
{
GColor ret;
ret.r = (unsigned char)(color[0]);
ret.g = (unsigned char)(color[1]);
ret.b = (unsigned char)(color[2]);
ret.a = (unsigned char)(color[3]);
return ret;
}
static GColor TgaColor(TGAColor tgaColor)
{
GColor color;
color.r = tgaColor.bgra[2];
color.g = tgaColor.bgra[1];
color.b = tgaColor.bgra[0];
color.a = tgaColor.bgra[3];
return color;
}
static GColor Lerp(TGAColor color1, TGAColor color2, float f);
static GColor Lerp(GColor color1, GColor color2, float f);
GMath::vec4f ToFloat01Color()
{
return ToFloat01Color(*this);
}
GMath::vec4f ToFloatColor()
{
return ToFloatColor(*this);
}
};
struct GHDRColor
{
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
float intensity;
static GMath::vec4f ToFloatColor(const GHDRColor& hdrColor)
{
GMath::vec4f ret;
float factor = std::pow(2, hdrColor.intensity);
ret.SetX(factor * float(hdrColor.r)/255);
ret.SetY(factor * float(hdrColor.g)/255);
ret.SetY(factor * float(hdrColor.b)/255);
ret.SetW(float(hdrColor.a)/255);
return ret;
}
GMath::vec4f ToFloatColor()
{
return ToFloatColor(*this);
}
};
class GRenderBuffer
{
public:
virtual ~GRenderBuffer(){}
bool CheckRangeValid(int x, int y)
{
if(data == nullptr)
{
GLog::LogError("buffer is nullptr! buffer type = ",typeid(*this).name());
return false;
}
if(x<0 || y<0 || x>=width || y>=height)
{
GLog::LogError("width = ", width, " height = ", height, "x = ", x, " y = ", y);
return false;
}
return true;
}
int width;
int height;
protected:
void* data;
};
class GFrameBuffer
{
public:
GFrameBuffer() {}
static const int MAX_COLORBUFF_COUNT = GRenderBufferType::kRBMax;
static bool CheckAttachIndexValid(int index);
std::vector<GColorBuffer*> drawRenderBuffers;
void AttachRenderBuffer(GColorBuffer* colorbuffer, int index);
void AttachRenderBuffer(GDepthStencilBuffer* depthStencilBuffer, bool isDepth=true);
void ClearRenderBuffer(int index, GColor clearColor);
void ClearDepthBuffer(float clearDepthValue);
void ClearStencilBuffer(int clearValue);
void DrawRenderBuffer(std::initializer_list<GRenderBufferType> renderBufferTypes);
std::vector<GRenderBufferType> drawRenderBufferTypes;
GColorBuffer* GetRenderBufer(GRenderBufferType renderBufferType);
const std::vector<GColorBuffer*>& GetDrawRenderBuffer();
int GetDrawRenderBufferCount();
GMath::vec2i GetSize();
GRenderBufferType GetRenderBufferType(GRenderBuffer *renderBuffer);
bool IsBlendEnabled(GRenderBufferType renderBufferType);
bool IsBlendEnabled(GRenderBuffer* renderBuffer);
void SetEnableBlend(GRenderBufferType renderBufferType, bool enable=true);
bool IsScissorTestEnabled(GRenderBufferType renderBufferType);
bool IsScissorTestEnabled(GRenderBuffer* renderBuffer);
void SetEnableScissorTest(GRenderBufferType renderBufferType, bool enable=true);
GDepthStencilBuffer* depthBuffer;
GDepthStencilBuffer* stencilBuffer;
private:
bool CheckRenderBufferSizeValid();
GColorBuffer* colorBuffer[MAX_COLORBUFF_COUNT];
std::vector<std::tuple<bool, GRenderBufferType>> enableBlend;
std::vector<std::tuple<bool, GRenderBufferType>> enableScissorTest;
};
class GColorBuffer : public GRenderBuffer
{
public:
GColorBuffer(int w, int h)
{
GLog::LogInfo("Create Color Buffer w = ",w," h = ", h);
data = nullptr;
if(w>0 && h>0)
{
width = w;
height = h;
data = new GColor[w*h];
}
else
{
GLog::LogError("w = ", w, " h = ", h);
}
}
virtual ~GColorBuffer()
{
if(data!=nullptr)
{
delete colorData();
}
data = nullptr;
}
void Clear(GColor color)
{
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
SetColor(i, j, color);
}
}
void SetColor(int x, int y, GColor color)
{
if(!CheckRangeValid(x,y)) return;
colorData()[y*width+x] = color;
}
GColor GetColor(int x, int y)
{
if(!CheckRangeValid(x,y)) return GColor::black;
return colorData()[y*width+x];
}
unsigned char* GetData()
{
return (unsigned char*)data;
}
private:
GColor* colorData()
{
return (GColor*)data;
}
};
class GDepthStencilBuffer : public GRenderBuffer
{
public:
GDepthStencilBuffer(int w, int h)
{
data = nullptr;
if(w>0 && h>0)
{
width = w;
height = h;
data = new int[w*h];
}
else
{
GLog::LogError("w = ", w, " h = ", h);
}
}
virtual ~GDepthStencilBuffer()
{
if(data!=nullptr)
{
delete depthStencilData();
}
data = nullptr;
}
void Clear(unsigned int depth=~0)
{
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
SetValue(i, j, depth);
}
}
void ClearF(float depth=1.0f)
{
Clear(DepthFToUInt(depth));
}
void SetValue(int x, int y, unsigned int depth)
{
if(!CheckRangeValid(x,y)) return;
depthStencilData()[y*width+x] = depth;
}
void SetFValue(int x, int y, float depth)
{
SetValue(x, y, DepthFToUInt(depth));
}
unsigned int GetValue(int x, int y)
{
if(!CheckRangeValid(x,y)) return 1;
return depthStencilData()[y*width+x];
}
float GetFValue(int x, int y)
{
unsigned int ivalue = GetValue(x, y);
return DepthUIntToF(ivalue);
}
private:
unsigned int DepthFToUInt(float fdepth)
{
fdepth = std::max(std::min(fdepth, 1.0f), -1.0f);
unsigned int idepth = (fdepth + 1.0) * 0.5 * UINT_MAX;
return idepth;
}
float DepthUIntToF(unsigned int idepth)
{
float fvalue = (double)idepth / UINT_MAX * 2.0 - 1.0;
return fvalue;
}
unsigned int* depthStencilData()
{
return (unsigned int*)data;
}
};
class GDataBuffer
{
public:
GDataBuffer(GDataBufferType t)
{
bufferType = t;
}
~GDataBuffer()
{
InvalidateData();
}
void SetData(void* data, int size, int offset=0)
{
if(data==nullptr || size<1)
{
GLog::LogError("data = ", data, " size = ", size);
return;
}
unsigned char* start = (unsigned char*)data;
_buffer.insert(_buffer.begin()+offset, start, start+size);
}
void InvalidateData()
{
_buffer.clear();
}
template<typename T>T* GetData(int offset)
{
return (T*)(_buffer.data()+offset);
}
void* buffer()
{
return _buffer.data();
}
GDataBufferType bufferType;
std::vector<unsigned char> _buffer;
};
struct GVertexAttribInfo
{
int slot;
bool enable;
int datumCount;
GDatumType datumType;
bool normalize;
int stride;
int offset;
};
class GVertexAttribInfoObject;
struct GVertexAttrib
{
int datumCount;
GDatumType datumType;
double data[4];
typedef std::vector<GVertexAttrib> VertexAttribArr;
typedef std::tuple<GVertexAttribInfo, VertexAttribArr> AttriInfo_AttribArr;
typedef std::vector<AttriInfo_AttribArr> AttriInfo_AttribArr_Arr;
static void InitArr_Arr(AttriInfo_AttribArr_Arr& slotAtrribArr_Arr, GVertexAttribInfoObject* vao);
static void AppendAttribToArr_Arr(AttriInfo_AttribArr_Arr& slotAtrribArr_Arr, int slot, GVertexAttrib& attrib);
};
class GVertexAttribInfoObject
{
public:
bool IsSlotExist(int slot);
GVertexAttribInfo* GetVertexAttriInfo(int slot);
int slotCount()
{
return vertexAttribInfoArray.size();
}
std::vector<GVertexAttribInfo> vertexAttribInfoArray;
GDataBuffer* vertexBuffer;
GDataBuffer* elemBuffer;
};
#endif // GBUFFER_H