EffectLoad.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //--------------------------------------------------------------------------------------
  2. // File: EffectLoad.h
  3. //
  4. // Direct3D 11 Effects header for the FX file loader
  5. // A CEffectLoader is created at load time to facilitate loading
  6. //
  7. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  8. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  9. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  10. // PARTICULAR PURPOSE.
  11. //
  12. // Copyright (c) Microsoft Corporation. All rights reserved.
  13. //
  14. // http://go.microsoft.com/fwlink/p/?LinkId=271568
  15. //--------------------------------------------------------------------------------------
  16. #pragma once
  17. namespace D3DX11Effects
  18. {
  19. // Ranges are used for dependency checking during load
  20. enum ERanges
  21. {
  22. ER_CBuffer = 0,
  23. ER_Texture, // Includes TBuffers
  24. ER_Sampler,
  25. ER_UnorderedAccessView,
  26. ER_Interfaces,
  27. ER_Count // This should be the size of the enum
  28. };
  29. struct SRange
  30. {
  31. uint32_t start;
  32. uint32_t last;
  33. CEffectVector<void *> vResources; // should be (last - start) in length, resource type depends on the range type
  34. };
  35. // Used during load to validate assignments
  36. D3D_SHADER_VARIABLE_TYPE GetSimpleParameterTypeFromObjectType(EObjectType ObjectType);
  37. // A class to facilitate loading an Effect. This class is a friend of CEffect.
  38. class CEffectLoader
  39. {
  40. friend HRESULT CEffect::CloneEffect(_In_ uint32_t Flags, _Outptr_ ID3DX11Effect** ppClonedEffect );
  41. protected:
  42. // Load-time allocations that eventually get moved happen out of the TempHeap. This heap will grow as needed
  43. CDataBlockStore m_BulkHeap;
  44. uint8_t *m_pData;
  45. SBinaryHeader5 *m_pHeader;
  46. DWORD m_Version;
  47. CEffect *m_pEffect;
  48. CEffectReflection *m_pReflection;
  49. D3DX11Core::CMemoryStream m_msStructured;
  50. D3DX11Core::CMemoryStream m_msUnstructured;
  51. // used to avoid repeated hash buffer allocations in LoadTypeAndAddToPool
  52. CEffectVector<uint8_t> m_HashBuffer;
  53. uint32_t m_dwBufferSize; // Size of data buffer in bytes
  54. // List of SInterface blocks created to back class instances bound to shaders
  55. CEffectVector<SInterface*> m_BackgroundInterfaces;
  56. // Pointers to pre-reallocation data
  57. SGlobalVariable *m_pOldVars;
  58. SShaderBlock *m_pOldShaders;
  59. SDepthStencilBlock *m_pOldDS;
  60. SBlendBlock *m_pOldAB;
  61. SRasterizerBlock *m_pOldRS;
  62. SConstantBuffer *m_pOldCBs;
  63. SSamplerBlock *m_pOldSamplers;
  64. uint32_t m_OldInterfaceCount;
  65. SInterface *m_pOldInterfaces;
  66. SShaderResource *m_pOldShaderResources;
  67. SUnorderedAccessView *m_pOldUnorderedAccessViews;
  68. SRenderTargetView *m_pOldRenderTargetViews;
  69. SDepthStencilView *m_pOldDepthStencilViews;
  70. SString *m_pOldStrings;
  71. SMemberDataPointer *m_pOldMemberDataBlocks;
  72. CEffectVectorOwner<SMember> *m_pvOldMemberInterfaces;
  73. SGroup *m_pOldGroups;
  74. uint32_t m_EffectMemory; // Effect private heap
  75. uint32_t m_ReflectionMemory; // Reflection private heap
  76. // Loader helpers
  77. HRESULT LoadCBs();
  78. HRESULT LoadNumericVariable(_In_ SConstantBuffer *pParentCB);
  79. HRESULT LoadObjectVariables();
  80. HRESULT LoadInterfaceVariables();
  81. HRESULT LoadTypeAndAddToPool(_Outptr_ SType **ppType, _In_ uint32_t dwOffset);
  82. HRESULT LoadStringAndAddToPool(_Outptr_result_maybenull_z_ char **ppString, _In_ uint32_t dwOffset);
  83. HRESULT LoadAssignments( _In_ uint32_t Assignments, _Out_writes_(Assignments) SAssignment **pAssignments,
  84. _In_ uint8_t *pBackingStore, _Out_opt_ uint32_t *pRTVAssignments, _Out_opt_ uint32_t *pFinalAssignments );
  85. HRESULT LoadGroups();
  86. HRESULT LoadTechnique( STechnique* pTech );
  87. HRESULT LoadAnnotations(uint32_t *pcAnnotations, SAnnotation **ppAnnotations);
  88. HRESULT ExecuteConstantAssignment(_In_ const SBinaryConstant *pConstant, _Out_writes_bytes_(4) void *pLHS, _In_ D3D_SHADER_VARIABLE_TYPE lhsType);
  89. uint32_t UnpackData(uint8_t *pDestData, uint8_t *pSrcData, uint32_t PackedDataSize, SType *pType, uint32_t *pBytesRead);
  90. // Build shader blocks
  91. HRESULT ConvertRangesToBindings(SShaderBlock *pShaderBlock, CEffectVector<SRange> *pvRanges );
  92. HRESULT GrabShaderData(SShaderBlock *pShaderBlock);
  93. HRESULT BuildShaderBlock(SShaderBlock *pShaderBlock);
  94. // Memory compactors
  95. HRESULT InitializeReflectionDataAndMoveStrings( uint32_t KnownSize = 0 );
  96. HRESULT ReallocateReflectionData( bool Cloning = false );
  97. HRESULT ReallocateEffectData( bool Cloning = false );
  98. HRESULT ReallocateShaderBlocks();
  99. template<class T> HRESULT ReallocateBlockAssignments(T* &pBlocks, uint32_t cBlocks, T* pOldBlocks = nullptr);
  100. HRESULT ReallocateAnnotationData(uint32_t cAnnotations, SAnnotation **ppAnnotations);
  101. HRESULT CalculateAnnotationSize(uint32_t cAnnotations, SAnnotation *pAnnotations);
  102. uint32_t CalculateShaderBlockSize();
  103. template<class T> uint32_t CalculateBlockAssignmentSize(T* &pBlocks, uint32_t cBlocks);
  104. HRESULT FixupCBPointer(_Inout_ SConstantBuffer **ppCB);
  105. HRESULT FixupShaderPointer(_Inout_ SShaderBlock **ppShaderBlock);
  106. HRESULT FixupDSPointer(_Inout_ SDepthStencilBlock **ppDSBlock);
  107. HRESULT FixupABPointer(_Inout_ SBlendBlock **ppABBlock);
  108. HRESULT FixupRSPointer(_Inout_ SRasterizerBlock **ppRSBlock);
  109. HRESULT FixupInterfacePointer(_Inout_ SInterface **ppInterface, _In_ bool CheckBackgroundInterfaces);
  110. HRESULT FixupShaderResourcePointer(_Inout_ SShaderResource **ppResource);
  111. HRESULT FixupUnorderedAccessViewPointer(_Inout_ SUnorderedAccessView **ppResource);
  112. HRESULT FixupRenderTargetViewPointer(_Inout_ SRenderTargetView **ppRenderTargetView);
  113. HRESULT FixupDepthStencilViewPointer(_Inout_ SDepthStencilView **ppDepthStencilView);
  114. HRESULT FixupSamplerPointer(_Inout_ SSamplerBlock **ppSampler);
  115. HRESULT FixupVariablePointer(_Inout_ SGlobalVariable **ppVar);
  116. HRESULT FixupStringPointer(_Inout_ SString **ppString);
  117. HRESULT FixupMemberDataPointer(_Inout_ SMemberDataPointer **ppMemberData);
  118. HRESULT FixupGroupPointer(_Inout_ SGroup **ppGroup);
  119. // Methods to retrieve data from the unstructured block
  120. // (these do not make copies; they simply return pointers into the block)
  121. HRESULT GetStringAndAddToReflection(_In_ uint32_t offset, _Outptr_result_maybenull_z_ char **ppPointer); // Returns a string from the file string block, updates m_EffectMemory
  122. HRESULT GetUnstructuredDataBlock(_In_ uint32_t offset, _Out_ uint32_t *pdwSize, _Outptr_result_buffer_(*pdwSize) void **ppData);
  123. // This function makes a copy of the array of SInterfaceParameters, but not a copy of the strings
  124. HRESULT GetInterfaceParametersAndAddToReflection( _In_ uint32_t InterfaceCount, _In_ uint32_t offset, _Outptr_result_buffer_all_maybenull_(InterfaceCount) SShaderBlock::SInterfaceParameter **ppInterfaces );
  125. public:
  126. HRESULT LoadEffect(_In_ CEffect *pEffect, _In_reads_bytes_(cbEffectBuffer) const void *pEffectBuffer, _In_ uint32_t cbEffectBuffer);
  127. };
  128. }