-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraphicsWin32.cpp
1434 lines (1244 loc) · 37.4 KB
/
GraphicsWin32.cpp
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Graphics.h"
#include "gl.h"
#define graphics_xstr(a) graphics_str(a)
#define graphics_str(a) #a
#define graphics_location "On line: " graphics_xstr(__LINE__) ", in file: " __FILE__
#if _DEBUG
#define GraphicsAssert(cond, msg) Graphics::Internal::Assert(cond, graphics_location, msg)
#else
#define GraphicsAssert(c, m)
#endif
#ifndef ATLAS_I32
#define ATLAS_I32
typedef int i32;
static_assert (sizeof(i32) == 4, "i32 should be defined as a 4 byte type");
#endif
// Helpers
namespace Graphics {
namespace Internal {
void Assert(bool cond, const char* location, const char* msg) {
if (!cond) {
char* invalidAddress = (char*)0;
*invalidAddress = '\0';
}
}
struct TextureFormatResult {
GLenum dataType;
GLenum dataFormat;
};
struct ShaderCompileResult {
bool success;
u32 program;
char* uniforms;
char* attributes;
u32 numAttribs;
u32 numUniforms;
ShaderCompileResult() {
success = false;
program = 0;
uniforms = 0;
attributes = 0;
numAttribs = 0;
numUniforms = 0;
}
};
inline u32 FindFirstAvailable(u32 mask) {
for (u32 i = 0; i < 32; ++i) {
if (!(mask & (1 << i))) {
return i;
}
}
GraphicsAssert(false, "Could not find available texture, all 32 slots are filled");
return 33;
}
inline GLenum DepthFuncToEnum(DepthFunc f) {
if (f == DepthFunc::Always) {
return GL_ALWAYS;
}
else if (f == DepthFunc::Never) {
return GL_NEVER;
}
else if (f == DepthFunc::Equal) {
return GL_EQUAL;
}
else if (f == DepthFunc::LEqual) {
return GL_LEQUAL;
}
else if (f == DepthFunc::Greater) {
return GL_GREATER;
}
else if (f == DepthFunc::GEqual) {
return GL_GEQUAL;
}
else if (f == DepthFunc::NotEqual) {
return GL_NOTEQUAL;
}
return GL_LESS;
}
inline GLenum GetTextureUnit(u32 index) {
GraphicsAssert(index <= 32, "Only supports up to GL_TEXTURE16");
return GL_TEXTURE0 + index;
}
inline GLenum BlendEqToEnum(BlendEquation b) {
if (b == BlendEquation::Subtract) {
return GL_FUNC_SUBTRACT;
}
else if (b == BlendEquation::ReverseSubtract) {
return GL_FUNC_REVERSE_SUBTRACT;
}
else if (b == BlendEquation::Min) {
return GL_MIN;
}
else if (b == BlendEquation::Max) {
return GL_MAX;
}
return GL_FUNC_ADD;
}
inline GLenum BlendfuncToEnum(BlendFunction b) {
GLenum result = GL_ZERO;
if (b == BlendFunction::One) {
result = GL_ONE;
}
else if (b == BlendFunction::SrcColor) {
result = GL_SRC_COLOR;
}
else if (b == BlendFunction::OneMinusSrcColor) {
result = GL_ONE_MINUS_SRC_COLOR;
}
else if (b == BlendFunction::DstColor) {
result = GL_DST_COLOR;
}
else if (b == BlendFunction::OneMinusDstColor) {
result = GL_ONE_MINUS_DST_COLOR;
}
else if (b == BlendFunction::SrcAlpha) {
result = GL_SRC_ALPHA;
}
else if (b == BlendFunction::OneMinusSrcAlpha) {
result = GL_ONE_MINUS_SRC_ALPHA;
}
else if (b == BlendFunction::DstAlpha) {
result = GL_DST_ALPHA;
}
else if (b == BlendFunction::OneMinusDstAlpha) {
result = GL_ONE_MINUS_DST_ALPHA;
}
else if (b == BlendFunction::ConstColor) {
result = GL_CONSTANT_COLOR;
}
else if (b == BlendFunction::OneMinusConstColor) {
result = GL_ONE_MINUS_CONSTANT_COLOR;
}
else if (b == BlendFunction::ConstAlpha) {
result = GL_CONSTANT_ALPHA;
}
else if (b == BlendFunction::OneMinusconstAlpha) {
result = GL_ONE_MINUS_CONSTANT_ALPHA;
}
else if (b == BlendFunction::SrcAlphaSaturate) {
result = GL_SRC_ALPHA_SATURATE;
}
return result;
}
inline GLenum DrawModeToEnum(DrawMode drawMode) {
GLenum mode = GL_TRIANGLES;
if (drawMode == DrawMode::Points) {
mode = GL_POINTS;
}
else if (drawMode == DrawMode::Lines) {
mode = GL_LINES;
}
else if (drawMode == DrawMode::LineStrip) {
mode = GL_LINE_STRIP;
}
else if (drawMode == DrawMode::TriangleStrip) {
mode = GL_TRIANGLE_STRIP;
}
else if (drawMode == DrawMode::TriangleFan) {
mode = GL_TRIANGLE_FAN;
}
return mode;
}
inline GLenum TextureGetInternalFormatFromEnum(TextureFormat component) {
if (component == TextureFormat::R8) {
return GL_R8;
}
else if (component == TextureFormat::RG8) {
return GL_RG8;
}
if (component == TextureFormat::RGB8) {
return GL_RGB8;
}
if (component == TextureFormat::RGBA8) {
return GL_RGBA8;
}
if (component == TextureFormat::R32F) {
return GL_R32F;
}
else if (component == TextureFormat::RG32F) {
return GL_RG32F;
}
if (component == TextureFormat::RGB32F) {
return GL_RGB32F;
}
if (component == TextureFormat::RGBA32F) {
return GL_RGBA32F;
}
return GL_DEPTH_COMPONENT; // Default to depth i guess
}
inline TextureFormatResult TextureGetDataFormatFromEnum(TextureFormat component) {
GLenum dataFormat = GL_DEPTH_COMPONENT;
GLenum dataType = GL_FLOAT;
if (component == TextureFormat::R8) {
dataFormat = GL_RED;
dataType = GL_UNSIGNED_BYTE;
}
else if (component == TextureFormat::RG8) {
dataFormat = GL_RG;
dataType = GL_UNSIGNED_BYTE;
}
if (component == TextureFormat::RGB8) {
dataFormat = GL_RGB;
dataType = GL_UNSIGNED_BYTE;
}
if (component == TextureFormat::RGBA8) {
dataFormat = GL_RGBA;
dataType = GL_UNSIGNED_BYTE;
}
if (component == TextureFormat::R32F) {
dataFormat = GL_RED;
dataType = GL_FLOAT;
}
else if (component == TextureFormat::RG32F) {
dataFormat = GL_RG;
dataType = GL_FLOAT;
}
if (component == TextureFormat::RGB32F) {
dataFormat = GL_RGB;
dataType = GL_FLOAT;
}
if (component == TextureFormat::RGBA32F) {
dataFormat = GL_RGBA;
dataType = GL_FLOAT;
}
TextureFormatResult result;
result.dataFormat = dataFormat;
result.dataType = dataType;
return result;
}
inline GLenum BufferTypeToEnum(BufferType bufferType) {
GLenum type = GL_FLOAT;
if (bufferType == BufferType::Int8) {
type = GL_BYTE;
}
else if (bufferType == BufferType::UInt8) {
type = GL_UNSIGNED_BYTE;
}
else if (bufferType == BufferType::Int16) {
type = GL_SHORT;
}
else if (bufferType == BufferType::UInt16) {
type = GL_UNSIGNED_SHORT;
}
else if (bufferType == BufferType::Int32) {
type = GL_INT;
}
else if (bufferType == BufferType::UInt32) {
type = GL_UNSIGNED_INT;
}
return type;
}
#if GRAPHICS_SHADERS_ENABLENAMES
struct ShaderBytesNeededResult {
u32 bytesNeeded;
u32 numberOfThing;
};
ShaderBytesNeededResult NumBytesNeededForAttributeNames(unsigned int handle) {
int count = -1;
int length;
char name[128];
int size;
GLenum type;
ShaderBytesNeededResult result;
//glUseProgram(handle);
glGetProgramiv(handle, GL_ACTIVE_ATTRIBUTES, &count);
result.numberOfThing = count;
result.bytesNeeded = 0;
for (int i = 0; i < count; ++i) {
memset(name, 0, sizeof(char) * 128);
glGetActiveAttrib(handle, (GLuint)i, 128, &length, &size, &type, name);
GraphicsAssert(length < 128, "Name too long");
result.bytesNeeded += length + 1;
//int attrib = glGetAttribLocation(handle, name);
}
//glUseProgram(0);
result.bytesNeeded += 1;
return result;
}
ShaderBytesNeededResult NumBytesNeededForUniformNames(unsigned int handle) {
int count = -1;
int length;
char name[128];
int size;
GLenum type;
char testName[256];
ShaderBytesNeededResult result;
//glUseProgram(handle);
glGetProgramiv(handle, GL_ACTIVE_UNIFORMS, &count);
result.numberOfThing = count;
result.bytesNeeded = 0;
for (int i = 0; i < count; ++i) {
memset(name, 0, sizeof(char) * 128);
glGetActiveUniform(handle, (GLuint)i, 128, &length, &size, &type, name);
GraphicsAssert(length < 128, "Name too long");
//int uniform = glGetUniformLocation(handle, name);
result.bytesNeeded += length + 1;
}
//glUseProgram(0);
result.bytesNeeded += 1;
return result;
}
#endif
ShaderCompileResult CompileOpenGLShader(const char* vertexSource, const char* fragmentSource, Dependencies* platform) {
ShaderCompileResult result;
result.success = true;
result.program = 0;
int success = 1;
// compile vertex shader
u32 vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
result.success = false;
#if _DEBUG
char infoLog[512];
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
GraphicsAssert(false, infoLog);
#endif
}
// compile fragment shader
u32 fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
result.success = false;
#if _DEBUG
char infoLog[512];
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
GraphicsAssert(false, infoLog);
#endif
}
// Link shaders into program
u32 shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
result.success = false;
#if _DEBUG
char infoLog[512];
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
GraphicsAssert(false, infoLog);
#endif
}
else {
result.program = shaderProgram;
}
// Delete shaders
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
#if GRAPHICS_SHADERS_ENABLENAMES
int length;
int size;
GLenum type;
ShaderBytesNeededResult attribNameInfo = Internal::NumBytesNeededForAttributeNames(result.program);
result.attributes = (char*)platform->Request(attribNameInfo.bytesNeeded);
result.numAttribs = attribNameInfo.numberOfThing;
char* iter = result.attributes;
for (u32 i = 0; i < attribNameInfo.numberOfThing; ++i) {
char* name = iter;
glGetActiveAttrib(result.program, i, 128, &length, &size, &type, name);
iter += length;
*iter = (i + 1 == attribNameInfo.numberOfThing)? '\0' : '\n';
iter += 1;
}
*iter = '\0';
ShaderBytesNeededResult uniformNameInfo = Internal::NumBytesNeededForUniformNames(result.program);
result.uniforms = (char*)platform->Request(uniformNameInfo.bytesNeeded);
result.numUniforms = uniformNameInfo.numberOfThing;
iter = result.uniforms;
for (u32 i = 0; i < uniformNameInfo.numberOfThing; ++i) {
char* name = iter;
glGetActiveUniform(result.program, i, 128, &length, &size, &type, name);
iter += length;
*iter = (i + 1 == uniformNameInfo.numberOfThing) ? '\0' : '\n';
iter += 1;
}
*iter = '\0';
#endif
return result;
}
}
}
/// Texture
/*void Graphics::Texture::SetPCM(bool pcm) {
GLenum attachTarget = GL_TEXTURE_2D;
glBindTexture(attachTarget, mId);
if (pcm) {
glTexParameteri(attachTarget, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(attachTarget, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
}
else { // TODO: None is wrong
glTexParameteri(attachTarget, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glTexParameteri(attachTarget, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
}
glBindTexture(attachTarget, 0);
}*/
void Graphics::Texture::Set(void* data, TextureFormat dataFormat, u32 width, u32 height, bool genMipMaps) {
glBindTexture(GL_TEXTURE_2D, mId);
GLenum internalFormat = Internal::TextureGetInternalFormatFromEnum(mInternalFormat);
Internal::TextureFormatResult f = Internal::TextureGetDataFormatFromEnum(dataFormat);
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, f.dataFormat, f.dataType, data);
if (genMipMaps) {
glGenerateMipmap(GL_TEXTURE_2D);
}
mWidth = width;
mHeight = height;
mIsMipMapped = genMipMaps;
glBindTexture(GL_TEXTURE_2D, 0);
}
/// Device
void Graphics::Device::SetFaceCulling(CullFace cull, FaceWind wind) {
if (mFaceCulling != cull) {
if (cull == CullFace::Back) {
if (mFaceCulling == CullFace::Off) {
glEnable(GL_CULL_FACE);
}
glCullFace(GL_BACK);
}
else if (cull == CullFace::Front) {
if (mFaceCulling == CullFace::Off) {
glEnable(GL_CULL_FACE);
}
glCullFace(GL_FRONT);
}
else if (cull == CullFace::FrontAndBack) {
if (mFaceCulling == CullFace::Off) {
glEnable(GL_CULL_FACE);
}
glCullFace(GL_FRONT_AND_BACK);
}
else { // Off
if (mFaceCulling != CullFace::Off) {
glDisable(GL_CULL_FACE);
}
}
mFaceCulling = cull;
}
if (mWindingOrder != wind) {
if (wind == FaceWind::CounterClockwise) {
glFrontFace(GL_CCW);
}
else {
glFrontFace(GL_CW);
}
mWindingOrder = wind;
}
}
void Graphics::Device::SetDepthState(bool enable, DepthFunc depthFunc, f32* depthRange) {
if (mDepth != enable) {
if (enable) {
glEnable(GL_DEPTH_TEST);
}
else {
glDisable(GL_DEPTH_TEST);
}
mDepth = enable;
}
if (mDepthFunc != depthFunc) {
GLenum func = Internal::DepthFuncToEnum(depthFunc);
glDepthFunc(func);
mDepthFunc = depthFunc;
}
if (depthRange != 0) {
glDepthRange(depthRange[0], depthRange[1]);
mDepthRange[0] = depthRange[0];
mDepthRange[1] = depthRange[1];
}
}
void Graphics::Device::SetBlendState(bool blend, f32* blendColor,
BlendFunction blendDstRgb, BlendFunction blendDstAlpha,
BlendEquation blendEquationRgb, BlendEquation blendEquationAlpha,
BlendFunction blendSrcRGB, BlendFunction blendSrcAlpha) {
if (blend != mBlend) {
if (blend) {
glEnable(GL_BLEND);
}
else {
glDisable(GL_BLEND);
}
mBlend = blend;
}
if (blendColor != 0) {
glBlendColor(blendColor[0], blendColor[1], blendColor[2], blendColor[3]);
mBlendColor[0] = blendColor[0];
mBlendColor[1] = blendColor[1];
mBlendColor[2] = blendColor[2];
mBlendColor[3] = blendColor[3];
}
if (mBlendDstAlpha != blendDstAlpha || mBlendDstRGB != blendDstRgb ||
mBlendSrcAlpba != blendSrcAlpha || mBlendSrcRGB != blendSrcRGB) {
GLenum srcAlpha = Internal::BlendfuncToEnum(blendSrcAlpha);
GLenum srcRgb = Internal::BlendfuncToEnum(blendSrcRGB);
GLenum dstAlpha = Internal::BlendfuncToEnum(blendDstAlpha);
GLenum dstRgb = Internal::BlendfuncToEnum(blendDstRgb);
if (blendDstAlpha == blendDstRgb && blendSrcAlpha == blendSrcRGB) { // Same
glBlendFunc(srcRgb, dstRgb);
}
else { // Seperate
glBlendFuncSeparate(srcRgb, dstRgb, srcAlpha, dstAlpha);
}
mBlendDstAlpha = blendDstAlpha;
mBlendDstRGB = blendDstRgb;
mBlendSrcAlpba = blendSrcAlpha;
mBlendSrcRGB = blendSrcRGB;
}
if (mBlendEquationAlpha != blendEquationAlpha || mBlendEquationRGB != blendEquationRgb) {
GLenum alphaEquation = Internal::BlendEqToEnum(blendEquationAlpha);
GLenum rgbEquation = Internal::BlendEqToEnum(blendEquationRgb);
if (blendEquationAlpha == blendEquationRgb) { // Same
glBlendEquation(rgbEquation);
}
else { // Seperate
glBlendEquationSeparate(rgbEquation, alphaEquation);
}
mBlendEquationAlpha = blendEquationAlpha;
mBlendEquationRGB = blendEquationRgb;
}
}
void Graphics::Device::Clear(f32 r, f32 g, f32 b, f32 depth) {
glClearColor(r, g, b, 1.0f);
glClearDepth(depth);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Graphics::Device::Clear(bool color, bool depth) {
if (color && !depth) {
glClear(GL_COLOR_BUFFER_BIT);
}
else if (!color && depth) {
glClear(GL_DEPTH_BUFFER_BIT);
}
else if (color && depth) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
}
void Graphics::Device::WriteMask(bool r, bool g, bool b, bool a, bool depth) {
glColorMask(r, g, b, a);
glDepthMask(depth);
}
void Graphics::Device::Clear(f32 r, f32 g, f32 b) {
glClearColor(r, g, b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
void Graphics::Device::Clear(f32 depth) {
glClearDepth(depth);
glClear(GL_DEPTH_BUFFER_BIT);
}
void Graphics::Device::SetViewport(u32 x, u32 y, u32 w, u32 h) {
if (x != mViewportRect[0] || y != mViewportRect[1] ||
w != mViewportRect[2] || h != mViewportRect[3]) {
glViewport(x, y, w, h);
mViewportRect[0] = x;
mViewportRect[1] = y;
mViewportRect[2] = w;
mViewportRect[3] = h;
}
}
void Graphics::Device::SetScissorState(bool enable, u32 x, u32 y, u32 w, u32 h) {
if (enable != mScissor) {
if (enable) {
glEnable(GL_SCISSOR_TEST);
}
else {
glDisable(GL_SCISSOR_TEST);
}
mScissor = enable;
}
if (x != mScissorRect[0] || y != mScissorRect[1] || w != mScissorRect[2] || h != mScissorRect[3]) {
glScissor(x, y, w, h);
mScissorRect[0] = x;
mScissorRect[1] = y;
mScissorRect[2] = w;
mScissorRect[3] = h;
}
}
Graphics::FrameBuffer* Graphics::Device::CreateFrameBuffer() {
Graphics::FrameBuffer* result = (Graphics::FrameBuffer*)mPlatform.Request(sizeof(Graphics::FrameBuffer));
glGenFramebuffers(1, &result->mId);
result->mColor0 = 0;
result->mColor1 = 0;
result->mColor2 = 0;
result->mColor3 = 0;
result->mColor4 = 0;
result->mColor5 = 0;
result->mDepth = 0;
result->mUserData = 0;
result->mOwner = this;
result->mAllocPrev = 0;
result->mAllocNext = mAllocatedFrameBuffers;
if (mAllocatedFrameBuffers != 0) {
mAllocatedFrameBuffers->mAllocPrev = result;
}
mAllocatedFrameBuffers = result;
result->mReadBufferConfig = GL_FRONT;
return result;
}
void Graphics::Device::Destroy(FrameBuffer* buffer) {
if (mBoundFrameBuffer == buffer->mId) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
mBoundFrameBuffer = 0;
}
glDeleteFramebuffers(1, &buffer->mId);
if (buffer->mAllocPrev != 0) { // Not head
buffer->mAllocPrev->mAllocNext = buffer->mAllocNext;
if (buffer->mAllocNext != 0) {
buffer->mAllocNext->mAllocPrev = buffer->mAllocPrev;
}
}
else { // Head
mAllocatedFrameBuffers = mAllocatedFrameBuffers->mAllocNext;
if (mAllocatedFrameBuffers != 0) {
mAllocatedFrameBuffers->mAllocPrev = 0;
}
}
mPlatform.Release(buffer);
}
Graphics::Buffer* Graphics::Device::CreateBuffer() {
Graphics::Buffer* result = (Graphics::Buffer*)mPlatform.Request(sizeof(Graphics::Buffer));
glGenBuffers(1, &result->mId);
result->mIndexBuffer = false;
result->mUserData = 0;
result->mOwner = this;
result->mAllocPrev = 0;
result->mAllocNext = mAllocatedBuffers;
if (mAllocatedBuffers != 0) {
mAllocatedBuffers->mAllocPrev = result;
}
mAllocatedBuffers = result;
return result;
}
Graphics::Buffer* Graphics::Device::CreateIndexBuffer() {
Graphics::Buffer* result = (Graphics::Buffer*)mPlatform.Request(sizeof(Graphics::Buffer));
glGenBuffers(1, &result->mId);
result->mIndexBuffer = true;
result->mUserData = 0;
result->mOwner = this;
result->mAllocPrev = 0;
result->mAllocNext = mAllocatedBuffers;
if (mAllocatedBuffers != 0) {
mAllocatedBuffers->mAllocPrev = result;
}
mAllocatedBuffers = result;
return result;
}
void Graphics::Device::Destroy(Buffer* buff) {
glDeleteBuffers(1, &buff->mId);
if (buff->mAllocPrev != 0) { // Not head
buff->mAllocPrev->mAllocNext = buff->mAllocNext;
if (buff->mAllocNext != 0) {
buff->mAllocNext->mAllocPrev = buff->mAllocPrev;
}
}
else { // Head
mAllocatedBuffers = mAllocatedBuffers->mAllocNext;
if (mAllocatedBuffers != 0) {
mAllocatedBuffers->mAllocPrev = 0;
}
}
mPlatform.Release(buff);
}
Graphics::VertexLayout* Graphics::Device::CreateVertexLayout() {
Graphics::VertexLayout* result = (Graphics::VertexLayout*)mPlatform.Request(sizeof(Graphics::VertexLayout));
glGenVertexArrays(1, &result->mId);
result->mUserData = 0;
result->mIndexBufferType = Graphics::BufferType::UInt32;
result->mHasIndexBuffer = false;
result->mAllocPrev = 0;
result->mAllocNext = mAllocatedStates;
if (mAllocatedStates != 0) {
mAllocatedStates->mAllocPrev = result;
}
mAllocatedStates = result;
return result;
}
void Graphics::Device::Destroy(VertexLayout* map) {
glBindVertexArray(0);
glDeleteVertexArrays(1, &map->mId);
if (map->mAllocPrev != 0) { // Not head
map->mAllocPrev->mAllocNext = map->mAllocNext;
if (map->mAllocNext != 0) {
map->mAllocNext->mAllocPrev = map->mAllocPrev;
}
}
else { // Head
mAllocatedStates = mAllocatedStates->mAllocNext;
if (mAllocatedStates != 0) {
mAllocatedStates->mAllocPrev = 0;
}
}
mPlatform.Release(map);
}
Graphics::Texture* Graphics::Device::CreateTexture(TextureFormat format) {
Graphics::Texture* result = (Graphics::Texture*)mPlatform.Request(sizeof(Graphics::Texture));
glGenTextures(1, &result->mId);
result->mWidth = 0;
result->mHeight = 0;
result->mInternalFormat = format;
result->mIsMipMapped = false;
result->mUserData = 0;
result->mCachedMin = GL_NEAREST_MIPMAP_LINEAR;
result->mCachedMag = GL_LINEAR;
result->mCachedS = GL_REPEAT;
result->mCachedR = GL_REPEAT;
result->mCachedT = GL_REPEAT;
result->mOwner = this;
result->mAllocPrev = 0;
result->mAllocNext = mAllocatedTextures;
if (mAllocatedTextures != 0) {
mAllocatedTextures->mAllocPrev = result;
}
mAllocatedTextures = result;
return result;
}
void Graphics::Device::Destroy(Texture* buff) {
glDeleteTextures(1, &buff->mId);
if (buff->mAllocPrev != 0) { // Not head
buff->mAllocPrev->mAllocNext = buff->mAllocNext;
if (buff->mAllocNext != 0) {
buff->mAllocNext->mAllocPrev = buff->mAllocPrev;
}
}
else { // Head
mAllocatedTextures = mAllocatedTextures->mAllocNext;
if (mAllocatedTextures != 0) {
mAllocatedTextures->mAllocPrev = 0;
}
}
mPlatform.Release(buff);
}
Graphics::Shader* Graphics::Device::CreateShader(const char* vertex, const char* fragment) {
Graphics::Shader* result = 0;
Graphics::Internal::ShaderCompileResult compileStatus =
Graphics::Internal::CompileOpenGLShader(vertex, fragment, &mPlatform);
GraphicsAssert(compileStatus.success, "Failed to compile shader");
if (compileStatus.success) {
result = (Graphics::Shader*)mPlatform.Request(sizeof(Graphics::Shader));
result->mProgram = compileStatus.program;
}
#if GRAPHICS_SHADERS_ENABLENAMES
result->mUniformNames = compileStatus.uniforms;
result->mNumUniforms = compileStatus.numUniforms;
result->mAttributeNames = compileStatus.attributes;
result->mNumAttributes = compileStatus.numAttribs;
#endif
result->mUserData = 0;
result->mOwner = this;
result->mAllocPrev = 0;
result->mAllocNext = mAllocatedShaders;
if (mAllocatedShaders != 0) {
mAllocatedShaders->mAllocPrev = result;
}
mAllocatedShaders = result;
return result;
}
void Graphics::Device::Destroy(Shader* shader) {
if (mBoundProgram == shader->mProgram) {
glUseProgram(0);
mBoundProgram = 0;
}
glDeleteProgram(shader->mProgram);
shader->mProgram = 0;
if (shader->mAllocPrev != 0) { // Not head
shader->mAllocPrev->mAllocNext = shader->mAllocNext;
if (shader->mAllocNext != 0) {
shader->mAllocNext->mAllocPrev = shader->mAllocPrev;
}
}
else { // Head
mAllocatedShaders = mAllocatedShaders->mAllocNext;
if (mAllocatedShaders != 0) {
mAllocatedShaders->mAllocPrev = 0;
}
}
#if GRAPHICS_SHADERS_ENABLENAMES
mPlatform.Release(shader->mUniformNames);
mPlatform.Release(shader->mAttributeNames);
#endif
mPlatform.Release(shader);
}
void Graphics::Device::SetRenderTarget(FrameBuffer* frameBuffer) {
const u32 attachments[8] = {
GL_COLOR_ATTACHMENT0,
GL_COLOR_ATTACHMENT1,
GL_COLOR_ATTACHMENT2,
GL_COLOR_ATTACHMENT3,
GL_COLOR_ATTACHMENT4,
GL_COLOR_ATTACHMENT5,
GL_COLOR_ATTACHMENT6,
GL_COLOR_ATTACHMENT7,
};
if (frameBuffer != 0) {
if (mBoundFrameBuffer != frameBuffer->mId) {
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer->mId);
mBoundFrameBuffer = frameBuffer->mId;
}
u32 numAttachments = frameBuffer->TargetCount();
if (numAttachments == 0) {
glDrawBuffer(GL_NONE);
if (frameBuffer->mReadBufferConfig != GL_NONE) {
glReadBuffer(GL_NONE);
frameBuffer->mReadBufferConfig = GL_NONE;
}
}
else {
GraphicsAssert(numAttachments <= 8, "Only supports up to 8 color attachments");
glDrawBuffers(numAttachments, attachments);
if (frameBuffer->mReadBufferConfig != GL_FRONT) {
glReadBuffer(GL_FRONT);
frameBuffer->mReadBufferConfig = GL_FRONT;
}
}
}
else if (mBoundFrameBuffer != 0) {
const u32 back_attach[1] = {GL_BACK};
glBindFramebuffer(GL_FRAMEBUFFER, 0);
mBoundFrameBuffer = 0;
glDrawBuffers(1, back_attach);
}
}
void Graphics::Device::Bind(Shader* shader) {
u32 program = 0;
if (shader != 0) {
program = shader->mProgram;
}
if (mBoundProgram != program) {
glUseProgram(program);
mBoundProgram = program;
{ // Unbind any previously bound textures
for (u32 i = 0; i < 32; ++i) {
if (mBoundTextures[i].texture != 0) {
glActiveTexture(Internal::GetTextureUnit(i));
glBindTexture(mBoundTextures[i].target, 0);
mBoundTextures[i].texture = 0;
mBoundTextures[i].target = 0;
mBoundTextures[i].index.id = 0;
mBoundTextures[i].index.valid = false;
}
}
glActiveTexture(GL_TEXTURE0);
}
}
}
void Graphics::Device::Bind(Index& slot, UniformType type, void* data, u32 count) {
GraphicsAssert(slot.valid, "Setting invalid uniform");
GraphicsAssert(slot.shader == mBoundProgram, "Binding index to wrong shader");
if (type == UniformType::Int1) {
glUniform1iv(slot.id, count, (const GLint*)data);
}
else if (type == UniformType::Int2) {
glUniform2iv(slot.id, count, (const GLint*)data);
}
else if (type == UniformType::Int3) {
glUniform3iv(slot.id, count, (const GLint*)data);
}
else if (type == UniformType::Int4) {
glUniform4iv(slot.id, count, (const GLint*)data);
}
else if (type == UniformType::Float1) {
glUniform1fv(slot.id, count, (GLfloat*)data);
}
else if (type == UniformType::Float2) {
glUniform2fv(slot.id, count, (GLfloat*)data);
}
else if (type == UniformType::Float3) {
glUniform3fv(slot.id, count, (GLfloat*)data);
}
else if (type == UniformType::Float4) {
glUniform4fv(slot.id, count, (GLfloat*)data);
}
else if (type == UniformType::Float9) {
glUniformMatrix3fv(slot.id, count, GL_FALSE, (GLfloat*)data);
}