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