summaryrefslogtreecommitdiffstats
path: root/tools/render-test/render-gl.cpp
blob: ea56a10b4c1c869edbcadb68f0cebccb2839a72c (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
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
// render-gl.cpp
#include "render-gl.h"

#include "options.h"
#include "render.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.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(glMapBuffer,          PFNGLMAPBUFFERPROC)         \
    F(glUnmapBuffer,        PFNGLUNMAPBUFFERPROC)       \
    F(glUseProgram,         PFNGLUSEPROGRAMPROC)        \
    F(glBindBufferBase,     PFNGLBINDBUFFERBASEPROC)    \
    F(glVertexAttribPointer,        PFNGLVERTEXATTRIBPOINTERPROC)       \
    F(glEnableVertexAttribArray,    PFNGLENABLEVERTEXATTRIBARRAYPROC)   \
    F(glDisableVertexAttribArray,   PFNGLDISABLEVERTEXATTRIBARRAYPROC)  \
    F(glDebugMessageCallback,       PFNGLDEBUGMESSAGECALLBACKPROC)      \
    /* end */

namespace renderer_test {

class GLRenderer : public Renderer, public ShaderCompiler
{
public:
    HDC     deviceContext;
    HGLRC   glContext;

    // Declre 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 interface

    virtual void initialize(void* inWindowHandle) override
    {
        auto windowHandle = (HWND) inWindowHandle;

        deviceContext = 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(deviceContext, &pixelFormatDesc);
        SetPixelFormat(deviceContext, pixelFormatIndex, &pixelFormatDesc);

        glContext = wglCreateContext(deviceContext);
        wglMakeCurrent(deviceContext, glContext);

        auto renderer = glGetString(GL_RENDERER);
        auto extensions = glGetString(GL_EXTENSIONS);

        // Load ech of our etension 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);
        }
    }

    void debugCallback(
        GLenum          source,
        GLenum          type,
        GLuint          id,
        GLenum          severity,
        GLsizei         length,
        GLchar const*   message)
    {
        OutputDebugStringA("GL: ");
        OutputDebugStringA(message);
        OutputDebugStringA("\n");

        switch (type)
        {
        case GL_DEBUG_TYPE_ERROR:
            assert(!"unexpected");
            break;

        default:
            break;
        }
    }

    static void APIENTRY staticDebugCallback(
        GLenum          source,
        GLenum          type,
        GLuint          id,
        GLenum          severity,
        GLsizei         length,
        GLchar const*   message,
        void const*     userParam)
    {
        ((GLRenderer*) userParam)->debugCallback(
            source, type, id, severity, length, message);
    }

    float clearColor[4] = { 0, 0, 0, 0 };
    virtual void setClearColor(float const* color) override
    {
        glClearColor(color[0], color[1], color[2], color[3]);
    }

    virtual void clearFrame() override
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    }

    virtual void presentFrame() override
    {
        glFlush();
        SwapBuffers(deviceContext);
    }

    virtual void captureScreenShot(char const* outputPath) override
    {
        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);
        if( !stbResult )
        {
            assert(!"unexpected");
        }

        delete(buffer);
    }

    virtual ShaderCompiler* getShaderCompiler() override
    {
        return this;
    }

    virtual Buffer* createBuffer(BufferDesc const& desc) override
    {
        // 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 (Buffer*)(uintptr_t)bufferID;
    }

    struct VertexAttributeFormat
    {
        GLint       componentCount;
        GLenum      componentType;
        GLboolean   normalized;
    };

    struct VertexAttributeDesc
    {
        VertexAttributeFormat   format;
        GLuint                  streamIndex;
        GLsizei                 offset;
    };

    enum
    {
        kMaxVertexStreams = 16,
    };

    struct InputLayoutImpl
    {
        VertexAttributeDesc attributes[kMaxVertexStreams];
        UInt attributeCount = 0;
    };

    static VertexAttributeFormat 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);

    #undef CASE

        }
    }

    virtual InputLayout* createInputLayout(InputElementDesc const* inputElements, UInt inputElementCount) override
    {
        InputLayoutImpl* inputLayout = new InputLayoutImpl();

        inputLayout->attributeCount = inputElementCount;
        for (UInt ii = 0; ii < inputElementCount; ++ii)
        {
            auto& inputAttr = inputElements[ii];
            auto& glAttr = inputLayout->attributes[ii];

            glAttr.streamIndex = 0;
            glAttr.format = getVertexAttributeFormat(inputAttr.format);
            glAttr.offset = inputAttr.offset;
        }

        return (InputLayout*)inputLayout;
    }

    virtual void* map(Buffer* buffer, MapFlavor flavor) override
    {
        GLenum target = GL_UNIFORM_BUFFER;

        GLuint access = 0;
        switch (flavor)
        {
        case MapFlavor::WriteDiscard:
            access = GL_WRITE_ONLY;
            break;
        }

        auto bufferID = (GLuint)(uintptr_t)buffer;
        glBindBuffer(target, bufferID);

        return glMapBuffer(target, access);
    }

    virtual void unmap(Buffer* buffer) override
    {
        GLenum target = GL_UNIFORM_BUFFER;

        auto bufferID = (GLuint)(uintptr_t)buffer;
        glUnmapBuffer(target);
    }

    InputLayoutImpl* boundInputLayout = nullptr;

    virtual void setInputLayout(InputLayout* inputLayout) override
    {
        boundInputLayout = (InputLayoutImpl*) inputLayout;
    }

    GLenum boundPrimitiveTopology = GL_TRIANGLES;

    virtual void setPrimitiveTopology(PrimitiveTopology topology) override
    {
        GLenum glTopology = 0;
        switch (topology)
        {
    #define CASE(NAME, VALUE) case PrimitiveTopology::NAME: glTopology = VALUE; break

        CASE(TriangleList, GL_TRIANGLES);

    #undef CASE
        }
        boundPrimitiveTopology = glTopology;
    }

    GLuint  boundVertexStreamBuffers[kMaxVertexStreams];
    UInt    boundVertexStreamStrides[kMaxVertexStreams];
    UInt    boundVertexStreamOffsets[kMaxVertexStreams];

    virtual void setVertexBuffers(UInt startSlot, UInt slotCount, Buffer* const* buffers, UInt const* strides, UInt const* offsets) override
    {
        for (UInt ii = 0; ii < slotCount; ++ii)
        {
            UInt slot = startSlot + ii;

            Buffer* buffer = buffers[ii];
            GLuint bufferID = (GLuint)(uintptr_t)buffer;

            boundVertexStreamBuffers[slot] = bufferID;
            boundVertexStreamStrides[slot] = strides[ii];
            boundVertexStreamOffsets[slot] = offsets[ii];
        }
    }

    virtual void setShaderProgram(ShaderProgram* program) override
    {
        GLuint programID = (GLuint)(uintptr_t)program;
        glUseProgram(programID);
    }

    virtual void setConstantBuffers(UInt startSlot, UInt slotCount, Buffer* const* buffers, UInt const* offsets) override
    {
        for (UInt ii = 0; ii < slotCount; ++ii)
        {
            UInt slot = startSlot + ii;

            Buffer* buffer = buffers[ii];
            GLuint bufferID = (GLuint)(uintptr_t)buffer;

            assert(!offsets || !offsets[ii]);

            glBindBufferBase(GL_UNIFORM_BUFFER, slot, bufferID);
        }
    }

    void flushStateForDraw()
    {
        auto layout = this->boundInputLayout;
        auto attrCount = layout->attributeCount;
        for (UInt ii = 0; ii < attrCount; ++ii)
        {
            auto& attr = layout->attributes[ii];

            auto streamIndex = attr.streamIndex;

            glBindBuffer(GL_ARRAY_BUFFER, boundVertexStreamBuffers[streamIndex]);

            glVertexAttribPointer(
                ii,
                attr.format.componentCount,
                attr.format.componentType,
                attr.format.normalized,
                boundVertexStreamStrides[streamIndex],
                (GLvoid*)(attr.offset + boundVertexStreamOffsets[streamIndex]));

            glEnableVertexAttribArray(ii);
        }
        for (UInt ii = attrCount; ii < kMaxVertexStreams; ++ii)
        {
            glDisableVertexAttribArray(ii);
        }
    }

    virtual void draw(UInt vertexCount, UInt startVertex = 0) override
    {
        flushStateForDraw();

        glDrawArrays(boundPrimitiveTopology, startVertex, vertexCount);
    }

    // ShaderCompiler interface

    virtual ShaderProgram* compileProgram(ShaderCompileRequest const& request) override
    {
        auto programID = glCreateProgram();

        auto vertexShaderID   = loadShader(GL_VERTEX_SHADER,   request.vertexShader  .source.text);
        auto fragmentShaderID = loadShader(GL_FRAGMENT_SHADER, request.fragmentShader.source.text);

        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);
            }

            glDeleteProgram(programID);
            return 0;
        }

        return (ShaderProgram*) (uintptr_t) programID;
    }

    GLuint loadShader(GLenum stage, char const* source)
    {
        auto shaderID = glCreateShader(stage);

        char const* 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
        }

        char const* prelude =
            "#define __GLSL__ 1\n"
            ;

        char const* sourceStrings[] =
        {
            stagePrelude,
            prelude,
            source,
        };

        glShaderSource(
            shaderID,
            sizeof(sourceStrings) / sizeof(sourceStrings[0]),
            &sourceStrings[0],
            nullptr);
        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;
    }
};



Renderer* createGLRenderer()
{
    return new GLRenderer();
}

} // renderer_test