D3D11ConstantBuffer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../../Graphics/Graphics.h"
  23. #include "../../Graphics/GraphicsImpl.h"
  24. #include "../../IO/Log.h"
  25. #include "../../Graphics/ConstantBuffer.h"
  26. #include "../../DebugNew.h"
  27. namespace Urho3D
  28. {
  29. ConstantBuffer::ConstantBuffer(Context* context) :
  30. Object(context),
  31. GPUObject(GetSubsystem<Graphics>())
  32. {
  33. }
  34. ConstantBuffer::~ConstantBuffer()
  35. {
  36. Release();
  37. }
  38. void ConstantBuffer::Release()
  39. {
  40. if (object_)
  41. {
  42. if (!graphics_)
  43. return;
  44. ((ID3D11Buffer*)object_)->Release();
  45. object_ = 0;
  46. }
  47. shadowData_.Reset();
  48. size_ = 0;
  49. }
  50. bool ConstantBuffer::SetSize(unsigned size)
  51. {
  52. Release();
  53. if (!size)
  54. {
  55. LOGERROR("Can not create zero-sized constant buffer");
  56. return false;
  57. }
  58. // Round up to next 16 bytes
  59. size += 15;
  60. size &= 0xfffffff0;
  61. size_ = size;
  62. dirty_ = false;
  63. shadowData_ = new unsigned char[size_];
  64. memset(shadowData_.Get(), 0, size_);
  65. if (graphics_)
  66. {
  67. D3D11_BUFFER_DESC bufferDesc;
  68. memset(&bufferDesc, 0, sizeof bufferDesc);
  69. bufferDesc.ByteWidth = size_;
  70. bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  71. bufferDesc.CPUAccessFlags = 0;
  72. bufferDesc.Usage = D3D11_USAGE_DEFAULT;
  73. graphics_->GetImpl()->GetDevice()->CreateBuffer(&bufferDesc, 0, (ID3D11Buffer**)&object_);
  74. if (!object_)
  75. {
  76. LOGERROR("Failed to create constant buffer");
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. void ConstantBuffer::SetParameter(unsigned offset, unsigned size, const void* data)
  83. {
  84. if (offset + size > size_)
  85. return; // Would overflow the buffer
  86. memcpy(&shadowData_[offset], data, size);
  87. dirty_ = true;
  88. }
  89. void ConstantBuffer::SetVector3ArrayParameter(unsigned offset, unsigned rows, const void* data)
  90. {
  91. if (offset + rows * 4 * sizeof(float) > size_)
  92. return; // Would overflow the buffer
  93. float* dest = (float*)&shadowData_[offset];
  94. const float* src = (const float*)data;
  95. while (rows--)
  96. {
  97. *dest++ = *src++;
  98. *dest++ = *src++;
  99. *dest++ = *src++;
  100. ++dest; // Skip over the w coordinate
  101. }
  102. dirty_ = true;
  103. }
  104. void ConstantBuffer::Apply()
  105. {
  106. if (dirty_ && object_)
  107. {
  108. graphics_->GetImpl()->GetDeviceContext()->UpdateSubresource((ID3D11Buffer*)object_, 0, 0, shadowData_.Get(), 0, 0);
  109. dirty_ = false;
  110. }
  111. }
  112. }