| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- //
- // Copyright (c) 2008-2014 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #pragma once
- #include "Drawable.h"
- #include "MathDefs.h"
- #include "Matrix3x4.h"
- #include "Ptr.h"
- #include "Rect.h"
- namespace Urho3D
- {
- class Camera;
- class Drawable;
- class Geometry;
- class Light;
- class Material;
- class Matrix3x4;
- class Pass;
- class ShaderVariation;
- class Texture2D;
- class VertexBuffer;
- class View;
- class Zone;
- struct LightBatchQueue;
- /// Queued 3D geometry draw call.
- struct Batch
- {
- /// Construct with defaults.
- Batch() :
- lightQueue_(0),
- shaderParameters_(0),
- isBase_(false)
- {
- }
-
- /// Construct from a drawable's source batch.
- Batch(const SourceBatch& rhs) :
- distance_(rhs.distance_),
- geometry_(rhs.geometry_),
- material_(rhs.material_),
- worldTransform_(rhs.worldTransform_),
- numWorldTransforms_(rhs.numWorldTransforms_),
- lightQueue_(0),
- shaderParameters_(rhs.shaderParameters_),
- geometryType_(rhs.geometryType_),
- overrideView_(rhs.overrideView_),
- isBase_(false)
- {
- }
-
- /// Calculate state sorting key, which consists of base pass flag, light, pass and geometry.
- void CalculateSortKey();
- /// Prepare for rendering.
- void Prepare(View* view, bool setModelTransform = true) const;
- /// Prepare and draw.
- void Draw(View* view) const;
-
- /// State sorting key.
- unsigned long long sortKey_;
- /// Distance from camera.
- float distance_;
- /// Geometry.
- Geometry* geometry_;
- /// Material.
- Material* material_;
- /// World transform(s). For a skinned model, these are the bone transforms.
- const Matrix3x4* worldTransform_;
- /// Number of world transforms.
- unsigned numWorldTransforms_;
- /// Camera.
- Camera* camera_;
- /// Zone.
- Zone* zone_;
- /// Light properties.
- LightBatchQueue* lightQueue_;
- /// Material pass.
- Pass* pass_;
- /// Vertex shader.
- ShaderVariation* vertexShader_;
- /// Pixel shader.
- ShaderVariation* pixelShader_;
- /// Shader parameters.
- HashMap<StringHash, MaterialShaderParameter>* shaderParameters_;
- /// %Geometry type.
- GeometryType geometryType_;
- /// Override view transform flag. When set, the camera's view transform is replaced with an identity matrix.
- bool overrideView_;
- /// Base batch flag. This tells to draw the object fully without light optimizations.
- bool isBase_;
- /// 8-bit light mask for stencil marking in deferred rendering.
- unsigned char lightMask_;
- };
- /// Data for one geometry instance.
- struct InstanceData
- {
- /// Construct undefined.
- InstanceData()
- {
- }
-
- /// Construct with transform and distance.
- InstanceData(const Matrix3x4* worldTransform, float distance) :
- worldTransform_(worldTransform),
- distance_(distance),
- shaderParameters_()
- {
- }
-
- /// World transform.
- const Matrix3x4* worldTransform_;
- /// Distance from camera.
- float distance_;
- /// Shader parameters.
- HashMap<StringHash, MaterialShaderParameter>* shaderParameters_;
- };
- /// Instanced 3D geometry draw call.
- struct BatchGroup : public Batch
- {
- /// Construct with defaults.
- BatchGroup() :
- startIndex_(M_MAX_UNSIGNED),
- hasInstanceShaderParameters_(false)
- {
- }
-
- /// Construct from a batch.
- BatchGroup(const Batch& batch) :
- Batch(batch),
- startIndex_(M_MAX_UNSIGNED),
- hasInstanceShaderParameters_(false)
- {
- }
- /// Destruct.
- ~BatchGroup()
- {
- }
-
- /// Add batch instance(s).
- void AddBatchInstances(const Batch& batch)
- {
- InstanceData newInstance;
- newInstance.distance_ = batch.distance_;
- if (batch.shaderParameters_)
- {
- newInstance.shaderParameters_ = batch.shaderParameters_;
- hasInstanceShaderParameters_ = true;
- }
-
- for (unsigned i = 0; i < batch.numWorldTransforms_; ++i)
- {
- newInstance.worldTransform_ = &batch.worldTransform_[i];
- instances_.Push(newInstance);
- }
- }
-
- /// Pre-set the instance transforms. Buffer must be big enough to hold all transforms.
- void SetTransforms(void* lockedData, unsigned& freeIndex);
- /// Prepare and draw.
- void Draw(View* view) const;
-
- /// Instance data.
- PODVector<InstanceData> instances_;
- /// Instance stream start index, or M_MAX_UNSIGNED if transforms not pre-set.
- unsigned startIndex_;
- /// Has instance shader parameters flag;
- bool hasInstanceShaderParameters_;
- };
- /// Instanced draw call grouping key.
- struct BatchGroupKey
- {
- /// Construct undefined.
- BatchGroupKey()
- {
- }
-
- /// Construct from a batch.
- BatchGroupKey(const Batch& batch) :
- zone_(batch.zone_),
- lightQueue_(batch.lightQueue_),
- pass_(batch.pass_),
- material_(batch.material_),
- shaderParameters_(batch.shaderParameters_),
- geometry_(batch.geometry_)
- {
- }
-
- /// Zone.
- Zone* zone_;
- /// Light properties.
- LightBatchQueue* lightQueue_;
- /// Material pass.
- Pass* pass_;
- /// Material.
- Material* material_;
- /// Shader parameters.
- HashMap<StringHash, MaterialShaderParameter>* shaderParameters_;
- /// Geometry.
- Geometry* geometry_;
-
- /// Test for equality with another batch group key.
- bool operator == (const BatchGroupKey& rhs) const { return zone_ == rhs.zone_ && lightQueue_ == rhs.lightQueue_ && pass_ == rhs.pass_ && material_ == rhs.material_ && geometry_ == rhs.geometry_ && shaderParameters_ == rhs.shaderParameters_; }
- /// Test for inequality with another batch group key.
- bool operator != (const BatchGroupKey& rhs) const { return zone_ != rhs.zone_ || lightQueue_ != rhs.lightQueue_ || pass_ != rhs.pass_ || material_ != rhs.material_ || geometry_ != rhs.geometry_ || shaderParameters_ != rhs.shaderParameters_; }
-
- /// Return hash value.
- unsigned ToHash() const;
- };
- /// Queue that contains both instanced and non-instanced draw calls.
- struct BatchQueue
- {
- public:
- /// Clear for new frame by clearing all groups and batches.
- void Clear(int maxSortedInstances);
- /// Sort non-instanced draw calls back to front.
- void SortBackToFront();
- /// Sort instanced and non-instanced draw calls front to back.
- void SortFrontToBack();
- /// Sort batches front to back while also maintaining state sorting.
- void SortFrontToBack2Pass(PODVector<Batch*>& batches);
- /// Pre-set instance transforms of all groups. The vertex buffer must be big enough to hold all transforms.
- void SetTransforms(void* lockedData, unsigned& freeIndex);
- /// Draw.
- void Draw(View* view, bool markToStencil = false, bool usingLightOptimization = false) const;
- /// Return the combined amount of instances.
- unsigned GetNumInstances() const;
- /// Return whether the batch group is empty.
- bool IsEmpty() const { return batches_.Empty() && batchGroups_.Empty(); }
-
- /// Instanced draw calls.
- HashMap<BatchGroupKey, BatchGroup> batchGroups_;
- /// Shader remapping table for 2-pass state and distance sort.
- HashMap<unsigned, unsigned> shaderRemapping_;
- /// Material remapping table for 2-pass state and distance sort.
- HashMap<unsigned short, unsigned short> materialRemapping_;
- /// Geometry remapping table for 2-pass state and distance sort.
- HashMap<unsigned short, unsigned short> geometryRemapping_;
-
- /// Unsorted non-instanced draw calls.
- PODVector<Batch> batches_;
- /// Sorted non-instanced draw calls.
- PODVector<Batch*> sortedBatches_;
- /// Sorted instanced draw calls.
- PODVector<BatchGroup*> sortedBatchGroups_;
- /// Maximum sorted instances.
- unsigned maxSortedInstances_;
- };
- /// Queue for shadow map draw calls
- struct ShadowBatchQueue
- {
- /// Shadow map camera.
- Camera* shadowCamera_;
- /// Shadow map viewport.
- IntRect shadowViewport_;
- /// Shadow caster draw calls.
- BatchQueue shadowBatches_;
- /// Directional light cascade near split distance.
- float nearSplit_;
- /// Directional light cascade far split distance.
- float farSplit_;
- };
- /// Queue for light related draw calls.
- struct LightBatchQueue
- {
- /// Per-pixel light.
- Light* light_;
- /// Shadow map depth texture.
- Texture2D* shadowMap_;
- /// Lit geometry draw calls, base (replace blend mode)
- BatchQueue litBaseBatches_;
- /// Lit geometry draw calls, non-base (additive)
- BatchQueue litBatches_;
- /// Shadow map split queues.
- Vector<ShadowBatchQueue> shadowSplits_;
- /// Per-vertex lights.
- PODVector<Light*> vertexLights_;
- /// Light volume draw calls.
- PODVector<Batch> volumeBatches_;
- };
- }
|