Comments (9)
Note that your code for 24-bit is incorrect. You are reading as if 24-bit depth was packed into 32-bit leaving each byte unused, which may not be the case.
How do you copy your depth texture to your staging texture?
from diligentengine.
Note that your code for 24-bit is incorrect. You are reading as if 24-bit depth was packed into 32-bit leaving each byte unused, which may not be the case.
How do you copy your depth texture to your staging texture?
I stopped use TEX_FORMAT_D24_UNORM_S8_UINT after found this format is not supported to copy back to RAM.
Copied by
math::v4f cursorZ;
const float ClearColor[] = { 0.350f, 0.350f, 0.350f, 1.0f };
auto& m_pSwapChain = this->p_rt->m_pSwapChain;
auto pRTV = m_pSwapChain->GetCurrentBackBufferRTV();
auto pDSV = m_pSwapChain->GetDepthBufferDSV();
auto df = pDSV->GetTexture()->GetDesc();
auto& pContext = this->p_rt->m_pImmediateContext;
auto pdepthtex = pDSV->GetTexture();
pContext->SetRenderTargets(0, nullptr, nullptr, RESOURCE_STATE_TRANSITION_MODE_NONE);
auto& m_UploadCompleteFence = _editmem.get<1>().m_UploadCompleteFence;
auto& cam = Camera::Main();
inputmanager& ipt = *p_ipt;
cursorZ.head<2>() = ipt.mouseinfo.operator pj2::mousestate::f2 & ();
editmemCounters& memedit = _editmem.get<1>();
cursorZ = cursorZ.cwiseMax(math::v4f::Ones()*4);
cursorZ[0] = std::min((UINT32)cursorZ[0], cam.m_ProjAttribs.Width);
cursorZ[1] = std::min((UINT32)cursorZ[1], cam.m_ProjAttribs.Height);
//pContext->UnmapTextureSubresource(pStagingTexture, StartDstMip + mip, StartDstSlice + slice);
Box SrcBox;
SrcBox.MaxX = cursorZ[0];
SrcBox.MinX = std::max(0u, SrcBox.MaxX - 3);
SrcBox.MaxY = cursorZ[1];
SrcBox.MinY = std::max(0u, SrcBox.MaxY - 3);
CopyTextureAttribs CopyAttribs(pdepthtex, RESOURCE_STATE_TRANSITION_MODE_TRANSITION,
memedit.pStagingTexture, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
CopyAttribs.pSrcBox = &SrcBox;
pContext->CopyTexture(CopyAttribs);
pContext->EnqueueSignal(m_UploadCompleteFence, ++_editmem.get<1>().m_UploadCompleteFenceValue);
const StateTransitionDesc Barrier{ pdepthtex, RESOURCE_STATE_COPY_SOURCE, RESOURCE_STATE_DEPTH_WRITE };
pContext->TransitionResourceStates(1, &Barrier);
pContext->SetRenderTargets(1, &pRTV, pDSV, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
from diligentengine.
You may consider using GPUCompletionAwaitQueue
Here is the code that does what you need.
from diligentengine.
Thanks you reply, I'll try it.
inline void CvtDepth(edtmem& edtm, span<float> num) {
constexpr int Mip = 0;
auto& UploadBuffDesc = edtm.get<1>().UploadBuffDesc;
const Uint32 Width = UploadBuffDesc.Width >> Mip;
const Uint32 Height = UploadBuffDesc.Height >> Mip;
auto& MappedData = edtm.get<1>().MappedData;
constexpr uint xs = 0x00FFFFFF;
auto pRGBAData = reinterpret_cast<float*>(MappedData.pData);
{
for (Uint32 y = 0, cnt = 0; y < Height; ++y)
{
auto pRGBA = pRGBAData + MappedData.Stride * y;
for (Uint32 x = 0; x < Width; ++x)
{
// depth bits
num[cnt++] = static_cast<float>(pRGBA[x]);
}
}
}
}
inputmanager& ipt = *p_ipt;
auto pdepthtex = rt_.m_pDepthDSV->GetTexture();
auto& m_UploadCompleteFence = _editmem.get<1>().m_UploadCompleteFence;
m_UploadCompleteFence->Wait(_editmem.get<1>().m_UploadCompleteFenceValue);
math::v4f cursorZ, zero4 = {};
inputmanager::updateParam uppara;
auto& worldPos = std::get<0>(uppara);
cursorZ.head<2>() = ipt.mouseinfo.operator pj2::mousestate::f2 & ();
editmemCounters& memedit = _editmem.get<1>();
cursorZ = cursorZ.cwiseMax(zero4);
cursorZ[0] = std::min((UINT32)cursorZ[0], cam.m_ProjAttribs.Width);
cursorZ[1] = std::min((UINT32)cursorZ[1], cam.m_ProjAttribs.Height);
pContext->UnmapTextureSubresource(memedit.pStagingTexture, 0,0);
pContext->MapTextureSubresource(memedit.pStagingTexture, 0, 0,
MAP_READ,
MAP_FLAG_DO_NOT_WAIT | MAP_FLAG_DISCARD | MAP_FLAG_NO_OVERWRITE, nullptr, memedit.MappedData);
auto& UploadBuffDesc = _editmem.get<1>().UploadBuffDesc;
UploadBuffDesc.Width = 1;
UploadBuffDesc.Height = 1;
assert(rt_.DepthBufferFormat != TEX_FORMAT_D24_UNORM_S8_UINT);
std::array<float, 16> depthNorm;
CvtDepth(_editmem, span(depthNorm.data(), 1));
cursorZ[2] = depthNorm[0];
const math::Matrix4f& inverse_projection_matrix = cam.m_ProjMatrix.inverse();
const math::Matrix4f& inverse_View_matrix = cam.m_ViewMatrix.matrix().inverse();
getRays(cursorZ, inverse_projection_matrix, worldPos, cam);
worldPos = inverse_View_matrix * worldPos;
freeUIVec = worldPos;
ipt.update(uppara);
here is update function to read depth before call the CopyTexture Code posted previously
from diligentengine.
Did you run this in Debug? Do you get any messages in the console?
This is incorrect:
cursorZ[0] = std::min((UINT32)cursorZ[0], cam.m_ProjAttribs.Width);
cursorZ[1] = std::min((UINT32)cursorZ[1], cam.m_ProjAttribs.Height);
It should be
cursorZ[0] = std::min((UINT32)cursorZ[0], cam.m_ProjAttribs.Width - 1);
cursorZ[1] = std::min((UINT32)cursorZ[1], cam.m_ProjAttribs.Height - 1);
from diligentengine.
I suggest you look at the code I sent and do the same way.
from diligentengine.
I suggest you look at the code I sent and do the same way.
Appreciate you reply, I'll take a look.
from diligentengine.
solved by remove UnmapTextureSubresource and MapTextureSubresource call in update function.
MapTextureSubresource once at init function would be enough. Maybe I need UnmapTextureSubresource before release, haven't try that
from diligentengine.
You read the data between MapTextureSubresource
and UnmapTextureSubresource
. In you snippet, you unmap right before mapping again, which does not make a lot of sense.
Once again, take a look at the code that I sent.
from diligentengine.
Related Issues (20)
- How to Transfer Variables from Vertex Shaders to Pixel Shaders in HLSL, Similar to Opengl's Vary HOT 1
- Tutorial19Application_RenderPass crash on android HOT 2
- Why does the projection matrix appear to be in a left-hand coordinate system HOT 5
- Hello, we want to use DiligentEngine as a part of commercial software, which has just started to develop. Is there anything we should pay attention to HOT 2
- Why do objects display in a left-hand coordinate system HOT 7
- WebGPU support. HOT 4
- Macro is too much , Affects code readability HOT 1
- Poor API design choices affect real usability HOT 2
- fake bind error at version 2.5.2? HOT 7
- Build failure on linux
- Windows build output HOT 7
- Wordpress site hijacked HOT 1
- Build Failure Involving libpng in DiligentEngine HOT 6
- Is build multiple shader in differrent thread safe? HOT 1
- windows build error HOT 2
- Website appears to have been hacked HOT 1
- DX11 crash when changing max frame latency HOT 4
- Build failure with FetchContent HOT 14
- Multiple swap chains frame lag? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from diligentengine.