DX11Shader.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "DX11Shader.h"
  2. #include <d3d11.h>
  3. #include "DX11Buffer.h"
  4. Framework::DX11Shader::DX11Shader(
  5. ID3D11Device* device, ID3D11DeviceContext* context, Critical& deviceLock)
  6. : Shader(),
  7. deviceLock(deviceLock)
  8. {
  9. this->device = device;
  10. this->context = context;
  11. }
  12. Framework::DX11Shader::~DX11Shader() {}
  13. // Creates a constant buffer that passes constant data to the shader
  14. // A maximum of 14 buffers can be created
  15. // zD3d11Device: The device used to create the buffer
  16. // groesse: The size of the buffer in bytes
  17. // index: The position of the buffer in the buffer array. Existing buffer
  18. // is replaced. Buffer 1 cannot be created if buffer 0 has not yet
  19. // been created, etc.
  20. bool Framework::DX11Shader::createConstBuffer(int groesse, int index)
  21. {
  22. if (index < 0 || index >= 14) return 0;
  23. bool ok = 1;
  24. while ((groesse / 16) * 16
  25. != groesse) // only multiples of 16 are allowed as size
  26. groesse++;
  27. while (!constBuffers->has(index))
  28. constBuffers->add(0);
  29. constBuffers->set(
  30. new Framework::DX11Buffer(
  31. 1, device, context, D3D11_BIND_CONSTANT_BUFFER, deviceLock),
  32. index);
  33. constBuffers->z(index)->setLength(groesse);
  34. return 1;
  35. }
  36. // Contents of the PixelShader class
  37. // Constructor
  38. Framework::DX11PixelShader::DX11PixelShader(
  39. ID3D11Device* device, ID3D11DeviceContext* context, Critical& deviceLock)
  40. : DX11Shader(device, context, deviceLock)
  41. {
  42. pixelShader = 0;
  43. }
  44. // Destructor
  45. Framework::DX11PixelShader::~DX11PixelShader()
  46. {
  47. if (pixelShader) pixelShader->Release();
  48. }
  49. // Sets the compiled shader
  50. // bytes: The bytes of the compiled code
  51. // length: the length of the byte array
  52. // return: true if bytes is valid, false otherwise
  53. bool Framework::DX11PixelShader::setCompiledByteArray(
  54. unsigned char* bytes, int length)
  55. {
  56. deviceLock.lock();
  57. HRESULT result = device->CreatePixelShader(bytes, length, 0, &pixelShader);
  58. deviceLock.unlock();
  59. return result == S_OK;
  60. }
  61. // After calling this function, this shader is used as pixel shader
  62. // zD3d11Context: The context object used with the shader
  63. void Framework::DX11PixelShader::useShader()
  64. {
  65. int maxI = constBuffers->getLastIndex();
  66. for (int i = 0; i <= maxI; i++)
  67. {
  68. if (!constBuffers->z(i)) continue;
  69. if (!((Framework::DX11Buffer*)constBuffers->z(i))->zBuffer())
  70. constBuffers->z(i)->copyToGPU();
  71. ID3D11Buffer* buf
  72. = ((Framework::DX11Buffer*)constBuffers->z(i))->zBuffer();
  73. deviceLock.lock();
  74. context->PSSetConstantBuffers(i, 1, &buf);
  75. deviceLock.unlock();
  76. }
  77. if (pixelShader)
  78. {
  79. deviceLock.lock();
  80. context->PSSetShader(pixelShader, 0, 0);
  81. deviceLock.unlock();
  82. }
  83. }
  84. // Contents of the VertexShader class
  85. // Constructor
  86. Framework::DX11VertexShader::DX11VertexShader(ID3D11Device* device,
  87. ID3D11DeviceContext* context,
  88. Framework::Critical& deviceLock)
  89. : DX11Shader(device, context, deviceLock)
  90. {
  91. vertexShader = 0;
  92. inputLayout = 0;
  93. shaderByteBuffer = 0;
  94. byteBufferSize = 0;
  95. }
  96. // Destructor
  97. Framework::DX11VertexShader::~DX11VertexShader()
  98. {
  99. if (vertexShader) vertexShader->Release();
  100. if (inputLayout) inputLayout->Release();
  101. }
  102. // Sets the compiled shader
  103. // bytes: The bytes of the compiled code
  104. // length: the length of the byte array
  105. // return: true if bytes is valid, false otherwise
  106. bool Framework::DX11VertexShader::setCompiledByteArray(
  107. unsigned char* bytes, int length)
  108. {
  109. shaderByteBuffer = (unsigned char*)bytes;
  110. byteBufferSize = length;
  111. deviceLock.lock();
  112. HRESULT result
  113. = device->CreateVertexShader(bytes, length, 0, &vertexShader);
  114. deviceLock.unlock();
  115. return result == S_OK;
  116. }
  117. // Creates an InputLayout for the shader
  118. // Must only be called after compile
  119. // zD3d11Device: The device used to create the layout
  120. // descArray: An array with initialization data
  121. // anz: The number of elements in the array
  122. bool Framework::DX11VertexShader::createInputLayout(
  123. D3D11_INPUT_ELEMENT_DESC* descArray, int anz)
  124. {
  125. if (!shaderByteBuffer) return 0;
  126. if (inputLayout) inputLayout->Release();
  127. inputLayout = 0;
  128. deviceLock.lock();
  129. HRESULT res = device->CreateInputLayout(
  130. descArray, anz, shaderByteBuffer, byteBufferSize, &inputLayout);
  131. deviceLock.unlock();
  132. if (res == S_OK)
  133. {
  134. shaderByteBuffer = 0;
  135. byteBufferSize = 0;
  136. }
  137. return res == S_OK;
  138. }
  139. // After calling this function, this shader is used as vertex shader
  140. // zD3d11Context: The context object used with the shader
  141. void Framework::DX11VertexShader::useShader()
  142. {
  143. int maxI = constBuffers->getLastIndex();
  144. for (int i = 0; i <= maxI; i++)
  145. {
  146. if (!constBuffers->z(i)) continue;
  147. if (!((Framework::DX11Buffer*)constBuffers->z(i))->zBuffer())
  148. constBuffers->z(i)->copyToGPU();
  149. ID3D11Buffer* buf
  150. = ((Framework::DX11Buffer*)constBuffers->z(i))->zBuffer();
  151. deviceLock.lock();
  152. context->VSSetConstantBuffers(i, 1, &buf);
  153. deviceLock.unlock();
  154. }
  155. if (inputLayout)
  156. {
  157. deviceLock.lock();
  158. context->IASetInputLayout(inputLayout);
  159. deviceLock.unlock();
  160. }
  161. if (vertexShader)
  162. {
  163. deviceLock.lock();
  164. context->VSSetShader(vertexShader, 0, 0);
  165. deviceLock.unlock();
  166. }
  167. }