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

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

// In order to use the Slang API, we need to include its header

#include <slang.h>

#ifdef _MSC_VER
#pragma warning(disable: 4996)
#endif
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "external/stb/stb_image_write.h"

// We will be rendering with Direct3D 11, so we need to include
// the Windows and D3D11 headers

#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#undef WIN32_LEAN_AND_MEAN
#undef NOMINMAX

#include <d3d11_2.h>
#include <d3dcompiler.h>

// We will use the C standard library just for printing error messages.
#include <stdio.h>

#ifdef _MSC_VER
#include <stddef.h>
#if (_MSC_VER < 1900)
#define snprintf sprintf_s
#endif
#endif
//

namespace renderer_test {

//


//

// Global variabels for the various D3D11 API objects to be used for rendering
ID3D11Buffer*       dxConstantBuffer;
ID3D11InputLayout*  dxInputLayout;
ID3D11Buffer*       dxVertexBuffer;
ID3D11VertexShader* dxVertexShader;
ID3D11PixelShader*  dxPixelShader;

// The Slang compiler currently generates HLSL source, so we'll need a utility
// routine (defined later) to translate that into D3D11 shader bytecode.
ID3DBlob* compileHLSLShader(
    char const* sourcePath,
    char const* source,
    char const* entryPointName,
    char const* dxProfileName);

static char const* vertexEntryPointName    = "vertexMain";
static char const* fragmentEntryPointName  = "fragmentMain";

static char const* vertexProfileName   = "vs_4_0";
static char const* fragmentProfileName = "ps_4_0";

ID3DBlob* gVertexShaderBlob;
ID3DBlob* gPixelShaderBlob;

//
// Definition of the HLSL-to-bytecode compilation logic.
//
ID3DBlob* compileHLSLShader(
    char const* sourcePath,
    char const* source,
    char const* entryPointName,
    char const* dxProfileName )
{
    // Rather than statically link against the `d3dcompile` library, we
    // dynamically load it.
    //
    // Note: A more realistic application would compile from HLSL text to D3D
    // shader bytecode as part of an offline process, rather than doing it
    // on-the-fly like this
    //
    static pD3DCompile D3DCompile_ = nullptr;
    if( !D3DCompile_ )
    {
        // TODO(tfoley): maybe want to search for one of a few versions of the DLL
        HMODULE d3dcompiler = LoadLibraryA("d3dcompiler_47.dll");
        if(!d3dcompiler)
        {
            fprintf(stderr, "error: failed load 'd3dcompiler_47.dll'\n");
            exit(1);
        }

        D3DCompile_ = (pD3DCompile)GetProcAddress(d3dcompiler, "D3DCompile");
        if( !D3DCompile_ )
        {
            fprintf(stderr, "error: failed load symbol 'D3DCompile'\n");
            exit(1);
        }
    }

    // For this example, we turn on debug output, and turn off all
    // optimization. A real application would only use these flags
    // when shader debugging is needed.
    UINT flags = 0;
    flags |= D3DCOMPILE_DEBUG;
    flags |= D3DCOMPILE_OPTIMIZATION_LEVEL0 | D3DCOMPILE_SKIP_OPTIMIZATION;

    // We will always define `__HLSL__` when compiling here, so that
    // input code can react differently to being compiled as pure HLSL.
    D3D_SHADER_MACRO defines[] = {
        { "__HLSL__", "1" },
        { nullptr, nullptr },
    };

    // The `D3DCompile` entry point takes a bunch of parameters, but we
    // don't really need most of them for Slang-generated code.
    ID3DBlob* dxShaderBlob = nullptr;
    ID3DBlob* dxErrorBlob = nullptr;
    HRESULT hr = D3DCompile_(
        source,
        strlen(source),
        sourcePath,
        &defines[0],
        nullptr,
        entryPointName,
        dxProfileName,
        flags,
        0,
        &dxShaderBlob,
        &dxErrorBlob);

    // If the HLSL-to-bytecode compilation produced any diagnostic messages
    // then we will print them out (whether or not the compilation failed).
    if( dxErrorBlob )
    {
        fputs(
            (char const*)dxErrorBlob->GetBufferPointer(),
            stderr);
        fflush(stderr);

        OutputDebugStringA(
            (char const*)dxErrorBlob->GetBufferPointer());

        dxErrorBlob->Release();
    }

    if( FAILED(hr) )
    {
        return nullptr;
    }

    return dxShaderBlob;
}




// Capture a texture to a file

static HRESULT captureTextureToFile(
    ID3D11Device*           dxDevice,
    ID3D11DeviceContext*    dxContext,
    ID3D11Texture2D*        dxTexture,
    char const*             outputPath)
{
    if(!dxContext) return E_INVALIDARG;
    if(!dxTexture) return E_INVALIDARG;

    D3D11_TEXTURE2D_DESC dxTextureDesc;
    dxTexture->GetDesc(&dxTextureDesc);

    // Don't bother supporing MSAA for right now
    if( dxTextureDesc.SampleDesc.Count > 1 )
    {
        fprintf(stderr, "ERROR: cannot capture multisample texture\n");
        return E_INVALIDARG;
    }

    HRESULT hr = S_OK;
    ID3D11Texture2D* dxStagingTexture = nullptr;

    if( dxTextureDesc.Usage == D3D11_USAGE_STAGING && (dxTextureDesc.CPUAccessFlags & D3D11_CPU_ACCESS_READ) )
    {
        dxStagingTexture = dxTexture;
        dxStagingTexture->AddRef();
    }
    else
    {
        // Modify the descriptor to give us a staging texture
        dxTextureDesc.BindFlags = 0;
        dxTextureDesc.MiscFlags &= ~D3D11_RESOURCE_MISC_TEXTURECUBE;
        dxTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
        dxTextureDesc.Usage = D3D11_USAGE_STAGING;

        hr = dxDevice->CreateTexture2D(&dxTextureDesc, 0, &dxStagingTexture);
        if( FAILED(hr) )
        {
            fprintf(stderr, "ERROR: failed to create staging texture\n");
            return hr;
        }
    
        dxContext->CopyResource(dxStagingTexture, dxTexture);
    }

    // Now just read back texels from the staging textures

    D3D11_MAPPED_SUBRESOURCE dxMappedResource;
    hr = dxContext->Map(dxStagingTexture, 0, D3D11_MAP_READ, 0, &dxMappedResource);
    if( FAILED(hr) )
    {
        fprintf(stderr, "ERROR: failed to map texture for read\n");
        return hr;
    }

    int stbResult = stbi_write_png(
        outputPath,
        dxTextureDesc.Width,
        dxTextureDesc.Height,
        4,
        dxMappedResource.pData,
        dxMappedResource.RowPitch);
    if( !stbResult )
    {
        fprintf(stderr, "ERROR: failed to write texture to file\n");
        return E_UNEXPECTED;
    }

    dxContext->Unmap(dxStagingTexture, 0);

    dxStagingTexture->Release();

    return S_OK;
}

//

class D3D11Renderer : public Renderer, public ShaderCompiler
{
public:
    IDXGISwapChain* dxSwapChain = NULL;
    ID3D11Device* dxDevice = NULL;
    ID3D11DeviceContext* dxImmediateContext = NULL;
    ID3D11Texture2D* dxBackBufferTexture = NULL;
    ID3D11RenderTargetView* dxBackBufferRTV = NULL;

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

        // Rather than statically link against D3D, we load it dynamically.

        HMODULE d3d11 = LoadLibraryA("d3d11.dll");
        if(!d3d11)
        {
            fprintf(stderr, "error: failed load 'd3d11.dll'\n");
            exit(1);
        }

        PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN D3D11CreateDeviceAndSwapChain_ =
            (PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN)GetProcAddress(
                d3d11,
                "D3D11CreateDeviceAndSwapChain");
        if(!D3D11CreateDeviceAndSwapChain_)
        {
            fprintf(stderr,
                "error: failed load symbol 'D3D11CreateDeviceAndSwapChain'\n");
            exit(1);
        }

        // We create our device in debug mode, just so that we can check that the
        // example doesn't trigger warnings.
        UINT deviceFlags = 0;
        deviceFlags |= D3D11_CREATE_DEVICE_DEBUG;

        // We will ask for the highest feature level that can be supported.

        D3D_FEATURE_LEVEL featureLevels[] = {
            D3D_FEATURE_LEVEL_11_1,
            D3D_FEATURE_LEVEL_11_0,
            D3D_FEATURE_LEVEL_10_1,
            D3D_FEATURE_LEVEL_10_0,
            D3D_FEATURE_LEVEL_9_3,
            D3D_FEATURE_LEVEL_9_2,
            D3D_FEATURE_LEVEL_9_1,
        };
        D3D_FEATURE_LEVEL dxFeatureLevel = D3D_FEATURE_LEVEL_9_1;

        // Our swap chain uses RGBA8 with sRGB, with double buffering.

        DXGI_SWAP_CHAIN_DESC dxSwapChainDesc = { 0 };
        dxSwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;

        // Note(tfoley): Disabling sRGB for DX back buffer for now, so that we
        // can get consistent output with OpenGL, where setting up sRGB will
        // probably be more involved.
//        dxSwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
        dxSwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;

        dxSwapChainDesc.SampleDesc.Count = 1;
        dxSwapChainDesc.SampleDesc.Quality = 0;
        dxSwapChainDesc.BufferCount = 2;
        dxSwapChainDesc.OutputWindow = windowHandle;
        dxSwapChainDesc.Windowed = TRUE;
        dxSwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
        dxSwapChainDesc.Flags = 0;

        // On a machine that does not have an up-to-date version of D3D installed,
        // the `D3D11CreateDeviceAndSwapChain` call will fail with `E_INVALIDARG`
        // if you ask for featuer level 11_1. The workaround is to call
        // `D3D11CreateDeviceAndSwapChain` up to twice: the first time with 11_1
        // at the start of the list of requested feature levels, and the second
        // time without it.

        HRESULT hr = S_OK;
        for( int ii = 0; ii < 2; ++ii )
        {
            hr = D3D11CreateDeviceAndSwapChain_(
                NULL,                    // adapter (use default)

                D3D_DRIVER_TYPE_WARP,
//                D3D_DRIVER_TYPE_HARDWARE,

                NULL,                    // software
                deviceFlags,
                &featureLevels[ii],
                (sizeof(featureLevels) / sizeof(featureLevels[0])) - 1,
                D3D11_SDK_VERSION,
                &dxSwapChainDesc,
                &dxSwapChain,
                &dxDevice,
                &dxFeatureLevel,
                &dxImmediateContext);

            // Failures with `E_INVALIDARG` might be due to feature level 11_1
            // not being supported. Other failures are real, though.
            if( hr != E_INVALIDARG )
                break;
        }
        if( FAILED(hr) )
        {
            exit(1);
        }

        // After we've created the swap chain, we can request a pointer to the
        // back buffer as a D3D11 texture, and create a render-target view from it.

        static const IID kIID_ID3D11Texture2D = {
            0x6f15aaf2, 0xd208, 0x4e89, 0x9a, 0xb4, 0x48,
            0x95, 0x35, 0xd3, 0x4f, 0x9c };
        dxSwapChain->GetBuffer(
            0,
            kIID_ID3D11Texture2D,
            (void**)&dxBackBufferTexture);

        dxDevice->CreateRenderTargetView(
            dxBackBufferTexture,
            NULL,
            &dxBackBufferRTV);

        // We immediately bind the back-buffer render target view, and we aren't
        // going to switch. We don't bother with a depth buffer.
        dxImmediateContext->OMSetRenderTargets(
            1,
            &dxBackBufferRTV,
            NULL);

        // Similarly, we are going to set up a viewport once, and then never
        // switch, since this is a simple test app.
        D3D11_VIEWPORT dxViewport;
        dxViewport.TopLeftX = 0;
        dxViewport.TopLeftY = 0;
        dxViewport.Width = (float) gWindowWidth;
        dxViewport.Height = (float) gWindowHeight;
        dxViewport.MaxDepth = 1; // TODO(tfoley): use reversed depth
        dxViewport.MinDepth = 0;
        dxImmediateContext->RSSetViewports(1, &dxViewport);
    }

    float clearColor[4] = { 0, 0, 0, 0 };
    virtual void setClearColor(float const* color) override
    {
        memcpy(clearColor, color, sizeof(clearColor));
    }

    virtual void clearFrame() override
    {
        dxImmediateContext->ClearRenderTargetView(
            dxBackBufferRTV,
            clearColor);
    }

    virtual void presentFrame() override
    {
        dxSwapChain->Present(0, 0);
    }

    virtual void captureScreenShot(char const* outputPath) override
    {
        HRESULT hr = captureTextureToFile(
            dxDevice,
            dxImmediateContext,
            dxBackBufferTexture,
            outputPath);
        if( FAILED(hr) )
        {
            fprintf(stderr, "error: could not capture screenshot to '%s'\n", outputPath);
            exit(1);
        }
    }

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

    virtual Buffer* createBuffer(BufferDesc const& desc) override
    {
        D3D11_BUFFER_DESC dxBufferDesc = { 0 };
        dxBufferDesc.ByteWidth = (UINT) desc.size;

        switch( desc.flavor )
        {
        case BufferFlavor::Constant:
            dxBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
            dxBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
            dxBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
            break;

        case BufferFlavor::Vertex:
            dxBufferDesc.Usage = D3D11_USAGE_DEFAULT;
            dxBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
            dxBufferDesc.CPUAccessFlags = 0;
            break;

        default:
            return nullptr;
        }

        D3D11_SUBRESOURCE_DATA dxInitData = { 0 };
        dxInitData.pSysMem = desc.initData;


        ID3D11Buffer* dxBuffer = nullptr;
        HRESULT hr = dxDevice->CreateBuffer(
            &dxBufferDesc,
            desc.initData ? &dxInitData : nullptr,
            &dxBuffer);
        if(FAILED(hr)) return nullptr;

        return (Buffer*) dxBuffer;
    }

    static DXGI_FORMAT mapFormat(Format format)
    {
        switch( format )
        {
        case Format::RGB_Float32:
            return DXGI_FORMAT_R32G32B32_FLOAT;

        default:
            return DXGI_FORMAT_UNKNOWN;
        }
    }

    virtual InputLayout* createInputLayout(InputElementDesc const* inputElements, UInt inputElementCount) override
    {
        D3D11_INPUT_ELEMENT_DESC dxInputElements[16] = {};

        char hlslBuffer[1024];
        char* hlslCursor = &hlslBuffer[0];

        hlslCursor += sprintf(hlslCursor, "float4 main(\n");

        for( UInt ii = 0; ii < inputElementCount; ++ii )
        {
            dxInputElements[ii].SemanticName            = inputElements[ii].semanticName;
            dxInputElements[ii].SemanticIndex           = (UINT) inputElements[ii].semanticIndex;
            dxInputElements[ii].Format                  = mapFormat(inputElements[ii].format);
            dxInputElements[ii].InputSlot               = 0;
            dxInputElements[ii].AlignedByteOffset       = (UINT) inputElements[ii].offset;
            dxInputElements[ii].InputSlotClass          = D3D11_INPUT_PER_VERTEX_DATA;
            dxInputElements[ii].InstanceDataStepRate    = 0;

            if(ii != 0)
            {
                hlslCursor+= sprintf(hlslCursor, ",\n");
            }

            char const* typeName = "Uknown";
            switch(inputElements[ii].format)
            {
            case Format::RGB_Float32:
                typeName = "float3";
                break;

            default:
                return nullptr;
            }

            hlslCursor+= sprintf(hlslCursor, "%s a%d : %s%d",
                typeName,
                (int) ii,
                inputElements[ii].semanticName,
                (int) inputElements[ii].semanticIndex);
        }

        hlslCursor += sprintf(hlslCursor, "\n) : SV_Position { return 0; }");

        auto dxVertexShaderBlob = compileHLSLShader("inputLayout", hlslBuffer, "main", "vs_4_0");
        if(!dxVertexShaderBlob)
            return nullptr;

        ID3D11InputLayout* dxInputLayout = nullptr;
        HRESULT hr = dxDevice->CreateInputLayout(
            &dxInputElements[0],
            (UINT) inputElementCount,
            dxVertexShaderBlob->GetBufferPointer(),
            dxVertexShaderBlob->GetBufferSize(),
            &dxInputLayout);

        dxVertexShaderBlob->Release();

        if(FAILED(hr))
            return nullptr;

        return (InputLayout*) dxInputLayout;
    }

    virtual void* map(Buffer* buffer, MapFlavor flavor) override
    {
        auto dxContext = dxImmediateContext;

        auto dxBuffer = (ID3D11Buffer*) buffer;

        D3D11_MAP dxMapFlavor;
        switch( flavor )
        {
        case MapFlavor::WriteDiscard:
            dxMapFlavor = D3D11_MAP_WRITE_DISCARD;
            break;

        default:
            return nullptr;
        }

        // We update our constant buffer per-frame, just for the purposes
        // of the example, but we don't actually load different data
        // per-frame (we always use an identity projection).
        D3D11_MAPPED_SUBRESOURCE dxMapped;
        HRESULT hr = dxContext->Map(dxBuffer, 0, dxMapFlavor, 0, &dxMapped);
        if(FAILED(hr))
            return nullptr;

        return dxMapped.pData;
    }

    virtual void unmap(Buffer* buffer) override
    {
        auto dxContext = dxImmediateContext;

        auto dxBuffer = (ID3D11Buffer*) buffer;

        dxContext->Unmap(dxBuffer, 0);
    }

    virtual void setInputLayout(InputLayout* inputLayout) override
    {
        auto dxContext = dxImmediateContext;
        auto dxInputLayout = (ID3D11InputLayout*) inputLayout;

        dxContext->IASetInputLayout(dxInputLayout);
    }

    virtual void setPrimitiveTopology(PrimitiveTopology topology) override
    {
        auto dxContext = dxImmediateContext;

        D3D11_PRIMITIVE_TOPOLOGY dxTopology;
        switch( topology )
        {
        case PrimitiveTopology::TriangleList:
            dxTopology = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
            break;

        default:
            return;
        }

        dxContext->IASetPrimitiveTopology(dxTopology);
    }

    virtual void setVertexBuffers(UInt startSlot, UInt slotCount, Buffer* const* buffers, UInt const* strides, UInt const* offsets) override
    {
        auto dxContext = dxImmediateContext;

        static const int kMaxVertexBuffers = 16;

        UINT dxVertexStrides[kMaxVertexBuffers];
        UINT dxVertexOffsets[kMaxVertexBuffers];

        for( UInt ii = 0; ii < slotCount; ++ii )
        {
            dxVertexStrides[ii] = (UINT) strides[ii];
            dxVertexOffsets[ii] = (UINT) offsets[ii];
        }

        auto dxVertexBuffers = (ID3D11Buffer* const*) buffers;

        dxContext->IASetVertexBuffers(
            (UINT) startSlot,
            (UINT) slotCount, &dxVertexBuffers[0], &dxVertexStrides[0], &dxVertexOffsets[0]);
    }

    virtual void setShaderProgram(ShaderProgram* inProgram) override
    {
        auto dxContext = dxImmediateContext;
        
        auto program = (D3D11ShaderProgram*) inProgram;

        dxContext->VSSetShader(program->dxVertexShader, NULL, 0);
        dxContext->PSSetShader(program->dxPixelShader,  NULL, 0);
    }

    virtual void setConstantBuffers(UInt startSlot, UInt slotCount, Buffer* const* buffers, UInt const* offsets) override
    {
        auto dxContext = dxImmediateContext;

        // TODO: actually use those offsets

        auto dxConstantBuffers = (ID3D11Buffer* const*) buffers;

        dxContext->VSSetConstantBuffers(
            (UINT) startSlot, (UINT) slotCount, &dxConstantBuffers[0]);
        dxContext->VSSetConstantBuffers(
            (UINT) startSlot, (UINT) slotCount, &dxConstantBuffers[0]);
    }


    virtual void draw(UInt vertexCount, UInt startVertex) override
    {
        auto dxContext = dxImmediateContext;

        dxContext->Draw((UINT) vertexCount, (UINT) startVertex);
    }


    // ShaderCompiler interface

    struct D3D11ShaderProgram
    {
        ID3D11VertexShader* dxVertexShader;
        ID3D11PixelShader*  dxPixelShader;
    };

    virtual ShaderProgram* compileProgram(ShaderCompileRequest const& request) override
    {
        auto dxVertexShaderBlob     = compileHLSLShader(request.vertexShader.source.path, request.vertexShader.source.text, request.vertexShader    .name,  request.vertexShader    .profile);
        if(!dxVertexShaderBlob)     return nullptr;

        auto dxFragmentShaderBlob   = compileHLSLShader(request.fragmentShader.source.path, request.fragmentShader.source.text, request.fragmentShader  .name,  request.fragmentShader  .profile);
        if(!dxFragmentShaderBlob)   return nullptr;

        ID3D11VertexShader* dxVertexShader;
        ID3D11PixelShader*  dxPixelShader;

        HRESULT vsResult = dxDevice->CreateVertexShader(  dxVertexShaderBlob  ->GetBufferPointer(),   dxVertexShaderBlob  ->GetBufferSize(), nullptr, &dxVertexShader);
        HRESULT psResult = dxDevice->CreatePixelShader(   dxFragmentShaderBlob->GetBufferPointer(),   dxFragmentShaderBlob->GetBufferSize(), nullptr, &dxPixelShader);

        dxVertexShaderBlob  ->Release();
        dxFragmentShaderBlob->Release();

        if(FAILED(vsResult)) return nullptr;
        if(FAILED(psResult)) return nullptr;

        D3D11ShaderProgram* shaderProgram = new D3D11ShaderProgram();
        shaderProgram->dxVertexShader   = dxVertexShader;
        shaderProgram->dxPixelShader    = dxPixelShader;
        return (ShaderProgram*) shaderProgram;
    }
};



Renderer* createD3D11Renderer()
{
    return new D3D11Renderer();
}

} // renderer_test