DX12Shader.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "DX12Shader.h"
  2. #include "d3dx12.h"
  3. #include "DX12CommandQueue.h"
  4. using namespace Framework;
  5. DX12Shader::DX12Shader(ID3D12Device5* device,
  6. DX12CopyCommandQueue* copy,
  7. DX12DirectCommandQueue* direct)
  8. : Shader()
  9. {
  10. shaderByteBuffer = 0;
  11. byteBufferSize = 0;
  12. this->device = device;
  13. this->copy = copy;
  14. this->direct = direct;
  15. }
  16. DX12Shader::~DX12Shader()
  17. {
  18. delete[] shaderByteBuffer;
  19. }
  20. // Creates a constant buffer that passes constant data to the shader
  21. // A maximum of 14 buffers can be created
  22. // zD3d11Device: The device used to create the buffer
  23. // size: The size of the buffer in bytes
  24. // index: The position of the buffer in the buffer array. Existing buffer
  25. // is replaced. Buffer 1 cannot be created if buffer 0 has not yet
  26. // been created, etc.
  27. bool DX12Shader::createConstBuffer(int size, int index)
  28. {
  29. if (index < 0 || index >= 14) return 0;
  30. while ((size / 256) * 256 != size)
  31. size++;
  32. while (!constBuffers->has(index))
  33. constBuffers->add(0);
  34. constBuffers->set(
  35. new Framework::DX12Buffer(1, device, D3D12_HEAP_FLAG_NONE), index);
  36. constBuffers->z(index)->setLength(size);
  37. constBuffers->z(index)->copyToGPU();
  38. return 1;
  39. }
  40. // Sets the compiled shader
  41. // zD3d11Device: The device used to create the shader
  42. // bytes: The bytes of the compiled code
  43. // length: the length of the byte array
  44. // return: true if bytes is valid, false otherwise
  45. bool DX12Shader::setCompiledByteArray(unsigned char* bytes, int length)
  46. {
  47. delete[] shaderByteBuffer;
  48. shaderByteBuffer = new unsigned char[length];
  49. memcpy(shaderByteBuffer, bytes, length);
  50. byteBufferSize = length;
  51. return 1;
  52. }
  53. // After calling this function, this shader is used as pixel shader
  54. // zD3d11Context: The context object used with the shader
  55. void DX12Shader::useShader()
  56. {
  57. // not needet in DirectX 12
  58. }
  59. // returns the compiled bytes
  60. unsigned char* DX12Shader::getCompiledShader() const
  61. {
  62. return shaderByteBuffer;
  63. }
  64. // returns the number of compiled bytes
  65. int DX12Shader::getCompiledLength() const
  66. {
  67. return byteBufferSize;
  68. }
  69. // Creates the root parameter for a constant buffer
  70. // index: The index of the buffer
  71. // view: contains the position and size of the buffer in memory after the call
  72. void DX12Shader::getViewDesc(int index, D3D12_CONSTANT_BUFFER_VIEW_DESC& view)
  73. {
  74. DX12Buffer* zB = (DX12Buffer*)constBuffers->z(index);
  75. if (!zB) return;
  76. view.SizeInBytes = (unsigned)zB->getElementCount() * zB->getElementLength();
  77. view.BufferLocation = zB->zBuffer()->GetGPUVirtualAddress();
  78. }