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

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

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

#include <slang.h>

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

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

#include <dxgi1_4.h>
#include <d3d12.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
//
using namespace Slang;

#define ENABLE_DEBUG_LAYER 1

namespace renderer_test {

// 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* vertexProfileName   = "vs_4_0";
static char const* fragmentProfileName = "ps_4_0";

//

class D3D12Renderer : public Renderer, public ShaderCompiler
{
public:
    IDXGISwapChain* dxSwapChain = NULL;

    ID3D12Device* dxDevice = NULL;

    virtual PROC loadProc(
        HMODULE module,
        char const* name)
    {
        PROC proc = GetProcAddress(module, name);
        if( !proc )
        {
            fprintf(stderr,
                "error: failed load symbol '%s'\n", name);
            exit(1);
        }
        return proc;
    }

    void checkResult(HRESULT result)
    {
        assert(SUCCEEDED(result));
    }

    virtual void initialize(void* inWindowHandle) override
    {
        auto windowHandle = (HWND) inWindowHandle;
        // Rather than statically link against D3D, we load it dynamically.

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

#define LOAD_PROC(TYPE, NAME) \
        TYPE NAME##_ = (TYPE) loadProc(d3d12, #NAME)


        UINT dxgiFactoryFlags = 0;

#if ENABLE_DEBUG_LAYER
        LOAD_PROC(PFN_D3D12_GET_DEBUG_INTERFACE, D3D12GetDebugInterface);

        ID3D12Debug* debugController;
        if(SUCCEEDED(D3D12GetDebugInterface_(IID_PPV_ARGS(&debugController))))
        {
            debugController->EnableDebugLayer();
            dxgiFactoryFlags |= DXGI_CREATE_FACTORY_DEBUG;
        }
#endif
        
        typedef HRESULT (WINAPI *PFN_DXGI_CREATE_FACTORY_2)(UINT Flags, REFIID riid, _COM_Outptr_ void **ppFactory);


        LOAD_PROC(PFN_DXGI_CREATE_FACTORY_2, CreateDXGIFactory2);

        IDXGIFactory4* dxgiFactory;
        checkResult(CreateDXGIFactory2_(dxgiFactoryFlags, IID_PPV_ARGS(&dxgiFactory)));

        D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;

        // Search for an adapter that meets our requirements
        IDXGIAdapter* adapter = nullptr;

        LOAD_PROC(PFN_D3D12_CREATE_DEVICE, D3D12CreateDevice);

        UINT adapterCounter = 0;
        for(;;)
        {
            UINT adapterIndex = adapterCounter++;
            IDXGIAdapter1* candidateAdapter = nullptr;
            if(dxgiFactory->EnumAdapters1(adapterIndex, &candidateAdapter) == DXGI_ERROR_NOT_FOUND)
                break;

            DXGI_ADAPTER_DESC1 desc;
            candidateAdapter->GetDesc1(&desc);

            if( desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE )
            {
                // TODO: may want to allow software driver as fallback
            }
            else if( SUCCEEDED(D3D12CreateDevice_(
                candidateAdapter, featureLevel, IID_PPV_ARGS(&dxDevice))) )
            {
                // We found one!
                adapter = candidateAdapter;
                break;
            }

            candidateAdapter->Release();
        }

        if(!adapter)
        {
            return;
        }

        // Command Queue

        D3D12_COMMAND_QUEUE_DESC queueDesc = {};
        queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;

        ID3D12CommandQueue* commandQueue;
        checkResult(dxDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&commandQueue)));

        // Swap Chain


        UINT frameCount = 2; // TODO: configure

        DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
        swapChainDesc.BufferCount = frameCount;
        swapChainDesc.Width = gWindowWidth;
        swapChainDesc.Height = gWindowHeight;
        swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
        swapChainDesc.SampleDesc.Count = 1;

        IDXGISwapChain1* swapChain;
        checkResult(dxgiFactory->CreateSwapChainForHwnd(
            commandQueue,
            windowHandle,
            &swapChainDesc,
            nullptr,
            nullptr,
            &swapChain));

        // Is this needed?
        dxgiFactory->MakeWindowAssociation(
            windowHandle, DXGI_MWA_NO_ALT_ENTER);

        IDXGISwapChain3* swapChainEx;
        swapChain->QueryInterface(IID_PPV_ARGS(&swapChainEx));

        UINT frameIndex = swapChainEx->GetCurrentBackBufferIndex();

        // Descriptor heaps

        D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
        rtvHeapDesc.NumDescriptors = frameCount;
        rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;

        ID3D12DescriptorHeap* rtvHeap;
        checkResult(dxDevice->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&rtvHeap)));

        UINT rtvDescriptorSize = dxDevice->GetDescriptorHandleIncrementSize(
            D3D12_DESCRIPTOR_HEAP_TYPE_RTV);

        D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = rtvHeap->GetCPUDescriptorHandleForHeapStart();

        // Create per-frame RTVs
        ID3D12Resource* backBufferResources[2];
        for( UINT ff = 0; ff < frameCount; ++ff )
        {
            checkResult(swapChainEx->GetBuffer(ff, IID_PPV_ARGS(&backBufferResources[ff])));
            dxDevice->CreateRenderTargetView(
                backBufferResources[ff],
                nullptr,
                rtvHandle);
            rtvHandle.ptr += rtvDescriptorSize;
        }

        ID3D12CommandAllocator* commandAllocator;
        checkResult(dxDevice->CreateCommandAllocator(
            D3D12_COMMAND_LIST_TYPE_DIRECT,
            IID_PPV_ARGS(&commandAllocator)));
    }

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

    virtual void clearFrame() override
    {
    }

    virtual void presentFrame() override
    {
    }

    virtual void captureScreenShot(char const* outputPath) override
    {
    }

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

    virtual Buffer* createBuffer(BufferDesc const& desc) override
    {
        return nullptr;
    }

    static DXGI_FORMAT mapFormat(Format format)
    {
        switch( format )
        {
        case Format::RGB_Float32:
            return DXGI_FORMAT_R32G32B32_FLOAT;
        case Format::RG_Float32:
            return DXGI_FORMAT_R32G32_FLOAT;
        default:
            return DXGI_FORMAT_UNKNOWN;
        }
    }

    virtual InputLayout* createInputLayout(InputElementDesc const* inputElements, UInt inputElementCount) override
    {
        return nullptr;
    }

    virtual void* map(Buffer* buffer, MapFlavor flavor) override
    {
        return nullptr;
    }

    virtual void unmap(Buffer* buffer) override
    {
    }

    virtual void setInputLayout(InputLayout* inputLayout) override
    {
    }

    virtual void setPrimitiveTopology(PrimitiveTopology topology) override
    {
    }

    virtual void setVertexBuffers(UInt startSlot, UInt slotCount, Buffer* const* buffers, UInt const* strides, UInt const* offsets) override
    {
    }

    virtual void setShaderProgram(ShaderProgram* inProgram) override
    {
    }

    virtual void setConstantBuffers(UInt startSlot, UInt slotCount, Buffer* const* buffers, UInt const* offsets) override
    {
    }

    virtual void draw(UInt vertexCount, UInt startVertex) override
    {
    }


    virtual void dispatchCompute(int x, int y, int z) override
    {
    }

    virtual BindingState * createBindingState(const ShaderInputLayout & layout)
    {
        return nullptr;
    }

    virtual void setBindingState(BindingState * state)
    {
    }

    virtual void serializeOutput(BindingState* state, const char * fileName)
    {
    }

    // ShaderCompiler interface

    virtual ShaderProgram* compileProgram(ShaderCompileRequest const& request) override
    {
        return nullptr;
    }

};

Renderer* createD3D12Renderer()
{
    return new D3D12Renderer();
}

} // renderer_test