Shader.h 6.1 KB

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