1 // 2 // Copyright 2012 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 LIBANGLE_RENDERER_D3D_D3D9_RENDERER9_H_ 10 #define LIBANGLE_RENDERER_D3D_D3D9_RENDERER9_H_ 11 12 #include "common/angleutils.h" 13 #include "common/mathutil.h" 14 #include "libANGLE/renderer/d3d/HLSLCompiler.h" 15 #include "libANGLE/renderer/d3d/RenderTargetD3D.h" 16 #include "libANGLE/renderer/d3d/RendererD3D.h" 17 #include "libANGLE/renderer/d3d/d3d9/DebugAnnotator9.h" 18 #include "libANGLE/renderer/d3d/d3d9/ShaderCache.h" 19 #include "libANGLE/renderer/d3d/d3d9/StateManager9.h" 20 #include "libANGLE/renderer/d3d/d3d9/VertexDeclarationCache.h" 21 #include "libANGLE/renderer/driver_utils.h" 22 23 namespace gl 24 { 25 class FramebufferAttachment; 26 } 27 28 namespace egl 29 { 30 class AttributeMap; 31 } 32 33 namespace rx 34 { 35 class Blit9; 36 class Context9; 37 class IndexDataManager; 38 class ProgramD3D; 39 class RenderTarget9; 40 class StreamingIndexBufferInterface; 41 class StaticIndexBufferInterface; 42 class VertexDataManager; 43 struct ClearParameters; 44 struct D3DUniform; 45 struct TranslatedAttribute; 46 47 class Renderer9 : public RendererD3D 48 { 49 public: 50 explicit Renderer9(egl::Display *display); 51 ~Renderer9() override; 52 53 egl::Error initialize() override; 54 bool resetDevice() override; 55 56 egl::ConfigSet generateConfigs() override; 57 void generateDisplayExtensions(egl::DisplayExtensions *outExtensions) const override; 58 59 void startScene(); 60 void endScene(); 61 62 angle::Result flush(const gl::Context *context); 63 angle::Result finish(const gl::Context *context); 64 65 bool isValidNativeWindow(EGLNativeWindowType window) const override; 66 NativeWindowD3D *createNativeWindow(EGLNativeWindowType window, 67 const egl::Config *config, 68 const egl::AttributeMap &attribs) const override; 69 70 SwapChainD3D *createSwapChain(NativeWindowD3D *nativeWindow, 71 HANDLE shareHandle, 72 IUnknown *d3dTexture, 73 GLenum backBufferFormat, 74 GLenum depthBufferFormat, 75 EGLint orientation, 76 EGLint samples) override; 77 egl::Error getD3DTextureInfo(const egl::Config *configuration, 78 IUnknown *d3dTexture, 79 const egl::AttributeMap &attribs, 80 EGLint *width, 81 EGLint *height, 82 GLsizei *samples, 83 gl::Format *glFormat, 84 const angle::Format **angleFormat, 85 UINT *arraySlice) const override; 86 egl::Error validateShareHandle(const egl::Config *config, 87 HANDLE shareHandle, 88 const egl::AttributeMap &attribs) const override; 89 90 ContextImpl *createContext(const gl::State &state, gl::ErrorSet *errorSet) override; 91 92 angle::Result allocateEventQuery(const gl::Context *context, IDirect3DQuery9 **outQuery); 93 void freeEventQuery(IDirect3DQuery9 *query); 94 95 // resource creation 96 angle::Result createVertexShader(d3d::Context *context, 97 const DWORD *function, 98 size_t length, 99 IDirect3DVertexShader9 **outShader); 100 angle::Result createPixelShader(d3d::Context *context, 101 const DWORD *function, 102 size_t length, 103 IDirect3DPixelShader9 **outShader); 104 HRESULT createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer); 105 HRESULT createIndexBuffer(UINT Length, 106 DWORD Usage, 107 D3DFORMAT Format, 108 IDirect3DIndexBuffer9 **ppIndexBuffer); 109 angle::Result setSamplerState(const gl::Context *context, 110 gl::ShaderType type, 111 int index, 112 gl::Texture *texture, 113 const gl::SamplerState &sampler); 114 angle::Result setTexture(const gl::Context *context, 115 gl::ShaderType type, 116 int index, 117 gl::Texture *texture); 118 119 angle::Result updateState(const gl::Context *context, gl::PrimitiveMode drawMode); 120 121 void setScissorRectangle(const gl::Rectangle &scissor, bool enabled); 122 void setViewport(const gl::Rectangle &viewport, 123 float zNear, 124 float zFar, 125 gl::PrimitiveMode drawMode, 126 GLenum frontFace, 127 bool ignoreViewport); 128 129 angle::Result applyRenderTarget(const gl::Context *context, 130 const RenderTarget9 *colorRenderTarget, 131 const RenderTarget9 *depthStencilRenderTarget); 132 void applyUniforms(ProgramD3D *programD3D); 133 bool applyPrimitiveType(gl::PrimitiveMode primitiveType, 134 GLsizei elementCount, 135 bool usesPointSize); 136 angle::Result applyVertexBuffer(const gl::Context *context, 137 gl::PrimitiveMode mode, 138 GLint first, 139 GLsizei count, 140 GLsizei instances, 141 TranslatedIndexData *indexInfo); 142 angle::Result applyIndexBuffer(const gl::Context *context, 143 const void *indices, 144 GLsizei count, 145 gl::PrimitiveMode mode, 146 gl::DrawElementsType type, 147 TranslatedIndexData *indexInfo); 148 149 void clear(const ClearParameters &clearParams, 150 const RenderTarget9 *colorRenderTarget, 151 const RenderTarget9 *depthStencilRenderTarget); 152 153 void markAllStateDirty(); 154 155 // lost device 156 bool testDeviceLost() override; 157 bool testDeviceResettable() override; 158 159 VendorID getVendorId() const; 160 DeviceIdentifier getAdapterIdentifier() const override; 161 getDevice()162 IDirect3DDevice9 *getDevice() { return mDevice; } 163 void *getD3DDevice() override; 164 165 unsigned int getReservedVertexUniformVectors() const; 166 unsigned int getReservedFragmentUniformVectors() const; 167 168 bool getShareHandleSupport() const; 169 170 int getMajorShaderModel() const override; 171 int getMinorShaderModel() const override; 172 std::string getShaderModelSuffix() const override; 173 174 DWORD getCapsDeclTypes() const; 175 176 // Pixel operations 177 angle::Result copyImage2D(const gl::Context *context, 178 const gl::Framebuffer *framebuffer, 179 const gl::Rectangle &sourceRect, 180 GLenum destFormat, 181 const gl::Offset &destOffset, 182 TextureStorage *storage, 183 GLint level) override; 184 angle::Result copyImageCube(const gl::Context *context, 185 const gl::Framebuffer *framebuffer, 186 const gl::Rectangle &sourceRect, 187 GLenum destFormat, 188 const gl::Offset &destOffset, 189 TextureStorage *storage, 190 gl::TextureTarget target, 191 GLint level) override; 192 angle::Result copyImage3D(const gl::Context *context, 193 const gl::Framebuffer *framebuffer, 194 const gl::Rectangle &sourceRect, 195 GLenum destFormat, 196 const gl::Offset &destOffset, 197 TextureStorage *storage, 198 GLint level) override; 199 angle::Result copyImage2DArray(const gl::Context *context, 200 const gl::Framebuffer *framebuffer, 201 const gl::Rectangle &sourceRect, 202 GLenum destFormat, 203 const gl::Offset &destOffset, 204 TextureStorage *storage, 205 GLint level) override; 206 207 angle::Result copyTexture(const gl::Context *context, 208 const gl::Texture *source, 209 GLint sourceLevel, 210 gl::TextureTarget srcTarget, 211 const gl::Box &sourceBox, 212 GLenum destFormat, 213 GLenum destType, 214 const gl::Offset &destOffset, 215 TextureStorage *storage, 216 gl::TextureTarget destTarget, 217 GLint destLevel, 218 bool unpackFlipY, 219 bool unpackPremultiplyAlpha, 220 bool unpackUnmultiplyAlpha) override; 221 angle::Result copyCompressedTexture(const gl::Context *context, 222 const gl::Texture *source, 223 GLint sourceLevel, 224 TextureStorage *storage, 225 GLint destLevel) override; 226 227 // RenderTarget creation 228 angle::Result createRenderTarget(const gl::Context *context, 229 int width, 230 int height, 231 GLenum format, 232 GLsizei samples, 233 RenderTargetD3D **outRT) override; 234 angle::Result createRenderTargetCopy(const gl::Context *context, 235 RenderTargetD3D *source, 236 RenderTargetD3D **outRT) override; 237 238 // Shader operations 239 angle::Result loadExecutable(d3d::Context *context, 240 const uint8_t *function, 241 size_t length, 242 gl::ShaderType type, 243 const std::vector<D3DVarying> &streamOutVaryings, 244 bool separatedOutputBuffers, 245 ShaderExecutableD3D **outExecutable) override; 246 angle::Result compileToExecutable(d3d::Context *context, 247 gl::InfoLog &infoLog, 248 const std::string &shaderHLSL, 249 gl::ShaderType type, 250 const std::vector<D3DVarying> &streamOutVaryings, 251 bool separatedOutputBuffers, 252 const angle::CompilerWorkaroundsD3D &workarounds, 253 ShaderExecutableD3D **outExectuable) override; 254 angle::Result ensureHLSLCompilerInitialized(d3d::Context *context) override; 255 256 UniformStorageD3D *createUniformStorage(size_t storageSize) override; 257 258 // Image operations 259 ImageD3D *createImage() override; 260 ExternalImageSiblingImpl *createExternalImageSibling(const gl::Context *context, 261 EGLenum target, 262 EGLClientBuffer buffer, 263 const egl::AttributeMap &attribs) override; 264 angle::Result generateMipmap(const gl::Context *context, 265 ImageD3D *dest, 266 ImageD3D *source) override; 267 angle::Result generateMipmapUsingD3D(const gl::Context *context, 268 TextureStorage *storage, 269 const gl::TextureState &textureState) override; 270 angle::Result copyImage(const gl::Context *context, 271 ImageD3D *dest, 272 ImageD3D *source, 273 const gl::Box &sourceBox, 274 const gl::Offset &destOffset, 275 bool unpackFlipY, 276 bool unpackPremultiplyAlpha, 277 bool unpackUnmultiplyAlpha) override; 278 TextureStorage *createTextureStorage2D(SwapChainD3D *swapChain, 279 const std::string &label) override; 280 TextureStorage *createTextureStorageEGLImage(EGLImageD3D *eglImage, 281 RenderTargetD3D *renderTargetD3D, 282 const std::string &label) override; 283 TextureStorage *createTextureStorageExternal(egl::Stream *stream, 284 const egl::Stream::GLTextureDescription &desc, 285 const std::string &label) override; 286 TextureStorage *createTextureStorage2D(GLenum internalformat, 287 bool renderTarget, 288 GLsizei width, 289 GLsizei height, 290 int levels, 291 const std::string &label, 292 bool hintLevelZeroOnly) override; 293 TextureStorage *createTextureStorageCube(GLenum internalformat, 294 bool renderTarget, 295 int size, 296 int levels, 297 bool hintLevelZeroOnly, 298 const std::string &label) override; 299 TextureStorage *createTextureStorage3D(GLenum internalformat, 300 bool renderTarget, 301 GLsizei width, 302 GLsizei height, 303 GLsizei depth, 304 int levels, 305 const std::string &label) override; 306 TextureStorage *createTextureStorage2DArray(GLenum internalformat, 307 bool renderTarget, 308 GLsizei width, 309 GLsizei height, 310 GLsizei depth, 311 int levels, 312 const std::string &label) override; 313 314 TextureStorage *createTextureStorage2DMultisample(GLenum internalformat, 315 GLsizei width, 316 GLsizei height, 317 int levels, 318 int samples, 319 bool fixedSampleLocations, 320 const std::string &label) override; 321 TextureStorage *createTextureStorage2DMultisampleArray(GLenum internalformat, 322 GLsizei width, 323 GLsizei height, 324 GLsizei depth, 325 int levels, 326 int samples, 327 bool fixedSampleLocations, 328 const std::string &label) override; 329 330 // Buffer creation 331 VertexBuffer *createVertexBuffer() override; 332 IndexBuffer *createIndexBuffer() override; 333 334 // Stream Creation 335 StreamProducerImpl *createStreamProducerD3DTexture(egl::Stream::ConsumerType consumerType, 336 const egl::AttributeMap &attribs) override; 337 338 // Buffer-to-texture and Texture-to-buffer copies 339 bool supportsFastCopyBufferToTexture(GLenum internalFormat) const override; 340 angle::Result fastCopyBufferToTexture(const gl::Context *context, 341 const gl::PixelUnpackState &unpack, 342 gl::Buffer *unpackBuffer, 343 unsigned int offset, 344 RenderTargetD3D *destRenderTarget, 345 GLenum destinationFormat, 346 GLenum sourcePixelsType, 347 const gl::Box &destArea) override; 348 349 // D3D9-renderer specific methods 350 angle::Result boxFilter(Context9 *context9, IDirect3DSurface9 *source, IDirect3DSurface9 *dest); 351 352 D3DPOOL getTexturePool(DWORD usage) const; 353 354 bool getLUID(LUID *adapterLuid) const override; 355 VertexConversionType getVertexConversionType(angle::FormatID vertexFormatID) const override; 356 GLenum getVertexComponentType(angle::FormatID vertexFormatID) const override; 357 358 // Warning: you should ensure binding really matches attrib.bindingIndex before using this 359 // function. 360 angle::Result getVertexSpaceRequired(const gl::Context *context, 361 const gl::VertexAttribute &attrib, 362 const gl::VertexBinding &binding, 363 size_t count, 364 GLsizei instances, 365 GLuint baseInstance, 366 unsigned int *bytesRequiredOut) const override; 367 368 angle::Result copyToRenderTarget(const gl::Context *context, 369 IDirect3DSurface9 *dest, 370 IDirect3DSurface9 *source, 371 bool fromManaged); 372 373 RendererClass getRendererClass() const override; 374 getD3D9DeviceType()375 D3DDEVTYPE getD3D9DeviceType() const { return mDeviceType; } 376 377 DeviceImpl *createEGLDevice() override; 378 getStateManager()379 StateManager9 *getStateManager() { return &mStateManager; } 380 381 angle::Result genericDrawArrays(const gl::Context *context, 382 gl::PrimitiveMode mode, 383 GLint first, 384 GLsizei count, 385 GLsizei instances); 386 387 angle::Result genericDrawElements(const gl::Context *context, 388 gl::PrimitiveMode mode, 389 GLsizei count, 390 gl::DrawElementsType type, 391 const void *indices, 392 GLsizei instances); 393 394 // Necessary hack for default framebuffers in D3D. 395 FramebufferImpl *createDefaultFramebuffer(const gl::FramebufferState &state) override; 396 getAnnotator()397 DebugAnnotator9 *getAnnotator() { return &mAnnotator; } 398 399 gl::Version getMaxSupportedESVersion() const override; 400 gl::Version getMaxConformantESVersion() const override; 401 402 angle::Result clearRenderTarget(const gl::Context *context, 403 RenderTargetD3D *renderTarget, 404 const gl::ColorF &clearColorValue, 405 const float clearDepthValue, 406 const unsigned int clearStencilValue) override; 407 408 bool canSelectViewInVertexShader() const override; 409 410 angle::Result getIncompleteTexture(const gl::Context *context, 411 gl::TextureType type, 412 gl::Texture **textureOut) override; 413 414 angle::Result ensureVertexDataManagerInitialized(const gl::Context *context); 415 416 void setGlobalDebugAnnotator() override; 417 418 std::string getRendererDescription() const override; 419 std::string getVendorString() const override; 420 std::string getVersionString() const override; 421 422 private: 423 angle::Result drawArraysImpl(const gl::Context *context, 424 gl::PrimitiveMode mode, 425 GLint startVertex, 426 GLsizei count, 427 GLsizei instances); 428 angle::Result drawElementsImpl(const gl::Context *context, 429 gl::PrimitiveMode mode, 430 GLsizei count, 431 gl::DrawElementsType type, 432 const void *indices, 433 GLsizei instances); 434 435 angle::Result applyShaders(const gl::Context *context, gl::PrimitiveMode drawMode); 436 437 angle::Result applyTextures(const gl::Context *context); 438 angle::Result applyTextures(const gl::Context *context, gl::ShaderType shaderType); 439 440 void generateCaps(gl::Caps *outCaps, 441 gl::TextureCapsMap *outTextureCaps, 442 gl::Extensions *outExtensions, 443 gl::Limitations *outLimitations) const override; 444 445 void initializeFeatures(angle::FeaturesD3D *features) const override; 446 447 angle::Result setBlendDepthRasterStates(const gl::Context *context, gl::PrimitiveMode drawMode); 448 449 void release(); 450 451 void applyUniformnfv(const D3DUniform *targetUniform, const GLfloat *v); 452 void applyUniformniv(const D3DUniform *targetUniform, const GLint *v); 453 void applyUniformnbv(const D3DUniform *targetUniform, const GLint *v); 454 455 angle::Result drawLineLoop(const gl::Context *context, 456 GLsizei count, 457 gl::DrawElementsType type, 458 const void *indices, 459 int minIndex, 460 gl::Buffer *elementArrayBuffer); 461 angle::Result drawIndexedPoints(const gl::Context *context, 462 GLsizei count, 463 gl::DrawElementsType type, 464 const void *indices, 465 int minIndex, 466 gl::Buffer *elementArrayBuffer); 467 468 angle::Result getCountingIB(const gl::Context *context, 469 size_t count, 470 StaticIndexBufferInterface **outIB); 471 472 angle::Result getNullColorRenderTarget(const gl::Context *context, 473 const RenderTarget9 *depthRenderTarget, 474 const RenderTarget9 **outColorRenderTarget); 475 476 D3DPOOL getBufferPool(DWORD usage) const; 477 478 HMODULE mD3d9Module; 479 480 egl::Error initializeDevice(); 481 D3DPRESENT_PARAMETERS getDefaultPresentParameters(); 482 void releaseDeviceResources(); 483 484 HRESULT getDeviceStatusCode(); 485 bool isRemovedDeviceResettable() const; 486 bool resetRemovedDevice(); 487 488 UINT mAdapter; 489 D3DDEVTYPE mDeviceType; 490 IDirect3D9 *mD3d9; // Always valid after successful initialization. 491 IDirect3D9Ex *mD3d9Ex; // Might be null if D3D9Ex is not supported. 492 IDirect3DDevice9 *mDevice; 493 IDirect3DDevice9Ex *mDeviceEx; // Might be null if D3D9Ex is not supported. 494 495 HLSLCompiler mCompiler; 496 497 Blit9 *mBlit; 498 499 HWND mDeviceWindow; 500 501 D3DCAPS9 mDeviceCaps; 502 D3DADAPTER_IDENTIFIER9 mAdapterIdentifier; 503 504 D3DPRIMITIVETYPE mPrimitiveType; 505 int mPrimitiveCount; 506 GLsizei mRepeatDraw; 507 508 bool mSceneStarted; 509 510 bool mVertexTextureSupport; 511 512 // current render target states 513 unsigned int mAppliedRenderTargetSerial; 514 unsigned int mAppliedDepthStencilSerial; 515 bool mDepthStencilInitialized; 516 bool mRenderTargetDescInitialized; 517 518 IDirect3DStateBlock9 *mMaskedClearSavedState; 519 520 StateManager9 mStateManager; 521 522 // Currently applied sampler states 523 struct CurSamplerState 524 { 525 CurSamplerState(); 526 527 bool forceSet; 528 size_t baseLevel; 529 gl::SamplerState samplerState; 530 }; 531 std::vector<CurSamplerState> mCurVertexSamplerStates; 532 std::vector<CurSamplerState> mCurPixelSamplerStates; 533 534 // Currently applied textures 535 std::vector<uintptr_t> mCurVertexTextures; 536 std::vector<uintptr_t> mCurPixelTextures; 537 538 unsigned int mAppliedIBSerial; 539 IDirect3DVertexShader9 *mAppliedVertexShader; 540 IDirect3DPixelShader9 *mAppliedPixelShader; 541 unsigned int mAppliedProgramSerial; 542 543 // A pool of event queries that are currently unused. 544 std::vector<IDirect3DQuery9 *> mEventQueryPool; 545 VertexShaderCache mVertexShaderCache; 546 PixelShaderCache mPixelShaderCache; 547 548 VertexDataManager *mVertexDataManager; 549 VertexDeclarationCache mVertexDeclarationCache; 550 551 IndexDataManager *mIndexDataManager; 552 StreamingIndexBufferInterface *mLineLoopIB; 553 StaticIndexBufferInterface *mCountingIB; 554 555 enum 556 { 557 NUM_NULL_COLORBUFFER_CACHE_ENTRIES = 12 558 }; 559 struct NullRenderTargetCacheEntry 560 { 561 UINT lruCount; 562 int width; 563 int height; 564 RenderTarget9 *renderTarget; 565 }; 566 567 std::array<NullRenderTargetCacheEntry, NUM_NULL_COLORBUFFER_CACHE_ENTRIES> 568 mNullRenderTargetCache; 569 UINT mMaxNullColorbufferLRU; 570 571 std::vector<TranslatedAttribute> mTranslatedAttribCache; 572 573 DebugAnnotator9 mAnnotator; 574 }; 575 576 } // namespace rx 577 578 #endif // LIBANGLE_RENDERER_D3D_D3D9_RENDERER9_H_ 579