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
|
// render-gl.cpp
#include "render-gl.h"
#include "options.h"
#include "render.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "core/basic.h"
#include "core/secure-crt.h"
#include "external/stb/stb_image_write.h"
// TODO(tfoley): eventually we should be able to run these
// tests on non-Windows targets to confirm that cross-compilation
// at least *works* on those platforms...
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#undef WIN32_LEAN_AND_MEAN
#undef NOMINMAX
#ifdef _MSC_VER
#include <stddef.h>
#if (_MSC_VER < 1900)
#define snprintf sprintf_s
#endif
#endif
#pragma comment(lib, "opengl32")
#include <GL/GL.h>
#include "external/glext.h"
// We define an "X-macro" for mapping over loadable OpenGL
// extension entry point that we will use, so that we can
// easily write generic code to iterate over them.
#define MAP_GL_EXTENSION_FUNCS(F) \
F(glCreateProgram, PFNGLCREATEPROGRAMPROC) \
F(glCreateShader, PFNGLCREATESHADERPROC) \
F(glShaderSource, PFNGLSHADERSOURCEPROC) \
F(glCompileShader, PFNGLCOMPILESHADERPROC) \
F(glGetShaderiv, PFNGLGETSHADERIVPROC) \
F(glDeleteShader, PFNGLDELETESHADERPROC) \
F(glAttachShader, PFNGLATTACHSHADERPROC) \
F(glLinkProgram, PFNGLLINKPROGRAMPROC) \
F(glGetProgramiv, PFNGLGETPROGRAMIVPROC) \
F(glGetProgramInfoLog, PFNGLGETPROGRAMINFOLOGPROC) \
F(glDeleteProgram, PFNGLDELETEPROGRAMPROC) \
F(glGetShaderInfoLog, PFNGLGETSHADERINFOLOGPROC) \
F(glGenBuffers, PFNGLGENBUFFERSPROC) \
F(glBindBuffer, PFNGLBINDBUFFERPROC) \
F(glBufferData, PFNGLBUFFERDATAPROC) \
F(glDeleteBuffers, PFNGLDELETEBUFFERSPROC) \
F(glMapBuffer, PFNGLMAPBUFFERPROC) \
F(glUnmapBuffer, PFNGLUNMAPBUFFERPROC) \
F(glUseProgram, PFNGLUSEPROGRAMPROC) \
F(glBindBufferBase, PFNGLBINDBUFFERBASEPROC) \
F(glVertexAttribPointer, PFNGLVERTEXATTRIBPOINTERPROC) \
F(glEnableVertexAttribArray, PFNGLENABLEVERTEXATTRIBARRAYPROC) \
F(glDisableVertexAttribArray, PFNGLDISABLEVERTEXATTRIBARRAYPROC) \
F(glDebugMessageCallback, PFNGLDEBUGMESSAGECALLBACKPROC) \
F(glDispatchCompute, PFNGLDISPATCHCOMPUTEPROC) \
F(glActiveTexture, PFNGLACTIVETEXTUREPROC) \
F(glCreateSamplers, PFNGLCREATESAMPLERSPROC) \
F(glDeleteSamplers, PFNGLDELETESAMPLERSPROC) \
F(glBindSampler, PFNGLBINDSAMPLERPROC) \
F(glTexImage3D, PFNGLTEXIMAGE3DPROC) \
F(glSamplerParameteri, PFNGLSAMPLERPARAMETERIPROC) \
/* end */
using namespace Slang;
namespace renderer_test {
class GLRenderer : public Renderer, public ShaderCompiler
{
public:
// Renderer implementation
virtual SlangResult initialize(void* inWindowHandle) override;
virtual void setClearColor(const float color[4]) override;
virtual void clearFrame() override;
virtual void presentFrame() override;
virtual SlangResult captureScreenShot(char const* outputPath) override;
virtual void serializeOutput(BindingState* state, const char* fileName) override;
virtual Buffer* createBuffer(const BufferDesc& desc) override;
virtual InputLayout* createInputLayout(const InputElementDesc* inputElements, UInt inputElementCount) override;
virtual BindingState * createBindingState(const ShaderInputLayout& layout) override;
virtual ShaderCompiler* getShaderCompiler() override;
virtual void* map(Buffer* buffer, MapFlavor flavor) override;
virtual void unmap(Buffer* buffer) override;
virtual void setInputLayout(InputLayout* inputLayout) override;
virtual void setPrimitiveTopology(PrimitiveTopology topology) override;
virtual void setBindingState(BindingState* state);
virtual void setVertexBuffers(UInt startSlot, UInt slotCount, Buffer*const* buffers, const UInt* strides, const UInt* offsets) override;
virtual void setShaderProgram(ShaderProgram* inProgram) override;
virtual void setConstantBuffers(UInt startSlot, UInt slotCount, Buffer*const* buffers, const UInt* offsets) override;
virtual void draw(UInt vertexCount, UInt startVertex) override;
virtual void dispatchCompute(int x, int y, int z) override;
virtual void submitGpuWork() override {}
virtual void waitForGpu() override {}
// ShaderCompiler implementation
virtual ShaderProgram* compileProgram(const ShaderCompileRequest& request) override;
protected:
enum
{
kMaxVertexStreams = 16,
};
struct VertexAttributeFormat
{
GLint componentCount;
GLenum componentType;
GLboolean normalized;
};
struct VertexAttributeDesc
{
VertexAttributeFormat format;
GLuint streamIndex;
GLsizei offset;
};
class InputLayoutImpl: public InputLayout
{
public:
VertexAttributeDesc m_attributes[kMaxVertexStreams];
UInt m_attributeCount = 0;
};
class BufferImpl: public Buffer
{
public:
BufferImpl(GLRenderer* renderer, GLuint id):
m_renderer(renderer),
m_id(id)
{}
~BufferImpl()
{
if (m_renderer)
{
m_renderer->glDeleteBuffers(1, &m_id);
}
}
GLRenderer* m_renderer;
GLuint m_id;
};
struct BindingEntry
{
ShaderInputType type;
GLuint handle;
List<int> binding;
int bindTarget;
int bufferSize;
bool isOutput = false;
};
class BindingStateImpl: public BindingState
{
public:
BindingStateImpl(GLRenderer* renderer):
m_renderer(renderer)
{
}
~BindingStateImpl()
{
if (m_renderer)
{
m_renderer->destroyBindingEntries(m_entries.Buffer(), int(m_entries.Count()));
}
}
GLRenderer* m_renderer;
List<BindingEntry> m_entries;
};
class ShaderProgramImpl : public ShaderProgram
{
public:
ShaderProgramImpl(GLRenderer* renderer, GLuint id):
m_renderer(renderer),
m_id(id)
{
}
~ShaderProgramImpl()
{
if (m_renderer)
{
m_renderer->glDeleteProgram(m_id);
}
}
GLuint m_id;
GLRenderer* m_renderer;
};
void destroyBindingEntry(const BindingEntry& entry);
void destroyBindingEntries(const BindingEntry* entries, int numEntries);
void bindBufferImpl(int target, UInt startSlot, UInt slotCount, Buffer*const* buffers, const UInt* offsets);
void flushStateForDraw();
GLuint loadShader(GLenum stage, char const* source);
void createInputBuffer(BindingEntry& rs, InputBufferDesc bufDesc, List<unsigned int>& bufferData);
void debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message);
void createInputTexture(BindingEntry& rs, InputTextureDesc texDesc, InputSamplerDesc samplerDesc);
void createInputSampler(BindingEntry& rs, InputSamplerDesc samplerDesc);
static void APIENTRY staticDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam);
static VertexAttributeFormat getVertexAttributeFormat(Format format);
HDC m_hdc;
HGLRC m_glContext;
float m_clearColor[4] = { 0, 0, 0, 0 };
RefPtr<ShaderProgramImpl> m_boundShaderProgram;
RefPtr<InputLayoutImpl> m_boundInputLayout;
GLenum m_boundPrimitiveTopology = GL_TRIANGLES;
GLuint m_boundVertexStreamBuffers[kMaxVertexStreams];
UInt m_boundVertexStreamStrides[kMaxVertexStreams];
UInt m_boundVertexStreamOffsets[kMaxVertexStreams];
// Declare a function pointer for each OpenGL
// extension function we need to load
#define DECLARE_GL_EXTENSION_FUNC(NAME, TYPE) TYPE NAME;
MAP_GL_EXTENSION_FUNCS(DECLARE_GL_EXTENSION_FUNC)
#undef DECLARE_GL_EXTENSION_FUNC
};
Renderer* createGLRenderer()
{
return new GLRenderer();
}
void GLRenderer::debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message)
{
::OutputDebugStringA("GL: ");
::OutputDebugStringA(message);
::OutputDebugStringA("\n");
switch (type)
{
case GL_DEBUG_TYPE_ERROR:
break;
default:
break;
}
}
/* static */void APIENTRY GLRenderer::staticDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
{
((GLRenderer*)userParam)->debugCallback(source, type, id, severity, length, message);
}
/* static */GLRenderer::VertexAttributeFormat GLRenderer::getVertexAttributeFormat(Format format)
{
switch (format)
{
default: assert(!"unexpected"); return VertexAttributeFormat();
#define CASE(NAME, COUNT, TYPE, NORMALIZED) \
case Format::NAME: do { VertexAttributeFormat result = {COUNT, TYPE, NORMALIZED}; return result; } while (0)
CASE(RGB_Float32, 3, GL_FLOAT, GL_FALSE);
CASE(RG_Float32, 2, GL_FLOAT, GL_FALSE);
#undef CASE
}
}
void GLRenderer::bindBufferImpl(int target, UInt startSlot, UInt slotCount, Buffer*const* buffers, const UInt* offsets)
{
for (UInt ii = 0; ii < slotCount; ++ii)
{
UInt slot = startSlot + ii;
BufferImpl* buffer = static_cast<BufferImpl*>(buffers[ii]);
GLuint bufferID = buffer ? buffer->m_id : 0;
assert(!offsets || !offsets[ii]);
glBindBufferBase(target, (GLuint)slot, bufferID);
}
}
void GLRenderer::flushStateForDraw()
{
auto layout = m_boundInputLayout.Ptr();
auto attrCount = layout->m_attributeCount;
for (UInt ii = 0; ii < attrCount; ++ii)
{
auto& attr = layout->m_attributes[ii];
auto streamIndex = attr.streamIndex;
glBindBuffer(GL_ARRAY_BUFFER, m_boundVertexStreamBuffers[streamIndex]);
glVertexAttribPointer(
(GLuint)ii,
attr.format.componentCount,
attr.format.componentType,
attr.format.normalized,
(GLsizei)m_boundVertexStreamStrides[streamIndex],
(GLvoid*)(attr.offset + m_boundVertexStreamOffsets[streamIndex]));
glEnableVertexAttribArray((GLuint)ii);
}
for (UInt ii = attrCount; ii < kMaxVertexStreams; ++ii)
{
glDisableVertexAttribArray((GLuint)ii);
}
}
GLuint GLRenderer::loadShader(GLenum stage, const char* source)
{
// GLSL is monumentally stupid. It officially requires the `#version` directive
// to be the first thing in the file, which wouldn't be so bad but the API
// doesn't provide a way to pass a `#define` into your shader other than by
// prepending it to the whole thing.
//
// We are going to solve this problem by doing some surgery on the source
// that was passed in.
const char* sourceBegin = source;
const char* sourceEnd = source + strlen(source);
// Look for a version directive in the user-provided source.
const char* versionBegin = strstr(source, "#version");
const char* versionEnd = nullptr;
if (versionBegin)
{
// If we found a directive, then scan for the end-of-line
// after it, and use that to specify the slice.
versionEnd = strchr(versionBegin, '\n');
if (!versionEnd)
{
versionEnd = sourceEnd;
}
else
{
versionEnd = versionEnd + 1;
}
}
else
{
// If we didn't find a directive, then treat it as being
// a zero-byte slice at the start of the string
versionBegin = sourceBegin;
versionEnd = sourceBegin;
}
enum { kMaxSourceStringCount = 16 };
const GLchar* sourceStrings[kMaxSourceStringCount];
GLint sourceStringLengths[kMaxSourceStringCount];
int sourceStringCount = 0;
const char* stagePrelude = "\n";
switch (stage)
{
#define CASE(NAME) case GL_##NAME##_SHADER: stagePrelude = "#define __GLSL_" #NAME "__ 1\n"; break
CASE(VERTEX);
CASE(TESS_CONTROL);
CASE(TESS_EVALUATION);
CASE(GEOMETRY);
CASE(FRAGMENT);
CASE(COMPUTE);
#undef CASE
}
const char* prelude =
"#define __GLSL__ 1\n"
;
#define ADD_SOURCE_STRING_SPAN(BEGIN, END) \
sourceStrings[sourceStringCount] = BEGIN; \
sourceStringLengths[sourceStringCount++] = GLint(END - BEGIN) \
/* end */
#define ADD_SOURCE_STRING(BEGIN) \
sourceStrings[sourceStringCount] = BEGIN; \
sourceStringLengths[sourceStringCount++] = GLint(strlen(BEGIN)) \
/* end */
ADD_SOURCE_STRING_SPAN(versionBegin, versionEnd);
ADD_SOURCE_STRING(stagePrelude);
ADD_SOURCE_STRING(prelude);
ADD_SOURCE_STRING_SPAN(sourceBegin, versionBegin);
ADD_SOURCE_STRING_SPAN(versionEnd, sourceEnd);
auto shaderID = glCreateShader(stage);
glShaderSource(
shaderID,
sourceStringCount,
&sourceStrings[0],
&sourceStringLengths[0]);
glCompileShader(shaderID);
GLint success = GL_FALSE;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &success);
if (!success)
{
int maxSize = 0;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &maxSize);
auto infoBuffer = (char*)malloc(maxSize);
int infoSize = 0;
glGetShaderInfoLog(shaderID, maxSize, &infoSize, infoBuffer);
if (infoSize > 0)
{
fprintf(stderr, "%s", infoBuffer);
::OutputDebugStringA(infoBuffer);
}
glDeleteShader(shaderID);
return 0;
}
return shaderID;
}
void GLRenderer::destroyBindingEntry(const BindingEntry& entry)
{
switch (entry.type)
{
case ShaderInputType::Buffer:
{
glDeleteBuffers(1, &entry.handle);
break;
}
case ShaderInputType::Texture:
case ShaderInputType::CombinedTextureSampler:
{
glDeleteTextures(1, &entry.handle);
break;
}
case ShaderInputType::Sampler:
{
glDeleteSamplers(1, &entry.handle);
break;
}
default: break;
}
}
void GLRenderer::destroyBindingEntries(const BindingEntry* entries, int numEntries)
{
for (int i = 0; i < numEntries; ++i)
{
destroyBindingEntry(entries[i]);
}
}
void GLRenderer::createInputBuffer(BindingEntry& rs, InputBufferDesc bufDesc, List<unsigned int>& bufferData)
{
rs.bindTarget = (bufDesc.type == InputBufferType::StorageBuffer ? GL_SHADER_STORAGE_BUFFER : GL_UNIFORM_BUFFER);
glGenBuffers(1, &rs.handle);
glBindBuffer(rs.bindTarget, rs.handle);
glBufferData(rs.bindTarget, bufferData.Count() * sizeof(unsigned int), bufferData.Buffer(), GL_STATIC_READ);
glBindBuffer(rs.bindTarget, 0);
}
void GLRenderer::createInputTexture(BindingEntry& rs, InputTextureDesc texDesc, InputSamplerDesc samplerDesc)
{
TextureData texData;
generateTextureData(texData, texDesc);
glGenTextures(1, &rs.handle);
switch (texDesc.dimension)
{
case 1:
if (texDesc.arrayLength > 0)
{
rs.bindTarget = GL_TEXTURE_1D_ARRAY;
glBindTexture(rs.bindTarget, rs.handle);
int slice = 0;
for (int i = 0; i < texData.arraySize; i++)
{
for (int j = 0; j < texData.mipLevels; j++)
{
glTexImage2D(rs.bindTarget, j, GL_RGBA8, texData.textureSize, i, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData.dataBuffer[slice].Buffer());
slice++;
}
}
}
else
{
rs.bindTarget = GL_TEXTURE_1D;
glBindTexture(rs.bindTarget, rs.handle);
for (int i = 0; i < texData.mipLevels; i++)
glTexImage1D(rs.bindTarget, i, GL_RGBA8, texData.textureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData.dataBuffer[i].Buffer());
}
break;
case 2:
if (texDesc.arrayLength > 0)
{
if (texDesc.isCube)
rs.bindTarget = GL_TEXTURE_CUBE_MAP_ARRAY;
else
rs.bindTarget = GL_TEXTURE_2D_ARRAY;
glBindTexture(rs.bindTarget, rs.handle);
for (auto i = 0u; i < texData.dataBuffer.Count(); i++)
glTexImage3D(rs.bindTarget, i % texData.mipLevels, GL_RGBA8, texData.textureSize, texData.textureSize, i, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData.dataBuffer[i].Buffer());
}
else
{
if (texDesc.isCube)
{
rs.bindTarget = GL_TEXTURE_CUBE_MAP;
glBindTexture(rs.bindTarget, rs.handle);
for (int j = 0; j < 6; j++)
{
for (int i = 0; i < texData.mipLevels; i++)
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + j, i, GL_RGBA8, texData.textureSize, texData.textureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData.dataBuffer[i + j*texData.mipLevels].Buffer());
}
}
else
{
rs.bindTarget = GL_TEXTURE_2D;
glBindTexture(rs.bindTarget, rs.handle);
for (int i = 0; i < texData.mipLevels; i++)
glTexImage2D(rs.bindTarget, i, GL_RGBA8, texData.textureSize, texData.textureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData.dataBuffer[i].Buffer());
}
}
break;
case 3:
rs.bindTarget = GL_TEXTURE_3D;
glBindTexture(rs.bindTarget, rs.handle);
for (int i = 0; i < texData.mipLevels; i++)
glTexImage3D(rs.bindTarget, i, GL_RGBA8, texData.textureSize, texData.textureSize, texData.textureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData.dataBuffer[i].Buffer());
break;
}
glTexParameteri(rs.bindTarget, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(rs.bindTarget, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(rs.bindTarget, GL_TEXTURE_WRAP_R, GL_REPEAT);
if (samplerDesc.isCompareSampler)
{
glTexParameteri(rs.bindTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(rs.bindTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(rs.bindTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, 8.0f);
}
else
{
glTexParameteri(rs.bindTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(rs.bindTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(rs.bindTarget, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(rs.bindTarget, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
}
}
void GLRenderer::createInputSampler(BindingEntry& rs, InputSamplerDesc samplerDesc)
{
glCreateSamplers(1, &rs.handle);
glSamplerParameteri(rs.handle, GL_TEXTURE_WRAP_S, GL_REPEAT);
glSamplerParameteri(rs.handle, GL_TEXTURE_WRAP_T, GL_REPEAT);
glSamplerParameteri(rs.handle, GL_TEXTURE_WRAP_R, GL_REPEAT);
if (samplerDesc.isCompareSampler)
{
glSamplerParameteri(rs.handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glSamplerParameteri(rs.handle, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glSamplerParameteri(rs.handle, GL_TEXTURE_MAX_ANISOTROPY_EXT, 8);
}
else
{
glSamplerParameteri(rs.handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glSamplerParameteri(rs.handle, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glSamplerParameteri(rs.handle, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glSamplerParameteri(rs.handle, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
}
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!! Renderer interface !!!!!!!!!!!!!!!!!!!!!!!!!!
SlangResult GLRenderer::initialize(void* inWindowHandle)
{
auto windowHandle = (HWND)inWindowHandle;
m_hdc = ::GetDC(windowHandle);
PIXELFORMATDESCRIPTOR pixelFormatDesc = { sizeof(PIXELFORMATDESCRIPTOR) };
pixelFormatDesc.nVersion = 1;
pixelFormatDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pixelFormatDesc.iPixelType = PFD_TYPE_RGBA;
pixelFormatDesc.cColorBits = 32;
pixelFormatDesc.cDepthBits = 24;
pixelFormatDesc.cStencilBits = 8;
pixelFormatDesc.iLayerType = PFD_MAIN_PLANE;
int pixelFormatIndex = ChoosePixelFormat(m_hdc, &pixelFormatDesc);
SetPixelFormat(m_hdc, pixelFormatIndex, &pixelFormatDesc);
m_glContext = wglCreateContext(m_hdc);
wglMakeCurrent(m_hdc, m_glContext);
auto renderer = glGetString(GL_RENDERER);
auto extensions = glGetString(GL_EXTENSIONS);
// Load each of our extension functions by name
#define LOAD_GL_EXTENSION_FUNC(NAME, TYPE) NAME = (TYPE) wglGetProcAddress(#NAME);
MAP_GL_EXTENSION_FUNCS(LOAD_GL_EXTENSION_FUNC)
#undef LOAD_GL_EXTENSION_FUNC
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glViewport(0, 0, gWindowWidth, gWindowHeight);
if (glDebugMessageCallback)
{
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(staticDebugCallback, this);
}
return SLANG_OK;
}
void GLRenderer::setClearColor(const float color[4])
{
glClearColor(color[0], color[1], color[2], color[3]);
}
void GLRenderer::clearFrame()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
void GLRenderer::presentFrame()
{
glFlush();
::SwapBuffers(m_hdc);
}
SlangResult GLRenderer::captureScreenShot(const char* outputPath)
{
int width = gWindowWidth;
int height = gWindowHeight;
int components = 4;
int rowStride = width*components;
GLubyte* buffer = (GLubyte*)::malloc(components * width * height);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// OpenGL's "upside down" convention bites us here, so we need
// to flip the data in the buffer by swapping rows
int halfHeight = height / 2;
for (int hh = 0; hh < halfHeight; ++hh)
{
// Get a pointer to the row data, and a pointer
// to the row on the "other end" of the image
GLubyte* rowA = buffer + rowStride*hh;
GLubyte* rowB = buffer + rowStride*(height - (hh + 1));
for (int ii = 0; ii < rowStride; ++ii)
{
auto a = rowA[ii];
auto b = rowB[ii];
rowA[ii] = b;
rowB[ii] = a;
}
}
//
int stbResult = stbi_write_png(outputPath, width, height, components, buffer, rowStride);
::free(buffer);
if (!stbResult)
{
assert(!"unexpected");
return SLANG_FAIL;
}
return SLANG_OK;
}
ShaderCompiler* GLRenderer::getShaderCompiler()
{
return this;
}
Buffer* GLRenderer::createBuffer(const BufferDesc& desc)
{
// TODO: should derive target from desc...
GLenum target = GL_UNIFORM_BUFFER;
// TODO: should derive from desc...
GLenum usage = GL_DYNAMIC_DRAW;
GLuint bufferID = 0;
glGenBuffers(1, &bufferID);
glBindBuffer(target, bufferID);
glBufferData(target, desc.size, desc.initData, usage);
return new BufferImpl(this, bufferID);
}
InputLayout* GLRenderer::createInputLayout(const InputElementDesc* inputElements, UInt inputElementCount)
{
InputLayoutImpl* inputLayout = new InputLayoutImpl;
inputLayout->m_attributeCount = inputElementCount;
for (UInt ii = 0; ii < inputElementCount; ++ii)
{
auto& inputAttr = inputElements[ii];
auto& glAttr = inputLayout->m_attributes[ii];
glAttr.streamIndex = 0;
glAttr.format = getVertexAttributeFormat(inputAttr.format);
glAttr.offset = (GLsizei)inputAttr.offset;
}
return (InputLayout*)inputLayout;
}
void* GLRenderer::map(Buffer* bufferIn, MapFlavor flavor)
{
BufferImpl* buffer = static_cast<BufferImpl*>(bufferIn);
GLenum target = GL_UNIFORM_BUFFER;
GLuint access = 0;
switch (flavor)
{
case MapFlavor::WriteDiscard:
case MapFlavor::HostWrite:
access = GL_WRITE_ONLY;
break;
case MapFlavor::HostRead:
access = GL_READ_ONLY;
break;
}
auto bufferID = buffer ? buffer->m_id : 0;
glBindBuffer(target, bufferID);
return glMapBuffer(target, access);
}
void GLRenderer::unmap(Buffer* bufferIn)
{
BufferImpl* buffer = static_cast<BufferImpl*>(bufferIn);
GLenum target = GL_UNIFORM_BUFFER;
auto bufferID = buffer ? buffer->m_id : 0;
glUnmapBuffer(target);
}
void GLRenderer::setInputLayout(InputLayout* inputLayout)
{
m_boundInputLayout = static_cast<InputLayoutImpl*>(inputLayout);
}
void GLRenderer::setPrimitiveTopology(PrimitiveTopology topology)
{
GLenum glTopology = 0;
switch (topology)
{
#define CASE(NAME, VALUE) case PrimitiveTopology::NAME: glTopology = VALUE; break
CASE(TriangleList, GL_TRIANGLES);
#undef CASE
}
m_boundPrimitiveTopology = glTopology;
}
void GLRenderer::setVertexBuffers(UInt startSlot, UInt slotCount, Buffer*const* buffers, const UInt* strides, const UInt* offsets)
{
for (UInt ii = 0; ii < slotCount; ++ii)
{
UInt slot = startSlot + ii;
BufferImpl* buffer = static_cast<BufferImpl*>(buffers[ii]);
GLuint bufferID = buffer ? buffer->m_id : 0;
m_boundVertexStreamBuffers[slot] = bufferID;
m_boundVertexStreamStrides[slot] = strides[ii];
m_boundVertexStreamOffsets[slot] = offsets[ii];
}
}
void GLRenderer::setShaderProgram(ShaderProgram* programIn)
{
ShaderProgramImpl* program = static_cast<ShaderProgramImpl*>(programIn);
m_boundShaderProgram = program;
GLuint programID = program ? program->m_id : 0;
glUseProgram(programID);
}
void GLRenderer::setConstantBuffers(UInt startSlot, UInt slotCount, Buffer*const* buffers, const UInt* offsets)
{
bindBufferImpl(GL_UNIFORM_BUFFER, startSlot, slotCount, buffers, offsets);
}
void GLRenderer::draw(UInt vertexCount, UInt startVertex = 0)
{
flushStateForDraw();
glDrawArrays(m_boundPrimitiveTopology, (GLint)startVertex, (GLsizei)vertexCount);
}
void GLRenderer::dispatchCompute(int x, int y, int z)
{
glDispatchCompute(x, y, z);
}
BindingState* GLRenderer::createBindingState(const ShaderInputLayout& layout)
{
BindingStateImpl* state = new BindingStateImpl(this);
for (auto & entry : layout.entries)
{
BindingEntry rsEntry;
rsEntry.isOutput = entry.isOutput;
rsEntry.binding = entry.glslBinding;
rsEntry.type = entry.type;
switch (entry.type)
{
case ShaderInputType::Buffer:
createInputBuffer(rsEntry, entry.bufferDesc, entry.bufferData);
break;
case ShaderInputType::Texture:
createInputTexture(rsEntry, entry.textureDesc, InputSamplerDesc());
break;
case ShaderInputType::CombinedTextureSampler:
createInputTexture(rsEntry, entry.textureDesc, entry.samplerDesc);
break;
case ShaderInputType::Sampler:
createInputSampler(rsEntry, entry.samplerDesc);
break;
}
state->m_entries.Add(rsEntry);
}
return state;
}
void GLRenderer::setBindingState(BindingState* stateIn)
{
BindingStateImpl* state = static_cast<BindingStateImpl*>(stateIn);
for (auto & entry : state->m_entries)
{
switch (entry.type)
{
case ShaderInputType::Buffer:
glBindBufferBase(entry.bindTarget, entry.binding[0], entry.handle);
break;
case ShaderInputType::Sampler:
for (auto b : entry.binding)
glBindSampler(b, entry.handle);
break;
case ShaderInputType::Texture:
case ShaderInputType::CombinedTextureSampler:
glActiveTexture(GL_TEXTURE0 + entry.binding[0]);
glBindTexture(entry.bindTarget, entry.handle);
break;
}
}
}
void GLRenderer::serializeOutput(BindingState* stateIn, const char* fileName)
{
BindingStateImpl * state = (BindingStateImpl*)stateIn;
FILE * f;
fopen_s(&f, fileName, "wt");
for (auto & entry : state->m_entries)
{
if (entry.isOutput)
{
glBindBuffer(entry.bindTarget, entry.handle);
auto ptr = (unsigned int *)glMapBuffer(entry.bindTarget, GL_READ_ONLY);
for (auto i = 0u; i < entry.bufferSize / sizeof(unsigned int); i++)
fprintf(f, "%X\n", ptr[i]);
glUnmapBuffer(entry.bindTarget);
}
}
fclose(f);
}
// ShaderCompiler interface
ShaderProgram* GLRenderer::compileProgram(const ShaderCompileRequest& request)
{
auto programID = glCreateProgram();
if (request.computeShader.name)
{
auto computeShaderID = loadShader(GL_COMPUTE_SHADER, request.computeShader.source.dataBegin);
glAttachShader(programID, computeShaderID);
glLinkProgram(programID);
glDeleteShader(computeShaderID);
}
else
{
auto vertexShaderID = loadShader(GL_VERTEX_SHADER, request.vertexShader.source.dataBegin);
auto fragmentShaderID = loadShader(GL_FRAGMENT_SHADER, request.fragmentShader.source.dataBegin);
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragmentShaderID);
glLinkProgram(programID);
glDeleteShader(vertexShaderID);
glDeleteShader(fragmentShaderID);
}
GLint success = GL_FALSE;
glGetProgramiv(programID, GL_LINK_STATUS, &success);
if (!success)
{
int maxSize = 0;
glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &maxSize);
auto infoBuffer = (char*)::malloc(maxSize);
int infoSize = 0;
glGetProgramInfoLog(programID, maxSize, &infoSize, infoBuffer);
if (infoSize > 0)
{
fprintf(stderr, "%s", infoBuffer);
OutputDebugStringA(infoBuffer);
}
::free(infoBuffer);
glDeleteProgram(programID);
return nullptr;
}
return new ShaderProgramImpl(this, programID);
}
} // renderer_test
|