D3D11ConstantBuffer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "../../Precompiled.h"
  23. #include "../../Graphics/Graphics.h"
  24. #include "../../Graphics/GraphicsImpl.h"
  25. #include "../../Graphics/ConstantBuffer.h"
  26. #include "../../IO/Log.h"
  27. #include "../../DebugNew.h"
  28. namespace Atomic
  29. {
  30. ConstantBuffer::ConstantBuffer(Context* context) :
  31. Object(context),
  32. GPUObject(GetSubsystem<Graphics>())
  33. {
  34. }
  35. ConstantBuffer::~ConstantBuffer()
  36. {
  37. Release();
  38. }
  39. void ConstantBuffer::Release()
  40. {
  41. if (object_)
  42. {
  43. if (!graphics_)
  44. return;
  45. ((ID3D11Buffer*)object_)->Release();
  46. object_ = 0;
  47. }
  48. shadowData_.Reset();
  49. size_ = 0;
  50. }
  51. bool ConstantBuffer::SetSize(unsigned size)
  52. {
  53. Release();
  54. if (!size)
  55. {
  56. LOGERROR("Can not create zero-sized constant buffer");
  57. return false;
  58. }
  59. // Round up to next 16 bytes
  60. size += 15;
  61. size &= 0xfffffff0;
  62. size_ = size;
  63. dirty_ = false;
  64. shadowData_ = new unsigned char[size_];
  65. memset(shadowData_.Get(), 0, size_);
  66. if (graphics_)
  67. {
  68. D3D11_BUFFER_DESC bufferDesc;
  69. memset(&bufferDesc, 0, sizeof bufferDesc);
  70. bufferDesc.ByteWidth = size_;
  71. bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  72. bufferDesc.CPUAccessFlags = 0;
  73. bufferDesc.Usage = D3D11_USAGE_DEFAULT;
  74. graphics_->GetImpl()->GetDevice()->CreateBuffer(&bufferDesc, 0, (ID3D11Buffer**)&object_);
  75. if (!object_)
  76. {
  77. LOGERROR("Failed to create constant buffer");
  78. return false;
  79. }
  80. }
  81. return true;
  82. }
  83. void ConstantBuffer::SetParameter(unsigned offset, unsigned size, const void* data)
  84. {
  85. if (offset + size > size_)
  86. return; // Would overflow the buffer
  87. memcpy(&shadowData_[offset], data, size);
  88. dirty_ = true;
  89. }
  90. void ConstantBuffer::SetVector3ArrayParameter(unsigned offset, unsigned rows, const void* data)
  91. {
  92. if (offset + rows * 4 * sizeof(float) > size_)
  93. return; // Would overflow the buffer
  94. float* dest = (float*)&shadowData_[offset];
  95. const float* src = (const float*)data;
  96. while (rows--)
  97. {
  98. *dest++ = *src++;
  99. *dest++ = *src++;
  100. *dest++ = *src++;
  101. ++dest; // Skip over the w coordinate
  102. }
  103. dirty_ = true;
  104. }
  105. void ConstantBuffer::Apply()
  106. {
  107. if (dirty_ && object_)
  108. {
  109. graphics_->GetImpl()->GetDeviceContext()->UpdateSubresource((ID3D11Buffer*)object_, 0, 0, shadowData_.Get(), 0, 0);
  110. dirty_ = false;
  111. }
  112. }
  113. }