OGLIndexBuffer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "ArrayPtr.h"
  25. #include "GPUObject.h"
  26. #include "Object.h"
  27. #include "GraphicsDefs.h"
  28. /// Hardware index buffer.
  29. class IndexBuffer : public Object, public GPUObject
  30. {
  31. OBJECT(IndexBuffer);
  32. public:
  33. /// Construct.
  34. IndexBuffer(Context* context);
  35. /// Destruct.
  36. virtual ~IndexBuffer();
  37. /// Mark the GPU resource destroyed on context destruction.
  38. virtual void OnDeviceLost();
  39. /// Recreate the GPU resource and restore data if applicable.
  40. virtual void OnDeviceReset();
  41. /// Release the buffer.
  42. virtual void Release();
  43. /// Enable shadowing in CPU memory. Shadowing is forced on if the graphics subsystem does not exist.
  44. void SetShadowed(bool enable);
  45. /// %Set buffer size and dynamic mode. Previous data will be lost.
  46. bool SetSize(unsigned indexCount, bool largeIndices, bool dynamic = false);
  47. /// %Set all data in the buffer.
  48. bool SetData(const void* data);
  49. /// %Set a data range in the buffer. Optionally discard data outside the range.
  50. bool SetDataRange(const void* data, unsigned start, unsigned count, bool discard = false);
  51. /// Lock the buffer for write-only editing. Return data pointer if successful. Optionally discard data outside the range.
  52. void* Lock(unsigned start, unsigned count, bool discard = false);
  53. /// Unlock the buffer and apply changes to the GPU buffer.
  54. void Unlock();
  55. /// Return whether CPU memory shadowing is enabled.
  56. bool IsShadowed() const { return shadowed_; }
  57. /// Return whether is dynamic.
  58. bool IsDynamic() const { return dynamic_; }
  59. /// Return whether is currently locked.
  60. bool IsLocked() const { return lockState_ != LOCK_NONE; }
  61. /// Return number of indices.
  62. unsigned GetIndexCount() const {return indexCount_; }
  63. /// Return index size.
  64. unsigned GetIndexSize() const { return indexSize_; }
  65. /// Return used vertex range from index range. Only supported for shadowed buffers.
  66. bool GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount);
  67. /// Return CPU memory shadow data.
  68. unsigned char* GetShadowData() const { return shadowData_.Get(); }
  69. private:
  70. /// Create buffer.
  71. bool Create();
  72. /// Update the shadow data to the GPU buffer.
  73. bool UpdateToGPU();
  74. /// Shadow data.
  75. SharedArrayPtr<unsigned char> shadowData_;
  76. /// Number of indices.
  77. unsigned indexCount_;
  78. /// Index size.
  79. unsigned indexSize_;
  80. /// Buffer locking state.
  81. LockState lockState_;
  82. /// Lock start vertex.
  83. unsigned lockStart_;
  84. /// Lock number of vertices.
  85. unsigned lockCount_;
  86. /// Scratch buffer for fallback locking.
  87. void* lockScratchData_;
  88. /// Shadowed flag.
  89. bool shadowed_;
  90. /// Dynamic flag.
  91. bool dynamic_;
  92. };