Skip to content

Commit

Permalink
Pass by ref, store by ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
sndels committed Nov 9, 2024
1 parent 12f7673 commit 57182ab
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 262 deletions.
6 changes: 3 additions & 3 deletions src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ void App::init(ScopedScratch scopeAlloc)
vk::BufferUsageFlagBits::eStorageBuffer,
asserted_cast<uint32_t>(kilobytes(16)), "ConstantsRing");

m_cam->init(scopeAlloc.child_scope(), &m_constantsRing);
m_cam->init(scopeAlloc.child_scope(), m_constantsRing);

m_world->init(scopeAlloc.child_scope(), &m_constantsRing, m_scenePath);
m_world->init(scopeAlloc.child_scope(), m_constantsRing, m_scenePath);

m_renderer->init(
scopeAlloc.child_scope(), m_swapchain->config(),
Expand Down Expand Up @@ -506,7 +506,7 @@ void App::drawFrame(ScopedScratch scopeAlloc, uint32_t scopeHighWatermark)
SceneStats &sceneStats = m_sceneStats[nextFrame];
sceneStats = {};
m_world->updateScene(
scopeAlloc.child_scope(), &m_sceneCameraTransform, &sceneStats);
scopeAlloc.child_scope(), m_sceneCameraTransform, sceneStats);

m_world->uploadMeshDatas(scopeAlloc.child_scope(), nextFrame);

Expand Down
2 changes: 1 addition & 1 deletion src/gfx/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,7 @@ std::filesystem::path Device::updateShaderCache(
try
{
expandIncludes(
alloc, sourcePath, topLevelSource, &fullSource, &uniqueIncludes, 0);
alloc, sourcePath, topLevelSource, fullSource, uniqueIncludes, 0);
}
catch (const std::exception &e)
{
Expand Down
53 changes: 23 additions & 30 deletions src/gfx/ShaderIncludes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ const StrSpan sLinePrefix{sLinePrefixCStr};
Pair<std::filesystem::path, String> getInclude(
Allocator &alloc, const std::filesystem::path &requestingSource,
StrSpan requestedSourceRelative,
HashSet<std::filesystem::path> *uniqueIncludes)
HashSet<std::filesystem::path> &uniqueIncludes)
{
WHEELS_ASSERT(uniqueIncludes != nullptr);

const std::filesystem::path requestingDir = requestingSource.parent_path();
const std::filesystem::path requestedSource =
(requestingDir /
Expand All @@ -39,7 +37,7 @@ Pair<std::filesystem::path, String> getInclude(
std::string{"Could not find '"} + requestedSource.generic_string() +
'\'');

uniqueIncludes->insert(requestedSource);
uniqueIncludes.insert(requestedSource);

String content = readFileString(alloc, requestedSource);

Expand Down Expand Up @@ -142,38 +140,33 @@ uint32_t parseLineNumber(StrSpan span)
}

StrSpan parseIncludePath(
StrSpan span, size_t *includePathStart, size_t *includePathLength)
StrSpan span, size_t &includePathStart, size_t &includePathLength)
{
WHEELS_ASSERT(span.starts_with(sIncludePrefix));
WHEELS_ASSERT(includePathStart != nullptr);
WHEELS_ASSERT(includePathLength != nullptr);

// Parse the include path
const Optional<size_t> includePathFrontQuatation = span.find_first('"');
if (!includePathFrontQuatation.has_value())
throw std::runtime_error("Parser expects relative paths.");
*includePathStart = *includePathFrontQuatation + 1;
includePathStart = *includePathFrontQuatation + 1;

const Optional<size_t> includePathNextQuotation =
StrSpan{&span[*includePathStart + 1], span.size() - *includePathStart}
StrSpan{&span[includePathStart + 1], span.size() - includePathStart}
.find_first('"');
if (!includePathNextQuotation.has_value())
throw std::runtime_error("Parser expects relative paths.");
*includePathLength = *includePathNextQuotation + 1;
includePathLength = *includePathNextQuotation + 1;

return StrSpan{&span[*includePathStart], *includePathLength};
return StrSpan{&span[includePathStart], includePathLength};
}

} // namespace

void expandIncludes(
Allocator &alloc, const std::filesystem::path &currentPath,
StrSpan currentSource, String *fullSource,
HashSet<std::filesystem::path> *uniqueIncludes, size_t includeDepth)
StrSpan currentSource, String &fullSource,
HashSet<std::filesystem::path> &uniqueIncludes, size_t includeDepth)
{
WHEELS_ASSERT(fullSource != nullptr);
WHEELS_ASSERT(uniqueIncludes != nullptr);

if (includeDepth > 100)
throw std::runtime_error(
currentPath.generic_string() +
Expand Down Expand Up @@ -236,7 +229,7 @@ void expandIncludes(
// Reached the end so copy the remaning span
const StrSpan frontSpan{
&currentSource[frontCursor], backCursor - frontCursor};
fullSource->extend(frontSpan);
fullSource.extend(frontSpan);
frontCursor = backCursor;
break;
}
Expand Down Expand Up @@ -271,15 +264,15 @@ void expandIncludes(
// Let's copy what's between the cursors before the include
const StrSpan frontSpan{
&currentSource[frontCursor], backCursor - frontCursor};
fullSource->extend(frontSpan);
fullSource.extend(frontSpan);

size_t includePathStart = 0;
size_t includePathLength = 0;
Optional<Pair<std::filesystem::path, String>> include;
try
{
const StrSpan includeRelPath = parseIncludePath(
tailSpan, &includePathStart, &includePathLength);
const StrSpan includeRelPath =
parseIncludePath(tailSpan, includePathStart, includePathLength);

include =
getInclude(alloc, currentPath, includeRelPath, uniqueIncludes);
Expand All @@ -297,10 +290,10 @@ void expandIncludes(
genericIncludePath.data(), genericIncludePath.size()};

// Tag include source for error reporting
fullSource->extend("\n#line 1 \"");
fullSource->extend(genericIncludeSpan);
fullSource->push_back('"');
fullSource->push_back('\n');
fullSource.extend("\n#line 1 \"");
fullSource.extend(genericIncludeSpan);
fullSource.push_back('"');
fullSource.push_back('\n');

expandIncludes(
alloc, include->first.string().c_str(), include->second, fullSource,
Expand All @@ -311,12 +304,12 @@ void expandIncludes(
snprintf(lineNumberStr.data(), 7, "%u", lineNumber + 1);

// Tag current source for error reporting
fullSource->extend("\n#line ");
fullSource->extend(lineNumberStr.data());
fullSource->push_back(' ');
fullSource->push_back('"');
fullSource->extend(genericCurrentSpan);
fullSource->push_back('"');
fullSource.extend("\n#line ");
fullSource.extend(lineNumberStr.data());
fullSource.push_back(' ');
fullSource.push_back('"');
fullSource.extend(genericCurrentSpan);
fullSource.push_back('"');
// No newline as we don't skip the one after the include directive

// Move cursors past the include path
Expand Down
4 changes: 2 additions & 2 deletions src/gfx/ShaderIncludes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

void expandIncludes(
wheels::Allocator &alloc, const std::filesystem::path &currentPath,
wheels::StrSpan currentSource, wheels::String *fullSource,
wheels::HashSet<std::filesystem::path> *uniqueIncludes,
wheels::StrSpan currentSource, wheels::String &fullSource,
wheels::HashSet<std::filesystem::path> &uniqueIncludes,
size_t includeDepth);

#endif // PROSPER_GFX_SHADER_INCLUDES_HPP
28 changes: 11 additions & 17 deletions src/gfx/ShaderReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ struct SpvResult
const size_t firstOpOffset = 5;

void firstPass(
Allocator &alloc, const uint32_t *words, size_t wordCount,
Span<SpvResult> results, uint32_t &pushConstantMetadataId)
Allocator &alloc, Span<const uint32_t> words, Span<SpvResult> results,
uint32_t &pushConstantMetadataId)
{
// Collect names and types
size_t opFirstWord = firstOpOffset;
while (opFirstWord < wordCount)
while (opFirstWord < words.size())
{
const uint16_t opWordCount =
static_cast<uint16_t>(words[opFirstWord] >> 16);
Expand Down Expand Up @@ -368,12 +368,11 @@ void firstPass(
}
}

void secondPass(
const uint32_t *words, size_t wordCount, Span<SpvResult> results)
void secondPass(Span<const uint32_t> words, Span<SpvResult> results)
{
// Collect decorations
size_t opFirstWord = firstOpOffset;
while (opFirstWord < wordCount)
while (opFirstWord < words.size())
{
const uint16_t opWordCount =
static_cast<uint16_t>(words[opFirstWord] >> 16);
Expand Down Expand Up @@ -814,19 +813,16 @@ void ShaderReflection::init(
for (const std::filesystem::path &include : sourceFiles)
m_sourceFiles.insert(include);

const uint32_t *words = spvWords.data();
const size_t wordCount = spvWords.size();

const uint32_t spvMagic = 0x0723'0203;
if (words[0] != spvMagic)
if (spvWords[0] != spvMagic)
throw std::runtime_error(
"Tried to read reflection from invalid SPIR-V words");

// bytes 0 | major | minor | 0, 0x0001'0300 is 1.3
// const uint32_t version = words[1];
// const uint32_t generatorMagic = words[2];
const uint32_t idBound = words[3];
// const uint32_t schema = words[4];
// const uint32_t generatorMagic = spvWords[2];
const uint32_t idBound = spvWords[3];
// const uint32_t schema = spvWords[4];

Array<SpvResult> results{scopeAlloc};
results.resize(idBound);
Expand All @@ -835,10 +831,8 @@ void ShaderReflection::init(

// Run in two passes because type definitons come after decorations.
// Data relations are simpler this way.
firstPass(
scopeAlloc, words, wordCount, results.mut_span(),
pushConstantMetadataId);
secondPass(words, wordCount, results.mut_span());
firstPass(scopeAlloc, spvWords, results.mut_span(), pushConstantMetadataId);
secondPass(spvWords, results.mut_span());

if (pushConstantMetadataId != sUninitialized)
m_pushConstantsBytesize =
Expand Down
31 changes: 13 additions & 18 deletions src/render/ForwardRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,10 @@ ForwardRenderer::~ForwardRenderer()

void ForwardRenderer::init(
ScopedScratch scopeAlloc, const InputDSLayouts &dsLayouts,
MeshletCuller *meshletCuller,
HierarchicalDepthDownsampler *hierarchicalDepthDownsampler)
MeshletCuller &meshletCuller,
HierarchicalDepthDownsampler &hierarchicalDepthDownsampler)
{
WHEELS_ASSERT(!m_initialized);
WHEELS_ASSERT(meshletCuller != nullptr);
WHEELS_ASSERT(hierarchicalDepthDownsampler != nullptr);

LOG_INFO("Creating ForwardRenderer");

Expand All @@ -84,8 +82,8 @@ void ForwardRenderer::init(
createDescriptorSets(scopeAlloc.child_scope());
createGraphicsPipelines(dsLayouts);

m_meshletCuller = meshletCuller;
m_hierarchicalDepthDownsampler = hierarchicalDepthDownsampler;
m_meshletCuller = &meshletCuller;
m_hierarchicalDepthDownsampler = &hierarchicalDepthDownsampler;

m_initialized = true;
}
Expand Down Expand Up @@ -116,7 +114,7 @@ ForwardRenderer::OpaqueOutput ForwardRenderer::recordOpaque(
ScopedScratch scopeAlloc, vk::CommandBuffer cb, const World &world,
const Camera &cam, const vk::Rect2D &renderArea,
const LightClusteringOutput &lightClusters, BufferHandle inOutDrawStats,
uint32_t nextFrame, bool applyIbl, DrawType drawType, DrawStats *drawStats)
uint32_t nextFrame, bool applyIbl, DrawType drawType, DrawStats &drawStats)
{
WHEELS_ASSERT(m_initialized);

Expand Down Expand Up @@ -161,7 +159,7 @@ ForwardRenderer::OpaqueOutput ForwardRenderer::recordOpaque(
.ibl = applyIbl,
.drawType = drawType,
},
drawStats, " FirstPhase");
" FirstPhase");

gRenderResources.buffers->release(firstPhaseCullingOutput.dataBuffer);
gRenderResources.buffers->release(firstPhaseCullingOutput.argumentBuffer);
Expand Down Expand Up @@ -204,7 +202,7 @@ ForwardRenderer::OpaqueOutput ForwardRenderer::recordOpaque(
.secondPhase = true,
.drawType = drawType,
},
drawStats, " SecondPhase");
" SecondPhase");

gRenderResources.buffers->release(secondPhaseCullingOutput.dataBuffer);
gRenderResources.buffers->release(
Expand All @@ -220,18 +218,17 @@ ForwardRenderer::OpaqueOutput ForwardRenderer::recordOpaque(
}

void ForwardRenderer::recordTransparent(
ScopedScratch scopeAlloc, vk::CommandBuffer cb,
MeshletCuller *meshletCuller, const World &world, const Camera &cam,
const TransparentInOut &inOutTargets,
ScopedScratch scopeAlloc, vk::CommandBuffer cb, const World &world,
const Camera &cam, const TransparentInOut &inOutTargets,
const LightClusteringOutput &lightClusters, BufferHandle inOutDrawStats,
uint32_t nextFrame, DrawType drawType, DrawStats *drawStats)
uint32_t nextFrame, DrawType drawType, DrawStats &drawStats)
{
WHEELS_ASSERT(m_initialized);

PROFILER_CPU_GPU_SCOPE(cb, "Transparent");

const MeshletCullerFirstPhaseOutput cullerOutput =
meshletCuller->recordFirstPhase(
m_meshletCuller->recordFirstPhase(
scopeAlloc.child_scope(), cb, MeshletCuller::Mode::Transparent,
world, cam, nextFrame, {}, "Transparent", drawStats);
WHEELS_ASSERT(!cullerOutput.secondPhaseInput.has_value());
Expand All @@ -250,7 +247,7 @@ void ForwardRenderer::recordTransparent(
.transparents = true,
.drawType = drawType,
},
drawStats, " Geometry");
" Geometry");

gRenderResources.buffers->release(cullerOutput.dataBuffer);
gRenderResources.buffers->release(cullerOutput.argumentBuffer);
Expand Down Expand Up @@ -488,10 +485,8 @@ void ForwardRenderer::recordDraw(
ScopedScratch scopeAlloc, vk::CommandBuffer cb, const World &world,
const Camera &cam, uint32_t nextFrame, const RecordInOut &inputsOutputs,
const LightClusteringOutput &lightClusters, const Options &options,
DrawStats *drawStats, const char *debugName)
const char *debugName)
{
WHEELS_ASSERT(drawStats != nullptr);

PROFILER_CPU_SCOPE(debugName);

const vk::Rect2D renderArea = getRect2D(inputsOutputs.inOutIllumination);
Expand Down
12 changes: 6 additions & 6 deletions src/render/ForwardRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class ForwardRenderer
};
void init(
wheels::ScopedScratch scopeAlloc, const InputDSLayouts &dsLayouts,
MeshletCuller *meshletCuller,
HierarchicalDepthDownsampler *hierarchicalDepthDownsampler);
MeshletCuller &meshletCuller,
HierarchicalDepthDownsampler &hierarchicalDepthDownsampler);

void recompileShaders(
wheels::ScopedScratch scopeAlloc,
Expand All @@ -53,7 +53,7 @@ class ForwardRenderer
const World &world, const Camera &cam, const vk::Rect2D &renderArea,
const LightClusteringOutput &lightClusters, BufferHandle inOutDrawStats,
uint32_t nextFrame, bool applyIbl, DrawType drawType,
DrawStats *drawStats);
DrawStats &drawStats);

struct TransparentInOut
{
Expand All @@ -62,10 +62,10 @@ class ForwardRenderer
};
void recordTransparent(
wheels::ScopedScratch scopeAlloc, vk::CommandBuffer cb,
MeshletCuller *meshletCuller, const World &world, const Camera &cam,
const World &world, const Camera &cam,
const TransparentInOut &inOutTargets,
const LightClusteringOutput &lightClusters, BufferHandle inOutDrawStats,
uint32_t nextFrame, DrawType drawType, DrawStats *drawStats);
uint32_t nextFrame, DrawType drawType, DrawStats &drawStats);

void releasePreserved();

Expand Down Expand Up @@ -108,7 +108,7 @@ class ForwardRenderer
const World &world, const Camera &cam, uint32_t nextFrame,
const RecordInOut &inputsOutputs,
const LightClusteringOutput &lightClusters, const Options &options,
DrawStats *drawStats, const char *debugName);
const char *debugName);

bool m_initialized{false};

Expand Down
Loading

0 comments on commit 57182ab

Please sign in to comment.