Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AppImplMswRendererDx.h
Go to the documentation of this file.
1 // The copyright in this software is being made available under the BSD License, included below.
2 // This software may be subject to other third party and contributor rights, including patent rights,
3 // and no such rights are granted under this license.
4 //
5 // Copyright (c) 2013, Microsoft Open Technologies, Inc.
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without modification,
9 // are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright notice,
12 // this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright notice,
14 // this list of conditions and the following disclaimer in the documentation
15 // and/or other materials provided with the distribution.
16 // - Neither the name of Microsoft Open Technologies, Inc. nor the names of its contributors
17 // may be used to endorse or promote products derived from this software
18 // without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 
30 #pragma once
31 
32 #include "cinder/app/App.h"
34 #include "cinder/app/RendererDx.h"
35 #include "cinder/dx/dx.h"
36 #include <d3dcompiler.h>
37 #include <d3d11.h>
38 #include <d3d11_1.h>
39 #include <dxgi.h>
40 #include <dxgi1_2.h>
41 
42 namespace cinder { namespace dx {
43 class Texture;
44 }}
45 
46 namespace cinder { namespace app {
47 
49  public:
50  struct LightData
51  {
52  LightData() : specular(Vec4f(0, 0, 0, 1)), constantAtt(1), linearAtt(1), quadAtt(1), type(0), spotExponent(1), spotCutoff(1) {}
56  float constantAtt;
57  float linearAtt;
58  float quadAtt;
59  int type;
62  float spotExponent;
63  float spotCutoff;
64  float padding[2];
65  };
66 
67  struct FixedVertex
68  {
69  FixedVertex(Vec4f POS = Vec4f(0, 0, 0, 1), Vec3f NORM = Vec3f(), Vec2f UV = Vec2f(), Vec4f COLOR = Vec4f()) : pos(POS), norm(NORM), uv(UV), color(COLOR) {}
70  FixedVertex(Vec3f POS, Vec3f NORM = Vec3f(), Vec2f UV = Vec2f(), Vec4f COLOR = Vec4f()) : pos(Vec4f(POS, 1)), norm(NORM), uv(UV), color(COLOR) {}
71  FixedVertex(Vec2f POS, Vec3f NORM = Vec3f(), Vec2f UV = Vec2f(), Vec4f COLOR = Vec4f()) : pos(Vec4f(POS.x, POS.y, 0, 1)), norm(NORM), uv(UV), color(COLOR) {}
76  };
77 
79  {
82  };
83 
84  AppImplMswRendererDx( App *aApp, RendererDx *aRenderer );
85 
86 #if defined( CINDER_MSW )
87  virtual bool initialize( HWND wnd, HDC dc, RendererRef sharedRenderer );
88 #elif defined( CINDER_WINRT)
89  virtual bool initialize( DX_WINDOW_TYPE wnd );
90 #endif
91 
92  virtual void prepareToggleFullScreen();
93  virtual void finishToggleFullScreen();
94  virtual void kill();
95  virtual void defaultResize() const;
96  virtual void swapBuffers() const;
97  virtual void makeCurrentContext();
98 
99  void enableVsync(bool enable = true) { mVsyncEnable = enable; }
100  bool vsyncEnabled() const { return mVsyncEnable; }
101  void setViewport(int x, int y, int width, int height) const;
102  void enableDepthTesting(bool enable = true);
103  void enableAlphaBlending(bool premultiplied = false);
104  void disableAlphaBlending();
105  void enableAdditiveBlending();
106  void enableDepthWriting(bool enable = true);
107 
108  bool getRenderFlag(RenderStateFlag flag) { return (mStateFlags & flag) == flag; }
109  void setRenderFlag(RenderStateFlag flag) { mStateFlags |= flag; }
110  void clearRenderFlag(RenderStateFlag flag) { mStateFlags &= ~(flag); }
111 
112  //used for texture batch drawing
113  const dx::Texture *mCurrentBatchTexture;
114  std::vector<std::pair<const dx::Texture*, std::vector<FixedVertex>>> mBatchedTextures;
115 
116  // matrix stacks
119 
123  std::vector<FixedVertex> mImmediateModeVerts;
127 
129  D3D_FEATURE_LEVEL mFeatureLevel;
130  #if defined( CINDER_WINRT ) || ( _WIN32_WINNT >= 0x0602 )
131  ID3D11DeviceContext1 *mDeviceContext;
132  IDXGISwapChain1 *mSwapChain;
133  ID3D11Device1 *md3dDevice;
134  #else
135  ID3D11DeviceContext *mDeviceContext;
136  IDXGISwapChain *mSwapChain;
137  ID3D11Device *md3dDevice;
138  #endif
139  ID3D11RenderTargetView *mMainFramebuffer;
140  ID3D11Texture2D *mDepthStencilTexture;
141  ID3D11DepthStencilView *mDepthStencilView;
142  ID3D11Buffer *mCBMatrices;
143  ID3D11Buffer *mCBLights;
144  ID3D11Buffer *mCBFixedParameters;
145  ID3D11BlendState *mBlendState;
146  D3D11_BLEND_DESC mBlendDesc;
147 
148  ID3D11VertexShader *mFixedColorVertexShader;
149  ID3D11PixelShader *mFixedColorPixelShader;
150  ID3D11VertexShader *mFixedColorLightVertexShader;
151  ID3D11PixelShader *mFixedColorLightPixelShader;
152  ID3D11VertexShader *mFixedTextureVertexShader;
153  ID3D11PixelShader *mFixedTexturePixelShader;
154  ID3D11VertexShader *mFixedTextureLightVertexShader;
155  ID3D11PixelShader *mFixedTextureLightPixelShader;
156 
157  ID3D11VertexShader *mVboPositionVS;
158  ID3D11VertexShader *mVboPositionNormalVS;
159  ID3D11VertexShader *mVboPositionTextureVS;
160  ID3D11VertexShader *mVboPositionColorVS;
161  ID3D11VertexShader *mVboPositionNormalTextureVS;
162  ID3D11VertexShader *mVboPositionNormalColorVS;
163  ID3D11VertexShader *mVboPositionColorTextureVS;
164  ID3D11VertexShader *mVboPositionNormalColorTextureVS;
165 
166  ID3D11VertexShader *mVboPositionLightVS;
167  ID3D11VertexShader *mVboPositionNormalLightVS;
168  ID3D11VertexShader *mVboPositionTextureLightVS;
169  ID3D11VertexShader *mVboPositionColorLightVS;
170  ID3D11VertexShader *mVboPositionNormalTextureLightVS;
171  ID3D11VertexShader *mVboPositionNormalColorLightVS;
172  ID3D11VertexShader *mVboPositionColorTextureLightVS;
174 
175  ID3D11InputLayout *mFixedLayout;
176  ID3D11Buffer *mVertexBuffer;
177  ID3D11Buffer *mIndexBuffer;
178 
179  // no culling, solid fill mode, forward-facing triangles are CCW, no depth bias, no depth bias clamp, anti-aliased lines
180  ID3D11RasterizerState *mDefaultRenderState;
181 
182  // no depth testing or stencil testing
183  ID3D11DepthStencilState *mDepthStencilState;
184  D3D11_DEPTH_STENCIL_DESC mDepthStencilDesc;
185 
186  protected:
187  bool initializeInternal( DX_WINDOW_TYPE wnd );
188  int initMultisample( int requestedLevelIdx );
189  bool createDevice(UINT createDeviceFlags);
190  bool createDeviceResources();
192 
198 
199  void handleLostDevice();
200  void getPlatformWindowDimensions(DX_WINDOW_TYPE wnd, float* width, float* height) const;
202 
206 };
207 
208 } } // namespace cinder::app
ID3D11Device * md3dDevice
Definition: AppImplMswRendererDx.h:137
Definition: AppImplMswRenderer.h:37
virtual void swapBuffers() const
Definition: AppImplMswRendererDx.cpp:325
void handleLostDevice()
Definition: AppImplMswRendererDx.cpp:1189
void enableAdditiveBlending()
Definition: AppImplMswRendererDx.cpp:408
ID3D11Buffer * mVertexBuffer
Definition: AppImplMswRendererDx.h:176
GLenum GLint GLint y
Definition: GLee.h:987
ID3D11Buffer * mIndexBuffer
Definition: AppImplMswRendererDx.h:177
ID3D11PixelShader * mFixedColorPixelShader
Definition: AppImplMswRendererDx.h:149
Definition: AppImplMswRendererDx.h:80
void enable(GLenum state)
Enables the OpenGL State state. Equivalent to calling to glEnable( state );.
Definition: dx.h:198
bool createShadersFeatureLevel_11_0()
Definition: AppImplMswRendererDx.cpp:1101
ID3D11DeviceContext * mDeviceContext
Definition: AppImplMswRendererDx.h:135
bool createDeviceResources()
Definition: AppImplMswRendererDx.cpp:683
Vec2< float > Vec2f
Definition: Vector.h:1314
ID3D11VertexShader * mVboPositionTextureLightVS
Definition: AppImplMswRendererDx.h:168
bool createShadersFeatureLevel_9_1()
Definition: AppImplMswRendererDx.cpp:852
ID3D11DepthStencilView * mDepthStencilView
Definition: AppImplMswRendererDx.h:141
ID3D11VertexShader * mVboPositionNormalColorTextureVS
Definition: AppImplMswRendererDx.h:164
IDXGISwapChain * mSwapChain
Definition: AppImplMswRendererDx.h:136
std::vector< FixedVertex > mImmediateModeVerts
Definition: AppImplMswRendererDx.h:123
Vec4f diffuse
Definition: AppImplMswRendererDx.h:53
virtual void prepareToggleFullScreen()
Definition: AppImplMswRendererDx.cpp:229
ID3D11VertexShader * mFixedTextureVertexShader
Definition: AppImplMswRendererDx.h:152
GLenum GLsizei width
Definition: GLee.h:969
ID3D11VertexShader * mVboPositionNormalColorLightVS
Definition: AppImplMswRendererDx.h:171
ID3D11PixelShader * mFixedTextureLightPixelShader
Definition: AppImplMswRendererDx.h:155
RenderStateFlag
Definition: AppImplMswRendererDx.h:78
Vec4< float > Vec4f
Definition: Vector.h:1320
MatrixStack mModelView
Definition: AppImplMswRendererDx.h:117
D3D11_BLEND_DESC mBlendDesc
Definition: AppImplMswRendererDx.h:146
ID3D11PixelShader * mFixedColorLightPixelShader
Definition: AppImplMswRendererDx.h:151
FixedVertex(Vec4f POS=Vec4f(0, 0, 0, 1), Vec3f NORM=Vec3f(), Vec2f UV=Vec2f(), Vec4f COLOR=Vec4f())
Definition: AppImplMswRendererDx.h:69
virtual void makeCurrentContext()
Definition: AppImplMswRendererDx.cpp:354
LightData()
Definition: AppImplMswRendererDx.h:52
ID3D11InputLayout * mFixedLayout
Definition: AppImplMswRendererDx.h:175
int mStateFlags
Definition: AppImplMswRendererDx.h:203
float constantAtt
Definition: AppImplMswRendererDx.h:56
Vec4f direction
Definition: AppImplMswRendererDx.h:61
Definition: MatrixStack.h:9
Definition: AppImplMswRendererDx.h:81
bool mLightingEnabled
Definition: AppImplMswRendererDx.h:125
ID3D11VertexShader * mVboPositionColorLightVS
Definition: AppImplMswRendererDx.h:169
FixedVertex(Vec2f POS, Vec3f NORM=Vec3f(), Vec2f UV=Vec2f(), Vec4f COLOR=Vec4f())
Definition: AppImplMswRendererDx.h:71
const dx::Texture * mCurrentBatchTexture
Definition: AppImplMswRendererDx.h:113
Vec4f pos
Definition: AppImplMswRendererDx.h:72
float quadAtt
Definition: AppImplMswRendererDx.h:58
bool createShadersFeatureLevel_9_3()
Definition: AppImplMswRendererDx.cpp:935
Vec4f pos
Definition: AppImplMswRendererDx.h:60
ID3D11PixelShader * mFixedTexturePixelShader
Definition: AppImplMswRendererDx.h:153
float spotCutoff
Definition: AppImplMswRendererDx.h:63
ID3D11VertexShader * mFixedColorLightVertexShader
Definition: AppImplMswRendererDx.h:150
bool createDevice(UINT createDeviceFlags)
Definition: AppImplMswRendererDx.cpp:625
ID3D11Texture2D * mDepthStencilTexture
Definition: AppImplMswRendererDx.h:140
ID3D11VertexShader * mVboPositionNormalLightVS
Definition: AppImplMswRendererDx.h:167
bool createShadersFeatureLevel_11_1()
Definition: AppImplMswRendererDx.cpp:1184
Vec4f color
Definition: AppImplMswRendererDx.h:75
void enableDepthWriting(bool enable=true)
Definition: AppImplMswRendererDx.cpp:421
ID3D11VertexShader * mVboPositionVS
Definition: AppImplMswRendererDx.h:157
ID3D11VertexShader * mVboPositionColorVS
Definition: AppImplMswRendererDx.h:160
ID3D11Buffer * mCBMatrices
Definition: AppImplMswRendererDx.h:142
Vec3< float > Vec3f
Definition: Vector.h:1317
Definition: AppImplMswRendererDx.h:50
void clearRenderFlag(RenderStateFlag flag)
Definition: AppImplMswRendererDx.h:110
Definition: App.h:127
void setViewport(int x, int y, int width, int height) const
Definition: AppImplMswRendererDx.cpp:359
Vec4f specular
Definition: AppImplMswRendererDx.h:54
GLenum GLsizei GLsizei height
Definition: GLee.h:1029
bool mFullScreen
Definition: AppImplMswRendererDx.h:204
D3D11_DEPTH_STENCIL_DESC mDepthStencilDesc
Definition: AppImplMswRendererDx.h:184
ID3D11VertexShader * mVboPositionColorTextureLightVS
Definition: AppImplMswRendererDx.h:172
float spotExponent
Definition: AppImplMswRendererDx.h:62
bool vsyncEnabled() const
Definition: AppImplMswRendererDx.h:100
virtual void kill()
Definition: AppImplMswRendererDx.cpp:1198
LightData mLights[8]
Definition: AppImplMswRendererDx.h:126
void enableAlphaBlending(bool premultiplied=false)
Definition: AppImplMswRendererDx.cpp:379
GLenum mImmediateModePrimitive
Definition: AppImplMswRendererDx.h:124
GLenum GLint x
Definition: GLee.h:987
virtual void defaultResize() const
Definition: AppImplMswRendererDx.cpp:299
std::shared_ptr< class Renderer > RendererRef
Definition: Renderer.h:85
void getPlatformWindowDimensions(DX_WINDOW_TYPE wnd, float *width, float *height) const
Definition: AppImplMswRendererDx.cpp:247
Represents an OpenGL Texture. Implicitly shared object.
Definition: DxTexture.h:59
void disableAlphaBlending()
Definition: AppImplMswRendererDx.cpp:400
bool initializeInternal(DX_WINDOW_TYPE wnd)
Definition: AppImplMswRendererDx.cpp:454
RendererDx * mRenderer
Definition: AppImplMswRendererDx.h:128
int initMultisample(int requestedLevelIdx)
Definition: AppImplMswRendererDx.cpp:619
ID3D11BlendState * mBlendState
Definition: AppImplMswRendererDx.h:145
bool mVsyncEnable
Definition: AppImplMswRendererDx.h:205
Vec4f ambient
Definition: AppImplMswRendererDx.h:55
bool getRenderFlag(RenderStateFlag flag)
Definition: AppImplMswRendererDx.h:108
virtual void finishToggleFullScreen()
Definition: AppImplMswRendererDx.cpp:234
void setRenderFlag(RenderStateFlag flag)
Definition: AppImplMswRendererDx.h:109
ID3D11VertexShader * mVboPositionNormalColorTextureLightVS
Definition: AppImplMswRendererDx.h:173
void releaseNonDeviceResources()
Definition: AppImplMswRendererDx.cpp:260
Vec4f mCurrentColor
Definition: AppImplMswRendererDx.h:120
Vec2f mCurrentUV
Definition: AppImplMswRendererDx.h:122
GLuint GLuint GLsizei GLenum type
Definition: GLee.h:963
bool createShadersFeatureLevel_10_1()
Definition: AppImplMswRendererDx.cpp:1018
ID3D11VertexShader * mVboPositionNormalVS
Definition: AppImplMswRendererDx.h:158
MatrixStack mProjection
Definition: AppImplMswRendererDx.h:118
Definition: AppImplMswRendererDx.h:67
float padding[2]
Definition: AppImplMswRendererDx.h:64
ID3D11DepthStencilState * mDepthStencilState
Definition: AppImplMswRendererDx.h:183
ID3D11VertexShader * mVboPositionTextureVS
Definition: AppImplMswRendererDx.h:159
Vec2f uv
Definition: AppImplMswRendererDx.h:74
AppImplMswRendererDx(App *aApp, RendererDx *aRenderer)
Definition: AppImplMswRendererDx.cpp:176
int int int * dx
Definition: GLee.h:17162
Vec3f norm
Definition: AppImplMswRendererDx.h:73
ID3D11RenderTargetView * mMainFramebuffer
Definition: AppImplMswRendererDx.h:139
bool createFramebufferResources()
Definition: AppImplMswRendererDx.cpp:701
ID3D11Buffer * mCBFixedParameters
Definition: AppImplMswRendererDx.h:144
ID3D11VertexShader * mVboPositionNormalTextureVS
Definition: AppImplMswRendererDx.h:161
ID3D11VertexShader * mVboPositionNormalColorVS
Definition: AppImplMswRendererDx.h:162
ID3D11Buffer * mCBLights
Definition: AppImplMswRendererDx.h:143
void enableDepthTesting(bool enable=true)
Definition: AppImplMswRendererDx.cpp:371
Vec3f mCurrentNormal
Definition: AppImplMswRendererDx.h:121
ID3D11VertexShader * mFixedColorVertexShader
Definition: AppImplMswRendererDx.h:148
ID3D11VertexShader * mVboPositionLightVS
Definition: AppImplMswRendererDx.h:166
int type
Definition: AppImplMswRendererDx.h:59
ID3D11VertexShader * mFixedTextureLightVertexShader
Definition: AppImplMswRendererDx.h:154
void enableVsync(bool enable=true)
Definition: AppImplMswRendererDx.h:99
FixedVertex(Vec3f POS, Vec3f NORM=Vec3f(), Vec2f UV=Vec2f(), Vec4f COLOR=Vec4f())
Definition: AppImplMswRendererDx.h:70
std::vector< std::pair< const dx::Texture *, std::vector< FixedVertex > > > mBatchedTextures
Definition: AppImplMswRendererDx.h:114
unsigned int GLenum
Definition: gldx.h:46
D3D_FEATURE_LEVEL mFeatureLevel
Definition: AppImplMswRendererDx.h:129
float linearAtt
Definition: AppImplMswRendererDx.h:57
ID3D11VertexShader * mVboPositionColorTextureVS
Definition: AppImplMswRendererDx.h:163
Definition: RendererDx.h:32
ID3D11RasterizerState * mDefaultRenderState
Definition: AppImplMswRendererDx.h:180
Definition: AppImplMswRendererDx.h:48
GLuint color
Definition: GLee.h:3198
ID3D11VertexShader * mVboPositionNormalTextureLightVS
Definition: AppImplMswRendererDx.h:170