DrawableProxy2D.cpp 9.3 KB

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