Home | History | Annotate | Download | only in d3d11
      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/d3d11/RenderStateCache.h"
     19 #include "libGLESv2/renderer/d3d11/InputLayoutCache.h"
     20 #include "libGLESv2/renderer/RenderTarget.h"
     21 
     22 namespace gl
     23 {
     24 class FramebufferAttachment;
     25 }
     26 
     27 namespace rx
     28 {
     29 
     30 class VertexDataManager;
     31 class IndexDataManager;
     32 class StreamingIndexBufferInterface;
     33 class Blit11;
     34 class Clear11;
     35 class PixelTransfer11;
     36 struct PackPixelsParams;
     37 
     38 enum
     39 {
     40     MAX_VERTEX_UNIFORM_VECTORS_D3D11 = 1024,
     41     MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 = 1024
     42 };
     43 
     44 class Renderer11 : public Renderer
     45 {
     46   public:
     47     Renderer11(egl::Display *display, HDC hDc);
     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 bool 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, bool rasterizerDiscard, bool transformFeedbackActive, const gl::VertexFormat inputLayout[]);
     81     virtual void applyUniforms(const gl::ProgramBinary &programBinary);
     82     virtual GLenum applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
     83                                      GLint first, GLsizei count, GLsizei instances);
     84     virtual GLenum applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
     85     virtual void applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[]);
     86 
     87     virtual void drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive);
     88     virtual void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices,
     89                               gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances);
     90 
     91     virtual void clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer);
     92 
     93     virtual void markAllStateDirty();
     94 
     95     // lost device
     96     void notifyDeviceLost();
     97     virtual bool isDeviceLost();
     98     virtual bool testDeviceLost(bool notify);
     99     virtual bool testDeviceResettable();
    100 
    101     // Renderer capabilities
    102     virtual DWORD getAdapterVendor() const;
    103     virtual std::string getRendererDescription() const;
    104     virtual GUID getAdapterIdentifier() const;
    105 
    106     virtual bool getBGRATextureSupport() const;
    107     virtual bool getDXT1TextureSupport() const;
    108     virtual bool getDXT3TextureSupport() const;
    109     virtual bool getDXT5TextureSupport() const;
    110     virtual bool getEventQuerySupport() const;
    111     virtual bool getFloat32TextureSupport() const;
    112     virtual bool getFloat32TextureFilteringSupport() const;
    113     virtual bool getFloat32TextureRenderingSupport() const;
    114     virtual bool getFloat16TextureSupport() const;
    115     virtual bool getFloat16TextureFilteringSupport() const;
    116     virtual bool getFloat16TextureRenderingSupport() const;
    117     virtual bool getRGB565TextureSupport() const;
    118     virtual bool getLuminanceTextureSupport() const;
    119     virtual bool getLuminanceAlphaTextureSupport() const;
    120     virtual bool getRGTextureSupport() const;
    121     virtual unsigned int getMaxVertexTextureImageUnits() const;
    122     virtual unsigned int getMaxCombinedTextureImageUnits() const;
    123     virtual unsigned int getReservedVertexUniformVectors() const;
    124     virtual unsigned int getReservedFragmentUniformVectors() const;
    125     virtual unsigned int getMaxVertexUniformVectors() const;
    126     virtual unsigned int getMaxFragmentUniformVectors() const;
    127     virtual unsigned int getMaxVaryingVectors() const;
    128     virtual unsigned int getMaxVertexShaderUniformBuffers() const;
    129     virtual unsigned int getMaxFragmentShaderUniformBuffers() const;
    130     virtual unsigned int getReservedVertexUniformBuffers() const;
    131     virtual unsigned int getReservedFragmentUniformBuffers() const;
    132     unsigned int getReservedVaryings() const;
    133     virtual unsigned int getMaxTransformFeedbackBuffers() const;
    134     virtual unsigned int getMaxTransformFeedbackSeparateComponents() const;
    135     virtual unsigned int getMaxTransformFeedbackInterleavedComponents() const;
    136     virtual unsigned int getMaxUniformBufferSize() const;
    137     virtual bool getNonPower2TextureSupport() const;
    138     virtual bool getDepthTextureSupport() const;
    139     virtual bool getOcclusionQuerySupport() const;
    140     virtual bool getInstancingSupport() const;
    141     virtual bool getTextureFilterAnisotropySupport() const;
    142     virtual bool getPBOSupport() const;
    143     virtual float getTextureMaxAnisotropy() const;
    144     virtual bool getShareHandleSupport() const;
    145     virtual bool getDerivativeInstructionSupport() const;
    146     virtual bool getPostSubBufferSupport() const;
    147     virtual int getMaxRecommendedElementsIndices() const;
    148     virtual int getMaxRecommendedElementsVertices() const;
    149 
    150     virtual int getMajorShaderModel() const;
    151     virtual float getMaxPointSize() const;
    152     virtual int getMaxViewportDimension() const;
    153     virtual int getMaxTextureWidth() const;
    154     virtual int getMaxTextureHeight() const;
    155     virtual int getMaxTextureDepth() const;
    156     virtual int getMaxTextureArrayLayers() const;
    157     virtual bool get32BitIndexSupport() const;
    158     virtual int getMinSwapInterval() const;
    159     virtual int getMaxSwapInterval() const;
    160 
    161     virtual GLsizei getMaxSupportedSamples() const;
    162     virtual GLsizei getMaxSupportedFormatSamples(GLenum internalFormat) const;
    163     virtual GLsizei getNumSampleCounts(GLenum internalFormat) const;
    164     virtual void getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const;
    165     int getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const;
    166 
    167     virtual unsigned int getMaxRenderTargets() const;
    168 
    169     // Pixel operations
    170     virtual bool copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source);
    171     virtual bool copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source);
    172     virtual bool copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source);
    173     virtual bool copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source);
    174 
    175     virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
    176                            GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level);
    177     virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
    178                            GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level);
    179     virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
    180                            GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level);
    181     virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
    182                            GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level);
    183 
    184     virtual bool blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
    185                           const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter);
    186     virtual void readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
    187                             GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels);
    188 
    189     // RenderTarget creation
    190     virtual RenderTarget *createRenderTarget(SwapChain *swapChain, bool depth);
    191     virtual RenderTarget *createRenderTarget(int width, int height, GLenum format, GLsizei samples);
    192 
    193     // Shader operations
    194     virtual ShaderExecutable *loadExecutable(const void *function, size_t length, rx::ShaderType type,
    195                                              const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
    196                                              bool separatedOutputBuffers);
    197     virtual ShaderExecutable *compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
    198                                                   const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
    199                                                   bool separatedOutputBuffers, D3DWorkaroundType workaround);
    200     virtual UniformStorage *createUniformStorage(size_t storageSize);
    201 
    202     // Image operations
    203     virtual Image *createImage();
    204     virtual void generateMipmap(Image *dest, Image *source);
    205     virtual TextureStorage *createTextureStorage2D(SwapChain *swapChain);
    206     virtual TextureStorage *createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels);
    207     virtual TextureStorage *createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels);
    208     virtual TextureStorage *createTextureStorage3D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels);
    209     virtual TextureStorage *createTextureStorage2DArray(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels);
    210 
    211     // Buffer creation
    212     virtual VertexBuffer *createVertexBuffer();
    213     virtual IndexBuffer *createIndexBuffer();
    214     virtual BufferStorage *createBufferStorage();
    215 
    216     // Query and Fence creation
    217     virtual QueryImpl *createQuery(GLenum type);
    218     virtual FenceImpl *createFence();
    219 
    220     // D3D11-renderer specific methods
    221     ID3D11Device *getDevice() { return mDevice; }
    222     ID3D11DeviceContext *getDeviceContext() { return mDeviceContext; };
    223     IDXGIFactory *getDxgiFactory() { return mDxgiFactory; };
    224 
    225     Blit11 *getBlitter() { return mBlit; }
    226 
    227     // Buffer-to-texture and Texture-to-buffer copies
    228     virtual bool supportsFastCopyBufferToTexture(GLenum internalFormat) const;
    229     virtual bool fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
    230                                          GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea);
    231 
    232     bool getRenderTargetResource(gl::FramebufferAttachment *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource);
    233     void unapplyRenderTargets();
    234     void setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView);
    235     void packPixels(ID3D11Texture2D *readTexture, const PackPixelsParams &params, void *pixelsOut);
    236 
    237     virtual bool getLUID(LUID *adapterLuid) const;
    238     virtual GLenum getNativeTextureFormat(GLenum internalFormat) const;
    239     virtual rx::VertexConversionType getVertexConversionType(const gl::VertexFormat &vertexFormat) const;
    240     virtual GLenum getVertexComponentType(const gl::VertexFormat &vertexFormat) const;
    241 
    242   private:
    243     DISALLOW_COPY_AND_ASSIGN(Renderer11);
    244 
    245     void drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer);
    246     void drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances);
    247 
    248     void readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format,
    249                          GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void *pixels);
    250 
    251     rx::Range getViewportBounds() const;
    252 
    253     bool blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
    254                               RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
    255                               bool colorBlit, bool depthBlit, bool stencilBlit);
    256     ID3D11Texture2D *resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource);
    257 
    258     static void invalidateFBOAttachmentSwizzles(gl::FramebufferAttachment *attachment, int mipLevel);
    259     static void invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer);
    260 
    261     HMODULE mD3d11Module;
    262     HMODULE mDxgiModule;
    263     HDC mDc;
    264 
    265     HLSLCompiler mCompiler;
    266 
    267     bool mDeviceLost;
    268 
    269     void initializeDevice();
    270     void releaseDeviceResources();
    271     int getMinorShaderModel() const;
    272     void release();
    273 
    274     RenderStateCache mStateCache;
    275 
    276     // Support flags
    277     bool mFloat16TextureSupport;
    278     bool mFloat16FilterSupport;
    279     bool mFloat16RenderSupport;
    280 
    281     bool mFloat32TextureSupport;
    282     bool mFloat32FilterSupport;
    283     bool mFloat32RenderSupport;
    284 
    285     bool mDXT1TextureSupport;
    286     bool mDXT3TextureSupport;
    287     bool mDXT5TextureSupport;
    288 
    289     bool mRGTextureSupport;
    290 
    291     bool mDepthTextureSupport;
    292 
    293     // Multisample format support
    294     struct MultisampleSupportInfo
    295     {
    296         unsigned int qualityLevels[D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT];
    297         unsigned int maxSupportedSamples;
    298     };
    299     MultisampleSupportInfo getMultisampleSupportInfo(DXGI_FORMAT format);
    300 
    301     typedef std::unordered_map<DXGI_FORMAT, MultisampleSupportInfo> MultisampleSupportMap;
    302     MultisampleSupportMap mMultisampleSupportMap;
    303 
    304     unsigned int mMaxSupportedSamples;
    305 
    306     // current render target states
    307     unsigned int mAppliedRenderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS];
    308     unsigned int mAppliedDepthbufferSerial;
    309     unsigned int mAppliedStencilbufferSerial;
    310     bool mDepthStencilInitialized;
    311     bool mRenderTargetDescInitialized;
    312     rx::RenderTarget::Desc mRenderTargetDesc;
    313 
    314     // Currently applied sampler states
    315     bool mForceSetVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
    316     gl::SamplerState mCurVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
    317 
    318     bool mForceSetPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
    319     gl::SamplerState mCurPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
    320 
    321     // Currently applied textures
    322     ID3D11ShaderResourceView *mCurVertexSRVs[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
    323     ID3D11ShaderResourceView *mCurPixelSRVs[gl::MAX_TEXTURE_IMAGE_UNITS];
    324 
    325     // Currently applied blend state
    326     bool mForceSetBlendState;
    327     gl::BlendState mCurBlendState;
    328     gl::ColorF mCurBlendColor;
    329     unsigned int mCurSampleMask;
    330 
    331     // Currently applied rasterizer state
    332     bool mForceSetRasterState;
    333     gl::RasterizerState mCurRasterState;
    334 
    335     // Currently applied depth stencil state
    336     bool mForceSetDepthStencilState;
    337     gl::DepthStencilState mCurDepthStencilState;
    338     int mCurStencilRef;
    339     int mCurStencilBackRef;
    340 
    341     // Currently applied scissor rectangle
    342     bool mForceSetScissor;
    343     bool mScissorEnabled;
    344     gl::Rectangle mCurScissor;
    345 
    346     // Currently applied viewport
    347     bool mForceSetViewport;
    348     gl::Rectangle mCurViewport;
    349     float mCurNear;
    350     float mCurFar;
    351 
    352     // Currently applied primitive topology
    353     D3D11_PRIMITIVE_TOPOLOGY mCurrentPrimitiveTopology;
    354 
    355     // Currently applied index buffer
    356     ID3D11Buffer *mAppliedIB;
    357     DXGI_FORMAT mAppliedIBFormat;
    358     unsigned int mAppliedIBOffset;
    359 
    360     // Currently applied transform feedback buffers
    361     ID3D11Buffer *mAppliedTFBuffers[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
    362     GLintptr mAppliedTFOffsets[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
    363 
    364     // Currently applied shaders
    365     ID3D11VertexShader *mAppliedVertexShader;
    366     ID3D11GeometryShader *mAppliedGeometryShader;
    367     ID3D11GeometryShader *mCurPointGeometryShader;
    368     ID3D11PixelShader *mAppliedPixelShader;
    369 
    370     dx_VertexConstants mVertexConstants;
    371     dx_VertexConstants mAppliedVertexConstants;
    372     ID3D11Buffer *mDriverConstantBufferVS;
    373     ID3D11Buffer *mCurrentVertexConstantBuffer;
    374     unsigned int mCurrentConstantBufferVS[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS];
    375 
    376     dx_PixelConstants mPixelConstants;
    377     dx_PixelConstants mAppliedPixelConstants;
    378     ID3D11Buffer *mDriverConstantBufferPS;
    379     ID3D11Buffer *mCurrentPixelConstantBuffer;
    380     unsigned int mCurrentConstantBufferPS[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS];
    381 
    382     ID3D11Buffer *mCurrentGeometryConstantBuffer;
    383 
    384     // Vertex, index and input layouts
    385     VertexDataManager *mVertexDataManager;
    386     IndexDataManager *mIndexDataManager;
    387     InputLayoutCache mInputLayoutCache;
    388 
    389     StreamingIndexBufferInterface *mLineLoopIB;
    390     StreamingIndexBufferInterface *mTriangleFanIB;
    391 
    392     // Texture copy resources
    393     Blit11 *mBlit;
    394     PixelTransfer11 *mPixelTransfer;
    395 
    396     // Masked clear resources
    397     Clear11 *mClear;
    398 
    399     // Sync query
    400     ID3D11Query *mSyncQuery;
    401 
    402     ID3D11Device *mDevice;
    403     D3D_FEATURE_LEVEL mFeatureLevel;
    404     ID3D11DeviceContext *mDeviceContext;
    405     IDXGIAdapter *mDxgiAdapter;
    406     DXGI_ADAPTER_DESC mAdapterDescription;
    407     char mDescription[128];
    408     IDXGIFactory *mDxgiFactory;
    409 
    410     // Cached device caps
    411     bool mBGRATextureSupport;
    412 };
    413 
    414 }
    415 #endif // LIBGLESV2_RENDERER_RENDERER11_H_
    416