D3D11ConstantBuffer.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // Copyright (c) 2008-2017 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. void ConstantBuffer::OnDeviceReset()
  31. {
  32. // No-op on Direct3D11
  33. }
  34. void ConstantBuffer::Release()
  35. {
  36. ATOMIC_SAFE_RELEASE(object_.ptr_);
  37. shadowData_.Reset();
  38. size_ = 0;
  39. }
  40. bool ConstantBuffer::SetSize(unsigned size)
  41. {
  42. Release();
  43. if (!size)
  44. {
  45. ATOMIC_LOGERROR("Can not create zero-sized constant buffer");
  46. return false;
  47. }
  48. // Round up to next 16 bytes
  49. size += 15;
  50. size &= 0xfffffff0;
  51. size_ = size;
  52. dirty_ = false;
  53. shadowData_ = new unsigned char[size_];
  54. memset(shadowData_.Get(), 0, size_);
  55. if (graphics_)
  56. {
  57. D3D11_BUFFER_DESC bufferDesc;
  58. memset(&bufferDesc, 0, sizeof bufferDesc);
  59. bufferDesc.ByteWidth = size_;
  60. bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  61. bufferDesc.CPUAccessFlags = 0;
  62. bufferDesc.Usage = D3D11_USAGE_DEFAULT;
  63. HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateBuffer(&bufferDesc, 0, (ID3D11Buffer**)&object_.ptr_);
  64. if (FAILED(hr))
  65. {
  66. ATOMIC_SAFE_RELEASE(object_.ptr_);
  67. ATOMIC_LOGD3DERROR("Failed to create constant buffer", hr);
  68. return false;
  69. }
  70. }
  71. return true;
  72. }
  73. void ConstantBuffer::Apply()
  74. {
  75. if (dirty_ && object_.ptr_)
  76. {
  77. graphics_->GetImpl()->GetDeviceContext()->UpdateSubresource((ID3D11Buffer*)object_.ptr_, 0, 0, shadowData_.Get(), 0, 0);
  78. dirty_ = false;
  79. }
  80. }
  81. }