DX12Shader.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. }
  79. DX12PixelShader::DX12PixelShader(ID3D12Device5* device,
  80. DX12CopyCommandQueue* copy,
  81. DX12DirectCommandQueue* direct)
  82. : DX12Shader(device, copy, direct)
  83. {}
  84. // Constructor
  85. DX12VertexShader::DX12VertexShader(ID3D12Device5* device,
  86. DX12CopyCommandQueue* copy,
  87. DX12DirectCommandQueue* direct)
  88. : DX12Shader(device, copy, direct)
  89. {
  90. inputLayout = 0;
  91. inputLayoutSize = 0;
  92. }
  93. // Destructor
  94. DX12VertexShader::~DX12VertexShader()
  95. {
  96. delete[] inputLayout;
  97. }
  98. // Creates an InputLayout for the shader
  99. // Must only be called after compile
  100. // zD3d11Device: The device used to create the layout
  101. // descArray: An array with initialization data
  102. // anz: The number of elements in the array
  103. bool DX12VertexShader::createInputLayout(
  104. D3D12_INPUT_ELEMENT_DESC* descArray, int anz)
  105. {
  106. delete[] inputLayout;
  107. inputLayout = new D3D12_INPUT_ELEMENT_DESC[anz];
  108. memcpy(inputLayout, descArray, anz * sizeof(D3D12_INPUT_ELEMENT_DESC));
  109. inputLayoutSize = anz;
  110. return 1;
  111. }
  112. // Returns the number of input parameters of the shader
  113. int DX12VertexShader::getInputLayoutSize() const
  114. {
  115. return inputLayoutSize;
  116. }
  117. // Returns a list of formats for each input value
  118. D3D12_INPUT_ELEMENT_DESC* DX12VertexShader::zInputLayout() const
  119. {
  120. return inputLayout;
  121. }