Skip to content

Commit

Permalink
Move BatchGroup to material template compilation.
Browse files Browse the repository at this point in the history
There is no longer a need to build a table of unique batch IDs since we now compile our materials. This also simplifies the batch lookup process and debugging slightly.
  • Loading branch information
Duttenheim committed May 7, 2024
1 parent 0895e44 commit 32a238d
Show file tree
Hide file tree
Showing 20 changed files with 102 additions and 347 deletions.
54 changes: 25 additions & 29 deletions code/foundation/util/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ namespace Math
namespace Util
{
class Blob;

class String
{
public:
Expand Down Expand Up @@ -353,6 +352,8 @@ class String
static String FromBase64(const String&);
/// convert from "anything"
template<typename T> static String From(const T& t);
/// Hash a string
static constexpr uint Hash(const char* c, std::size_t s);

/// construct a hex string from an int
template<typename INTEGER>
Expand Down Expand Up @@ -691,20 +692,8 @@ String::IsValid() const
inline uint32_t
String::HashCode() const
{
uint32_t hash = 0;
const char* ptr = this->AsCharPtr();
SizeT len = this->strLen;
SizeT i;
for (i = 0; i < len; i++)
{
hash += ptr[i];
hash += hash << 10;
hash ^= hash >> 6;
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
return Hash(ptr, this->strLen);
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -1190,6 +1179,27 @@ String::AppendMat4(const Math::mat4& val)
this->Append(FromMat4(val));
}
#endif


//------------------------------------------------------------------------------
/**
*/
constexpr uint
String::Hash(const char* c, std::size_t s)
{
uint hash = 0;
std::size_t i;
for (i = 0; i < s; i++)
{
hash += c[i];
hash += hash << 10;
hash ^= hash >> 6;
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}

inline auto Format = &String::Sprintf;

Expand All @@ -1209,22 +1219,8 @@ Util::String operator ""_str(const char* c, std::size_t s);
constexpr uint
operator ""_hash(const char* c, std::size_t s)
{
uint hash = 0;
const char* ptr = c;
std::size_t len = s;
std::size_t i;
for (i = 0; i < len; i++)
{
hash += ptr[i];
hash += hash << 10;
hash ^= hash >> 6;
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
return Util::String::Hash(c, s);
}


//------------------------------------------------------------------------------

2 changes: 0 additions & 2 deletions code/render/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ ENDIF()
antialiasquality.cc
antialiasquality.h
barrier.h
batchgroup.cc
batchgroup.h
buffer.cc
buffer.h
gpubuffertypes.h
Expand Down
57 changes: 0 additions & 57 deletions code/render/coregraphics/batchgroup.cc

This file was deleted.

59 changes: 0 additions & 59 deletions code/render/coregraphics/batchgroup.h

This file was deleted.

10 changes: 5 additions & 5 deletions code/render/frame/framescriptloader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1138,9 +1138,9 @@ void
FrameScriptLoader::ParseSubpassBatch(const Ptr<Frame::FrameScript>& script, Frame::FrameSubpass* subpass, JzonValue* node)
{
JzonValue* name = jzon_get(node, "name");
CoreGraphics::BatchGroup::Code batch = CoreGraphics::BatchGroup::FromName(name->string_value);
MaterialTemplates::BatchGroup batch = MaterialTemplates::BatchGroupFromName(name->string_value);
n_assert(name != nullptr);
if (MaterialTemplates::Configs.FindIndex(batch) == InvalidIndex)
if (batch == MaterialTemplates::BatchGroup::Invalid)
{
n_warning("[FrameScript] - Skipping, batch with name '%s' because it's not rendered by any material\n", name->string_value);
return;
Expand All @@ -1158,7 +1158,7 @@ FrameScriptLoader::ParseSubpassBatch(const Ptr<Frame::FrameScript>& script, Fram
ParseResourceDependencies(script, op, inputs);
}

op->batch = CoreGraphics::BatchGroup::FromName(name->string_value);
op->batch = batch;
subpass->AddChild(op);
}

Expand All @@ -1169,9 +1169,9 @@ void
FrameScriptLoader::ParseSubpassSortedBatch(const Ptr<Frame::FrameScript>& script, Frame::FrameSubpass* subpass, JzonValue* node)
{
JzonValue* name = jzon_get(node, "name");
CoreGraphics::BatchGroup::Code batch = CoreGraphics::BatchGroup::FromName(name->string_value);
MaterialTemplates::BatchGroup batch = MaterialTemplates::BatchGroupFromName(name->string_value);
n_assert(name != nullptr);
if (MaterialTemplates::Configs.FindIndex(batch) == InvalidIndex)
if (batch == MaterialTemplates::BatchGroup::Invalid)
{
n_warning("Skipping, batch with name '%s' because it's not rendered by any material\n", name->string_value);
return;
Expand Down
8 changes: 4 additions & 4 deletions code/render/frame/framesubpassbatch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ FrameSubpassBatch::AllocCompiled(Memory::ArenaAllocator<BIG_CHUNK>& allocator)
/**
*/
void
FrameSubpassBatch::DrawBatch(const CoreGraphics::CmdBufferId cmdBuf, CoreGraphics::BatchGroup::Code batch, const Graphics::GraphicsEntityId id, const IndexT bufferIndex)
FrameSubpassBatch::DrawBatch(const CoreGraphics::CmdBufferId cmdBuf, MaterialTemplates::BatchGroup batch, const Graphics::GraphicsEntityId id, const IndexT bufferIndex)
{
// get current view and visibility draw list
const Visibility::ObserverContext::VisibilityDrawList* drawList = Visibility::ObserverContext::GetVisibilityDrawList(id);
const Util::Array<MaterialTemplates::Entry*>& types = MaterialTemplates::Configs[batch];
const Util::Array<MaterialTemplates::Entry*>& types = MaterialTemplates::Configs[(uint)batch];
if (types.Size() != 0 && (drawList != nullptr))
{
for (IndexT typeIdx = 0; typeIdx < types.Size(); typeIdx++)
Expand Down Expand Up @@ -153,11 +153,11 @@ FrameSubpassBatch::DrawBatch(const CoreGraphics::CmdBufferId cmdBuf, CoreGraphic
/**
*/
void
FrameSubpassBatch::DrawBatch(const CoreGraphics::CmdBufferId cmdBuf, CoreGraphics::BatchGroup::Code batch, const Graphics::GraphicsEntityId id, const SizeT numInstances, const IndexT baseInstance, const IndexT bufferIndex)
FrameSubpassBatch::DrawBatch(const CoreGraphics::CmdBufferId cmdBuf, MaterialTemplates::BatchGroup batch, const Graphics::GraphicsEntityId id, const SizeT numInstances, const IndexT baseInstance, const IndexT bufferIndex)
{
// get current view and visibility draw list
const Visibility::ObserverContext::VisibilityDrawList* drawList = Visibility::ObserverContext::GetVisibilityDrawList(id);
const Util::Array<MaterialTemplates::Entry*>& types = MaterialTemplates::Configs[batch];
const Util::Array<MaterialTemplates::Entry*>& types = MaterialTemplates::Configs[(uint)batch];
if (types.Size() != 0 && (drawList != nullptr))
{
for (IndexT typeIdx = 0; typeIdx < types.Size(); typeIdx++)
Expand Down
10 changes: 5 additions & 5 deletions code/render/frame/framesubpassbatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
//------------------------------------------------------------------------------
#include "frameop.h"
#include "coregraphics/batchgroup.h"
#include "materials/materialtemplates.h"
#include "graphics/graphicsentity.h"
namespace Frame
{
Expand All @@ -24,16 +24,16 @@ class FrameSubpassBatch : public FrameOp
{
void Run(const CoreGraphics::CmdBufferId cmdBuf, const IndexT frameIndex, const IndexT bufferIndex) override;

CoreGraphics::BatchGroup::Code batch;
MaterialTemplates::BatchGroup batch;
};

FrameOp::Compiled* AllocCompiled(Memory::ArenaAllocator<BIG_CHUNK>& allocator);
CoreGraphics::BatchGroup::Code batch;
MaterialTemplates::BatchGroup batch;

/// Do the actual drawing
static void DrawBatch(const CoreGraphics::CmdBufferId cmdBuf, CoreGraphics::BatchGroup::Code batch, const Graphics::GraphicsEntityId id, const IndexT bufferIndex);
static void DrawBatch(const CoreGraphics::CmdBufferId cmdBuf, MaterialTemplates::BatchGroup batch, const Graphics::GraphicsEntityId id, const IndexT bufferIndex);
/// Do the actual drawing, but with duplicate instances
static void DrawBatch(const CoreGraphics::CmdBufferId cmdBuf, CoreGraphics::BatchGroup::Code batch, const Graphics::GraphicsEntityId id, const SizeT numInstances, const IndexT baseInstance, const IndexT bufferIndex);
static void DrawBatch(const CoreGraphics::CmdBufferId cmdBuf, MaterialTemplates::BatchGroup batch, const Graphics::GraphicsEntityId id, const SizeT numInstances, const IndexT baseInstance, const IndexT bufferIndex);
};

} // namespace Frame2
5 changes: 2 additions & 3 deletions code/render/frame/framesubpassorderedbatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
//------------------------------------------------------------------------------
#include "frameop.h"
#include "coregraphics/batchgroup.h"
namespace Frame
{
class FrameSubpassOrderedBatch : public FrameOp
Expand All @@ -24,12 +23,12 @@ class FrameSubpassOrderedBatch : public FrameOp
{
void Run(const CoreGraphics::CmdBufferId cmdBuf, const IndexT frameIndex, const IndexT bufferIndex) override;

CoreGraphics::BatchGroup::Code batch;
MaterialTemplates::BatchGroup batch;
};

FrameOp::Compiled* AllocCompiled(Memory::ArenaAllocator<BIG_CHUNK>& allocator);

CoreGraphics::BatchGroup::Code batch;
MaterialTemplates::BatchGroup batch;
};

} // namespace Frame2
3 changes: 0 additions & 3 deletions code/render/graphics/graphicsserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "framesync/framesynctimer.h"
#include "util/stringatom.h"
#include "ids/idgenerationpool.h"
#include "coregraphics/batchgroup.h"
#include "stage.h"
#include "graphicsentity.h"
#include "coregraphics/graphicsdevice.h"
Expand Down Expand Up @@ -123,7 +122,6 @@ class GraphicsServer : public Core::RefCounted
void OnWindowResized(CoreGraphics::WindowId wndId);

private:
friend class CoreGraphics::BatchGroup;

Ids::IdGenerationPool entityPool;

Expand All @@ -136,7 +134,6 @@ class GraphicsServer : public Core::RefCounted

Util::Array<Ptr<Stage>> stages;
Util::Array<Ptr<View>> views;
CoreGraphics::BatchGroup batchGroupRegistry;
Ptr<View> currentView;

Ptr<CoreGraphics::DisplayDevice> displayDevice;
Expand Down
9 changes: 4 additions & 5 deletions code/render/lighting/lightcontext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ struct
CoreGraphics::TextureId terrainShadowMap = CoreGraphics::InvalidTextureId;
uint terrainShadowMapSize;
uint terrainSize;
CoreGraphics::BatchGroup::Code spotlightsBatchCode;
CoreGraphics::BatchGroup::Code globalLightsBatchCode;
MaterialTemplates::BatchGroup spotlightsBatchCode;
MaterialTemplates::BatchGroup globalLightsBatchCode;

CSMUtil csmUtil{ Shared::NUM_CASCADES };

Expand Down Expand Up @@ -153,8 +153,8 @@ LightContext::Create(const Ptr<Frame::FrameScript>& frameScript)
#endif

// create shadow mapping frame script
lightServerState.spotlightsBatchCode = CoreGraphics::BatchGroup::FromName("SpotLightShadow");
lightServerState.globalLightsBatchCode = CoreGraphics::BatchGroup::FromName("GlobalShadow");
lightServerState.spotlightsBatchCode = MaterialTemplates::BatchGroupFromName("SpotLightShadow");
lightServerState.globalLightsBatchCode = MaterialTemplates::BatchGroupFromName("GlobalShadow");
lightServerState.globalLightShadowMap = frameScript->GetTexture("SunShadowDepth");
lightServerState.localLightShadows = frameScript->GetTexture("LocalLightShadow");
if (lightServerState.terrainShadowMap == CoreGraphics::InvalidTextureId)
Expand Down Expand Up @@ -580,7 +580,6 @@ LightContext::SetupAreaLight(

const MaterialTemplates::MaterialTemplateValue& value = MaterialTemplates::base::__AreaLight.__EmissiveColor;
void* mem = Materials::MaterialLoader::AllocateConstantMemory(value.GetSize());
const Materials::ShaderConfigBatchConstant* batchConstant = &MaterialTemplates::base::__AreaLight.__LightMeshes_EmissiveColor;

MaterialInterfaces::ArealightMaterial* data = (MaterialInterfaces::ArealightMaterial*)StackAlloc(matTemplate->bufferSize);
(color * intensity).store(data->EmissiveColor);
Expand Down
4 changes: 2 additions & 2 deletions code/render/materials/material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ MaterialGetTemplate(const MaterialId mat)
/**
*/
const Materials::BatchIndex
MaterialGetBatchIndex(const MaterialId mat, const CoreGraphics::BatchGroup::Code code)
MaterialGetBatchIndex(const MaterialId mat, const MaterialTemplates::BatchGroup batch)
{
return materialAllocator.Get<Material_Template>(mat.id)->passes[code]->index;
return materialAllocator.Get<Material_Template>(mat.id)->passes[batch]->index;
}

//------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 32a238d

Please sign in to comment.