DX12Shader.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "DX12Shader.h"
  2. #include "d3dx12.h"
  3. #include "DX12CommandQueue.h"
  4. using namespace Framework;
  5. DX12Shader::DX12Shader(ID3D12Device* 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(new DX12VertexBuffer(1, device, copy, direct), index);
  35. constBuffers->z(index)->setLength(size);
  36. constBuffers->z(index)->copyData();
  37. return 1;
  38. }
  39. // Sets the compiled shader
  40. // zD3d11Device: The device used to create the shader
  41. // bytes: The bytes of the compiled code
  42. // length: the length of the byte array
  43. // return: true if bytes is valid, false otherwise
  44. bool DX12Shader::setCompiledByteArray(unsigned char* bytes, int length)
  45. {
  46. delete[] shaderByteBuffer;
  47. shaderByteBuffer = new unsigned char[length];
  48. memcpy(shaderByteBuffer, bytes, length);
  49. byteBufferSize = length;
  50. return 1;
  51. }
  52. // After calling this function, this shader is used as pixel shader
  53. // zD3d11Context: The context object used with the shader
  54. void DX12Shader::useShader()
  55. {
  56. // not needet in DirectX 12
  57. }
  58. // returns the compiled bytes
  59. unsigned char* DX12Shader::getCompiledShader() const
  60. {
  61. return shaderByteBuffer;
  62. }
  63. // returns the number of compiled bytes
  64. int DX12Shader::getCompiledLength() const
  65. {
  66. return byteBufferSize;
  67. }
  68. // Creates the root parameter for a constant buffer
  69. // index: The index of the buffer
  70. // view: contains the position and size of the buffer in memory after the call
  71. void DX12Shader::getViewDesc(int index, D3D12_CONSTANT_BUFFER_VIEW_DESC& view)
  72. {
  73. DX12Buffer* zB = (DX12Buffer*)constBuffers->z(index);
  74. if (!zB) return;
  75. view.SizeInBytes = zB->getElementCount() * zB->getElementLength();
  76. view.BufferLocation = zB->zBuffer()->GetGPUVirtualAddress();
  77. }
  78. DX12PixelShader::DX12PixelShader(ID3D12Device* device,
  79. DX12CopyCommandQueue* copy,
  80. DX12DirectCommandQueue* direct)
  81. : DX12Shader(device, copy, direct)
  82. {}
  83. // Constructor
  84. DX12VertexShader::DX12VertexShader(ID3D12Device* device,
  85. DX12CopyCommandQueue* copy,
  86. DX12DirectCommandQueue* direct)
  87. : DX12Shader(device, copy, direct)
  88. {
  89. inputLayout = 0;
  90. inputLayoutSize = 0;
  91. }
  92. // Destructor
  93. DX12VertexShader::~DX12VertexShader()
  94. {
  95. delete[] inputLayout;
  96. }
  97. // Creates an InputLayout for the shader
  98. // Must only be called after compile
  99. // zD3d11Device: The device used to create the layout
  100. // descArray: An array with initialization data
  101. // anz: The number of elements in the array
  102. bool DX12VertexShader::createInputLayout(
  103. D3D12_INPUT_ELEMENT_DESC* descArray, int anz)
  104. {
  105. delete[] inputLayout;
  106. inputLayout = new D3D12_INPUT_ELEMENT_DESC[anz];
  107. memcpy(inputLayout, descArray, anz * sizeof(D3D12_INPUT_ELEMENT_DESC));
  108. inputLayoutSize = anz;
  109. return 1;
  110. }
  111. // Returns the number of input parameters of the shader
  112. int DX12VertexShader::getInputLayoutSize() const
  113. {
  114. return inputLayoutSize;
  115. }
  116. // Returns a list of formats for each input value
  117. D3D12_INPUT_ELEMENT_DESC* DX12VertexShader::zInputLayout() const
  118. {
  119. return inputLayout;
  120. }