Shader.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #pragma once
  2. #include "Array.h"
  3. #include "Critical.h"
  4. struct ID3D10Blob;
  5. struct ID3D11PixelShader;
  6. struct ID3D11VertexShader;
  7. struct ID3D11Device;
  8. struct ID3D11DeviceContext;
  9. struct D3D11_INPUT_ELEMENT_DESC;
  10. struct ID3D11Buffer;
  11. struct ID3D11InputLayout;
  12. namespace Framework
  13. {
  14. class Text;
  15. class DXBuffer;
  16. class DX12CopyCommandQueue;
  17. class DX12DirectCommandQueue;
  18. enum ShaderType
  19. {
  20. UNBEKANNT,
  21. VERTEX,
  22. PIXEL
  23. };
  24. //! A Shader class that manages all constant buffers of a shader
  25. class Shader : public virtual ReferenceCounter
  26. {
  27. protected:
  28. ShaderType type;
  29. RCArray<DXBuffer>* constBuffers;
  30. public:
  31. //! Constructor
  32. DLLEXPORT Shader();
  33. //! Destructor
  34. DLLEXPORT virtual ~Shader();
  35. //! Sets the compiled shader
  36. //! zD3d11Device: The device with which the shader should be created
  37. //! \param bytes The bytes of the compiled code
  38. //! \param length The length of the byte array
  39. //! \return true if bytes is valid, false otherwise
  40. virtual bool setCompiledByteArray(unsigned char* bytes, int length) = 0;
  41. //! After calling this function, this shader is used as a pixel shader
  42. //! zD3d11Context: The context object with which the shader should
  43. //! be used
  44. virtual void useShader() = 0;
  45. //! Creates a constant buffer that passes constant data to the shader.
  46. //! A maximum of 14 buffers can be created.
  47. //! zD3d11Device: The device with which the buffer should be created
  48. //! \param size The size of the buffer in bytes
  49. //! \param index The position of the buffer in the buffer array. An
  50. //! existing buffer will be replaced. Buffer 1 cannot be created
  51. //! if buffer 0 has not been created yet, etc.
  52. virtual bool createConstBuffer(int size, int index) = 0;
  53. //! Deletes a constant buffer
  54. //! \param index The index of the buffer to delete. Buffer 0 cannot
  55. //! be deleted while buffer 1 still exists, etc.
  56. DLLEXPORT bool removeConstBuffer(int index);
  57. //! Copies data into a constant buffer
  58. //! zD3d11Context: The context object to use for copying
  59. //! \param data A pointer to a byte array of the buffer's size
  60. //! \param index The index of the buffer
  61. //! \param len The length of the data in bytes (-1 for the maximum
  62. //! buffer size)
  63. DLLEXPORT bool fillConstBuffer(char* data, int index, int len = -1);
  64. //! Returns the length of a constant buffer
  65. //! \param index The index of the buffer
  66. DLLEXPORT int getConstBufferLaenge(int index) const;
  67. //! Returns the shader type
  68. DLLEXPORT ShaderType getType() const;
  69. //! Returns the index of the first uninitialized buffer
  70. DLLEXPORT int getFirstUninitializedBufferIndex() const;
  71. };
  72. class DX11Shader : public Shader
  73. {
  74. protected:
  75. ID3D11Device* device;
  76. ID3D11DeviceContext* context;
  77. Critical& deviceLock;
  78. public:
  79. DLLEXPORT DX11Shader(ID3D11Device* device,
  80. ID3D11DeviceContext* context,
  81. Critical& deviceLock);
  82. DLLEXPORT virtual ~DX11Shader();
  83. //! Creates a constant buffer that passes constant data to the shader.
  84. //! A maximum of 14 buffers can be created.
  85. //! zD3d11Device: The device with which the buffer should be created
  86. //! \param size The size of the buffer in bytes
  87. //! \param index The position of the buffer in the buffer array. An
  88. //! existing buffer will be replaced. Buffer 1 cannot be created
  89. //! if buffer 0 has not been created yet, etc.
  90. DLLEXPORT virtual bool createConstBuffer(int size, int index) override;
  91. };
  92. //! Manages a pixel shader
  93. class DX11PixelShader : public DX11Shader
  94. {
  95. private:
  96. ID3D11PixelShader* pixelShader;
  97. public:
  98. //! Constructor
  99. DLLEXPORT DX11PixelShader(ID3D11Device* device,
  100. ID3D11DeviceContext* context,
  101. Critical& deviceLock);
  102. //! Destructor
  103. DLLEXPORT ~DX11PixelShader();
  104. //! Sets the compiled shader
  105. //! zD3d11Device: The device with which the shader should be created
  106. //! \param bytes The bytes of the compiled code
  107. //! \param length The length of the byte array
  108. //! \return true if bytes is valid, false otherwise
  109. DLLEXPORT bool setCompiledByteArray(
  110. unsigned char* bytes, int length) override;
  111. //! After calling this function, this shader is used as a pixel shader
  112. //! zD3d11Context: The context object with which the shader should
  113. //! be used
  114. DLLEXPORT void useShader() override;
  115. };
  116. //! Manages a vertex shader
  117. class DX11VertexShader : public DX11Shader
  118. {
  119. private:
  120. ID3D11VertexShader* vertexShader;
  121. ID3D11InputLayout* inputLayout;
  122. unsigned char* shaderByteBuffer;
  123. int byteBufferSize;
  124. public:
  125. //! Constructor
  126. DLLEXPORT DX11VertexShader(ID3D11Device* device,
  127. ID3D11DeviceContext* context,
  128. Critical& deviceLock);
  129. //! Destructor
  130. DLLEXPORT ~DX11VertexShader();
  131. //! Sets the compiled shader
  132. //! zD3d11Device: The device with which the shader should be created
  133. //! \param bytes The bytes of the compiled code
  134. //! \param length The length of the byte array
  135. //! \return true if bytes is valid, false otherwise
  136. DLLEXPORT bool setCompiledByteArray(
  137. unsigned char* bytes, int length) override;
  138. //! Creates an input layout for the shader.
  139. //! May only be called after compile.
  140. //! zD3d11Device: The device with which the layout should be created
  141. //! \param descArray An array with initialization data
  142. //! \param anz The number of elements in the array
  143. DLLEXPORT bool createInputLayout(
  144. D3D11_INPUT_ELEMENT_DESC* descArray, int anz);
  145. //! After calling this function, this shader is used as a vertex shader
  146. //! zD3d11Context: The context object with which the shader should
  147. //! be used
  148. DLLEXPORT void useShader() override;
  149. };
  150. } // namespace Framework