Commit e530fef5 authored by Laszlo Agocs's avatar Laszlo Agocs

Try using the texture comparison stuff

...not yet finished.
parent 0b886474
......@@ -2,9 +2,12 @@
layout(location = 0) in vec3 vECVertNormal;
layout(location = 1) in vec3 vECVertPos;
layout(location = 2) in vec4 vLCVertPos;
layout(location = 0) out vec4 fragColor;
layout(binding = 1) uniform sampler2DShadow shadowMap;
void main()
{
vec3 ECCameraPosition = vec3(0.0, 0.0, 4.0); // matches C++
......@@ -31,5 +34,9 @@ void main()
float RV = max(0.0, dot(R, V));
vec3 sColor = att * intensity * color * pow(RV, specularExp);
fragColor = vec4(ka + kd * dColor + ks * sColor, 1.0);
float sc = textureProj(shadowMap, vLCVertPos);
if (sc > 0)
fragColor = vec4(ka + kd * dColor + ks * sColor, 1.0);
else
fragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
......@@ -7,6 +7,7 @@ layout(std140, binding = 0) uniform buf {
mat4 mvp;
mat4 mv;
mat4 m;
mat4 lightViewProj;
mat3 n;
} ubuf;
......@@ -14,9 +15,15 @@ out gl_PerVertex { vec4 gl_Position; };
layout(location = 0) out vec3 vECVertNormal;
layout(location = 1) out vec3 vECVertPos;
layout(location = 2) out vec4 vLCVertPos;
void main()
{
mat4 bias = mat4(0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0);
vLCVertPos = bias * ubuf.lightViewProj * ubuf.m * position;
vECVertNormal = normalize(ubuf.n * normal);
vECVertPos = vec3(ubuf.m * position);
gl_Position = ubuf.mvp * position;
......
......@@ -72,6 +72,7 @@ struct {
QRhiBuffer *vbuf = nullptr;
QRhiBuffer *ibuf = nullptr;
QRhiBuffer *ubuf = nullptr;
QRhiSampler *shadowSampler = nullptr;
QRhiShaderResourceBindings *srb = nullptr;
QRhiGraphicsPipeline *ps = nullptr;
QRhiResourceUpdateBatch *initialUpdates = nullptr;
......@@ -81,10 +82,11 @@ struct {
QRhiTextureRenderTarget *rt = nullptr;
QRhiRenderPassDescriptor *rtRp = nullptr;
QRhiTexture *shadowMap = nullptr;
QRhiShaderResourceBindings *shadowSrb = nullptr;
QRhiGraphicsPipeline *shadowPs = nullptr;
} d;
const int UBLOCK_SIZE = 64 + 64 + 64 + 48;
const int UBLOCK_SIZE = 64 * 4 + 48;
const int UBUF_SLOTS = 4; // 2 objects * 2 passes with different cameras
void Window::customInit()
......@@ -105,10 +107,21 @@ void Window::customInit()
d.ubuf->build();
d.releasePool << d.ubuf;
d.shadowMap = m_r->newTexture(QRhiTexture::D32F, QSize(512, 512), 1, QRhiTexture::RenderTarget);
d.releasePool << d.shadowMap;
d.shadowMap->build();
d.shadowSampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
d.releasePool << d.shadowSampler;
d.shadowSampler->setTextureCompareOp(QRhiSampler::LessOrEqual);
d.shadowSampler->build();
d.srb = m_r->newShaderResourceBindings();
d.releasePool << d.srb;
const QRhiShaderResourceBinding::StageFlags stages = QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage;
d.srb->setBindings({ QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(0, stages, d.ubuf, 64) });
d.srb->setBindings({ QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(0, stages, d.ubuf, UBLOCK_SIZE),
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.shadowMap, d.shadowSampler) });
d.srb->build();
d.ps = m_r->newGraphicsPipeline();
......@@ -138,10 +151,6 @@ void Window::customInit()
d.initialUpdates->uploadStaticBuffer(d.vbuf, sizeof(quadVertexData), sizeof(cube), cube);
d.initialUpdates->uploadStaticBuffer(d.ibuf, quadIndexData);
d.shadowMap = m_r->newTexture(QRhiTexture::D32F, QSize(512, 512), 1, QRhiTexture::RenderTarget);
d.releasePool << d.shadowMap;
d.shadowMap->build();
QRhiTextureRenderTargetDescription rtDesc;
rtDesc.setDepthTexture(d.shadowMap);
d.rt = m_r->newTextureRenderTarget(rtDesc);
......@@ -151,6 +160,11 @@ void Window::customInit()
d.rt->setRenderPassDescriptor(d.rtRp);
d.rt->build();
d.shadowSrb = m_r->newShaderResourceBindings();
d.releasePool << d.shadowSrb;
d.shadowSrb->setBindings({ QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(0, stages, d.ubuf, UBLOCK_SIZE) });
d.shadowSrb->build();
d.shadowPs = m_r->newGraphicsPipeline();
d.releasePool << d.shadowPs;
d.shadowPs->setShaderStages({
......@@ -166,7 +180,7 @@ void Window::customInit()
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 }
});
d.shadowPs->setVertexInputLayout(inputLayout);
d.shadowPs->setShaderResourceBindings(d.srb);
d.shadowPs->setShaderResourceBindings(d.shadowSrb);
d.shadowPs->setRenderPassDescriptor(d.rtRp);
d.shadowPs->build();
}
......@@ -178,16 +192,16 @@ void Window::customRelease()
d.releasePool.clear();
}
static void enqueueScene(QRhiCommandBuffer *cb, int oneRoundedUniformBlockSize, int firstUbufSlot)
static void enqueueScene(QRhiCommandBuffer *cb, QRhiShaderResourceBindings *srb, int oneRoundedUniformBlockSize, int firstUbufSlot)
{
// draw the ground (the quad)
cb->setShaderResources(d.srb, { { 0, quint32(firstUbufSlot * oneRoundedUniformBlockSize) } });
cb->setShaderResources(srb, { { 0, quint32(firstUbufSlot * oneRoundedUniformBlockSize) } });
cb->setVertexInput(0, { { d.vbuf, 0 } }, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16);
cb->drawIndexed(6);
// Draw the object (the cube). Both vertex and uniform data are in the same
// buffer, right after the quad's.
cb->setShaderResources(d.srb, { qMakePair(0, quint32((firstUbufSlot + 1) * oneRoundedUniformBlockSize)) });
cb->setShaderResources(srb, { qMakePair(0, quint32((firstUbufSlot + 1) * oneRoundedUniformBlockSize)) });
cb->setVertexInput(0, { { d.vbuf, sizeof(quadVertexData) } });
cb->draw(36);
}
......@@ -209,6 +223,10 @@ void Window::customRender()
const int oneRoundedUniformBlockSize = m_r->ubufAligned(UBLOCK_SIZE);
QMatrix4x4 lightViewProj = m_r->clipSpaceCorrMatrix();
lightViewProj.perspective(45.0f, 1, 0.01f, 1000.0f);
lightViewProj.lookAt(lightPos, shadowCamCenter, shadowCamUp);
// uniform data for the ground
if (d.winProj != m_proj) {
d.winProj = m_proj;
......@@ -228,14 +246,12 @@ void Window::customRender()
u->updateDynamicBuffer(d.ubuf, 64, 64, mv.constData());
m = m_r->clipSpaceCorrMatrix() * m;
u->updateDynamicBuffer(d.ubuf, 128, 64, m.constData());
u->updateDynamicBuffer(d.ubuf, 192, 64, lightViewProj.constData());
QMatrix3x3 n = m.normalMatrix();
u->updateDynamicBuffer(d.ubuf, 192, 48, n.constData());
u->updateDynamicBuffer(d.ubuf, 256, 48, n.constData());
// for the shadow pass
mvp = m_r->clipSpaceCorrMatrix();
mvp.perspective(45.0f, 1, 0.01f, 1000.0f);
mvp.lookAt(lightPos, shadowCamCenter, shadowCamUp);
mvp *= mv;
mvp = lightViewProj * mv;
u->updateDynamicBuffer(d.ubuf, 2 * oneRoundedUniformBlockSize, 64, mvp.constData());
}
......@@ -258,14 +274,12 @@ void Window::customRender()
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize + 64, 64, mv.constData());
m = m_r->clipSpaceCorrMatrix() * m;
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize + 128, 64, m.constData());
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize + 192, 64, lightViewProj.constData());
QMatrix3x3 n = m.normalMatrix();
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize + 192, 48, n.constData());
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize + 256, 48, n.constData());
// for the shadow pass
mvp = m_r->clipSpaceCorrMatrix();
mvp.perspective(45.0f, 1, 0.01f, 1000.0f);
mvp.lookAt(lightPos, shadowCamCenter, shadowCamUp);
mvp *= mv;
mvp = lightViewProj * mv;
u->updateDynamicBuffer(d.ubuf, 3 * oneRoundedUniformBlockSize, 64, mvp.constData());
cb->resourceUpdate(u);
......@@ -275,13 +289,13 @@ void Window::customRender()
cb->beginPass(d.rt, QRhiColorClearValue(), { 1.0f, 0 });
cb->setGraphicsPipeline(d.shadowPs);
cb->setViewport({ 0, 0, float(shadowMapSize.width()), float(shadowMapSize.height()) });
enqueueScene(cb, oneRoundedUniformBlockSize, 2);
enqueueScene(cb, d.shadowSrb, oneRoundedUniformBlockSize, 2);
cb->endPass();
// main pass
cb->beginPass(m_sc->currentFrameRenderTarget(), { 0.4f, 0.7f, 0.0f, 1.0f }, { 1.0f, 0 });
cb->setGraphicsPipeline(d.ps);
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
enqueueScene(cb, oneRoundedUniformBlockSize, 0);
enqueueScene(cb, d.srb, oneRoundedUniformBlockSize, 0);
cb->endPass();
}
......@@ -2256,6 +2256,8 @@ bool QD3D11Sampler::build()
D3D11_SAMPLER_DESC desc;
memset(&desc, 0, sizeof(desc));
desc.Filter = toD3DFilter(m_minFilter, m_magFilter, m_mipmapMode);
if (m_compareOp != Never)
desc.Filter = D3D11_FILTER(desc.Filter | 0x80);
desc.AddressU = toD3DAddressMode(m_addressU);
desc.AddressV = toD3DAddressMode(m_addressV);
desc.AddressW = toD3DAddressMode(m_addressW);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment