aboutsummaryrefslogtreecommitdiffstats
path: root/Samples/CommonSrc/Render/Render_D3D1X_Device.h
blob: 63e8e67cbc65455f71bf0b66264a75bbadea81b4 (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
/************************************************************************************

Filename    :   Render_D3D1X_Device.h
Content     :   RenderDevice implementation header for D3DX10/11.
Created     :   September 10, 2012
Authors     :   Andrew Reisse

Copyright   :   Copyright 2012 Oculus VR, LLC All Rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

// ***** IMPORTANT:
// This file can be included twice, once with OVR_D3D_VERSION=10 and
// once with OVR_D3D_VERSION=11.


#ifndef OVR_D3D_VERSION
#error define OVR_D3D_VERSION to 10 or 11
#endif

#include "Kernel/OVR_String.h"
#include "Kernel/OVR_Array.h"

#if (OVR_D3D_VERSION == 10 && !defined(_OVR_RENDERER_D3D10)) || \
    (OVR_D3D_VERSION == 11 && !defined(_OVR_RENDERER_D3D11))

#include "../Render/Render_Device.h"

#include <WinSock2.h>
#include <WS2tcpip.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#if (OVR_D3D_VERSION == 10)
#define _OVR_RENDERER_D3D10
#include <d3d10_1.h>

namespace OVR { namespace Render { namespace D3D10 {

#else // 11

#define _OVR_RENDERER_D3D11
#include <d3d11.h>

namespace OVR { namespace Render { namespace D3D11 {
#endif

class RenderDevice;

#ifdef D3D1x_
#undef D3D1x_
#endif
#ifdef ID3D1x
#undef ID3D1x
#endif

#if (OVR_D3D_VERSION == 10)
typedef ID3D10Device1           ID3D1xDevice;
typedef ID3D10Device1           ID3D1xDeviceContext;
typedef ID3D10RenderTargetView  ID3D1xRenderTargetView;
typedef ID3D10Texture2D         ID3D1xTexture2D;
typedef ID3D10ShaderResourceView ID3D1xShaderResourceView;
typedef ID3D10DepthStencilView  ID3D1xDepthStencilView;
typedef ID3D10DepthStencilState ID3D1xDepthStencilState;
typedef ID3D10InputLayout       ID3D1xInputLayout;
typedef ID3D10Buffer            ID3D1xBuffer;
typedef ID3D10Resource          ID3D1xResource;
typedef ID3D10VertexShader      ID3D1xVertexShader;
typedef ID3D10PixelShader       ID3D1xPixelShader;
typedef ID3D10GeometryShader    ID3D1xGeometryShader;
typedef ID3D10BlendState        ID3D1xBlendState;
typedef ID3D10RasterizerState   ID3D1xRasterizerState;
typedef ID3D10SamplerState      ID3D1xSamplerState;
typedef ID3D10Query             ID3D1xQuery;
typedef ID3D10Blob              ID3D1xBlob;
typedef D3D10_VIEWPORT          D3D1x_VIEWPORT;
typedef D3D10_QUERY_DESC        D3D1x_QUERY_DESC;
#define D3D1x_(x)               D3D10_##x
#define ID3D1x(x)               ID3D10##x

#else // D3D 11
typedef ID3D11Device            ID3D1xDevice;
typedef ID3D11DeviceContext     ID3D1xDeviceContext;
typedef ID3D11RenderTargetView  ID3D1xRenderTargetView;
typedef ID3D11Texture2D         ID3D1xTexture2D;
typedef ID3D11ShaderResourceView ID3D1xShaderResourceView;
typedef ID3D11DepthStencilView  ID3D1xDepthStencilView;
typedef ID3D11DepthStencilState ID3D1xDepthStencilState;
typedef ID3D11InputLayout       ID3D1xInputLayout;
typedef ID3D11Buffer            ID3D1xBuffer;
typedef ID3D11Resource          ID3D1xResource;
typedef ID3D10Blob              ID3D1xBlob;
typedef ID3D11VertexShader      ID3D1xVertexShader;
typedef ID3D11PixelShader       ID3D1xPixelShader;
typedef ID3D11GeometryShader    ID3D1xGeometryShader;
typedef ID3D11BlendState        ID3D1xBlendState;
typedef ID3D11RasterizerState   ID3D1xRasterizerState;
typedef ID3D11SamplerState      ID3D1xSamplerState;
typedef ID3D11Query             ID3D1xQuery;
typedef D3D11_VIEWPORT          D3D1x_VIEWPORT;
typedef D3D11_QUERY_DESC        D3D1x_QUERY_DESC;
#define D3D1x_(x)               D3D11_##x
#define ID3D1x(x)               ID3D11##x
#endif

class Buffer;


class ShaderBase : public Render::Shader
{
public:
    RenderDevice*   Ren;
    unsigned char*  UniformData;
    int             UniformsSize;

    struct Uniform
    {
        String Name;
        int    Offset;
        int    Size;
    };
    Array<Uniform> UniformInfo;

    ShaderBase(RenderDevice* r, ShaderStage stage);
    ~ShaderBase();

    void InitUniforms(ID3D10Blob* s);
    bool SetUniform(const char* name, int n, const float* v);
    //virtual bool UseTransposeMatrix() const { return 1; }

    void UpdateBuffer(Buffer* b);
};


template<Render::ShaderStage SStage, class D3DShaderType>
class Shader : public ShaderBase
{
public:
    D3DShaderType*  D3DShader;

    Shader(RenderDevice* r, D3DShaderType* s) : ShaderBase(r, SStage), D3DShader(s) {}
    Shader(RenderDevice* r, ID3D1xBlob* s) : ShaderBase(r, SStage)
    {
        Load(s);
        InitUniforms(s);
    }
    ~Shader()
    {
        if(D3DShader)
        {
            D3DShader->Release();
        }
    }
    bool Load(ID3D1xBlob* shader)
    {
        return Load(shader->GetBufferPointer(), shader->GetBufferSize());
    }

    // These functions have specializations.
    bool Load(void* shader, size_t size);
    void Set(PrimitiveType prim) const;
    void SetUniformBuffer(Render::Buffer* buffers, int i = 0);
};

typedef Shader<Render::Shader_Vertex, ID3D1xVertexShader> VertexShader;
typedef Shader<Render::Shader_Geometry, ID3D1xGeometryShader> GeomShader;
typedef Shader<Render::Shader_Fragment, ID3D1xPixelShader> PixelShader;


class Buffer : public Render::Buffer
{
public:
    RenderDevice*     Ren;
    Ptr<ID3D1xBuffer> D3DBuffer;
    size_t            Size;
    int               Use;
    bool              Dynamic;

public:
    Buffer(RenderDevice* r) :
        Ren(r),
        Size(0),
        Use(0),
        Dynamic(false)
    {
    }
    ~Buffer();

    ID3D1xBuffer* GetBuffer()
    {
        return D3DBuffer;
    }

    virtual size_t GetSize()
    {
        return Size;
    }
    virtual void*  Map(size_t start, size_t size, int flags = 0);
    virtual bool   Unmap(void* m);
    virtual bool   Data(int use, const void* buffer, size_t size);
};


class Texture : public Render::Texture
{
public:
    RenderDevice*                   Ren;
    Ptr<ID3D1xTexture2D>            Tex;
    Ptr<ID3D1xShaderResourceView>   TexSv;
    Ptr<ID3D1xRenderTargetView>     TexRtv;
    Ptr<ID3D1xDepthStencilView>     TexDsv;
	Ptr<ID3D1xTexture2D>			TexStaging;
    mutable Ptr<ID3D1xSamplerState> Sampler;
    int                             Width, Height;
    int                             Samples;
    int                             Format;

    Texture(RenderDevice* r, int fmt, int w, int h);
    ~Texture();

    virtual int GetWidth() const
    {
        return Width;
    }
    virtual int GetHeight() const
    {
        return Height;
    }
    virtual int GetSamples() const
    {
        return Samples;
    }

    virtual void SetSampleMode(int sm);

    virtual void Set(int slot, Render::ShaderStage stage = Render::Shader_Fragment) const;

	virtual ovrTexture Get_ovrTexture();

	virtual void* GetInternalImplementation();
};


class RenderDevice : public Render::RenderDevice
{
public:
    Ptr<IDXGIFactory>           DXGIFactory;
    HWND                        Window;

    Ptr<ID3D1xDevice>           Device;
    Ptr<ID3D1xDeviceContext>    Context;
    Ptr<IDXGISwapChain>         SwapChain;
    Ptr<IDXGIAdapter>           Adapter;
    Ptr<IDXGIOutput>            FullscreenOutput;
    int                         FSDesktopX, FSDesktopY;
    int                         PreFullscreenX, PreFullscreenY, PreFullscreenW, PreFullscreenH;

    Ptr<ID3D1xTexture2D>        BackBuffer;
    Ptr<ID3D1xRenderTargetView> BackBufferRT;
    Ptr<Texture>                CurRenderTarget;
    Ptr<Texture>                CurDepthBuffer;
    Ptr<ID3D1xRasterizerState>  Rasterizer;
    Ptr<ID3D1xBlendState>       BlendState;
    D3D1x_VIEWPORT              D3DViewport;

    Ptr<ID3D1xDepthStencilState> DepthStates[1 + 2 * Compare_Count];
    Ptr<ID3D1xDepthStencilState> CurDepthState;
    Ptr<ID3D1xInputLayout>      ModelVertexIL;
    Ptr<ID3D1xInputLayout>      DistortionVertexIL;
    Ptr<ID3D1xInputLayout>      HeightmapVertexIL;

    Ptr<ID3D1xSamplerState>     SamplerStates[Sample_Count];

    struct StandardUniformData
    {
        Matrix4f  Proj;
        Matrix4f  View;
    }                        StdUniforms;
    Ptr<Buffer>              UniformBuffers[Shader_Count];
    int                      MaxTextureSet[Shader_Count];

    Ptr<VertexShader>        VertexShaders[VShader_Count];
    Ptr<PixelShader>         PixelShaders[FShader_Count];
    Ptr<GeomShader>          pStereoShaders[Prim_Count];
    Ptr<Buffer>              CommonUniforms[8];
    Ptr<ShaderSet>           ExtraShaders;
    Ptr<ShaderFill>          DefaultFill;

    Ptr<Buffer>              QuadVertexBuffer;

    Array<Ptr<Texture> >     DepthBuffers;

public:
    RenderDevice(const RendererParams& p, HWND window);
    ~RenderDevice();

    // Implement static initializer function to create this class.
    static Render::RenderDevice* CreateDevice(const RendererParams& rp, void* oswnd);

    // if needRecreate == true it will recreate DXGIFactory and Adapter
    // to get the latest info about monitors (including just connected/
    // disconnected ones). Note, SwapChain will be released in this case
    // and it should be recreated.
    void         UpdateMonitorOutputs(bool needRecreate = false);

    virtual void SetViewport(const Recti& vp);
    virtual void SetWindowSize(int w, int h);
    virtual bool SetParams(const RendererParams& newParams);

	// Returns details needed by CAPI distortion rendering.
	virtual ovrRenderAPIConfig Get_ovrRenderAPIConfig() const;

    virtual void Present ( bool withVsync );
    virtual void WaitUntilGpuIdle();
    virtual void Flush();

    virtual bool SetFullscreen(DisplayMode fullscreen);
	virtual size_t QueryGPUMemorySize();

    virtual void Clear(float r = 0, float g = 0, float b = 0, float a = 1,
                       float depth = 1,
                       bool clearColor = true, bool clearDepth = true);
    virtual void Rect(float left, float top, float right, float bottom)
    {
        OVR_UNUSED4(left, top, right, bottom);
    }

    virtual Buffer* CreateBuffer();
    virtual Texture* CreateTexture(int format, int width, int height, const void* data, int mipcount=1);
    
    static void GenerateSubresourceData(
                    unsigned imageWidth, unsigned imageHeight, int format, unsigned imageDimUpperLimit,
                    const void* rawBytes,
                    D3D1x_(SUBRESOURCE_DATA)* subresData,
                    unsigned& largestMipWidth, unsigned& largestMipHeight, unsigned& byteSize, unsigned& effectiveMipCount);

    Texture* GetDepthBuffer(int w, int h, int ms);

    virtual void ResolveMsaa(OVR::Render::Texture* msaaTex, OVR::Render::Texture* outputTex) OVR_OVERRIDE;

    virtual void BeginRendering();
    virtual void SetRenderTarget(Render::Texture* color,
                                 Render::Texture* depth = NULL, Render::Texture* stencil = NULL);
    virtual void SetDepthMode(bool enable, bool write, CompareFunc func = Compare_Less);
    virtual void SetWorldUniforms(const Matrix4f& proj);
    virtual void SetCommonUniformBuffer(int i, Render::Buffer* buffer);
    virtual void SetExtraShaders(ShaderSet* s)
    {
        ExtraShaders = s;
    }

    // Overridden to apply proper blend state.
    virtual void FillRect(float left, float top, float right, float bottom, Color c, const Matrix4f* view=NULL);
    virtual void FillGradientRect(float left, float top, float right, float bottom, Color col_top, Color col_btm, const Matrix4f* view);
	virtual void RenderText(const struct Font* font, const char* str, float x, float y, float size, Color c, const Matrix4f* view=NULL);
    virtual void RenderImage(float left, float top, float right, float bottom, ShaderFill* image, unsigned char alpha=255, const Matrix4f* view=NULL);

    virtual void Render(const Matrix4f& matrix, Model* model);
    virtual void Render(const Fill* fill, Render::Buffer* vertices, Render::Buffer* indices,
                        const Matrix4f& matrix, int offset, int count, PrimitiveType prim = Prim_Triangles, MeshType meshType = Mesh_Scene);
    virtual void RenderWithAlpha(   const Fill* fill, Render::Buffer* vertices, Render::Buffer* indices,
                                    const Matrix4f& matrix, int offset, int count, PrimitiveType prim = Prim_Triangles);

    virtual Fill *CreateSimpleFill(int flags = Fill::F_Solid);

    virtual Render::Shader *LoadBuiltinShader(ShaderStage stage, int shader);

    bool RecreateSwapChain();
    virtual ID3D10Blob* CompileShader(const char* profile, const char* src, const char* mainName = "main");
    virtual ShaderBase* CreateStereoShader(PrimitiveType prim, Render::Shader* vs);

    ID3D1xSamplerState* GetSamplerState(int sm);

    void SetTexture(Render::ShaderStage stage, int slot, const Texture* t);

    // GPU Profiling
    virtual void BeginGpuEvent(const char* markerText, uint32_t markerColor);
    virtual void EndGpuEvent();
};


}}} // namespace OVR::Render::D3D1?

#endif