Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / src / libGLESv2 / renderer / d3d11 / Renderer11.h
1 //
2 // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // Renderer11.h: Defines a back-end specific class for the D3D11 renderer.
8
9 #ifndef LIBGLESV2_RENDERER_RENDERER11_H_
10 #define LIBGLESV2_RENDERER_RENDERER11_H_
11
12 #include "common/angleutils.h"
13 #include "libGLESv2/angletypes.h"
14 #include "libGLESv2/mathutil.h"
15
16 #include "libGLESv2/renderer/Renderer.h"
17 #include "libGLESv2/renderer/d3d11/RenderStateCache.h"
18 #include "libGLESv2/renderer/d3d11/InputLayoutCache.h"
19 #include "libGLESv2/renderer/RenderTarget.h"
20
21 namespace gl
22 {
23 class Renderbuffer;
24 }
25
26 namespace rx
27 {
28
29 class VertexDataManager;
30 class IndexDataManager;
31 class StreamingIndexBufferInterface;
32
33 enum
34 {
35     MAX_VERTEX_UNIFORM_VECTORS_D3D11 = 1024,
36     MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 = 1024
37 };
38
39 class Renderer11 : public Renderer
40 {
41   public:
42     Renderer11(egl::Display *display, HDC hDc);
43     virtual ~Renderer11();
44
45     static Renderer11 *makeRenderer11(Renderer *renderer);
46
47     virtual EGLint initialize();
48     virtual bool resetDevice();
49
50     virtual int generateConfigs(ConfigDesc **configDescList);
51     virtual void deleteConfigs(ConfigDesc *configDescList);
52
53     virtual void sync(bool block);
54
55     virtual SwapChain *createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat);
56
57     virtual void setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &sampler);
58     virtual void setTexture(gl::SamplerType type, int index, gl::Texture *texture);
59
60     virtual void setRasterizerState(const gl::RasterizerState &rasterState);
61     virtual void setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::Color &blendColor,
62                                unsigned int sampleMask);
63     virtual void setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
64                                       int stencilBackRef, bool frontFaceCCW);
65
66     virtual void setScissorRectangle(const gl::Rectangle &scissor, bool enabled);
67     virtual bool setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
68                              bool ignoreViewport);
69
70     virtual bool applyPrimitiveType(GLenum mode, GLsizei count);
71     virtual bool applyRenderTarget(gl::Framebuffer *frameBuffer);
72     virtual void applyShaders(gl::ProgramBinary *programBinary);
73     virtual void applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray);
74     virtual GLenum applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances);
75     virtual GLenum applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
76
77     virtual void drawArrays(GLenum mode, GLsizei count, GLsizei instances);
78     virtual void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances);
79
80     virtual void clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer);
81
82     virtual void markAllStateDirty();
83
84     // lost device
85     void notifyDeviceLost();
86     virtual bool isDeviceLost();
87     virtual bool testDeviceLost(bool notify);
88     virtual bool testDeviceResettable();
89
90     // Renderer capabilities
91     virtual DWORD getAdapterVendor() const;
92     virtual std::string getRendererDescription() const;
93     virtual GUID getAdapterIdentifier() const;
94
95     virtual bool getBGRATextureSupport() const;
96     virtual bool getDXT1TextureSupport();
97     virtual bool getDXT3TextureSupport();
98     virtual bool getDXT5TextureSupport();
99     virtual bool getEventQuerySupport();
100     virtual bool getFloat32TextureSupport(bool *filtering, bool *renderable);
101     virtual bool getFloat16TextureSupport(bool *filtering, bool *renderable);
102     virtual bool getLuminanceTextureSupport();
103     virtual bool getLuminanceAlphaTextureSupport();
104     virtual unsigned int getMaxVertexTextureImageUnits() const;
105     virtual unsigned int getMaxCombinedTextureImageUnits() const;
106     virtual unsigned int getReservedVertexUniformVectors() const;
107     virtual unsigned int getReservedFragmentUniformVectors() const;
108     virtual unsigned int getMaxVertexUniformVectors() const;
109     virtual unsigned int getMaxFragmentUniformVectors() const;
110     virtual unsigned int getMaxVaryingVectors() const;
111     virtual bool getNonPower2TextureSupport() const;
112     virtual bool getDepthTextureSupport() const;
113     virtual bool getOcclusionQuerySupport() const;
114     virtual bool getInstancingSupport() const;
115     virtual bool getTextureFilterAnisotropySupport() const;
116     virtual float getTextureMaxAnisotropy() const;
117     virtual bool getShareHandleSupport() const;
118     virtual bool getDerivativeInstructionSupport() const;
119     virtual bool getPostSubBufferSupport() const;
120
121     virtual int getMajorShaderModel() const;
122     virtual float getMaxPointSize() const;
123     virtual int getMaxViewportDimension() const;
124     virtual int getMaxTextureWidth() const;
125     virtual int getMaxTextureHeight() const;
126     virtual bool get32BitIndexSupport() const;
127     virtual int getMinSwapInterval() const;
128     virtual int getMaxSwapInterval() const;
129
130     virtual GLsizei getMaxSupportedSamples() const;
131     int getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const;
132
133     virtual unsigned int getMaxRenderTargets() const;
134
135     // Pixel operations
136     virtual bool copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source);
137     virtual bool copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source);
138
139     virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
140                            GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level);
141     virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
142                            GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level);
143
144     bool copyTexture(ID3D11ShaderResourceView *source, const gl::Rectangle &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight,
145                      ID3D11RenderTargetView *dest, const gl::Rectangle &destArea, unsigned int destWidth, unsigned int destHeight, GLenum destFormat);
146
147     virtual bool blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
148                           bool blitRenderTarget, bool blitDepthStencil);
149     virtual void readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
150                             GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels);
151
152     // RenderTarget creation
153     virtual RenderTarget *createRenderTarget(SwapChain *swapChain, bool depth);
154     virtual RenderTarget *createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth);
155
156     // Shader operations
157     virtual ShaderExecutable *loadExecutable(const void *function, size_t length, rx::ShaderType type);
158     virtual ShaderExecutable *compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround);
159
160     // Image operations
161     virtual Image *createImage();
162     virtual void generateMipmap(Image *dest, Image *source);
163     virtual TextureStorage *createTextureStorage2D(SwapChain *swapChain);
164     virtual TextureStorage *createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height);
165     virtual TextureStorage *createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size);
166
167     // Buffer creation
168     virtual VertexBuffer *createVertexBuffer();
169     virtual IndexBuffer *createIndexBuffer();
170     virtual BufferStorage *createBufferStorage();
171
172     // Query and Fence creation
173     virtual QueryImpl *createQuery(GLenum type);
174     virtual FenceImpl *createFence();
175
176     // D3D11-renderer specific methods
177     ID3D11Device *getDevice() { return mDevice; }
178     ID3D11DeviceContext *getDeviceContext() { return mDeviceContext; };
179     IDXGIFactory *getDxgiFactory() { return mDxgiFactory; };
180
181     bool getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource);
182     void unapplyRenderTargets();
183     void setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView);
184
185     virtual bool getLUID(LUID *adapterLuid) const;
186
187   private:
188     DISALLOW_COPY_AND_ASSIGN(Renderer11);
189
190     void drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer);
191     void drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances);
192
193     void readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
194                          GLenum sourceFormat, GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
195                          GLint packAlignment, void *pixels);
196
197     void maskedClear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer);
198     rx::Range getViewportBounds() const;
199
200     bool blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget, 
201                               RenderTarget *drawRenderTarget, bool wholeBufferCopy);
202     ID3D11Texture2D *resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource);
203
204     HMODULE mD3d11Module;
205     HMODULE mDxgiModule;
206     HDC mDc;
207
208     bool mDeviceLost;
209
210     void initializeDevice();
211     void releaseDeviceResources();
212     int getMinorShaderModel() const;
213     void release();
214
215     RenderStateCache mStateCache;
216
217     // Support flags
218     bool mFloat16TextureSupport;
219     bool mFloat16FilterSupport;
220     bool mFloat16RenderSupport;
221
222     bool mFloat32TextureSupport;
223     bool mFloat32FilterSupport;
224     bool mFloat32RenderSupport;
225
226     bool mDXT1TextureSupport;
227     bool mDXT3TextureSupport;
228     bool mDXT5TextureSupport;
229
230     bool mDepthTextureSupport;
231
232     // Multisample format support
233     struct MultisampleSupportInfo
234     {
235         unsigned int qualityLevels[D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT];
236     };
237
238     typedef std::unordered_map<DXGI_FORMAT, MultisampleSupportInfo, std::hash<int> > MultisampleSupportMap;
239     MultisampleSupportMap mMultisampleSupportMap;
240
241     unsigned int mMaxSupportedSamples;
242
243     // current render target states
244     unsigned int mAppliedRenderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS];
245     unsigned int mAppliedDepthbufferSerial;
246     unsigned int mAppliedStencilbufferSerial;
247     bool mDepthStencilInitialized;
248     bool mRenderTargetDescInitialized;
249     rx::RenderTarget::Desc mRenderTargetDesc;
250     unsigned int mCurDepthSize;
251     unsigned int mCurStencilSize;
252
253     // Currently applied sampler states
254     bool mForceSetVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
255     gl::SamplerState mCurVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
256
257     bool mForceSetPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
258     gl::SamplerState mCurPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
259
260     // Currently applied textures
261     unsigned int mCurVertexTextureSerials[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
262     unsigned int mCurPixelTextureSerials[gl::MAX_TEXTURE_IMAGE_UNITS];
263
264     // Currently applied blend state
265     bool mForceSetBlendState;
266     gl::BlendState mCurBlendState;
267     gl::Color mCurBlendColor;
268     unsigned int mCurSampleMask;
269
270     // Currently applied rasterizer state
271     bool mForceSetRasterState;
272     gl::RasterizerState mCurRasterState;
273
274     // Currently applied depth stencil state
275     bool mForceSetDepthStencilState;
276     gl::DepthStencilState mCurDepthStencilState;
277     int mCurStencilRef;
278     int mCurStencilBackRef;
279
280     // Currently applied scissor rectangle
281     bool mForceSetScissor;
282     bool mScissorEnabled;
283     gl::Rectangle mCurScissor;
284
285     // Currently applied viewport
286     bool mForceSetViewport;
287     gl::Rectangle mCurViewport;
288     float mCurNear;
289     float mCurFar;
290
291     // Currently applied primitive topology
292     D3D11_PRIMITIVE_TOPOLOGY mCurrentPrimitiveTopology;
293
294     unsigned int mAppliedIBSerial;
295     unsigned int mAppliedStorageIBSerial;
296     unsigned int mAppliedIBOffset;
297
298     unsigned int mAppliedProgramBinarySerial;
299     bool mIsGeometryShaderActive;
300
301     dx_VertexConstants mVertexConstants;
302     dx_VertexConstants mAppliedVertexConstants;
303     ID3D11Buffer *mDriverConstantBufferVS;
304     ID3D11Buffer *mCurrentVertexConstantBuffer;
305
306     dx_PixelConstants mPixelConstants;
307     dx_PixelConstants mAppliedPixelConstants;
308     ID3D11Buffer *mDriverConstantBufferPS;
309     ID3D11Buffer *mCurrentPixelConstantBuffer;
310
311     ID3D11Buffer *mCurrentGeometryConstantBuffer;
312
313     // Vertex, index and input layouts
314     VertexDataManager *mVertexDataManager;
315     IndexDataManager *mIndexDataManager;
316     InputLayoutCache mInputLayoutCache;
317
318     StreamingIndexBufferInterface *mLineLoopIB;
319     StreamingIndexBufferInterface *mTriangleFanIB;
320
321     // Texture copy resources
322     bool mCopyResourcesInitialized;
323     ID3D11Buffer *mCopyVB;
324     ID3D11SamplerState *mCopySampler;
325     ID3D11InputLayout *mCopyIL;
326     ID3D11VertexShader *mCopyVS;
327     ID3D11PixelShader *mCopyRGBAPS;
328     ID3D11PixelShader *mCopyRGBPS;
329     ID3D11PixelShader *mCopyLumPS;
330     ID3D11PixelShader *mCopyLumAlphaPS;
331
332     // Masked clear resources
333     bool mClearResourcesInitialized;
334     ID3D11Buffer *mClearVB;
335     ID3D11InputLayout *mClearIL;
336     ID3D11VertexShader *mClearVS;
337     ID3D11PixelShader *mClearSinglePS;
338     ID3D11PixelShader *mClearMultiplePS;
339     ID3D11RasterizerState *mClearScissorRS;
340     ID3D11RasterizerState *mClearNoScissorRS;
341
342     // Sync query
343     ID3D11Query *mSyncQuery;
344
345     ID3D11Device *mDevice;
346     D3D_FEATURE_LEVEL mFeatureLevel;
347     ID3D11DeviceContext *mDeviceContext;
348     IDXGIAdapter *mDxgiAdapter;
349     DXGI_ADAPTER_DESC mAdapterDescription;
350     char mDescription[128];
351     IDXGIFactory *mDxgiFactory;
352
353     // Cached device caps
354     bool mBGRATextureSupport;
355 };
356
357 }
358 #endif // LIBGLESV2_RENDERER_RENDERER11_H_