RenderTiny_D3D1X_Device.h
Go to the documentation of this file.
00001 /************************************************************************************
00002 
00003 Filename    :   RenderTiny_D3D1X_Device.h
00004 Content     :   RenderDevice implementation header for D3DX10.
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 #ifndef INC_RenderTiny_D3D1X_Device_h
00025 #define INC_RenderTiny_D3D1X_Device_h
00026 
00027 #include "Kernel/OVR_String.h"
00028 #include "Kernel/OVR_Array.h"
00029 
00030 #include "RenderTiny_Device.h"
00031 #include <Windows.h>
00032 
00033 #define _OVR_RENDERER_D3D10
00034 #include <d3d10.h>
00035 
00036 namespace OVR { namespace RenderTiny { namespace D3D10 {
00037 
00038 class RenderDevice;
00039 class Buffer;
00040 
00041 typedef ID3D10Device            ID3D1xDevice;
00042 typedef ID3D10Device            ID3D1xDeviceContext;
00043 typedef ID3D10RenderTargetView  ID3D1xRenderTargetView;
00044 typedef ID3D10Texture2D         ID3D1xTexture2D;
00045 typedef ID3D10ShaderResourceView ID3D1xShaderResourceView;
00046 typedef ID3D10DepthStencilView  ID3D1xDepthStencilView;
00047 typedef ID3D10DepthStencilState ID3D1xDepthStencilState;
00048 typedef ID3D10InputLayout       ID3D1xInputLayout;
00049 typedef ID3D10Buffer            ID3D1xBuffer;
00050 typedef ID3D10VertexShader      ID3D1xVertexShader;
00051 typedef ID3D10PixelShader       ID3D1xPixelShader;
00052 typedef ID3D10GeometryShader    ID3D1xGeometryShader;
00053 typedef ID3D10BlendState        ID3D1xBlendState;
00054 typedef ID3D10RasterizerState   ID3D1xRasterizerState;
00055 typedef ID3D10SamplerState      ID3D1xSamplerState;
00056 typedef ID3D10Query             ID3D1xQuery;
00057 typedef ID3D10Blob              ID3D1xBlob;
00058 typedef D3D10_VIEWPORT          D3D1x_VIEWPORT;
00059 typedef D3D10_QUERY_DESC        D3D1x_QUERY_DESC;
00060 #define D3D1x_(x)               D3D10_##x
00061 #define ID3D1x(x)               ID3D10##x
00062 
00063 
00064 
00065 class ShaderBase : public RenderTiny::Shader
00066 {
00067 public:
00068     RenderDevice*   Ren;
00069     unsigned char*  UniformData;
00070     int             UniformsSize;
00071 
00072     struct Uniform
00073     {
00074         String Name;
00075         int    Offset, Size;
00076     };
00077     Array<Uniform> UniformInfo;
00078 
00079     ShaderBase(RenderDevice* r, ShaderStage stage);
00080     ~ShaderBase();
00081 
00082     void InitUniforms(ID3D10Blob* s);
00083     bool SetUniform(const char* name, int n, const float* v);
00084  
00085     void UpdateBuffer(Buffer* b);
00086 };
00087 
00088 template<RenderTiny::ShaderStage SStage, class D3DShaderType>
00089 class Shader : public ShaderBase
00090 {
00091 public:
00092     D3DShaderType*  D3DShader;
00093 
00094     Shader(RenderDevice* r, D3DShaderType* s) : ShaderBase(r, SStage), D3DShader(s) {}
00095     Shader(RenderDevice* r, ID3D1xBlob* s) : ShaderBase(r, SStage)
00096     {
00097         Load(s);
00098         InitUniforms(s);
00099     }
00100     ~Shader()
00101     {
00102         if (D3DShader)        
00103             D3DShader->Release();        
00104     }
00105     bool Load(ID3D1xBlob* shader)
00106     {
00107         return Load(shader->GetBufferPointer(), shader->GetBufferSize());
00108     }
00109 
00110     // These functions have specializations.
00111     bool Load(void* shader, size_t size);
00112     void Set(PrimitiveType prim) const;
00113     void SetUniformBuffer(RenderTiny::Buffer* buffers, int i = 0);
00114 };
00115 
00116 typedef Shader<RenderTiny::Shader_Vertex,  ID3D1xVertexShader> VertexShader;
00117 typedef Shader<RenderTiny::Shader_Fragment, ID3D1xPixelShader> PixelShader;
00118 
00119 
00120 class Buffer : public RenderTiny::Buffer
00121 {
00122 public:
00123     RenderDevice*     Ren;
00124     Ptr<ID3D1xBuffer> D3DBuffer;
00125     size_t            Size;
00126     int               Use;
00127     bool              Dynamic;
00128 
00129 public:
00130     Buffer(RenderDevice* r) : Ren(r), Size(0), Use(0) {}
00131     ~Buffer();
00132 
00133     ID3D1xBuffer* GetBuffer()
00134     {
00135         return D3DBuffer;
00136     }
00137 
00138     virtual size_t GetSize()
00139     {
00140         return Size;
00141     }
00142     virtual void*  Map(size_t start, size_t size, int flags = 0);
00143     virtual bool   Unmap(void *m);
00144     virtual bool   Data(int use, const void* buffer, size_t size);
00145 };
00146 
00147 class Texture : public RenderTiny::Texture
00148 {
00149 public:
00150     RenderDevice*                    Ren;
00151     Ptr<ID3D1xTexture2D>            Tex;
00152     Ptr<ID3D1xShaderResourceView>   TexSv;
00153     Ptr<ID3D1xRenderTargetView>     TexRtv;
00154     Ptr<ID3D1xDepthStencilView>     TexDsv;
00155     mutable Ptr<ID3D1xSamplerState> Sampler;
00156     int                             Width, Height;
00157     int                             Samples;
00158 
00159     Texture(RenderDevice* r, int fmt, int w, int h);
00160     ~Texture();
00161 
00162     virtual int GetWidth() const
00163     {
00164         return Width;
00165     }
00166     virtual int GetHeight() const
00167     {
00168         return Height;
00169     }
00170     virtual int GetSamples() const
00171     {
00172         return Samples;
00173     }
00174 
00175     virtual void SetSampleMode(int sm);
00176 
00177     virtual void Set(int slot, RenderTiny::ShaderStage stage = RenderTiny::Shader_Fragment) const;
00178 };
00179 
00180 class RenderDevice : public RenderTiny::RenderDevice
00181 {
00182 public:
00183     Ptr<IDXGIFactory>           DXGIFactory;
00184     HWND                        Window;
00185 
00186     Ptr<ID3D1xDevice>           Device;
00187     Ptr<ID3D1xDeviceContext>    Context;
00188     Ptr<IDXGISwapChain>         SwapChain;
00189     Ptr<IDXGIAdapter>           Adapter;
00190     Ptr<IDXGIOutput>            FullscreenOutput;
00191     int                         FSDesktopX, FSDesktopY;    
00192 
00193     Ptr<ID3D1xTexture2D>        BackBuffer;
00194     Ptr<ID3D1xRenderTargetView> BackBufferRT;
00195     Ptr<Texture>                CurRenderTarget;
00196     Ptr<Texture>                CurDepthBuffer;
00197     Ptr<ID3D1xRasterizerState>  Rasterizer;
00198     Ptr<ID3D1xBlendState>       BlendState;    
00199     D3D1x_VIEWPORT              D3DViewport;
00200 
00201     Ptr<ID3D1xDepthStencilState> DepthStates[1 + 2 * Compare_Count];
00202     Ptr<ID3D1xDepthStencilState> CurDepthState;
00203     Ptr<ID3D1xInputLayout>      ModelVertexIL;
00204 
00205     Ptr<ID3D1xSamplerState>     SamplerStates[Sample_Count];
00206 
00207     struct StandardUniformData
00208     {
00209         Matrix4f  Proj;
00210         Matrix4f  View;
00211     }                        StdUniforms;
00212     Ptr<Buffer>              UniformBuffers[Shader_Count];
00213     int                      MaxTextureSet[Shader_Count];
00214 
00215     Ptr<VertexShader>        VertexShaders[VShader_Count];
00216     Ptr<PixelShader>         PixelShaders[FShader_Count];  
00217     Ptr<Buffer>              CommonUniforms[8];
00218     Ptr<ShaderFill>          DefaultFill;
00219 
00220     Ptr<Buffer>              QuadVertexBuffer;
00221 
00222     Array<Ptr<Texture> >     DepthBuffers;
00223 
00224 public:
00225     RenderDevice(const RendererParams& p, HWND window);
00226     ~RenderDevice();
00227 
00228     // Implement static initializer function to create this class.
00229     static RenderTiny::RenderDevice* CreateDevice(const RendererParams& rp, void* oswnd);
00230 
00231     void        UpdateMonitorOutputs();
00232 
00233     virtual void SetRealViewport(const Viewport& vp);
00234     virtual bool SetParams(const RendererParams& newParams);
00235   
00236     virtual void Present();
00237     virtual void ForceFlushGPU();
00238 
00239     virtual bool SetFullscreen(DisplayMode fullscreen);
00240 
00241     virtual void Clear(float r = 0, float g = 0, float b = 0, float a = 1, float depth = 1);
00242 
00243     virtual Buffer* CreateBuffer();
00244     virtual Texture* CreateTexture(int format, int width, int height, const void* data, int mipcount=1);
00245 
00246     Texture* GetDepthBuffer(int w, int h, int ms);
00247 
00248     virtual void BeginRendering();
00249     virtual void SetRenderTarget(RenderTiny::Texture* color,
00250                                  RenderTiny::Texture* depth = NULL, RenderTiny::Texture* stencil = NULL);
00251     virtual void SetDepthMode(bool enable, bool write, CompareFunc func = Compare_Less);
00252     virtual void SetWorldUniforms(const Matrix4f& proj);
00253     virtual void SetCommonUniformBuffer(int i, RenderTiny::Buffer* buffer);
00254 
00255     virtual void Render(const Matrix4f& matrix, Model* model);
00256     virtual void Render(const ShaderFill* fill, RenderTiny::Buffer* vertices, RenderTiny::Buffer* indices,
00257                         const Matrix4f& matrix, int offset, int count, PrimitiveType prim = Prim_Triangles);
00258 
00259     virtual ShaderFill *CreateSimpleFill() { return DefaultFill; }
00260 
00261     virtual RenderTiny::Shader *LoadBuiltinShader(ShaderStage stage, int shader);
00262 
00263     bool                RecreateSwapChain();
00264     virtual ID3D10Blob* CompileShader(const char* profile, const char* src, const char* mainName = "main");
00265 
00266     ID3D1xSamplerState* GetSamplerState(int sm);
00267 
00268     void                SetTexture(RenderTiny::ShaderStage stage, int slot, const Texture* t);
00269 };
00270 
00271 }}} // Render::D3D10
00272 
00273 #endif


oculus_sdk
Author(s):
autogenerated on Mon Oct 6 2014 03:01:19