2 // Copyright (c) 2012-2014 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.
7 // Renderer11.h: Defines a back-end specific class for the D3D11 renderer.
9 #ifndef LIBGLESV2_RENDERER_RENDERER11_H_
10 #define LIBGLESV2_RENDERER_RENDERER11_H_
12 #include "common/angleutils.h"
13 #include "libGLESv2/angletypes.h"
14 #include "common/mathutil.h"
16 #include "libGLESv2/renderer/Renderer.h"
17 #include "libGLESv2/renderer/d3d/HLSLCompiler.h"
18 #include "libGLESv2/renderer/d3d/d3d11/RenderStateCache.h"
19 #include "libGLESv2/renderer/d3d/d3d11/InputLayoutCache.h"
20 #include "libGLESv2/renderer/RenderTarget.h"
24 class FramebufferAttachment;
30 class VertexDataManager;
31 class IndexDataManager;
32 class StreamingIndexBufferInterface;
35 class PixelTransfer11;
36 struct PackPixelsParams;
40 MAX_VERTEX_UNIFORM_VECTORS_D3D11 = 1024,
41 MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 = 1024
44 class Renderer11 : public Renderer
47 Renderer11(egl::Display *display, EGLNativeDisplayType hDc, EGLint requestedDisplay);
48 virtual ~Renderer11();
50 static Renderer11 *makeRenderer11(Renderer *renderer);
52 virtual EGLint initialize();
53 virtual bool resetDevice();
55 virtual int generateConfigs(ConfigDesc **configDescList);
56 virtual void deleteConfigs(ConfigDesc *configDescList);
58 virtual void sync(bool block);
60 virtual SwapChain *createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat);
62 virtual void generateSwizzle(gl::Texture *texture);
63 virtual void setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &sampler);
64 virtual void setTexture(gl::SamplerType type, int index, gl::Texture *texture);
66 virtual bool setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[]);
68 virtual void setRasterizerState(const gl::RasterizerState &rasterState);
69 virtual void setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
70 unsigned int sampleMask);
71 virtual void setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
72 int stencilBackRef, bool frontFaceCCW);
74 virtual void setScissorRectangle(const gl::Rectangle &scissor, bool enabled);
75 virtual bool setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
78 virtual bool applyPrimitiveType(GLenum mode, GLsizei count);
79 virtual bool applyRenderTarget(gl::Framebuffer *frameBuffer);
80 virtual void applyShaders(gl::ProgramBinary *programBinary, const gl::VertexFormat inputLayout[], const gl::Framebuffer *framebuffer,
81 bool rasterizerDiscard, bool transformFeedbackActive);
82 virtual void applyUniforms(const gl::ProgramBinary &programBinary);
83 virtual GLenum applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], const gl::VertexAttribCurrentValueData currentValues[],
84 GLint first, GLsizei count, GLsizei instances);
85 virtual GLenum applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
86 virtual void applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[]);
88 virtual void drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive);
89 virtual void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices,
90 gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances);
92 virtual void clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer);
94 virtual void markAllStateDirty();
97 void notifyDeviceLost();
98 virtual bool isDeviceLost();
99 virtual bool testDeviceLost(bool notify);
100 virtual bool testDeviceResettable();
102 virtual DWORD getAdapterVendor() const;
103 virtual std::string getRendererDescription() const;
104 virtual GUID getAdapterIdentifier() const;
106 virtual unsigned int getMaxVertexTextureImageUnits() const;
107 virtual unsigned int getMaxCombinedTextureImageUnits() const;
108 virtual unsigned int getReservedVertexUniformVectors() const;
109 virtual unsigned int getReservedFragmentUniformVectors() const;
110 virtual unsigned int getMaxVertexUniformVectors() const;
111 virtual unsigned int getMaxFragmentUniformVectors() const;
112 virtual unsigned int getMaxVaryingVectors() const;
113 virtual unsigned int getMaxVertexShaderUniformBuffers() const;
114 virtual unsigned int getMaxFragmentShaderUniformBuffers() const;
115 virtual unsigned int getReservedVertexUniformBuffers() const;
116 virtual unsigned int getReservedFragmentUniformBuffers() const;
117 unsigned int getReservedVaryings() const;
118 virtual unsigned int getMaxTransformFeedbackBuffers() const;
119 virtual unsigned int getMaxTransformFeedbackSeparateComponents() const;
120 virtual unsigned int getMaxTransformFeedbackInterleavedComponents() const;
121 virtual unsigned int getMaxUniformBufferSize() const;
122 virtual bool getShareHandleSupport() const;
123 virtual bool getPostSubBufferSupport() const;
124 virtual int getMaxRecommendedElementsIndices() const;
125 virtual int getMaxRecommendedElementsVertices() const;
127 virtual int getMajorShaderModel() const;
128 virtual int getMinSwapInterval() const;
129 virtual int getMaxSwapInterval() const;
132 virtual bool copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source);
133 virtual bool copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source);
134 virtual bool copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source);
135 virtual bool copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source);
137 virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
138 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level);
139 virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
140 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level);
141 virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
142 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level);
143 virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
144 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level);
146 virtual bool blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
147 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter);
148 virtual void readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
149 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels);
151 // RenderTarget creation
152 virtual RenderTarget *createRenderTarget(SwapChain *swapChain, bool depth);
153 virtual RenderTarget *createRenderTarget(int width, int height, GLenum format, GLsizei samples);
156 virtual ShaderExecutable *loadExecutable(const void *function, size_t length, rx::ShaderType type,
157 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
158 bool separatedOutputBuffers);
159 virtual ShaderExecutable *compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
160 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
161 bool separatedOutputBuffers, D3DWorkaroundType workaround);
162 virtual UniformStorage *createUniformStorage(size_t storageSize);
165 virtual Image *createImage();
166 virtual void generateMipmap(Image *dest, Image *source);
167 virtual TextureStorage *createTextureStorage2D(SwapChain *swapChain);
168 virtual TextureStorage *createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels);
169 virtual TextureStorage *createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels);
170 virtual TextureStorage *createTextureStorage3D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels);
171 virtual TextureStorage *createTextureStorage2DArray(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels);
174 virtual Texture2DImpl *createTexture2D();
175 virtual TextureCubeImpl *createTextureCube();
176 virtual Texture3DImpl *createTexture3D();
177 virtual Texture2DArrayImpl *createTexture2DArray();
180 virtual BufferImpl *createBuffer();
181 virtual VertexBuffer *createVertexBuffer();
182 virtual IndexBuffer *createIndexBuffer();
184 // Vertex Array creation
185 virtual VertexArrayImpl *createVertexArray();
187 // Query and Fence creation
188 virtual QueryImpl *createQuery(GLenum type);
189 virtual FenceImpl *createFence();
191 // D3D11-renderer specific methods
192 ID3D11Device *getDevice() { return mDevice; }
193 ID3D11DeviceContext *getDeviceContext() { return mDeviceContext; };
194 IDXGIFactory *getDxgiFactory() { return mDxgiFactory; };
196 Blit11 *getBlitter() { return mBlit; }
198 // Buffer-to-texture and Texture-to-buffer copies
199 virtual bool supportsFastCopyBufferToTexture(GLenum internalFormat) const;
200 virtual bool fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
201 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea);
203 bool getRenderTargetResource(gl::FramebufferAttachment *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource);
204 void unapplyRenderTargets();
205 void setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView);
206 void packPixels(ID3D11Texture2D *readTexture, const PackPixelsParams ¶ms, void *pixelsOut);
208 virtual bool getLUID(LUID *adapterLuid) const;
209 virtual rx::VertexConversionType getVertexConversionType(const gl::VertexFormat &vertexFormat) const;
210 virtual GLenum getVertexComponentType(const gl::VertexFormat &vertexFormat) const;
213 DISALLOW_COPY_AND_ASSIGN(Renderer11);
215 virtual void generateCaps(gl::Caps *outCaps, gl::TextureCapsMap *outTextureCaps, gl::Extensions *outExtensions) const;
217 void drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer);
218 void drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances);
220 void readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format,
221 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void *pixels);
223 bool blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
224 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
225 bool colorBlit, bool depthBlit, bool stencilBlit);
226 ID3D11Texture2D *resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource);
228 static void invalidateFBOAttachmentSwizzles(gl::FramebufferAttachment *attachment, int mipLevel);
229 static void invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer);
231 HMODULE mD3d11Module;
234 EGLint mRequestedDisplay;
236 HLSLCompiler mCompiler;
240 void initializeDevice();
241 void releaseDeviceResources();
242 int getMinorShaderModel() const;
245 RenderStateCache mStateCache;
247 // current render target states
248 unsigned int mAppliedRenderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS];
249 unsigned int mAppliedDepthbufferSerial;
250 unsigned int mAppliedStencilbufferSerial;
251 bool mDepthStencilInitialized;
252 bool mRenderTargetDescInitialized;
253 rx::RenderTarget::Desc mRenderTargetDesc;
255 // Currently applied sampler states
256 bool mForceSetVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
257 gl::SamplerState mCurVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
259 bool mForceSetPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
260 gl::SamplerState mCurPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
262 // Currently applied textures
263 ID3D11ShaderResourceView *mCurVertexSRVs[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
264 ID3D11ShaderResourceView *mCurPixelSRVs[gl::MAX_TEXTURE_IMAGE_UNITS];
266 // Currently applied blend state
267 bool mForceSetBlendState;
268 gl::BlendState mCurBlendState;
269 gl::ColorF mCurBlendColor;
270 unsigned int mCurSampleMask;
272 // Currently applied rasterizer state
273 bool mForceSetRasterState;
274 gl::RasterizerState mCurRasterState;
276 // Currently applied depth stencil state
277 bool mForceSetDepthStencilState;
278 gl::DepthStencilState mCurDepthStencilState;
280 int mCurStencilBackRef;
282 // Currently applied scissor rectangle
283 bool mForceSetScissor;
284 bool mScissorEnabled;
285 gl::Rectangle mCurScissor;
287 // Currently applied viewport
288 bool mForceSetViewport;
289 gl::Rectangle mCurViewport;
293 // Currently applied primitive topology
294 D3D11_PRIMITIVE_TOPOLOGY mCurrentPrimitiveTopology;
296 // Currently applied index buffer
297 ID3D11Buffer *mAppliedIB;
298 DXGI_FORMAT mAppliedIBFormat;
299 unsigned int mAppliedIBOffset;
301 // Currently applied transform feedback buffers
302 ID3D11Buffer *mAppliedTFBuffers[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
303 GLintptr mAppliedTFOffsets[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
305 // Currently applied shaders
306 ID3D11VertexShader *mAppliedVertexShader;
307 ID3D11GeometryShader *mAppliedGeometryShader;
308 ID3D11GeometryShader *mCurPointGeometryShader;
309 ID3D11PixelShader *mAppliedPixelShader;
311 dx_VertexConstants mVertexConstants;
312 dx_VertexConstants mAppliedVertexConstants;
313 ID3D11Buffer *mDriverConstantBufferVS;
314 ID3D11Buffer *mCurrentVertexConstantBuffer;
315 unsigned int mCurrentConstantBufferVS[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS];
317 dx_PixelConstants mPixelConstants;
318 dx_PixelConstants mAppliedPixelConstants;
319 ID3D11Buffer *mDriverConstantBufferPS;
320 ID3D11Buffer *mCurrentPixelConstantBuffer;
321 unsigned int mCurrentConstantBufferPS[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS];
323 ID3D11Buffer *mCurrentGeometryConstantBuffer;
325 // Vertex, index and input layouts
326 VertexDataManager *mVertexDataManager;
327 IndexDataManager *mIndexDataManager;
328 InputLayoutCache mInputLayoutCache;
330 StreamingIndexBufferInterface *mLineLoopIB;
331 StreamingIndexBufferInterface *mTriangleFanIB;
333 // Texture copy resources
335 PixelTransfer11 *mPixelTransfer;
337 // Masked clear resources
341 ID3D11Query *mSyncQuery;
343 ID3D11Device *mDevice;
344 D3D_FEATURE_LEVEL mFeatureLevel;
345 ID3D11DeviceContext *mDeviceContext;
346 IDXGIAdapter *mDxgiAdapter;
347 DXGI_ADAPTER_DESC mAdapterDescription;
348 char mDescription[128];
349 IDXGIFactory *mDxgiFactory;
353 #endif // LIBGLESV2_RENDERER_RENDERER11_H_