DrawableProxy2D.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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_PROXYGEOMETRY),
  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);
  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. for (unsigned d = 0; d < drawables_.Size(); ++d)
  126. {
  127. if (!drawables_[d]->GetVisibility())
  128. continue;
  129. const Vector<Vertex2D>& vertices = drawables_[d]->GetVertices();
  130. for (unsigned i = 0; i < vertices.Size(); ++i)
  131. dest[i] = vertices[i];
  132. dest += vertices.Size();
  133. }
  134. vertexBuffer_->Unlock();
  135. }
  136. else
  137. LOGERROR("Failed to lock vertex buffer");
  138. }
  139. }
  140. UpdateGeometryType DrawableProxy2D::GetUpdateGeometryType()
  141. {
  142. return UPDATE_MAIN_THREAD;
  143. }
  144. void DrawableProxy2D::AddDrawable(Drawable2D* drawable)
  145. {
  146. if (!drawable)
  147. return;
  148. if (drawables_.Contains(drawable))
  149. return;
  150. drawables_.Push(drawable);
  151. orderDirty_ = true;
  152. }
  153. void DrawableProxy2D::RemoveDrawable(Drawable2D* drawable)
  154. {
  155. if (!drawable)
  156. return;
  157. drawables_.Remove(drawable);
  158. orderDirty_ = true;
  159. }
  160. bool DrawableProxy2D::CheckVisibility(Drawable2D* drawable) const
  161. {
  162. const BoundingBox& box = drawable->GetWorldBoundingBox();
  163. if (frustum_)
  164. return frustum_->IsInsideFast(box) != OUTSIDE;
  165. return frustumBoundingBox_.IsInsideFast(box) != OUTSIDE;
  166. }
  167. void DrawableProxy2D::OnWorldBoundingBoxUpdate()
  168. {
  169. // Set a large dummy bounding box to ensure the proxy is rendered
  170. boundingBox_.Define(-M_LARGE_VALUE, M_LARGE_VALUE);
  171. worldBoundingBox_ = boundingBox_;
  172. }
  173. void CheckDrawableVisibility(const WorkItem* item, unsigned threadIndex)
  174. {
  175. DrawableProxy2D* proxy = reinterpret_cast<DrawableProxy2D*>(item->aux_);
  176. Drawable2D** start = reinterpret_cast<Drawable2D**>(item->start_);
  177. Drawable2D** end = reinterpret_cast<Drawable2D**>(item->end_);
  178. while (start != end)
  179. {
  180. Drawable2D* drawable = *start++;
  181. if (proxy->CheckVisibility(drawable) && drawable->GetUsedMaterial() && drawable->GetVertices().Size())
  182. drawable->SetVisibility(true);
  183. else
  184. drawable->SetVisibility(false);
  185. }
  186. }
  187. void DrawableProxy2D::HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData)
  188. {
  189. using namespace BeginViewUpdate;
  190. // Check that we are updating the correct scene
  191. if (GetScene() != eventData[P_SCENE].GetPtr())
  192. return;
  193. PROFILE(UpdateDrawableProxy2D);
  194. if (orderDirty_)
  195. {
  196. Sort(drawables_.Begin(), drawables_.End(), CompareDrawable2Ds);
  197. orderDirty_ = false;
  198. }
  199. Camera* camera = static_cast<Camera*>(eventData[P_CAMERA].GetPtr());
  200. frustum_ = &camera->GetFrustum();
  201. if (camera->IsOrthographic() && camera->GetNode()->GetWorldDirection() == Vector3::FORWARD)
  202. {
  203. // Define bounding box with min and max points
  204. frustumBoundingBox_.Define(frustum_->vertices_[2], frustum_->vertices_[4]);
  205. frustum_ = 0;
  206. }
  207. {
  208. PROFILE(CheckDrawableVisibility);
  209. WorkQueue* queue = GetSubsystem<WorkQueue>();
  210. int numWorkItems = queue->GetNumThreads() + 1; // Worker threads + main thread
  211. int drawablesPerItem = drawables_.Size() / numWorkItems;
  212. PODVector<Drawable2D*>::Iterator start = drawables_.Begin();
  213. for (int i = 0; i < numWorkItems; ++i)
  214. {
  215. SharedPtr<WorkItem> item = queue->GetFreeItem();
  216. item->priority_ = M_MAX_UNSIGNED;
  217. item->workFunction_ = CheckDrawableVisibility;
  218. item->aux_ = this;
  219. PODVector<Drawable2D*>::Iterator end = drawables_.End();
  220. if (i < numWorkItems - 1 && end - start > drawablesPerItem)
  221. end = start + drawablesPerItem;
  222. item->start_ = &(*start);
  223. item->end_ = &(*end);
  224. queue->AddWorkItem(item);
  225. start = end;
  226. }
  227. queue->Complete(M_MAX_UNSIGNED);
  228. }
  229. vertexCount_ = 0;
  230. for (unsigned i = 0; i < drawables_.Size(); ++i)
  231. {
  232. if (drawables_[i]->GetVisibility())
  233. vertexCount_ += drawables_[i]->GetVertices().Size();
  234. }
  235. indexCount_ = vertexCount_ / 4 * 6;
  236. // Go through the drawables to form geometries & batches, but upload the actual vertex data later
  237. materials_.Clear();
  238. Material* material = 0;
  239. unsigned iStart = 0;
  240. unsigned iCount = 0;
  241. unsigned vStart = 0;
  242. unsigned vCount = 0;
  243. for (unsigned d = 0; d < drawables_.Size(); ++d)
  244. {
  245. if (!drawables_[d]->GetVisibility())
  246. continue;
  247. Material* usedMaterial = drawables_[d]->GetUsedMaterial();
  248. const Vector<Vertex2D>& vertices = drawables_[d]->GetVertices();
  249. if (material != usedMaterial)
  250. {
  251. if (material)
  252. {
  253. AddBatch(material, iStart, iCount, vStart, vCount);
  254. iStart += iCount;
  255. iCount = 0;
  256. vStart += vCount;
  257. vCount = 0;
  258. }
  259. material = usedMaterial;
  260. }
  261. iCount += vertices.Size() / 4 * 6;
  262. vCount += vertices.Size();
  263. }
  264. if (material)
  265. AddBatch(material, iStart, iCount, vStart, vCount);
  266. // Now the amount of batches is known. Build the part of source batches that are sensitive to threading issues
  267. // (material & geometry pointers)
  268. unsigned count = materials_.Size();
  269. batches_.Resize(count);
  270. for (unsigned i = 0; i < count; ++i)
  271. {
  272. batches_[i].material_ = materials_[i];
  273. batches_[i].geometry_ = geometries_[i];
  274. }
  275. }
  276. void DrawableProxy2D::AddBatch(Material* material, unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount)
  277. {
  278. if (!material || indexCount == 0 || vertexCount == 0)
  279. return;
  280. materials_.Push(SharedPtr<Material>(material));
  281. unsigned batchSize = materials_.Size();
  282. if (geometries_.Size() < batchSize)
  283. {
  284. SharedPtr<Geometry> geometry(new Geometry(context_));
  285. geometry->SetIndexBuffer(indexBuffer_);
  286. geometry->SetVertexBuffer(0, vertexBuffer_, MASK_VERTEX2D);
  287. geometries_.Push(geometry);
  288. }
  289. geometries_[batchSize - 1]->SetDrawRange(TRIANGLE_LIST, indexStart, indexCount, vertexStart, vertexCount, false);
  290. }
  291. }