DrawableProxy2D.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #include "Precompiled.h"
  23. #include "Camera.h"
  24. #include "Context.h"
  25. #include "Drawable2D.h"
  26. #include "DrawableProxy2D.h"
  27. #include "Geometry.h"
  28. #include "GraphicsEvents.h"
  29. #include "IndexBuffer.h"
  30. #include "Log.h"
  31. #include "Material.h"
  32. #include "Node.h"
  33. #include "Profiler.h"
  34. #include "Scene.h"
  35. #include "VertexBuffer.h"
  36. #include "Sort.h"
  37. #include "DebugNew.h"
  38. namespace Urho3D
  39. {
  40. DrawableProxy2D::DrawableProxy2D(Context* context) :
  41. Drawable(context, DRAWABLE_GEOMETRY),
  42. indexBuffer_(new IndexBuffer(context_)),
  43. vertexBuffer_(new VertexBuffer(context_)),
  44. orderDirty_(true),
  45. indexCount_(0),
  46. vertexCount_(0)
  47. {
  48. SubscribeToEvent(E_BEGINVIEWUPDATE, HANDLER(DrawableProxy2D, HandleBeginViewUpdate));
  49. }
  50. DrawableProxy2D::~DrawableProxy2D()
  51. {
  52. }
  53. void DrawableProxy2D::RegisterObject(Context* context)
  54. {
  55. context->RegisterFactory<DrawableProxy2D>();
  56. }
  57. void DrawableProxy2D::UpdateBatches(const FrameInfo& frame)
  58. {
  59. unsigned count = batches_.Size();
  60. // Update non-thread critical parts of the source batches
  61. for (unsigned i = 0; i < count; ++i)
  62. {
  63. batches_[i].distance_ = 10.0f + (count - i) * 0.001f;
  64. batches_[i].worldTransform_ = &Matrix3x4::IDENTITY;
  65. }
  66. }
  67. void DrawableProxy2D::UpdateGeometry(const FrameInfo& frame)
  68. {
  69. // Fill index buffer
  70. if (indexBuffer_->GetIndexCount() < indexCount_)
  71. {
  72. bool largeIndices = indexCount_ > 0xffff;
  73. indexBuffer_->SetSize(indexCount_, largeIndices, true);
  74. void* buffer = indexBuffer_->Lock(0, indexCount_, true);
  75. if (buffer)
  76. {
  77. unsigned quadCount = indexCount_ / 6;
  78. if (largeIndices)
  79. {
  80. unsigned* dest = reinterpret_cast<unsigned*>(buffer);
  81. for (unsigned i = 0; i < quadCount; ++i)
  82. {
  83. unsigned base = i * 4;
  84. dest[0] = base;
  85. dest[1] = base + 1;
  86. dest[2] = base + 2;
  87. dest[3] = base;
  88. dest[4] = base + 2;
  89. dest[5] = base + 3;
  90. dest += 6;
  91. }
  92. }
  93. else
  94. {
  95. unsigned short* dest = reinterpret_cast<unsigned short*>(buffer);
  96. for (unsigned i = 0; i < quadCount; ++i)
  97. {
  98. unsigned base = i * 4;
  99. dest[0] = (unsigned short)(base);
  100. dest[1] = (unsigned short)(base + 1);
  101. dest[2] = (unsigned short)(base + 2);
  102. dest[3] = (unsigned short)(base);
  103. dest[4] = (unsigned short)(base + 2);
  104. dest[5] = (unsigned short)(base + 3);
  105. dest += 6;
  106. }
  107. }
  108. indexBuffer_->Unlock();
  109. }
  110. else
  111. {
  112. LOGERROR("Failed to lock index buffer");
  113. return;
  114. }
  115. }
  116. if (vertexBuffer_->GetVertexCount() < vertexCount_)
  117. vertexBuffer_->SetSize(vertexCount_, MASK_VERTEX2D);
  118. if (vertexCount_)
  119. {
  120. Vertex2D* dest = reinterpret_cast<Vertex2D*>(vertexBuffer_->Lock(0, vertexCount_, true));
  121. if (dest)
  122. {
  123. Material* material = 0;
  124. unsigned iStart = 0;
  125. unsigned iCount = 0;
  126. unsigned vStart = 0;
  127. unsigned vCount = 0;
  128. for (unsigned d = 0; d < drawables_.Size(); ++d)
  129. {
  130. if (!drawablesVisible_[d])
  131. continue;
  132. const Vector<Vertex2D>& vertices = drawables_[d]->GetVertices();
  133. for (unsigned i = 0; i < vertices.Size(); ++i)
  134. dest[i] = vertices[i];
  135. dest += vertices.Size();
  136. }
  137. vertexBuffer_->Unlock();
  138. }
  139. else
  140. LOGERROR("Failed to lock vertex buffer");
  141. }
  142. }
  143. UpdateGeometryType DrawableProxy2D::GetUpdateGeometryType()
  144. {
  145. return UPDATE_MAIN_THREAD;
  146. }
  147. void DrawableProxy2D::AddDrawable(Drawable2D* drawable)
  148. {
  149. if (!drawable)
  150. return;
  151. if (drawables_.Contains(drawable))
  152. return;
  153. drawables_.Push(drawable);
  154. orderDirty_ = true;
  155. }
  156. void DrawableProxy2D::RemoveDrawable(Drawable2D* drawable)
  157. {
  158. if (!drawable)
  159. return;
  160. drawables_.Remove(drawable);
  161. orderDirty_ = true;
  162. }
  163. void DrawableProxy2D::OnWorldBoundingBoxUpdate()
  164. {
  165. // Set a large dummy bounding box to ensure the proxy is rendered
  166. boundingBox_.Define(-M_LARGE_VALUE, M_LARGE_VALUE);
  167. worldBoundingBox_ = boundingBox_;
  168. }
  169. void DrawableProxy2D::HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData)
  170. {
  171. using namespace BeginViewUpdate;
  172. // Check that we are updating the correct scene
  173. if (GetScene() != eventData[P_SCENE].GetPtr())
  174. return;
  175. PROFILE(UpdateDrawableProxy2D);
  176. if (orderDirty_)
  177. {
  178. Sort(drawables_.Begin(), drawables_.End(), CompareDrawable2Ds);
  179. orderDirty_ = false;
  180. }
  181. vertexCount_ = 0;
  182. if (drawablesVisible_.Size() != drawables_.Size())
  183. drawablesVisible_.Resize(drawables_.Size());
  184. /// \todo We could add frustum culling, but that would be problematic if we have several viewports. Right now all Drawable2D's
  185. /// are always submitted for rendering
  186. for (unsigned i = 0; i < drawables_.Size(); ++i)
  187. {
  188. Material* usedMaterial = drawables_[i]->GetUsedMaterial();
  189. const Vector<Vertex2D>& vertices = drawables_[i]->GetVertices();
  190. if (drawables_[i]->GetUsedMaterial() && vertices.Size())
  191. {
  192. drawablesVisible_[i] = true;
  193. vertexCount_ += vertices.Size();
  194. }
  195. else
  196. drawablesVisible_[i] = false;
  197. }
  198. indexCount_ = vertexCount_ / 4 * 6;
  199. // Go through the drawables to form geometries & batches, but upload the actual vertex data later
  200. materials_.Clear();
  201. Material* material = 0;
  202. unsigned iStart = 0;
  203. unsigned iCount = 0;
  204. unsigned vStart = 0;
  205. unsigned vCount = 0;
  206. for (unsigned d = 0; d < drawables_.Size(); ++d)
  207. {
  208. if (!drawablesVisible_[d])
  209. continue;
  210. Material* usedMaterial = drawables_[d]->GetUsedMaterial();
  211. const Vector<Vertex2D>& vertices = drawables_[d]->GetVertices();
  212. if (material != usedMaterial)
  213. {
  214. if (material)
  215. {
  216. AddBatch(material, iStart, iCount, vStart, vCount);
  217. iStart += iCount;
  218. iCount = 0;
  219. vStart += vCount;
  220. vCount = 0;
  221. }
  222. material = usedMaterial;
  223. }
  224. iCount += vertices.Size() / 4 * 6;
  225. vCount += vertices.Size();
  226. }
  227. if (material)
  228. AddBatch(material, iStart, iCount, vStart, vCount);
  229. // Now the amount of batches is known. Build the part of source batches that are sensitive to threading issues
  230. // (material & geometry pointers)
  231. unsigned count = materials_.Size();
  232. batches_.Resize(count);
  233. for (unsigned i = 0; i < count; ++i)
  234. {
  235. batches_[i].material_ = materials_[i];
  236. batches_[i].geometry_ = geometries_[i];
  237. }
  238. }
  239. void DrawableProxy2D::AddBatch(Material* material, unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount)
  240. {
  241. if (!material || indexCount == 0 || vertexCount == 0)
  242. return;
  243. materials_.Push(SharedPtr<Material>(material));
  244. unsigned batchSize = materials_.Size();
  245. if (geometries_.Size() < batchSize)
  246. {
  247. SharedPtr<Geometry> geometry(new Geometry(context_));
  248. geometry->SetIndexBuffer(indexBuffer_);
  249. geometry->SetVertexBuffer(0, vertexBuffer_, MASK_VERTEX2D);
  250. geometries_.Push(geometry);
  251. }
  252. geometries_[batchSize - 1]->SetDrawRange(TRIANGLE_LIST, indexStart, indexCount, vertexStart, vertexCount, false);
  253. }
  254. }