1//
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.
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 "common/mathutil.h"
15
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"
21
22namespace gl
23{
24class FramebufferAttachment;
25}
26
27namespace rx
28{
29
30class VertexDataManager;
31class IndexDataManager;
32class StreamingIndexBufferInterface;
33class Blit11;
34class Clear11;
35class PixelTransfer11;
36struct PackPixelsParams;
37
38enum
39{
40    MAX_VERTEX_UNIFORM_VECTORS_D3D11 = 1024,
41    MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 = 1024
42};
43
44class Renderer11 : public Renderer
45{
46  public:
47    Renderer11(egl::Display *display, EGLNativeDisplayType hDc, EGLint requestedDisplay);
48    virtual ~Renderer11();
49
50    static Renderer11 *makeRenderer11(Renderer *renderer);
51
52    virtual EGLint initialize();
53    virtual bool resetDevice();
54
55    virtual int generateConfigs(ConfigDesc **configDescList);
56    virtual void deleteConfigs(ConfigDesc *configDescList);
57
58    virtual void sync(bool block);
59
60    virtual SwapChain *createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat);
61
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);
65
66    virtual bool setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[]);
67
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);
73
74    virtual void setScissorRectangle(const gl::Rectangle &scissor, bool enabled);
75    virtual void setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
76                             bool ignoreViewport);
77
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 gl::Error applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], const gl::VertexAttribCurrentValueData currentValues[],
84                                        GLint first, GLsizei count, GLsizei instances);
85    virtual gl::Error applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
86    virtual void applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[]);
87
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);
91
92    virtual gl::Error clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer);
93
94    virtual void markAllStateDirty();
95
96    // lost device
97    void notifyDeviceLost();
98    virtual bool isDeviceLost();
99    virtual bool testDeviceLost(bool notify);
100    virtual bool testDeviceResettable();
101
102    virtual DWORD getAdapterVendor() const;
103    virtual std::string getRendererDescription() const;
104    virtual GUID getAdapterIdentifier() const;
105
106    virtual unsigned int getReservedVertexUniformVectors() const;
107    virtual unsigned int getReservedFragmentUniformVectors() const;
108    virtual unsigned int getReservedVertexUniformBuffers() const;
109    virtual unsigned int getReservedFragmentUniformBuffers() const;
110    virtual bool getShareHandleSupport() const;
111    virtual bool getPostSubBufferSupport() const;
112
113    virtual int getMajorShaderModel() const;
114    virtual int getMinSwapInterval() const;
115    virtual int getMaxSwapInterval() const;
116
117    // Pixel operations
118    virtual bool copyToRenderTarget2D(TextureStorage *dest, TextureStorage *source);
119    virtual bool copyToRenderTargetCube(TextureStorage *dest, TextureStorage *source);
120    virtual bool copyToRenderTarget3D(TextureStorage *dest, TextureStorage *source);
121    virtual bool copyToRenderTarget2DArray(TextureStorage *dest, TextureStorage *source);
122
123    virtual bool copyImage2D(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
124                             GLint xoffset, GLint yoffset, TextureStorage *storage, GLint level);
125    virtual bool copyImageCube(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
126                               GLint xoffset, GLint yoffset, TextureStorage *storage, GLenum target, GLint level);
127    virtual bool copyImage3D(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
128                             GLint xoffset, GLint yoffset, GLint zOffset, TextureStorage *storage, GLint level);
129    virtual bool copyImage2DArray(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
130                                  GLint xoffset, GLint yoffset, GLint zOffset, TextureStorage *storage, GLint level);
131
132    virtual bool blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
133                          const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter);
134
135    virtual gl::Error readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
136                                 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, uint8_t *pixels);
137
138    // RenderTarget creation
139    virtual RenderTarget *createRenderTarget(SwapChain *swapChain, bool depth);
140    virtual RenderTarget *createRenderTarget(int width, int height, GLenum format, GLsizei samples);
141
142    // Shader creation
143    virtual ShaderImpl *createShader(GLenum type);
144    virtual ProgramImpl *createProgram();
145
146    // Shader operations
147    virtual void releaseShaderCompiler();
148    virtual ShaderExecutable *loadExecutable(const void *function, size_t length, rx::ShaderType type,
149                                             const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
150                                             bool separatedOutputBuffers);
151    virtual ShaderExecutable *compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
152                                                  const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
153                                                  bool separatedOutputBuffers, D3DWorkaroundType workaround);
154    virtual UniformStorage *createUniformStorage(size_t storageSize);
155
156    // Image operations
157    virtual Image *createImage();
158    virtual void generateMipmap(Image *dest, Image *source);
159    virtual TextureStorage *createTextureStorage2D(SwapChain *swapChain);
160    virtual TextureStorage *createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels);
161    virtual TextureStorage *createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels);
162    virtual TextureStorage *createTextureStorage3D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels);
163    virtual TextureStorage *createTextureStorage2DArray(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels);
164
165    // Texture creation
166    virtual TextureImpl *createTexture(GLenum target);
167
168    // Buffer creation
169    virtual BufferImpl *createBuffer();
170    virtual VertexBuffer *createVertexBuffer();
171    virtual IndexBuffer *createIndexBuffer();
172
173    // Vertex Array creation
174    virtual VertexArrayImpl *createVertexArray();
175
176    // Query and Fence creation
177    virtual QueryImpl *createQuery(GLenum type);
178    virtual FenceImpl *createFence();
179
180    // Transform Feedback creation
181    virtual TransformFeedbackImpl* createTransformFeedback();
182
183    // D3D11-renderer specific methods
184    ID3D11Device *getDevice() { return mDevice; }
185    ID3D11DeviceContext *getDeviceContext() { return mDeviceContext; };
186    IDXGIFactory *getDxgiFactory() { return mDxgiFactory; };
187
188    Blit11 *getBlitter() { return mBlit; }
189
190    // Buffer-to-texture and Texture-to-buffer copies
191    virtual bool supportsFastCopyBufferToTexture(GLenum internalFormat) const;
192    virtual bool fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
193                                         GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea);
194
195    bool getRenderTargetResource(gl::FramebufferAttachment *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource);
196    void unapplyRenderTargets();
197    void setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView);
198    void packPixels(ID3D11Texture2D *readTexture, const PackPixelsParams &params, uint8_t *pixelsOut);
199
200    virtual bool getLUID(LUID *adapterLuid) const;
201    virtual rx::VertexConversionType getVertexConversionType(const gl::VertexFormat &vertexFormat) const;
202    virtual GLenum getVertexComponentType(const gl::VertexFormat &vertexFormat) const;
203
204  private:
205    DISALLOW_COPY_AND_ASSIGN(Renderer11);
206
207    virtual void generateCaps(gl::Caps *outCaps, gl::TextureCapsMap *outTextureCaps, gl::Extensions *outExtensions) const;
208
209    void drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer);
210    void drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances);
211
212    gl::Error readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format,
213                              GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, uint8_t *pixels);
214
215    bool blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
216                              RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
217                              bool colorBlit, bool depthBlit, bool stencilBlit);
218    ID3D11Texture2D *resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource);
219
220    static void invalidateFBOAttachmentSwizzles(gl::FramebufferAttachment *attachment, int mipLevel);
221    static void invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer);
222
223    HMODULE mD3d11Module;
224    HMODULE mDxgiModule;
225    HDC mDc;
226    EGLint mRequestedDisplay;
227
228    HLSLCompiler mCompiler;
229
230    bool mDeviceLost;
231
232    void initializeDevice();
233    void releaseDeviceResources();
234    int getMinorShaderModel() const;
235    void release();
236
237    RenderStateCache mStateCache;
238
239    // current render target states
240    unsigned int mAppliedRenderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS];
241    unsigned int mAppliedDepthbufferSerial;
242    unsigned int mAppliedStencilbufferSerial;
243    bool mDepthStencilInitialized;
244    bool mRenderTargetDescInitialized;
245    rx::RenderTarget::Desc mRenderTargetDesc;
246
247    // Currently applied sampler states
248    std::vector<bool> mForceSetVertexSamplerStates;
249    std::vector<gl::SamplerState> mCurVertexSamplerStates;
250
251    std::vector<bool> mForceSetPixelSamplerStates;
252    std::vector<gl::SamplerState> mCurPixelSamplerStates;
253
254    // Currently applied textures
255    std::vector<ID3D11ShaderResourceView*> mCurVertexSRVs;
256    std::vector<ID3D11ShaderResourceView*> mCurPixelSRVs;
257
258    // Currently applied blend state
259    bool mForceSetBlendState;
260    gl::BlendState mCurBlendState;
261    gl::ColorF mCurBlendColor;
262    unsigned int mCurSampleMask;
263
264    // Currently applied rasterizer state
265    bool mForceSetRasterState;
266    gl::RasterizerState mCurRasterState;
267
268    // Currently applied depth stencil state
269    bool mForceSetDepthStencilState;
270    gl::DepthStencilState mCurDepthStencilState;
271    int mCurStencilRef;
272    int mCurStencilBackRef;
273
274    // Currently applied scissor rectangle
275    bool mForceSetScissor;
276    bool mScissorEnabled;
277    gl::Rectangle mCurScissor;
278
279    // Currently applied viewport
280    bool mForceSetViewport;
281    gl::Rectangle mCurViewport;
282    float mCurNear;
283    float mCurFar;
284
285    // Currently applied primitive topology
286    D3D11_PRIMITIVE_TOPOLOGY mCurrentPrimitiveTopology;
287
288    // Currently applied index buffer
289    ID3D11Buffer *mAppliedIB;
290    DXGI_FORMAT mAppliedIBFormat;
291    unsigned int mAppliedIBOffset;
292
293    // Currently applied transform feedback buffers
294    ID3D11Buffer *mAppliedTFBuffers[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
295    GLintptr mAppliedTFOffsets[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
296
297    // Currently applied shaders
298    ID3D11VertexShader *mAppliedVertexShader;
299    ID3D11GeometryShader *mAppliedGeometryShader;
300    ID3D11GeometryShader *mCurPointGeometryShader;
301    ID3D11PixelShader *mAppliedPixelShader;
302
303    dx_VertexConstants mVertexConstants;
304    dx_VertexConstants mAppliedVertexConstants;
305    ID3D11Buffer *mDriverConstantBufferVS;
306    ID3D11Buffer *mCurrentVertexConstantBuffer;
307    unsigned int mCurrentConstantBufferVS[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS];
308
309    dx_PixelConstants mPixelConstants;
310    dx_PixelConstants mAppliedPixelConstants;
311    ID3D11Buffer *mDriverConstantBufferPS;
312    ID3D11Buffer *mCurrentPixelConstantBuffer;
313    unsigned int mCurrentConstantBufferPS[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS];
314
315    ID3D11Buffer *mCurrentGeometryConstantBuffer;
316
317    // Vertex, index and input layouts
318    VertexDataManager *mVertexDataManager;
319    IndexDataManager *mIndexDataManager;
320    InputLayoutCache mInputLayoutCache;
321
322    StreamingIndexBufferInterface *mLineLoopIB;
323    StreamingIndexBufferInterface *mTriangleFanIB;
324
325    // Texture copy resources
326    Blit11 *mBlit;
327    PixelTransfer11 *mPixelTransfer;
328
329    // Masked clear resources
330    Clear11 *mClear;
331
332    // Sync query
333    ID3D11Query *mSyncQuery;
334
335    ID3D11Device *mDevice;
336    D3D_FEATURE_LEVEL mFeatureLevel;
337    ID3D11DeviceContext *mDeviceContext;
338    IDXGIAdapter *mDxgiAdapter;
339    DXGI_ADAPTER_DESC mAdapterDescription;
340    char mDescription[128];
341    IDXGIFactory *mDxgiFactory;
342};
343
344}
345#endif // LIBGLESV2_RENDERER_RENDERER11_H_
346