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/d3d/d3d9/ShaderCache.h" 16 #include "libGLESv2/renderer/d3d/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 class StaticIndexBufferInterface; 31 struct TranslatedAttribute; 32 class Blit9; 33 34 class Renderer9 : public Renderer 35 { 36 public: 37 Renderer9(egl::Display *display, EGLNativeDisplayType hDc, EGLint requestedDisplay); 38 virtual ~Renderer9(); 39 40 static Renderer9 *makeRenderer9(Renderer *renderer); 41 42 virtual EGLint initialize(); 43 virtual bool resetDevice(); 44 45 virtual int generateConfigs(ConfigDesc **configDescList); 46 virtual void deleteConfigs(ConfigDesc *configDescList); 47 48 void startScene(); 49 void endScene(); 50 51 virtual void sync(bool block); 52 53 virtual SwapChain *createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat); 54 55 IDirect3DQuery9* allocateEventQuery(); 56 void freeEventQuery(IDirect3DQuery9* query); 57 58 // resource creation 59 IDirect3DVertexShader9 *createVertexShader(const DWORD *function, size_t length); 60 IDirect3DPixelShader9 *createPixelShader(const DWORD *function, size_t length); 61 HRESULT createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer); 62 HRESULT createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer); 63 virtual void generateSwizzle(gl::Texture *texture); 64 virtual void setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &sampler); 65 virtual void setTexture(gl::SamplerType type, int index, gl::Texture *texture); 66 67 virtual bool setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[]); 68 69 virtual void setRasterizerState(const gl::RasterizerState &rasterState); 70 virtual void setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor, 71 unsigned int sampleMask); 72 virtual void setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef, 73 int stencilBackRef, bool frontFaceCCW); 74 75 virtual void setScissorRectangle(const gl::Rectangle &scissor, bool enabled); 76 virtual void setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace, 77 bool ignoreViewport); 78 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 bool applyPrimitiveType(GLenum primitiveType, GLsizei elementCount); 84 virtual gl::Error applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], const gl::VertexAttribCurrentValueData currentValues[], 85 GLint first, GLsizei count, GLsizei instances); 86 virtual gl::Error applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo); 87 88 virtual void applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[]); 89 90 virtual void drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive); 91 virtual void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, 92 gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances); 93 94 virtual gl::Error 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 IDirect3DDevice9 *getDevice() { return mDevice; } 105 virtual DWORD getAdapterVendor() const; 106 virtual std::string getRendererDescription() const; 107 virtual GUID getAdapterIdentifier() const; 108 109 virtual unsigned int getReservedVertexUniformVectors() const; 110 virtual unsigned int getReservedFragmentUniformVectors() const; 111 virtual unsigned int getReservedVertexUniformBuffers() const; 112 virtual unsigned int getReservedFragmentUniformBuffers() const; 113 virtual bool getShareHandleSupport() const; 114 virtual bool getPostSubBufferSupport() const; 115 116 virtual int getMajorShaderModel() const; 117 DWORD getCapsDeclTypes() const; 118 virtual int getMinSwapInterval() const; 119 virtual int getMaxSwapInterval() const; 120 121 // Pixel operations 122 virtual bool copyToRenderTarget2D(TextureStorage *dest, TextureStorage *source); 123 virtual bool copyToRenderTargetCube(TextureStorage *dest, TextureStorage *source); 124 virtual bool copyToRenderTarget3D(TextureStorage *dest, TextureStorage *source); 125 virtual bool copyToRenderTarget2DArray(TextureStorage *dest, TextureStorage *source); 126 127 virtual bool copyImage2D(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, 128 GLint xoffset, GLint yoffset, TextureStorage *storage, GLint level); 129 virtual bool copyImageCube(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, 130 GLint xoffset, GLint yoffset, TextureStorage *storage, GLenum target, GLint level); 131 virtual bool copyImage3D(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, 132 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorage *storage, GLint level); 133 virtual bool copyImage2DArray(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, 134 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorage *storage, GLint level); 135 136 virtual bool blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect, 137 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter); 138 139 virtual gl::Error readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, 140 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, uint8_t *pixels); 141 142 // RenderTarget creation 143 virtual RenderTarget *createRenderTarget(SwapChain *swapChain, bool depth); 144 virtual RenderTarget *createRenderTarget(int width, int height, GLenum format, GLsizei samples); 145 146 // Shader creation 147 virtual ShaderImpl *createShader(GLenum type); 148 virtual ProgramImpl *createProgram(); 149 150 // Shader operations 151 virtual void releaseShaderCompiler(); 152 virtual ShaderExecutable *loadExecutable(const void *function, size_t length, rx::ShaderType type, 153 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings, 154 bool separatedOutputBuffers); 155 virtual ShaderExecutable *compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, 156 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings, 157 bool separatedOutputBuffers, D3DWorkaroundType workaround); 158 virtual UniformStorage *createUniformStorage(size_t storageSize); 159 160 // Image operations 161 virtual Image *createImage(); 162 virtual void generateMipmap(Image *dest, Image *source); 163 virtual TextureStorage *createTextureStorage2D(SwapChain *swapChain); 164 virtual TextureStorage *createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels); 165 virtual TextureStorage *createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels); 166 virtual TextureStorage *createTextureStorage3D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels); 167 virtual TextureStorage *createTextureStorage2DArray(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels); 168 169 // Texture creation 170 virtual TextureImpl *createTexture(GLenum target); 171 172 // Buffer creation 173 virtual BufferImpl *createBuffer(); 174 virtual VertexBuffer *createVertexBuffer(); 175 virtual IndexBuffer *createIndexBuffer(); 176 177 // Vertex Array creation 178 virtual VertexArrayImpl *createVertexArray(); 179 180 // Query and Fence creation 181 virtual QueryImpl *createQuery(GLenum type); 182 virtual FenceImpl *createFence(); 183 184 // Transform Feedback creation 185 virtual TransformFeedbackImpl* createTransformFeedback(); 186 187 // Buffer-to-texture and Texture-to-buffer copies 188 virtual bool supportsFastCopyBufferToTexture(GLenum internalFormat) const; 189 virtual bool fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget, 190 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea); 191 192 // D3D9-renderer specific methods 193 bool boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest); 194 195 D3DPOOL getTexturePool(DWORD usage) const; 196 197 virtual bool getLUID(LUID *adapterLuid) const; 198 virtual rx::VertexConversionType getVertexConversionType(const gl::VertexFormat &vertexFormat) const; 199 virtual GLenum getVertexComponentType(const gl::VertexFormat &vertexFormat) const; 200 201 private: 202 DISALLOW_COPY_AND_ASSIGN(Renderer9); 203 204 virtual void generateCaps(gl::Caps *outCaps, gl::TextureCapsMap *outTextureCaps, gl::Extensions *outExtensions) const; 205 206 void release(); 207 208 void applyUniformnfv(gl::LinkedUniform *targetUniform, const GLfloat *v); 209 void applyUniformniv(gl::LinkedUniform *targetUniform, const GLint *v); 210 void applyUniformnbv(gl::LinkedUniform *targetUniform, const GLint *v); 211 212 void drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer); 213 void drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer); 214 215 StaticIndexBufferInterface *getCountingIB(size_t count); 216 217 bool copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged); 218 gl::FramebufferAttachment *getNullColorbuffer(gl::FramebufferAttachment *depthbuffer); 219 220 D3DPOOL getBufferPool(DWORD usage) const; 221 222 HMODULE mD3d9Module; 223 HDC mDc; 224 225 void initializeDevice(); 226 D3DPRESENT_PARAMETERS getDefaultPresentParameters(); 227 void releaseDeviceResources(); 228 229 HRESULT getDeviceStatusCode(); 230 bool isRemovedDeviceResettable() const; 231 bool resetRemovedDevice(); 232 233 UINT mAdapter; 234 D3DDEVTYPE mDeviceType; 235 IDirect3D9 *mD3d9; // Always valid after successful initialization. 236 IDirect3D9Ex *mD3d9Ex; // Might be null if D3D9Ex is not supported. 237 IDirect3DDevice9 *mDevice; 238 IDirect3DDevice9Ex *mDeviceEx; // Might be null if D3D9Ex is not supported. 239 240 HLSLCompiler mCompiler; 241 242 Blit9 *mBlit; 243 244 HWND mDeviceWindow; 245 246 bool mDeviceLost; 247 D3DCAPS9 mDeviceCaps; 248 D3DADAPTER_IDENTIFIER9 mAdapterIdentifier; 249 250 D3DPRIMITIVETYPE mPrimitiveType; 251 int mPrimitiveCount; 252 GLsizei mRepeatDraw; 253 254 bool mSceneStarted; 255 int mMinSwapInterval; 256 int mMaxSwapInterval; 257 258 bool mVertexTextureSupport; 259 260 // current render target states 261 unsigned int mAppliedRenderTargetSerial; 262 unsigned int mAppliedDepthbufferSerial; 263 unsigned int mAppliedStencilbufferSerial; 264 bool mDepthStencilInitialized; 265 bool mRenderTargetDescInitialized; 266 rx::RenderTarget::Desc mRenderTargetDesc; 267 unsigned int mCurStencilSize; 268 unsigned int mCurDepthSize; 269 270 IDirect3DStateBlock9 *mMaskedClearSavedState; 271 272 // previously set render states 273 bool mForceSetDepthStencilState; 274 gl::DepthStencilState mCurDepthStencilState; 275 int mCurStencilRef; 276 int mCurStencilBackRef; 277 bool mCurFrontFaceCCW; 278 279 bool mForceSetRasterState; 280 gl::RasterizerState mCurRasterState; 281 282 bool mForceSetScissor; 283 gl::Rectangle mCurScissor; 284 bool mScissorEnabled; 285 286 bool mForceSetViewport; 287 gl::Rectangle mCurViewport; 288 float mCurNear; 289 float mCurFar; 290 float mCurDepthFront; 291 292 bool mForceSetBlendState; 293 gl::BlendState mCurBlendState; 294 gl::ColorF mCurBlendColor; 295 GLuint mCurSampleMask; 296 297 // Currently applied sampler states 298 std::vector<bool> mForceSetVertexSamplerStates; 299 std::vector<gl::SamplerState> mCurVertexSamplerStates; 300 301 std::vector<bool> mForceSetPixelSamplerStates; 302 std::vector<gl::SamplerState> mCurPixelSamplerStates; 303 304 // Currently applied textures 305 std::vector<unsigned int> mCurVertexTextureSerials; 306 std::vector<unsigned int> mCurPixelTextureSerials; 307 308 unsigned int mAppliedIBSerial; 309 IDirect3DVertexShader9 *mAppliedVertexShader; 310 IDirect3DPixelShader9 *mAppliedPixelShader; 311 unsigned int mAppliedProgramSerial; 312 313 rx::dx_VertexConstants mVertexConstants; 314 rx::dx_PixelConstants mPixelConstants; 315 bool mDxUniformsDirty; 316 317 // A pool of event queries that are currently unused. 318 std::vector<IDirect3DQuery9*> mEventQueryPool; 319 VertexShaderCache mVertexShaderCache; 320 PixelShaderCache mPixelShaderCache; 321 322 VertexDataManager *mVertexDataManager; 323 VertexDeclarationCache mVertexDeclarationCache; 324 325 IndexDataManager *mIndexDataManager; 326 StreamingIndexBufferInterface *mLineLoopIB; 327 StaticIndexBufferInterface *mCountingIB; 328 329 enum { NUM_NULL_COLORBUFFER_CACHE_ENTRIES = 12 }; 330 struct NullColorbufferCacheEntry 331 { 332 UINT lruCount; 333 int width; 334 int height; 335 gl::FramebufferAttachment *buffer; 336 } mNullColorbufferCache[NUM_NULL_COLORBUFFER_CACHE_ENTRIES]; 337 UINT mMaxNullColorbufferLRU; 338 339 }; 340 341 } 342 #endif // LIBGLESV2_RENDERER_RENDERER9_H_ 343