Home | History | Annotate | Download | only in d3d9
      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 // Renderer9.h: Defines a back-end specific class for the D3D9 renderer.
      8 
      9 #ifndef LIBGLESV2_RENDERER_RENDERER9_H_
     10 #define LIBGLESV2_RENDERER_RENDERER9_H_
     11 
     12 #include "common/angleutils.h"
     13 #include "common/mathutil.h"
     14 #include "libGLESv2/renderer/d3d/HLSLCompiler.h"
     15 #include "libGLESv2/renderer/d3d9/ShaderCache.h"
     16 #include "libGLESv2/renderer/d3d9/VertexDeclarationCache.h"
     17 #include "libGLESv2/renderer/Renderer.h"
     18 #include "libGLESv2/renderer/RenderTarget.h"
     19 
     20 namespace gl
     21 {
     22 class FramebufferAttachment;
     23 }
     24 
     25 namespace rx
     26 {
     27 class VertexDataManager;
     28 class IndexDataManager;
     29 class StreamingIndexBufferInterface;
     30 struct TranslatedAttribute;
     31 class Blit9;
     32 
     33 class Renderer9 : public Renderer
     34 {
     35   public:
     36     Renderer9(egl::Display *display, HDC hDc);
     37     virtual ~Renderer9();
     38 
     39     static Renderer9 *makeRenderer9(Renderer *renderer);
     40 
     41     virtual EGLint initialize();
     42     virtual bool resetDevice();
     43 
     44     virtual int generateConfigs(ConfigDesc **configDescList);
     45     virtual void deleteConfigs(ConfigDesc *configDescList);
     46 
     47     void startScene();
     48     void endScene();
     49 
     50     virtual void sync(bool block);
     51 
     52     virtual SwapChain *createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat);
     53 
     54     IDirect3DQuery9* allocateEventQuery();
     55     void freeEventQuery(IDirect3DQuery9* query);
     56 
     57     // resource creation
     58     IDirect3DVertexShader9 *createVertexShader(const DWORD *function, size_t length);
     59     IDirect3DPixelShader9 *createPixelShader(const DWORD *function, size_t length);
     60     HRESULT createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer);
     61     HRESULT createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer);
     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 applyRenderTarget(gl::Framebuffer *frameBuffer);
     79     virtual void applyShaders(gl::ProgramBinary *programBinary, bool rasterizerDiscard, bool transformFeedbackActive, const gl::VertexFormat inputLayout[]);
     80     virtual void applyUniforms(const gl::ProgramBinary &programBinary);
     81     virtual bool applyPrimitiveType(GLenum primitiveType, GLsizei elementCount);
     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 
     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 void 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     // Renderer capabilities
    103     IDirect3DDevice9 *getDevice() { return mDevice; }
    104     virtual DWORD getAdapterVendor() const;
    105     virtual std::string getRendererDescription() const;
    106     virtual GUID getAdapterIdentifier() const;
    107 
    108     virtual bool getBGRATextureSupport() const;
    109     virtual bool getDXT1TextureSupport() const;
    110     virtual bool getDXT3TextureSupport() const;
    111     virtual bool getDXT5TextureSupport() const;
    112     virtual bool getEventQuerySupport() const;
    113     virtual bool getFloat32TextureSupport() const;
    114     virtual bool getFloat32TextureFilteringSupport() const;
    115     virtual bool getFloat32TextureRenderingSupport() const;
    116     virtual bool getFloat16TextureSupport() const;
    117     virtual bool getFloat16TextureFilteringSupport() const;
    118     virtual bool getFloat16TextureRenderingSupport() const;
    119     virtual bool getRGB565TextureSupport() const;
    120     virtual bool getLuminanceTextureSupport() const;
    121     virtual bool getLuminanceAlphaTextureSupport() const;
    122     virtual bool getRGTextureSupport() const;
    123     virtual unsigned int getMaxVertexTextureImageUnits() const;
    124     virtual unsigned int getMaxCombinedTextureImageUnits() const;
    125     virtual unsigned int getReservedVertexUniformVectors() const;
    126     virtual unsigned int getReservedFragmentUniformVectors() const;
    127     virtual unsigned int getMaxVertexUniformVectors() const;
    128     virtual unsigned int getMaxFragmentUniformVectors() const;
    129     virtual unsigned int getMaxVaryingVectors() const;
    130     virtual unsigned int getMaxVertexShaderUniformBuffers() const;
    131     virtual unsigned int getMaxFragmentShaderUniformBuffers() const;
    132     virtual unsigned int getReservedVertexUniformBuffers() const;
    133     virtual unsigned int getReservedFragmentUniformBuffers() const;
    134     virtual unsigned int getMaxTransformFeedbackBuffers() const;
    135     virtual unsigned int getMaxTransformFeedbackSeparateComponents() const;
    136     virtual unsigned int getMaxTransformFeedbackInterleavedComponents() const;
    137     virtual unsigned int getMaxUniformBufferSize() const;
    138     virtual bool getNonPower2TextureSupport() const;
    139     virtual bool getDepthTextureSupport() const;
    140     virtual bool getOcclusionQuerySupport() const;
    141     virtual bool getInstancingSupport() const;
    142     virtual bool getTextureFilterAnisotropySupport() const;
    143     virtual bool getPBOSupport() const;
    144     virtual float getTextureMaxAnisotropy() const;
    145     virtual bool getShareHandleSupport() const;
    146     virtual bool getDerivativeInstructionSupport() const;
    147     virtual bool getPostSubBufferSupport() const;
    148     virtual int getMaxRecommendedElementsIndices() const;
    149     virtual int getMaxRecommendedElementsVertices() const;
    150 
    151     virtual int getMajorShaderModel() const;
    152     virtual float getMaxPointSize() const;
    153     virtual int getMaxViewportDimension() const;
    154     virtual int getMaxTextureWidth() const;
    155     virtual int getMaxTextureHeight() const;
    156     virtual int getMaxTextureDepth() const;
    157     virtual int getMaxTextureArrayLayers() const;
    158     virtual bool get32BitIndexSupport() const;
    159     DWORD getCapsDeclTypes() const;
    160     virtual int getMinSwapInterval() const;
    161     virtual int getMaxSwapInterval() const;
    162 
    163     virtual GLsizei getMaxSupportedSamples() const;
    164     virtual GLsizei getMaxSupportedFormatSamples(GLenum internalFormat) const;
    165     virtual GLsizei getNumSampleCounts(GLenum internalFormat) const;
    166     virtual void getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const;
    167     int getNearestSupportedSamples(D3DFORMAT format, int requested) const;
    168 
    169     virtual unsigned int getMaxRenderTargets() const;
    170 
    171     D3DFORMAT ConvertTextureInternalFormat(GLenum internalformat);
    172 
    173     // Pixel operations
    174     virtual bool copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source);
    175     virtual bool copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source);
    176     virtual bool copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source);
    177     virtual bool copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source);
    178 
    179     virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
    180                            GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level);
    181     virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
    182                            GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level);
    183     virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
    184                            GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level);
    185     virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
    186                            GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level);
    187 
    188     virtual bool blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
    189                           const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter);
    190     virtual void readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
    191                             GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels);
    192 
    193     // RenderTarget creation
    194     virtual RenderTarget *createRenderTarget(SwapChain *swapChain, bool depth);
    195     virtual RenderTarget *createRenderTarget(int width, int height, GLenum format, GLsizei samples);
    196 
    197     // Shader operations
    198     virtual ShaderExecutable *loadExecutable(const void *function, size_t length, rx::ShaderType type,
    199                                              const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
    200                                              bool separatedOutputBuffers);
    201     virtual ShaderExecutable *compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
    202                                                   const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
    203                                                   bool separatedOutputBuffers, D3DWorkaroundType workaround);
    204     virtual UniformStorage *createUniformStorage(size_t storageSize);
    205 
    206     // Image operations
    207     virtual Image *createImage();
    208     virtual void generateMipmap(Image *dest, Image *source);
    209     virtual TextureStorage *createTextureStorage2D(SwapChain *swapChain);
    210     virtual TextureStorage *createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels);
    211     virtual TextureStorage *createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels);
    212     virtual TextureStorage *createTextureStorage3D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels);
    213     virtual TextureStorage *createTextureStorage2DArray(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels);
    214 
    215     // Buffer creation
    216     virtual VertexBuffer *createVertexBuffer();
    217     virtual IndexBuffer *createIndexBuffer();
    218     virtual BufferStorage *createBufferStorage();
    219 
    220     // Query and Fence creation
    221     virtual QueryImpl *createQuery(GLenum type);
    222     virtual FenceImpl *createFence();
    223 
    224     // Buffer-to-texture and Texture-to-buffer copies
    225     virtual bool supportsFastCopyBufferToTexture(GLenum internalFormat) const;
    226     virtual bool fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
    227                                          GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea);
    228 
    229     // D3D9-renderer specific methods
    230     bool boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest);
    231 
    232     D3DPOOL getTexturePool(DWORD usage) const;
    233 
    234     virtual bool getLUID(LUID *adapterLuid) const;
    235     virtual GLenum getNativeTextureFormat(GLenum internalFormat) const;
    236     virtual rx::VertexConversionType getVertexConversionType(const gl::VertexFormat &vertexFormat) const;
    237     virtual GLenum getVertexComponentType(const gl::VertexFormat &vertexFormat) const;
    238 
    239   private:
    240     DISALLOW_COPY_AND_ASSIGN(Renderer9);
    241 
    242     void deinitialize();
    243 
    244     void applyUniformnfv(gl::LinkedUniform *targetUniform, const GLfloat *v);
    245     void applyUniformniv(gl::LinkedUniform *targetUniform, const GLint *v);
    246     void applyUniformnbv(gl::LinkedUniform *targetUniform, const GLint *v);
    247 
    248     void drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer);
    249     void drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer);
    250 
    251     bool copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged);
    252     gl::FramebufferAttachment *getNullColorbuffer(gl::FramebufferAttachment *depthbuffer);
    253 
    254     D3DPOOL getBufferPool(DWORD usage) const;
    255 
    256     HMODULE mD3d9Module;
    257     HDC mDc;
    258 
    259     void initializeDevice();
    260     D3DPRESENT_PARAMETERS getDefaultPresentParameters();
    261     void releaseDeviceResources();
    262 
    263     HRESULT getDeviceStatusCode();
    264     bool isRemovedDeviceResettable() const;
    265     bool resetRemovedDevice();
    266 
    267     UINT mAdapter;
    268     D3DDEVTYPE mDeviceType;
    269     IDirect3D9 *mD3d9;  // Always valid after successful initialization.
    270     IDirect3D9Ex *mD3d9Ex;  // Might be null if D3D9Ex is not supported.
    271     IDirect3DDevice9 *mDevice;
    272     IDirect3DDevice9Ex *mDeviceEx;  // Might be null if D3D9Ex is not supported.
    273 
    274     HLSLCompiler mCompiler;
    275 
    276     Blit9 *mBlit;
    277 
    278     HWND mDeviceWindow;
    279 
    280     bool mDeviceLost;
    281     D3DCAPS9 mDeviceCaps;
    282     D3DADAPTER_IDENTIFIER9 mAdapterIdentifier;
    283 
    284     D3DPRIMITIVETYPE mPrimitiveType;
    285     int mPrimitiveCount;
    286     GLsizei mRepeatDraw;
    287 
    288     bool mSceneStarted;
    289     bool mSupportsNonPower2Textures;
    290     bool mSupportsTextureFilterAnisotropy;
    291     int mMinSwapInterval;
    292     int mMaxSwapInterval;
    293 
    294     bool mOcclusionQuerySupport;
    295     bool mEventQuerySupport;
    296     bool mVertexTextureSupport;
    297 
    298     bool mDepthTextureSupport;
    299 
    300     bool mRGB565TextureSupport;
    301 
    302     bool mFloat32TextureSupport;
    303     bool mFloat32FilterSupport;
    304     bool mFloat32RenderSupport;
    305 
    306     bool mFloat16TextureSupport;
    307     bool mFloat16FilterSupport;
    308     bool mFloat16RenderSupport;
    309 
    310     bool mDXT1TextureSupport;
    311     bool mDXT3TextureSupport;
    312     bool mDXT5TextureSupport;
    313 
    314     bool mLuminanceTextureSupport;
    315     bool mLuminanceAlphaTextureSupport;
    316 
    317     bool mRGTextureSupport;
    318 
    319     struct MultisampleSupportInfo
    320     {
    321         bool supportedSamples[D3DMULTISAMPLE_16_SAMPLES + 1];
    322         unsigned int maxSupportedSamples;
    323     };
    324     typedef std::map<D3DFORMAT, MultisampleSupportInfo> MultisampleSupportMap;
    325     MultisampleSupportMap mMultiSampleSupport;
    326     unsigned int mMaxSupportedSamples;
    327 
    328     MultisampleSupportInfo getMultiSampleSupport(D3DFORMAT format);
    329 
    330     // current render target states
    331     unsigned int mAppliedRenderTargetSerial;
    332     unsigned int mAppliedDepthbufferSerial;
    333     unsigned int mAppliedStencilbufferSerial;
    334     bool mDepthStencilInitialized;
    335     bool mRenderTargetDescInitialized;
    336     rx::RenderTarget::Desc mRenderTargetDesc;
    337     unsigned int mCurStencilSize;
    338     unsigned int mCurDepthSize;
    339 
    340     IDirect3DStateBlock9 *mMaskedClearSavedState;
    341 
    342     // previously set render states
    343     bool mForceSetDepthStencilState;
    344     gl::DepthStencilState mCurDepthStencilState;
    345     int mCurStencilRef;
    346     int mCurStencilBackRef;
    347     bool mCurFrontFaceCCW;
    348 
    349     bool mForceSetRasterState;
    350     gl::RasterizerState mCurRasterState;
    351 
    352     bool mForceSetScissor;
    353     gl::Rectangle mCurScissor;
    354     bool mScissorEnabled;
    355 
    356     bool mForceSetViewport;
    357     gl::Rectangle mCurViewport;
    358     float mCurNear;
    359     float mCurFar;
    360     float mCurDepthFront;
    361 
    362     bool mForceSetBlendState;
    363     gl::BlendState mCurBlendState;
    364     gl::ColorF mCurBlendColor;
    365     GLuint mCurSampleMask;
    366 
    367     // Currently applied sampler states
    368     bool mForceSetVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
    369     gl::SamplerState mCurVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
    370 
    371     bool mForceSetPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
    372     gl::SamplerState mCurPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
    373 
    374     // Currently applied textures
    375     unsigned int mCurVertexTextureSerials[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
    376     unsigned int mCurPixelTextureSerials[gl::MAX_TEXTURE_IMAGE_UNITS];
    377 
    378     unsigned int mAppliedIBSerial;
    379     IDirect3DVertexShader9 *mAppliedVertexShader;
    380     IDirect3DPixelShader9 *mAppliedPixelShader;
    381     unsigned int mAppliedProgramSerial;
    382 
    383     rx::dx_VertexConstants mVertexConstants;
    384     rx::dx_PixelConstants mPixelConstants;
    385     bool mDxUniformsDirty;
    386 
    387     // A pool of event queries that are currently unused.
    388     std::vector<IDirect3DQuery9*> mEventQueryPool;
    389     VertexShaderCache mVertexShaderCache;
    390     PixelShaderCache mPixelShaderCache;
    391 
    392     VertexDataManager *mVertexDataManager;
    393     VertexDeclarationCache mVertexDeclarationCache;
    394 
    395     IndexDataManager *mIndexDataManager;
    396     StreamingIndexBufferInterface *mLineLoopIB;
    397 
    398     enum { NUM_NULL_COLORBUFFER_CACHE_ENTRIES = 12 };
    399     struct NullColorbufferCacheEntry
    400     {
    401         UINT lruCount;
    402         int width;
    403         int height;
    404         gl::FramebufferAttachment *buffer;
    405     } mNullColorbufferCache[NUM_NULL_COLORBUFFER_CACHE_ENTRIES];
    406     UINT mMaxNullColorbufferLRU;
    407 
    408 };
    409 
    410 }
    411 #endif // LIBGLESV2_RENDERER_RENDERER9_H_
    412