OGLIndexBuffer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Copyright (c) 2008-2014 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. #pragma once
  23. #include "ArrayPtr.h"
  24. #include "GPUObject.h"
  25. #include "Object.h"
  26. #include "GraphicsDefs.h"
  27. namespace Urho3D
  28. {
  29. /// Hardware index buffer.
  30. class URHO3D_API IndexBuffer : public Object, public GPUObject
  31. {
  32. OBJECT(IndexBuffer);
  33. public:
  34. /// Construct.
  35. IndexBuffer(Context* context);
  36. /// Destruct.
  37. virtual ~IndexBuffer();
  38. /// Recreate the GPU resource and restore data if applicable.
  39. virtual void OnDeviceReset();
  40. /// Release the buffer.
  41. virtual void Release();
  42. /// Enable shadowing in CPU memory. Shadowing is forced on if the graphics subsystem does not exist.
  43. void SetShadowed(bool enable);
  44. /// Set buffer size and dynamic mode. Previous data will be lost.
  45. bool SetSize(unsigned indexCount, bool largeIndices, bool dynamic = false);
  46. /// Set all data in the buffer.
  47. bool SetData(const void* data);
  48. /// Set a data range in the buffer. Optionally discard data outside the range.
  49. bool SetDataRange(const void* data, unsigned start, unsigned count, bool discard = false);
  50. /// Lock the buffer for write-only editing. Return data pointer if successful. Optionally discard data outside the range.
  51. void* Lock(unsigned start, unsigned count, bool discard = false);
  52. /// Unlock the buffer and apply changes to the GPU buffer.
  53. void Unlock();
  54. /// Return whether CPU memory shadowing is enabled.
  55. bool IsShadowed() const { return shadowed_; }
  56. /// Return whether is dynamic.
  57. bool IsDynamic() const { return dynamic_; }
  58. /// Return whether is currently locked.
  59. bool IsLocked() const { return lockState_ != LOCK_NONE; }
  60. /// Return number of indices.
  61. unsigned GetIndexCount() const {return indexCount_; }
  62. /// Return index size.
  63. unsigned GetIndexSize() const { return indexSize_; }
  64. /// Return used vertex range from index range. Only supported for shadowed buffers.
  65. bool GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount);
  66. /// Return CPU memory shadow data.
  67. unsigned char* GetShadowData() const { return shadowData_.Get(); }
  68. /// Return shared array pointer to the CPU memory shadow data.
  69. SharedArrayPtr<unsigned char> GetShadowDataShared() const { return shadowData_; }
  70. private:
  71. /// Create buffer.
  72. bool Create();
  73. /// Update the shadow data to the GPU buffer.
  74. bool UpdateToGPU();
  75. /// Shadow data.
  76. SharedArrayPtr<unsigned char> shadowData_;
  77. /// Number of indices.
  78. unsigned indexCount_;
  79. /// Index size.
  80. unsigned indexSize_;
  81. /// Buffer locking state.
  82. LockState lockState_;
  83. /// Lock start vertex.
  84. unsigned lockStart_;
  85. /// Lock number of vertices.
  86. unsigned lockCount_;
  87. /// Scratch buffer for fallback locking.
  88. void* lockScratchData_;
  89. /// Shadowed flag.
  90. bool shadowed_;
  91. /// Dynamic flag.
  92. bool dynamic_;
  93. };
  94. }