Skip to content

Commit

Permalink
Automatic update (#398)
Browse files Browse the repository at this point in the history
- Remove Buffer::MapType enum.
- Fix RenderContext::resolveResource.
  • Loading branch information
skallweitNV committed Oct 24, 2023
1 parent a464cc1 commit 8526e89
Show file tree
Hide file tree
Showing 25 changed files with 65 additions and 63 deletions.
18 changes: 7 additions & 11 deletions Source/Falcor/Core/API/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ void Buffer::setBlob(const void* pData, size_t offset, size_t size)
if (mMemoryType == MemoryType::Upload)
{
bool wasMapped = mMappedPtr != nullptr;
uint8_t* pDst = (uint8_t*)map(MapType::Write) + offset;
uint8_t* pDst = (uint8_t*)map() + offset;
std::memcpy(pDst, pData, size);
if (!wasMapped)
unmap();
Expand All @@ -286,7 +286,7 @@ void Buffer::getBlob(void* pData, size_t offset, size_t size) const
if (mMemoryType == MemoryType::ReadBack)
{
bool wasMapped = mMappedPtr != nullptr;
const uint8_t* pSrc = (const uint8_t*)map(MapType::Read) + offset;
const uint8_t* pSrc = (const uint8_t*)map() + offset;
std::memcpy(pData, pSrc, size);
if (!wasMapped)
unmap();
Expand All @@ -301,16 +301,12 @@ void Buffer::getBlob(void* pData, size_t offset, size_t size) const
}
}

void* Buffer::map(MapType type) const
void* Buffer::map() const
{
if (type == MapType::WriteDiscard)
FALCOR_THROW("MapType::WriteDiscard not supported anymore");

if (type == MapType::Write && mMemoryType != MemoryType::Upload)
FALCOR_THROW("Trying to map a buffer for writing, but it wasn't created with the write permissions.");

if (type == MapType::Read && mMemoryType != MemoryType::ReadBack)
FALCOR_THROW("Trying to map a buffer for reading, but it wasn't created with the read permissions.");
FALCOR_CHECK(
mMemoryType == MemoryType::Upload || mMemoryType == MemoryType::ReadBack,
"Trying to map a buffer that wasn't created with the upload or readback flags."
);

if (!mMappedPtr)
FALCOR_GFX_CALL(mGfxBufferResource->map(nullptr, &mMappedPtr));
Expand Down
28 changes: 1 addition & 27 deletions Source/Falcor/Core/API/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,6 @@ class FALCOR_API Buffer : public Resource
{
FALCOR_OBJECT(Buffer)
public:
enum class MapType
{
Read, ///< Map the buffer for read access.
Write, ///< Map the buffer for write access.
WriteDiscard, ///< Deprecated and not supported.
};

/// Constructor for raw buffer.
Buffer(ref<Device> pDevice, size_t size, ResourceBindFlags bindFlags, MemoryType memoryType, const void* pInitData);

Expand Down Expand Up @@ -272,11 +265,8 @@ class FALCOR_API Buffer : public Resource

/**
* Map the buffer.
* Only buffers with MemoryType::Upload or MemoryType::ReadBack can be mapped.
* To map a buffer with MemoryType::Upload, use MapType::Write.
* To map a buffer with MemoryType::ReadBack, use MapType::Read.
*/
void* map(MapType Type) const;
void* map() const;

/**
* Unmap the buffer
Expand Down Expand Up @@ -364,20 +354,4 @@ inline std::string to_string(MemoryType c)
#undef a2s
}

inline std::string to_string(Buffer::MapType mt)
{
#define t2s(t_) \
case Buffer::MapType::t_: \
return #t_;
switch (mt)
{
t2s(Read);
t2s(Write);
t2s(WriteDiscard);
default:
FALCOR_UNREACHABLE();
return "";
}
#undef t2s
}
} // namespace Falcor
2 changes: 1 addition & 1 deletion Source/Falcor/Core/API/CopyContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ void CopyContext::ReadTextureTask::getData(void* pData, size_t size) const
mpFence->wait();

uint8_t* pDst = reinterpret_cast<uint8_t*>(pData);
const uint8_t* pSrc = reinterpret_cast<const uint8_t*>(mpBuffer->map(Buffer::MapType::Read));
const uint8_t* pSrc = reinterpret_cast<const uint8_t*>(mpBuffer->map());

for (uint32_t z = 0; z < mDepth; z++)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Falcor/Core/API/GpuTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ double GpuTimer::getElapsedTime()
if (mDataPending)
{
uint64_t result[2];
uint64_t* pRes = (uint64_t*)mpResolveStagingBuffer->map(Buffer::MapType::Read);
uint64_t* pRes = (uint64_t*)mpResolveStagingBuffer->map();
result[0] = pRes[0];
result[1] = pRes[1];
mpResolveStagingBuffer->unmap();
Expand Down
18 changes: 18 additions & 0 deletions Source/Falcor/Core/API/RenderContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,10 @@ void RenderContext::raytrace(Program* pProgram, RtProgramVars* pVars, uint32_t w

void RenderContext::resolveSubresource(const ref<Texture>& pSrc, uint32_t srcSubresource, const ref<Texture>& pDst, uint32_t dstSubresource)
{
// TODO it would be better to just use barriers on the subresources.
resourceBarrier(pSrc.get(), Resource::State::ResolveSource);
resourceBarrier(pDst.get(), Resource::State::ResolveDest);

auto resourceEncoder = getLowLevelData()->getResourceCommandEncoder();
gfx::SubresourceRange srcRange = {};
srcRange.baseArrayLayer = pSrc->getSubresourceArraySlice(srcSubresource);
Expand All @@ -570,13 +574,27 @@ void RenderContext::resolveSubresource(const ref<Texture>& pSrc, uint32_t srcSub

void RenderContext::resolveResource(const ref<Texture>& pSrc, const ref<Texture>& pDst)
{
FALCOR_CHECK(pSrc->getType() == Resource::Type::Texture2DMultisample, "Source texture must be multi-sampled.");
FALCOR_CHECK(pDst->getType() == Resource::Type::Texture2D, "Destination texture must not be multi-sampled.");
FALCOR_CHECK(pSrc->getFormat() == pDst->getFormat(), "Source and destination textures must have the same format.");
FALCOR_CHECK(
pSrc->getWidth() == pDst->getWidth() && pSrc->getHeight() == pDst->getHeight(),
"Source and destination textures must have the same dimensions."
);
FALCOR_CHECK(pSrc->getArraySize() == pDst->getArraySize(), "Source and destination textures must have the same array size.");
FALCOR_CHECK(pSrc->getMipCount() == pDst->getMipCount(), "Source and destination textures must have the same mip count.");

resourceBarrier(pSrc.get(), Resource::State::ResolveSource);
resourceBarrier(pDst.get(), Resource::State::ResolveDest);

auto resourceEncoder = getLowLevelData()->getResourceCommandEncoder();

gfx::SubresourceRange srcRange = {};
srcRange.layerCount = pSrc->getArraySize();
srcRange.mipLevelCount = pSrc->getMipCount();
gfx::SubresourceRange dstRange = {};
dstRange.layerCount = pDst->getArraySize();
dstRange.mipLevelCount = pDst->getMipCount();

resourceEncoder->resolveResource(
pSrc->getGfxTextureResource(),
Expand Down
2 changes: 1 addition & 1 deletion Source/Falcor/Rendering/Materials/BSDFIntegrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace Falcor
pRenderContext->submit(true);

// Read back final results.
const float3* finalResults = reinterpret_cast<const float3*>(mpStagingBuffer->map(Buffer::MapType::Read));
const float3* finalResults = reinterpret_cast<const float3*>(mpStagingBuffer->map());
std::vector<float3> output(finalResults, finalResults + gridCount);
mpStagingBuffer->unmap();

Expand Down
2 changes: 1 addition & 1 deletion Source/Falcor/Rendering/Utils/PixelStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ namespace Falcor
if (mEnabled)
{
// Map the stats buffer.
const uint4* result = static_cast<const uint4*>(mpReductionResult->map(Buffer::MapType::Read));
const uint4* result = static_cast<const uint4*>(mpReductionResult->map());
FALCOR_ASSERT(result);

const uint32_t totalPathLength = result[kRayTypeCount].x;
Expand Down
2 changes: 1 addition & 1 deletion Source/Falcor/Scene/Lights/LightCollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ namespace Falcor

FALCOR_ASSERT(mStagingBufferValid);
FALCOR_ASSERT(mpTriangleData && mpFluxData);
const void* mappedData = mpStagingBuffer->map(Buffer::MapType::Read);
const void* mappedData = mpStagingBuffer->map();

uint64_t offset = 0;
const PackedEmissiveTriangle* triangleData = reinterpret_cast<const PackedEmissiveTriangle*>(reinterpret_cast<uintptr_t>(mappedData) + offset);
Expand Down
2 changes: 1 addition & 1 deletion Source/Falcor/Scene/Material/MaterialSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ namespace Falcor

// Wait for results to become available. Then optimize the materials.
mpFence->wait();
const TextureAnalyzer::Result* results = static_cast<const TextureAnalyzer::Result*>(pResultsStaging->map(Buffer::MapType::Read));
const TextureAnalyzer::Result* results = static_cast<const TextureAnalyzer::Result*>(pResultsStaging->map());
Material::TextureOptimizationStats stats = {};

for (size_t i = 0; i < textures.size(); i++)
Expand Down
2 changes: 1 addition & 1 deletion Source/Falcor/Scene/SDFs/SparseBrickSet/SDFSBS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ namespace Falcor
pRenderContext->submit(true);

// Read back final results.
uint32_t finalResults = *reinterpret_cast<uint32_t*>(mpCountStagingBuffer->map(Buffer::MapType::Read));
uint32_t finalResults = *reinterpret_cast<uint32_t*>(mpCountStagingBuffer->map());
mpCountStagingBuffer->unmap();
return finalResults;
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Falcor/Scene/SDFs/SparseVoxelOctree/SDFSVO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ namespace Falcor

// Copy surface containing voxels count from staging buffer to CPU.
mpReadbackFence->wait();
const uint32_t* pSurfaceContainingVoxels = reinterpret_cast<const uint32_t*>(mpSurfaceVoxelCounterStagingBuffer->map(Buffer::MapType::Read));
const uint32_t* pSurfaceContainingVoxels = reinterpret_cast<const uint32_t*>(mpSurfaceVoxelCounterStagingBuffer->map());
std::memcpy(&finestLevelVoxelCount, pSurfaceContainingVoxels, sizeof(uint32_t));
mpSurfaceVoxelCounterStagingBuffer->unmap();
}
Expand Down Expand Up @@ -310,7 +310,7 @@ namespace Falcor

// Copy child count from staging buffer to CPU.
mpReadbackFence->wait();
const uint32_t* pVoxelCountPerLevel = reinterpret_cast<const uint32_t*>(mpVoxelCountPerLevelStagingBuffer->map(Buffer::MapType::Read));
const uint32_t* pVoxelCountPerLevel = reinterpret_cast<const uint32_t*>(mpVoxelCountPerLevelStagingBuffer->map());
std::memcpy(voxelCountsPerLevel.data(), pVoxelCountPerLevel, sizeof(uint32_t) * (mLevelCount - 1));
mpVoxelCountPerLevelStagingBuffer->unmap();

Expand Down
2 changes: 1 addition & 1 deletion Source/Falcor/Utils/Debug/PixelDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ bool PixelDebug::copyDataToCPU()
if (mEnabled)
{
// Copy data from readback buffer to CPU buffers.
const uint8_t* data = reinterpret_cast<const uint8_t*>(mpReadbackBuffer->map(Buffer::MapType::Read));
const uint8_t* data = reinterpret_cast<const uint8_t*>(mpReadbackBuffer->map());
const uint32_t* counterData = reinterpret_cast<const uint32_t*>(data);
data += mpCounterBuffer->getSize();
const PrintRecord* printData = reinterpret_cast<const PrintRecord*>(data);
Expand Down
2 changes: 1 addition & 1 deletion Source/Falcor/Utils/Debug/WarpProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void WarpProfiler::readBackData()
mpFence->wait();
mHistograms.resize(mBinCount * kWarpSize);

const uint32_t* data = reinterpret_cast<const uint32_t*>(mpHistogramStagingBuffer->map(Buffer::MapType::Read));
const uint32_t* data = reinterpret_cast<const uint32_t*>(mpHistogramStagingBuffer->map());
std::memcpy(mHistograms.data(), data, mHistograms.size() * sizeof(uint32_t));
mpHistogramStagingBuffer->unmap();

Expand Down
4 changes: 2 additions & 2 deletions Source/Falcor/Utils/UI/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1267,8 +1267,8 @@ void Gui::render(RenderContext* pContext, const ref<Fbo>& pFbo, float elapsedTim
mpWrapper->mpPipelineState->setVao(pVao);

// Upload the data
ImDrawVert* pVerts = (ImDrawVert*)pVao->getVertexBuffer(0)->map(Buffer::MapType::Write);
uint16_t* pIndices = (uint16_t*)pVao->getIndexBuffer()->map(Buffer::MapType::Write);
ImDrawVert* pVerts = (ImDrawVert*)pVao->getVertexBuffer(0)->map();
uint16_t* pIndices = (uint16_t*)pVao->getIndexBuffer()->map();

for (int n = 0; n < pDrawData->CmdListsCount; n++)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Falcor/Utils/UI/TextRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void TextRenderer::renderText(RenderContext* pRenderContext, const std::string&
setCbData(pDstFbo);
const auto& pVao = mpVaos[mVaoIndex];
const auto& pVb = pVao->getVertexBuffer(0);
Vertex* verts = reinterpret_cast<Vertex*>(pVb->map(Buffer::MapType::Write));
Vertex* verts = reinterpret_cast<Vertex*>(pVb->map());

float startX = pos.x;
uint32_t vertexCount = 0; // Not the same as text.size(), since some special characters are ignored
Expand Down
2 changes: 1 addition & 1 deletion Source/RenderPasses/BSDFViewer/BSDFViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ void BSDFViewer::readPixelData()
{
mpFence->wait();
FALCOR_ASSERT(mpPixelStagingBuffer);
mPixelData = *static_cast<const PixelData*>(mpPixelStagingBuffer->map(Buffer::MapType::Read));
mPixelData = *static_cast<const PixelData*>(mpPixelStagingBuffer->map());
mpPixelStagingBuffer->unmap();

mPixelDataAvailable = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ std::optional<std::pair<double, double>> ColorMapPass::AutoRanging::getMinMax(
{
mpFence->wait();

const void* values = mpReductionResult->map(Buffer::MapType::Read);
const void* values = mpReductionResult->map();

switch (formatType)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/RenderPasses/SDFEditor/SDFEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ void SDFEditor::execute(RenderContext* pRenderContext, const RenderData& renderD

// Wait for the picking info from the previous frame.
mpReadbackFence->wait();
mPickingInfo = *reinterpret_cast<const SDFPickingInfo*>(mpPickingInfoReadBack->map(Buffer::MapType::Read));
mPickingInfo = *reinterpret_cast<const SDFPickingInfo*>(mpPickingInfoReadBack->map());
mpPickingInfoReadBack->unmap();

setup2DGUI();
Expand Down
2 changes: 1 addition & 1 deletion Source/RenderPasses/TestPasses/TestPyTorchPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ bool TestPyTorchPass::verifyData(const uint3 dim, const uint32_t offset, TestPyT
// Wait for results to be available.
pRenderContext->submit(true);

const uint32_t counter = *reinterpret_cast<const uint32_t*>(mpCounterStagingBuffer->map(Buffer::MapType::Read));
const uint32_t counter = *reinterpret_cast<const uint32_t*>(mpCounterStagingBuffer->map());
mpCounterStagingBuffer->unmap();
FALCOR_ASSERT(counter <= elemCount);

Expand Down
14 changes: 13 additions & 1 deletion Source/Samples/MultiSampling/MultiSampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ void MultiSampling::onLoad(RenderContext* pRenderContext)
128, 128, ResourceFormat::RGBA32Float, kSampleCount, 1, ResourceBindFlags::ShaderResource | ResourceBindFlags::RenderTarget
);
mpFbo->attachColorTarget(tex, 0);

mpResolvedTexture = getDevice()->createTexture2D(128, 128, ResourceFormat::RGBA32Float, 1, 1);
}

void MultiSampling::onFrameRender(RenderContext* pRenderContext, const ref<Fbo>& pTargetFbo)
Expand All @@ -83,7 +85,17 @@ void MultiSampling::onFrameRender(RenderContext* pRenderContext, const ref<Fbo>&
mpRasterPass->getState()->setVao(mpVao);
mpRasterPass->draw(pRenderContext, kTriangleCount * 3, 0);

pRenderContext->blit(mpFbo->getColorTexture(0)->getSRV(), pTargetFbo->getRenderTargetView(0));
if (mFrame++ % 2 == 0)
{
// For even frames, resolve to texture and then blit.
pRenderContext->resolveResource(mpFbo->getColorTexture(0), mpResolvedTexture);
pRenderContext->blit(mpResolvedTexture->getSRV(), pTargetFbo->getRenderTargetView(0));
}
else
{
// For odd frames, blit directly.
pRenderContext->blit(mpFbo->getColorTexture(0)->getSRV(), pTargetFbo->getRenderTargetView(0));
}
}

int runMain(int argc, char** argv)
Expand Down
2 changes: 2 additions & 0 deletions Source/Samples/MultiSampling/MultiSampling.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ class MultiSampling : public SampleApp
ref<RasterPass> mpRasterPass;
ref<Vao> mpVao;
ref<Fbo> mpFbo;
ref<Texture> mpResolvedTexture;
uint32_t mFrame = 0;
};
4 changes: 2 additions & 2 deletions Source/Tools/FalcorTest/Tests/Core/BufferAccessTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ GPU_TEST(BufferUploadWrite)
GPU_TEST(BufferUploadMap)
{
ref<Buffer> pBuffer = createTestBuffer(ctx, MemoryType::Upload, false);
uint32_t* pData = reinterpret_cast<uint32_t*>(pBuffer->map(Buffer::MapType::Write));
uint32_t* pData = reinterpret_cast<uint32_t*>(pBuffer->map());
for (uint32_t i = 0; i < kElementCount; ++i)
pData[i] = kTestData[i];
pBuffer->unmap();
Expand All @@ -150,7 +150,7 @@ GPU_TEST(BufferReadbackMap)
ref<Buffer> pBuffer = createTestBuffer(ctx, MemoryType::ReadBack, false);
initBufferIndirect(ctx, pBuffer);

const uint32_t* pData = reinterpret_cast<const uint32_t*>(pBuffer->map(Buffer::MapType::Read));
const uint32_t* pData = reinterpret_cast<const uint32_t*>(pBuffer->map());
checkData(ctx, pData);
pBuffer->unmap();
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Tools/FalcorTest/Tests/Core/BufferTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ GPU_TEST(BufferWrite)

if (!useInitData)
{
uint4* data = reinterpret_cast<uint4*>(bufA->map(Buffer::MapType::Write));
uint4* data = reinterpret_cast<uint4*>(bufA->map());
std::memcpy(data, &testData, 16);
bufA->unmap();
}
Expand All @@ -203,7 +203,7 @@ GPU_TEST(BufferWrite)
uint4 testData2 = testData * 10u;

{
uint4* data = reinterpret_cast<uint4*>(bufA->map(Buffer::MapType::Write));
uint4* data = reinterpret_cast<uint4*>(bufA->map());
std::memcpy(data, &testData2, 16);
bufA->unmap();
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Tools/FalcorTest/Tests/Core/CoreTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ GPU_TEST(TransientHeapRecycling)
// is missed.
pRenderContext->copyBufferRegion(B.get(), 0, A.get(), 0, 4);
pRenderContext->submit(true);
// A->map(Buffer::MapType::Read);
// A->map();
// A->unmap();
}
} // namespace Falcor
2 changes: 1 addition & 1 deletion Source/Tools/FalcorTest/Tests/Core/LargeBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void testCopyRegion(GPUUnitTestContext& ctx, size_t bufferSize)
ctx.getRenderContext()->submit(true);

// Check the result.
const uint32_t* result = static_cast<const uint32_t*>(pReadback->map(Buffer::MapType::Read));
const uint32_t* result = static_cast<const uint32_t*>(pReadback->map());
for (size_t i = 0; i < data.size(); i++)
{
EXPECT_EQ(result[i], data[i]) << "i = " << i;
Expand Down

0 comments on commit 8526e89

Please sign in to comment.