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 // 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 "libGLESv2/mathutil.h" 14 #include "libGLESv2/renderer/ShaderCache.h" 15 #include "libGLESv2/renderer/VertexDeclarationCache.h" 16 #include "libGLESv2/renderer/Renderer.h" 17 #include "libGLESv2/renderer/RenderTarget.h" 18 19 namespace gl 20 { 21 class Renderbuffer; 22 } 23 24 namespace rx 25 { 26 class VertexDataManager; 27 class IndexDataManager; 28 class StreamingIndexBufferInterface; 29 struct TranslatedAttribute; 30 31 class Renderer9 : public Renderer 32 { 33 public: 34 Renderer9(egl::Display *display, HDC hDc, bool softwareDevice); 35 virtual ~Renderer9(); 36 37 static Renderer9 *makeRenderer9(Renderer *renderer); 38 39 virtual EGLint initialize(); 40 virtual bool resetDevice(); 41 42 virtual int generateConfigs(ConfigDesc **configDescList); 43 virtual void deleteConfigs(ConfigDesc *configDescList); 44 45 void startScene(); 46 void endScene(); 47 48 virtual void sync(bool block); 49 50 virtual SwapChain *createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat); 51 52 IDirect3DQuery9* allocateEventQuery(); 53 void freeEventQuery(IDirect3DQuery9* query); 54 55 // resource creation 56 IDirect3DVertexShader9 *createVertexShader(const DWORD *function, size_t length); 57 IDirect3DPixelShader9 *createPixelShader(const DWORD *function, size_t length); 58 HRESULT createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer); 59 HRESULT createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer); 60 #if 0 61 void *createTexture2D(); 62 void *createTextureCube(); 63 void *createQuery(); 64 void *createIndexBuffer(); 65 void *createVertexbuffer(); 66 67 // state setup 68 void applyShaders(); 69 void applyConstants(); 70 #endif 71 virtual void setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &sampler); 72 virtual void setTexture(gl::SamplerType type, int index, gl::Texture *texture); 73 74 virtual void setRasterizerState(const gl::RasterizerState &rasterState); 75 virtual void setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, 76 unsigned int sampleMask); 77 virtual void setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef, 78 int stencilBackRef, bool frontFaceCCW); 79 80 virtual void setScissorRectangle(const gl::Rectangle &scissor, bool enabled); 81 virtual bool setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace, 82 bool ignoreViewport); 83 84 virtual bool applyRenderTarget(gl::Framebuffer *frameBuffer); 85 virtual void applyShaders(gl::ProgramBinary *programBinary); 86 virtual void applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray); 87 virtual bool applyPrimitiveType(GLenum primitiveType, GLsizei elementCount); 88 virtual GLenum applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances); 89 virtual GLenum applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo); 90 91 virtual void drawArrays(GLenum mode, GLsizei count, GLsizei instances); 92 virtual void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances); 93 94 virtual void clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer); 95 96 virtual void markAllStateDirty(); 97 98 // lost device 99 void notifyDeviceLost(); 100 virtual bool isDeviceLost(); 101 virtual bool testDeviceLost(bool notify); 102 virtual bool testDeviceResettable(); 103 104 // Renderer capabilities 105 IDirect3DDevice9 *getDevice() { return mDevice; } 106 virtual DWORD getAdapterVendor() const; 107 virtual std::string getRendererDescription() const; 108 virtual GUID getAdapterIdentifier() const; 109 110 virtual bool getBGRATextureSupport() const; 111 virtual bool getDXT1TextureSupport(); 112 virtual bool getDXT3TextureSupport(); 113 virtual bool getDXT5TextureSupport(); 114 virtual bool getEventQuerySupport(); 115 virtual bool getFloat32TextureSupport(bool *filtering, bool *renderable); 116 virtual bool getFloat16TextureSupport(bool *filtering, bool *renderable); 117 virtual bool getLuminanceTextureSupport(); 118 virtual bool getLuminanceAlphaTextureSupport(); 119 virtual unsigned int getMaxVertexTextureImageUnits() const; 120 virtual unsigned int getMaxCombinedTextureImageUnits() const; 121 virtual unsigned int getReservedVertexUniformVectors() const; 122 virtual unsigned int getReservedFragmentUniformVectors() const; 123 virtual unsigned int getMaxVertexUniformVectors() const; 124 virtual unsigned int getMaxFragmentUniformVectors() const; 125 virtual unsigned int getMaxVaryingVectors() const; 126 virtual bool getNonPower2TextureSupport() const; 127 virtual bool getDepthTextureSupport() const; 128 virtual bool getOcclusionQuerySupport() const; 129 virtual bool getInstancingSupport() const; 130 virtual bool getTextureFilterAnisotropySupport() const; 131 virtual float getTextureMaxAnisotropy() const; 132 virtual bool getShareHandleSupport() const; 133 virtual bool getDerivativeInstructionSupport() const; 134 virtual bool getPostSubBufferSupport() const; 135 136 virtual int getMajorShaderModel() const; 137 virtual float getMaxPointSize() const; 138 virtual int getMaxViewportDimension() const; 139 virtual int getMaxTextureWidth() const; 140 virtual int getMaxTextureHeight() const; 141 virtual bool get32BitIndexSupport() const; 142 DWORD getCapsDeclTypes() const; 143 virtual int getMinSwapInterval() const; 144 virtual int getMaxSwapInterval() const; 145 146 virtual GLsizei getMaxSupportedSamples() const; 147 int getNearestSupportedSamples(D3DFORMAT format, int requested) const; 148 149 virtual unsigned int getMaxRenderTargets() const; 150 151 D3DFORMAT ConvertTextureInternalFormat(GLint internalformat); 152 153 // Pixel operations 154 virtual bool copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source); 155 virtual bool copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source); 156 157 virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, 158 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level); 159 virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, 160 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level); 161 162 virtual bool blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect, 163 bool blitRenderTarget, bool blitDepthStencil); 164 virtual void readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, 165 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels); 166 167 // RenderTarget creation 168 virtual RenderTarget *createRenderTarget(SwapChain *swapChain, bool depth); 169 virtual RenderTarget *createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth); 170 171 // Shader operations 172 virtual ShaderExecutable *loadExecutable(const void *function, size_t length, rx::ShaderType type); 173 virtual ShaderExecutable *compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround); 174 175 // Image operations 176 virtual Image *createImage(); 177 virtual void generateMipmap(Image *dest, Image *source); 178 virtual TextureStorage *createTextureStorage2D(SwapChain *swapChain); 179 virtual TextureStorage *createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height); 180 virtual TextureStorage *createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size); 181 182 // Buffer creation 183 virtual VertexBuffer *createVertexBuffer(); 184 virtual IndexBuffer *createIndexBuffer(); 185 virtual BufferStorage *createBufferStorage(); 186 187 // Query and Fence creation 188 virtual QueryImpl *createQuery(GLenum type); 189 virtual FenceImpl *createFence(); 190 191 // D3D9-renderer specific methods 192 bool boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest); 193 194 D3DPOOL getTexturePool(DWORD usage) const; 195 196 virtual bool getLUID(LUID *adapterLuid) const; 197 198 private: 199 DISALLOW_COPY_AND_ASSIGN(Renderer9); 200 201 void deinitialize(); 202 203 void applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v); 204 void applyUniformniv(gl::Uniform *targetUniform, const GLint *v); 205 void applyUniformnbv(gl::Uniform *targetUniform, const GLint *v); 206 207 void drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer); 208 void drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer); 209 210 void getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray); 211 bool copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged); 212 gl::Renderbuffer *getNullColorbuffer(gl::Renderbuffer *depthbuffer); 213 214 D3DPOOL getBufferPool(DWORD usage) const; 215 216 HMODULE mD3d9Module; 217 HDC mDc; 218 219 void initializeDevice(); 220 D3DPRESENT_PARAMETERS getDefaultPresentParameters(); 221 void releaseDeviceResources(); 222 223 HRESULT getDeviceStatusCode(); 224 bool isRemovedDeviceResettable() const; 225 bool resetRemovedDevice(); 226 227 UINT mAdapter; 228 D3DDEVTYPE mDeviceType; 229 bool mSoftwareDevice; // FIXME: Deprecate 230 IDirect3D9 *mD3d9; // Always valid after successful initialization. 231 IDirect3D9Ex *mD3d9Ex; // Might be null if D3D9Ex is not supported. 232 IDirect3DDevice9 *mDevice; 233 IDirect3DDevice9Ex *mDeviceEx; // Might be null if D3D9Ex is not supported. 234 235 Blit *mBlit; 236 237 HWND mDeviceWindow; 238 239 bool mDeviceLost; 240 D3DCAPS9 mDeviceCaps; 241 D3DADAPTER_IDENTIFIER9 mAdapterIdentifier; 242 243 D3DPRIMITIVETYPE mPrimitiveType; 244 int mPrimitiveCount; 245 GLsizei mRepeatDraw; 246 247 bool mSceneStarted; 248 bool mSupportsNonPower2Textures; 249 bool mSupportsTextureFilterAnisotropy; 250 int mMinSwapInterval; 251 int mMaxSwapInterval; 252 253 bool mOcclusionQuerySupport; 254 bool mEventQuerySupport; 255 bool mVertexTextureSupport; 256 257 bool mDepthTextureSupport; 258 259 bool mFloat32TextureSupport; 260 bool mFloat32FilterSupport; 261 bool mFloat32RenderSupport; 262 263 bool mFloat16TextureSupport; 264 bool mFloat16FilterSupport; 265 bool mFloat16RenderSupport; 266 267 bool mDXT1TextureSupport; 268 bool mDXT3TextureSupport; 269 bool mDXT5TextureSupport; 270 271 bool mLuminanceTextureSupport; 272 bool mLuminanceAlphaTextureSupport; 273 274 std::map<D3DFORMAT, bool *> mMultiSampleSupport; 275 GLsizei mMaxSupportedSamples; 276 277 // current render target states 278 unsigned int mAppliedRenderTargetSerial; 279 unsigned int mAppliedDepthbufferSerial; 280 unsigned int mAppliedStencilbufferSerial; 281 bool mDepthStencilInitialized; 282 bool mRenderTargetDescInitialized; 283 rx::RenderTarget::Desc mRenderTargetDesc; 284 unsigned int mCurStencilSize; 285 unsigned int mCurDepthSize; 286 287 IDirect3DStateBlock9 *mMaskedClearSavedState; 288 289 // previously set render states 290 bool mForceSetDepthStencilState; 291 gl::DepthStencilState mCurDepthStencilState; 292 int mCurStencilRef; 293 int mCurStencilBackRef; 294 bool mCurFrontFaceCCW; 295 296 bool mForceSetRasterState; 297 gl::RasterizerState mCurRasterState; 298 299 bool mForceSetScissor; 300 gl::Rectangle mCurScissor; 301 bool mScissorEnabled; 302 303 bool mForceSetViewport; 304 gl::Rectangle mCurViewport; 305 float mCurNear; 306 float mCurFar; 307 float mCurDepthFront; 308 309 bool mForceSetBlendState; 310 gl::BlendState mCurBlendState; 311 gl::Color mCurBlendColor; 312 GLuint mCurSampleMask; 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 unsigned int mCurVertexTextureSerials[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS]; 323 unsigned int mCurPixelTextureSerials[gl::MAX_TEXTURE_IMAGE_UNITS]; 324 325 unsigned int mAppliedIBSerial; 326 unsigned int mAppliedProgramBinarySerial; 327 328 rx::dx_VertexConstants mVertexConstants; 329 rx::dx_PixelConstants mPixelConstants; 330 bool mDxUniformsDirty; 331 332 // A pool of event queries that are currently unused. 333 std::vector<IDirect3DQuery9*> mEventQueryPool; 334 VertexShaderCache mVertexShaderCache; 335 PixelShaderCache mPixelShaderCache; 336 337 VertexDataManager *mVertexDataManager; 338 VertexDeclarationCache mVertexDeclarationCache; 339 340 IndexDataManager *mIndexDataManager; 341 StreamingIndexBufferInterface *mLineLoopIB; 342 343 enum { NUM_NULL_COLORBUFFER_CACHE_ENTRIES = 12 }; 344 struct NullColorbufferCacheEntry 345 { 346 UINT lruCount; 347 int width; 348 int height; 349 gl::Renderbuffer *buffer; 350 } mNullColorbufferCache[NUM_NULL_COLORBUFFER_CACHE_ENTRIES]; 351 UINT mMaxNullColorbufferLRU; 352 353 }; 354 355 } 356 #endif // LIBGLESV2_RENDERER_RENDERER9_H_ 357