DrawableProxy2D.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 "WorkQueue.h"
  38. #include "DebugNew.h"
  39. namespace Urho3D
  40. {
  41. DrawableProxy2D::DrawableProxy2D(Context* context) :
  42. Drawable(context, DRAWABLE_GEOMETRY),
  43. indexBuffer_(new IndexBuffer(context_)),
  44. vertexBuffer_(new VertexBuffer(context_)),
  45. orderDirty_(true),
  46. frustum_(0),
  47. indexCount_(0),
  48. vertexCount_(0)
  49. {
  50. SubscribeToEvent(E_BEGINVIEWUPDATE, HANDLER(DrawableProxy2D, HandleBeginViewUpdate));
  51. }
  52. DrawableProxy2D::~DrawableProxy2D()
  53. {
  54. }
  55. void DrawableProxy2D::RegisterObject(Context* context)
  56. {
  57. context->RegisterFactory<DrawableProxy2D>();
  58. }
  59. void DrawableProxy2D::UpdateBatches(const FrameInfo& frame)
  60. {
  61. unsigned count = batches_.Size();
  62. // Update non-thread critical parts of the source batches
  63. for (unsigned i = 0; i < count; ++i)
  64. {
  65. batches_[i].distance_ = 10.0f + (count - i) * 0.001f;
  66. batches_[i].worldTransform_ = &Matrix3x4::IDENTITY;
  67. }
  68. }
  69. void DrawableProxy2D::UpdateGeometry(const FrameInfo& frame)
  70. {
  71. // Fill index buffer
  72. if (indexBuffer_->GetIndexCount() < indexCount_)
  73. {
  74. bool largeIndices = vertexCount_ > 0xffff;
  75. indexBuffer_->SetSize(indexCount_, largeIndices, true);
  76. void* buffer = indexBuffer_->Lock(0, indexCount_, true);
  77. if (buffer)
  78. {
  79. unsigned quadCount = indexCount_ / 6;
  80. if (largeIndices)
  81. {
  82. unsigned* dest = reinterpret_cast<unsigned*>(buffer);
  83. for (unsigned i = 0; i < quadCount; ++i)
  84. {
  85. unsigned base = i * 4;
  86. dest[0] = base;
  87. dest[1] = base + 1;
  88. dest[2] = base + 2;
  89. dest[3] = base;
  90. dest[4] = base + 2;
  91. dest[5] = base + 3;
  92. dest += 6;
  93. }
  94. }
  95. else
  96. {
  97. unsigned short* dest = reinterpret_cast<unsigned short*>(buffer);
  98. for (unsigned i = 0; i < quadCount; ++i)
  99. {
  100. unsigned base = i * 4;
  101. dest[0] = (unsigned short)(base);
  102. dest[1] = (unsigned short)(base + 1);
  103. dest[2] = (unsigned short)(base + 2);
  104. dest[3] = (unsigned short)(base);
  105. dest[4] = (unsigned short)(base + 2);
  106. dest[5] = (unsigned short)(base + 3);
  107. dest += 6;
  108. }
  109. }
  110. indexBuffer_->Unlock();
  111. }
  112. else
  113. {
  114. LOGERROR("Failed to lock index buffer");
  115. return;
  116. }
  117. }
  118. if (vertexBuffer_->GetVertexCount() < vertexCount_)
  119. vertexBuffer_->SetSize(vertexCount_, MASK_VERTEX2D);
  120. if (vertexCount_)
  121. {
  122. Vertex2D* dest = reinterpret_cast<Vertex2D*>(vertexBuffer_->Lock(0, vertexCount_, true));
  123. if (dest)
  124. {
  125. Material* material = 0;
  126. unsigned iStart = 0;
  127. unsigned iCount = 0;
  128. unsigned vStart = 0;
  129. unsigned vCount = 0;
  130. for (unsigned d = 0; d < drawables_.Size(); ++d)
  131. {
  132. if (!drawables_[d]->GetVisibility())
  133. continue;
  134. const Vector<Vertex2D>& vertices = drawables_[d]->GetVertices();
  135. for (unsigned i = 0; i < vertices.Size(); ++i)
  136. dest[i] = vertices[i];
  137. dest += vertices.Size();
  138. }
  139. vertexBuffer_->Unlock();
  140. }
  141. else
  142. LOGERROR("Failed to lock vertex buffer");
  143. }
  144. }
  145. UpdateGeometryType DrawableProxy2D::GetUpdateGeometryType()
  146. {
  147. return UPDATE_MAIN_THREAD;
  148. }
  149. void DrawableProxy2D::AddDrawable(Drawable2D* drawable)
  150. {
  151. if (!drawable)
  152. return;
  153. if (drawables_.Contains(drawable))
  154. return;
  155. drawables_.Push(drawable);
  156. orderDirty_ = true;
  157. }
  158. void DrawableProxy2D::RemoveDrawable(Drawable2D* drawable)
  159. {
  160. if (!drawable)
  161. return;
  162. drawables_.Remove(drawable);
  163. orderDirty_ = true;
  164. }
  165. bool DrawableProxy2D::CheckVisibility(Drawable2D* drawable) const
  166. {
  167. const BoundingBox& box = drawable->GetWorldBoundingBox();
  168. if (frustum_)
  169. return frustum_->IsInsideFast(box) != OUTSIDE;
  170. return frustumBoundingBox_.IsInsideFast(box) != OUTSIDE;
  171. }
  172. void DrawableProxy2D::OnWorldBoundingBoxUpdate()
  173. {
  174. // Set a large dummy bounding box to ensure the proxy is rendered
  175. boundingBox_.Define(-M_LARGE_VALUE, M_LARGE_VALUE);
  176. worldBoundingBox_ = boundingBox_;
  177. }
  178. void CheckDrawableVisibility(const WorkItem* item, unsigned threadIndex)
  179. {
  180. DrawableProxy2D* proxy = reinterpret_cast<DrawableProxy2D*>(item->aux_);
  181. Drawable2D** start = reinterpret_cast<Drawable2D**>(item->start_);
  182. Drawable2D** end = reinterpret_cast<Drawable2D**>(item->end_);
  183. while (start != end)
  184. {
  185. Drawable2D* drawable = *start++;
  186. if (proxy->CheckVisibility(drawable) && drawable->GetUsedMaterial() && drawable->GetVertices().Size())
  187. drawable->SetVisibility(true);
  188. else
  189. drawable->SetVisibility(false);
  190. }
  191. }
  192. void DrawableProxy2D::HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData)
  193. {
  194. using namespace BeginViewUpdate;
  195. // Check that we are updating the correct scene
  196. if (GetScene() != eventData[P_SCENE].GetPtr())
  197. return;
  198. PROFILE(UpdateDrawableProxy2D);
  199. if (orderDirty_)
  200. {
  201. Sort(drawables_.Begin(), drawables_.End(), CompareDrawable2Ds);
  202. orderDirty_ = false;
  203. }
  204. Camera* camera = static_cast<Camera*>(eventData[P_CAMERA].GetPtr());
  205. frustum_ = &camera->GetFrustum();
  206. if (camera->IsOrthographic() && camera->GetNode()->GetWorldDirection() == Vector3::FORWARD)
  207. {
  208. // Define bounding box with min and max points
  209. frustumBoundingBox_.Define(frustum_->vertices_[2], frustum_->vertices_[4]);
  210. frustum_ = 0;
  211. }
  212. {
  213. PROFILE(CheckDrawableVisibility);
  214. WorkQueue* queue = GetSubsystem<WorkQueue>();
  215. int numWorkItems = queue->GetNumThreads() + 1; // Worker threads + main thread
  216. int drawablesPerItem = drawables_.Size() / numWorkItems;
  217. PODVector<Drawable2D*>::Iterator start = drawables_.Begin();
  218. for (int i = 0; i < numWorkItems; ++i)
  219. {
  220. SharedPtr<WorkItem> item = queue->GetFreeItem();
  221. item->priority_ = M_MAX_UNSIGNED;
  222. item->workFunction_ = CheckDrawableVisibility;
  223. item->aux_ = this;
  224. PODVector<Drawable2D*>::Iterator end = drawables_.End();
  225. if (i < numWorkItems - 1 && end - start > drawablesPerItem)
  226. end = start + drawablesPerItem;
  227. item->start_ = &(*start);
  228. item->end_ = &(*end);
  229. queue->AddWorkItem(item);
  230. start = end;
  231. }
  232. queue->Complete(M_MAX_UNSIGNED);
  233. }
  234. vertexCount_ = 0;
  235. for (unsigned i = 0; i < drawables_.Size(); ++i)
  236. {
  237. if (drawables_[i]->GetVisibility())
  238. vertexCount_ += drawables_[i]->GetVertices().Size();
  239. }
  240. indexCount_ = vertexCount_ / 4 * 6;
  241. // Go through the drawables to form geometries & batches, but upload the actual vertex data later
  242. materials_.Clear();
  243. Material* material = 0;
  244. unsigned iStart = 0;
  245. unsigned iCount = 0;
  246. unsigned vStart = 0;
  247. unsigned vCount = 0;
  248. for (unsigned d = 0; d < drawables_.Size(); ++d)
  249. {
  250. if (!drawables_[d]->GetVisibility())
  251. continue;
  252. Material* usedMaterial = drawables_[d]->GetUsedMaterial();
  253. const Vector<Vertex2D>& vertices = drawables_[d]->GetVertices();
  254. if (material != usedMaterial)
  255. {
  256. if (material)
  257. {
  258. AddBatch(material, iStart, iCount, vStart, vCount);
  259. iStart += iCount;
  260. iCount = 0;
  261. vStart += vCount;
  262. vCount = 0;
  263. }
  264. material = usedMaterial;
  265. }
  266. iCount += vertices.Size() / 4 * 6;
  267. vCount += vertices.Size();
  268. }
  269. if (material)
  270. AddBatch(material, iStart, iCount, vStart, vCount);
  271. // Now the amount of batches is known. Build the part of source batches that are sensitive to threading issues
  272. // (material & geometry pointers)
  273. unsigned count = materials_.Size();
  274. batches_.Resize(count);
  275. for (unsigned i = 0; i < count; ++i)
  276. {
  277. batches_[i].material_ = materials_[i];
  278. batches_[i].geometry_ = geometries_[i];
  279. }
  280. }
  281. void DrawableProxy2D::AddBatch(Material* material, unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount)
  282. {
  283. if (!material || indexCount == 0 || vertexCount == 0)
  284. return;
  285. materials_.Push(SharedPtr<Material>(material));
  286. unsigned batchSize = materials_.Size();
  287. if (geometries_.Size() < batchSize)
  288. {
  289. SharedPtr<Geometry> geometry(new Geometry(context_));
  290. geometry->SetIndexBuffer(indexBuffer_);
  291. geometry->SetVertexBuffer(0, vertexBuffer_, MASK_VERTEX2D);
  292. geometries_.Push(geometry);
  293. }
  294. geometries_[batchSize - 1]->SetDrawRange(TRIANGLE_LIST, indexStart, indexCount, vertexStart, vertexCount, false);
  295. }
  296. }