Render_D3D1X_Device.h
Go to the documentation of this file.
00001 /************************************************************************************
00002 
00003 Filename    :   Render_D3D1X_Device.h
00004 Content     :   RenderDevice implementation header for D3DX10/11.
00005 Created     :   September 10, 2012
00006 Authors     :   Andrew Reisse
00007 
00008 Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
00009 
00010 Licensed under the Apache License, Version 2.0 (the "License");
00011 you may not use this file except in compliance with the License.
00012 You may obtain a copy of the License at
00013 
00014 http://www.apache.org/licenses/LICENSE-2.0
00015 
00016 Unless required by applicable law or agreed to in writing, software
00017 distributed under the License is distributed on an "AS IS" BASIS,
00018 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019 See the License for the specific language governing permissions and
00020 limitations under the License.
00021 
00022 ************************************************************************************/
00023 
00024 // ***** IMPORTANT:
00025 // This file can be included twice, once with OVR_D3D_VERSION=10 and
00026 // once with OVR_D3D_VERSION=11.
00027 
00028 
00029 #ifndef OVR_D3D_VERSION
00030 #error define OVR_D3D_VERSION to 10 or 11
00031 #endif
00032 
00033 #include "Kernel/OVR_String.h"
00034 #include "Kernel/OVR_Array.h"
00035 
00036 #if (OVR_D3D_VERSION == 10 && !defined(_OVR_RENDERER_D3D10)) || \
00037     (OVR_D3D_VERSION == 11 && !defined(_OVR_RENDERER_D3D11))
00038 
00039 #include "../Render/Render_Device.h"
00040 
00041 #include <Windows.h>
00042 
00043 #if (OVR_D3D_VERSION == 10)
00044 #define _OVR_RENDERER_D3D10
00045 #include <d3d10.h>
00046 
00047 namespace OVR { namespace Render { namespace D3D10 {
00048 
00049 #else // 11
00050 
00051 #define _OVR_RENDERER_D3D11
00052 #include <d3d11.h>
00053 
00054 namespace OVR { namespace Render { namespace D3D11 {
00055 #endif
00056 
00057 class RenderDevice;
00058 
00059 #ifdef D3D1x_
00060 #undef D3D1x_
00061 #endif
00062 #ifdef ID3D1x
00063 #undef ID3D1x
00064 #endif
00065 
00066 #if (OVR_D3D_VERSION == 10)
00067 typedef ID3D10Device            ID3D1xDevice;
00068 typedef ID3D10Device            ID3D1xDeviceContext;
00069 typedef ID3D10RenderTargetView  ID3D1xRenderTargetView;
00070 typedef ID3D10Texture2D         ID3D1xTexture2D;
00071 typedef ID3D10ShaderResourceView ID3D1xShaderResourceView;
00072 typedef ID3D10DepthStencilView  ID3D1xDepthStencilView;
00073 typedef ID3D10DepthStencilState ID3D1xDepthStencilState;
00074 typedef ID3D10InputLayout       ID3D1xInputLayout;
00075 typedef ID3D10Buffer            ID3D1xBuffer;
00076 typedef ID3D10VertexShader      ID3D1xVertexShader;
00077 typedef ID3D10PixelShader       ID3D1xPixelShader;
00078 typedef ID3D10GeometryShader    ID3D1xGeometryShader;
00079 typedef ID3D10BlendState        ID3D1xBlendState;
00080 typedef ID3D10RasterizerState   ID3D1xRasterizerState;
00081 typedef ID3D10SamplerState      ID3D1xSamplerState;
00082 typedef ID3D10Query             ID3D1xQuery;
00083 typedef ID3D10Blob              ID3D1xBlob;
00084 typedef D3D10_VIEWPORT          D3D1x_VIEWPORT;
00085 typedef D3D10_QUERY_DESC        D3D1x_QUERY_DESC;
00086 #define D3D1x_(x)               D3D10_##x
00087 #define ID3D1x(x)               ID3D10##x
00088 
00089 #else // D3D 11
00090 typedef ID3D11Device            ID3D1xDevice;
00091 typedef ID3D11DeviceContext     ID3D1xDeviceContext;
00092 typedef ID3D11RenderTargetView  ID3D1xRenderTargetView;
00093 typedef ID3D11Texture2D         ID3D1xTexture2D;
00094 typedef ID3D11ShaderResourceView ID3D1xShaderResourceView;
00095 typedef ID3D11DepthStencilView  ID3D1xDepthStencilView;
00096 typedef ID3D11DepthStencilState ID3D1xDepthStencilState;
00097 typedef ID3D11InputLayout       ID3D1xInputLayout;
00098 typedef ID3D11Buffer            ID3D1xBuffer;
00099 typedef ID3D10Blob              ID3D1xBlob;
00100 typedef ID3D11VertexShader      ID3D1xVertexShader;
00101 typedef ID3D11PixelShader       ID3D1xPixelShader;
00102 typedef ID3D11GeometryShader    ID3D1xGeometryShader;
00103 typedef ID3D11BlendState        ID3D1xBlendState;
00104 typedef ID3D11RasterizerState   ID3D1xRasterizerState;
00105 typedef ID3D11SamplerState      ID3D1xSamplerState;
00106 typedef ID3D11Query             ID3D1xQuery;
00107 typedef D3D11_VIEWPORT          D3D1x_VIEWPORT;
00108 typedef D3D11_QUERY_DESC        D3D1x_QUERY_DESC;
00109 #define D3D1x_(x)               D3D11_##x
00110 #define ID3D1x(x)               ID3D11##x
00111 #endif
00112 
00113 class Buffer;
00114 
00115 class ShaderBase : public Render::Shader
00116 {
00117 public:
00118     RenderDevice*   Ren;
00119     unsigned char*  UniformData;
00120     int             UniformsSize;
00121 
00122     struct Uniform
00123     {
00124         String Name;
00125         int    Offset, Size;
00126     };
00127     Array<Uniform> UniformInfo;
00128 
00129     ShaderBase(RenderDevice* r, ShaderStage stage);
00130     ~ShaderBase();
00131 
00132     void InitUniforms(ID3D10Blob* s);
00133     bool SetUniform(const char* name, int n, const float* v);
00134     //virtual bool UseTransposeMatrix() const { return 1; }
00135 
00136     void UpdateBuffer(Buffer* b);
00137 };
00138 
00139 template<Render::ShaderStage SStage, class D3DShaderType>
00140 class Shader : public ShaderBase
00141 {
00142 public:
00143     D3DShaderType*  D3DShader;
00144 
00145     Shader(RenderDevice* r, D3DShaderType* s) : ShaderBase(r, SStage), D3DShader(s) {}
00146     Shader(RenderDevice* r, ID3D1xBlob* s) : ShaderBase(r, SStage)
00147     {
00148         Load(s);
00149         InitUniforms(s);
00150     }
00151     ~Shader()
00152     {
00153         if(D3DShader)
00154         {
00155             D3DShader->Release();
00156         }
00157     }
00158     bool Load(ID3D1xBlob* shader)
00159     {
00160         return Load(shader->GetBufferPointer(), shader->GetBufferSize());
00161     }
00162 
00163     // These functions have specializations.
00164     bool Load(void* shader, size_t size);
00165     void Set(PrimitiveType prim) const;
00166     void SetUniformBuffer(Render::Buffer* buffers, int i = 0);
00167 };
00168 
00169 typedef Shader<Render::Shader_Vertex, ID3D1xVertexShader> VertexShader;
00170 typedef Shader<Render::Shader_Geometry, ID3D1xGeometryShader> GeomShader;
00171 typedef Shader<Render::Shader_Fragment, ID3D1xPixelShader> PixelShader;
00172 
00173 
00174 class Buffer : public Render::Buffer
00175 {
00176 public:
00177     RenderDevice*     Ren;
00178     Ptr<ID3D1xBuffer> D3DBuffer;
00179     size_t            Size;
00180     int               Use;
00181     bool              Dynamic;
00182 
00183 public:
00184     Buffer(RenderDevice* r) : Ren(r), Size(0), Use(0) {}
00185     ~Buffer();
00186 
00187     ID3D1xBuffer* GetBuffer()
00188     {
00189         return D3DBuffer;
00190     }
00191 
00192     virtual size_t GetSize()
00193     {
00194         return Size;
00195     }
00196     virtual void*  Map(size_t start, size_t size, int flags = 0);
00197     virtual bool   Unmap(void *m);
00198     virtual bool   Data(int use, const void* buffer, size_t size);
00199 };
00200 
00201 class Texture : public Render::Texture
00202 {
00203 public:
00204     RenderDevice*                    Ren;
00205     Ptr<ID3D1xTexture2D>            Tex;
00206     Ptr<ID3D1xShaderResourceView>   TexSv;
00207     Ptr<ID3D1xRenderTargetView>     TexRtv;
00208     Ptr<ID3D1xDepthStencilView>     TexDsv;
00209     mutable Ptr<ID3D1xSamplerState> Sampler;
00210     int                             Width, Height;
00211     int                             Samples;
00212 
00213     Texture(RenderDevice* r, int fmt, int w, int h);
00214     ~Texture();
00215 
00216     virtual int GetWidth() const
00217     {
00218         return Width;
00219     }
00220     virtual int GetHeight() const
00221     {
00222         return Height;
00223     }
00224     virtual int GetSamples() const
00225     {
00226         return Samples;
00227     }
00228 
00229     virtual void SetSampleMode(int sm);
00230 
00231     virtual void Set(int slot, Render::ShaderStage stage = Render::Shader_Fragment) const;
00232 };
00233 
00234 class RenderDevice : public Render::RenderDevice
00235 {
00236 public:
00237     Ptr<IDXGIFactory>           DXGIFactory;
00238     HWND                        Window;
00239 
00240     Ptr<ID3D1xDevice>           Device;
00241     Ptr<ID3D1xDeviceContext>    Context;
00242     Ptr<IDXGISwapChain>         SwapChain;
00243     Ptr<IDXGIAdapter>           Adapter;
00244     Ptr<IDXGIOutput>            FullscreenOutput;
00245     int                         FSDesktopX, FSDesktopY;
00246     int                         PreFullscreenX, PreFullscreenY, PreFullscreenW, PreFullscreenH;
00247 
00248     Ptr<ID3D1xTexture2D>        BackBuffer;
00249     Ptr<ID3D1xRenderTargetView> BackBufferRT;
00250     Ptr<Texture>                CurRenderTarget;
00251     Ptr<Texture>                CurDepthBuffer;
00252     Ptr<ID3D1xRasterizerState>  Rasterizer;
00253     Ptr<ID3D1xBlendState>       BlendState;
00254     int                         NumViewports;
00255     D3D1x_VIEWPORT              Viewports[2];
00256 
00257     Ptr<ID3D1xDepthStencilState> DepthStates[1 + 2 * Compare_Count];
00258     Ptr<ID3D1xDepthStencilState> CurDepthState;
00259     Ptr<ID3D1xInputLayout>      ModelVertexIL;
00260 
00261     Ptr<ID3D1xSamplerState>     SamplerStates[Sample_Count];
00262 
00263     struct StandardUniformData
00264     {
00265         Matrix4f  Proj;
00266         Matrix4f  View;
00267     }                        StdUniforms;
00268     Ptr<Buffer>              UniformBuffers[Shader_Count];
00269     int                      MaxTextureSet[Shader_Count];
00270 
00271     Ptr<VertexShader>        VertexShaders[VShader_Count];
00272     Ptr<PixelShader>         PixelShaders[FShader_Count];
00273     Ptr<GeomShader>          pStereoShaders[Prim_Count];
00274     Ptr<Buffer>              CommonUniforms[8];
00275     Ptr<ShaderSet>           ExtraShaders;
00276     Ptr<ShaderFill>          DefaultFill;
00277 
00278     Ptr<Buffer>              QuadVertexBuffer;
00279 
00280     Array<Ptr<Texture> >     DepthBuffers;
00281 
00282 public:
00283     RenderDevice(const RendererParams& p, HWND window);
00284     ~RenderDevice();
00285 
00286     // Implement static initializer function to create this class.
00287     static Render::RenderDevice* CreateDevice(const RendererParams& rp, void* oswnd);
00288 
00289     // if needRecreate == true it will recreate DXGIFactory and Adapter
00290     // to get the latest info about monitors (including just connected/
00291     // disconnected ones). Note, SwapChain will be released in this case
00292     // and it should be recreated.
00293     void        UpdateMonitorOutputs(bool needRecreate = false);
00294 
00295     virtual void SetMultipleViewports(int n, const Viewport* vps);
00296     virtual void SetWindowSize(int w, int h);
00297     virtual bool SetParams(const RendererParams& newParams);
00298     //virtual void SetScissor(int x, int y, int w, int h);
00299 
00300     virtual void Present();
00301     virtual void ForceFlushGPU();
00302 
00303     virtual bool SetFullscreen(DisplayMode fullscreen);
00304         virtual UPInt QueryGPUMemorySize();
00305 
00306     virtual void Clear(float r = 0, float g = 0, float b = 0, float a = 1, float depth = 1);
00307     virtual void Rect(float left, float top, float right, float bottom)
00308     {
00309         OVR_UNUSED4(left, top, right, bottom);
00310     }
00311 
00312     virtual Buffer* CreateBuffer();
00313     virtual Texture* CreateTexture(int format, int width, int height, const void* data, int mipcount=1);
00314     
00315     static void GenerateSubresourceData(
00316                     unsigned imageWidth, unsigned imageHeight, int format, unsigned imageDimUpperLimit,
00317                     const void* rawBytes,
00318                     D3D1x_(SUBRESOURCE_DATA)* subresData,
00319                     unsigned& largestMipWidth, unsigned& largestMipHeight, unsigned& byteSize, unsigned& effectiveMipCount);
00320 
00321     Texture* GetDepthBuffer(int w, int h, int ms);
00322 
00323     virtual void BeginRendering();
00324     virtual void SetRenderTarget(Render::Texture* color,
00325                                  Render::Texture* depth = NULL, Render::Texture* stencil = NULL);
00326     virtual void SetDepthMode(bool enable, bool write, CompareFunc func = Compare_Less);
00327     virtual void SetWorldUniforms(const Matrix4f& proj);
00328     virtual void SetCommonUniformBuffer(int i, Render::Buffer* buffer);
00329     virtual void SetExtraShaders(ShaderSet* s)
00330     {
00331         ExtraShaders = s;
00332     }
00333 
00334 
00335     // Overrident to apply proper blend state.
00336     virtual void FillRect(float left, float top, float right, float bottom, Color c);
00337     virtual void FillGradientRect(float left, float top, float right, float bottom, Color col_top, Color col_btm);
00338         virtual void RenderText(const struct Font* font, const char* str, float x, float y, float size, Color c);
00339     virtual void RenderImage(float left, float top, float right, float bottom, ShaderFill* image);
00340 
00341     virtual void Render(const Matrix4f& matrix, Model* model);
00342     virtual void Render(const Fill* fill, Render::Buffer* vertices, Render::Buffer* indices,
00343                         const Matrix4f& matrix, int offset, int count, PrimitiveType prim = Prim_Triangles);
00344     virtual void RenderWithAlpha(   const Fill* fill, Render::Buffer* vertices, Render::Buffer* indices,
00345                                     const Matrix4f& matrix, int offset, int count, PrimitiveType prim = Prim_Triangles);
00346 
00347     virtual Fill *CreateSimpleFill(int flags = Fill::F_Solid);
00348 
00349     virtual Render::Shader *LoadBuiltinShader(ShaderStage stage, int shader);
00350 
00351     bool RecreateSwapChain();
00352     virtual ID3D10Blob* CompileShader(const char* profile, const char* src, const char* mainName = "main");
00353     virtual ShaderBase* CreateStereoShader(PrimitiveType prim, Render::Shader* vs);
00354 
00355     ID3D1xSamplerState* GetSamplerState(int sm);
00356 
00357     void SetTexture(Render::ShaderStage stage, int slot, const Texture* t);
00358 };
00359 
00360 }}}
00361 
00362 #endif


oculus_sdk
Author(s):
autogenerated on Fri Aug 28 2015 11:53:11