Commit 5b7b391c authored by Laszlo Agocs's avatar Laszlo Agocs

Make it work for vk

Introduce a new getter in QRhi.
parent 5f24ae0a
......@@ -9,6 +9,7 @@ layout(binding = 1) uniform sampler2DShadow shadowMap;
layout(std140, binding = 0) uniform buf {
mat4 mvp;
mat4 lightMvp;
mat4 shadowBias;
int useShadow;
} ubuf;
......
......@@ -5,6 +5,7 @@ layout(location = 0) in vec4 position;
layout(std140, binding = 0) uniform buf {
mat4 mvp;
mat4 lightMvp;
mat4 shadowBias;
int useShadow;
} ubuf;
......@@ -14,11 +15,6 @@ layout(location = 0) out vec4 vLCVertPos;
void main()
{
// [-1,1] -> [0,1]
mat4 shadowMatrix = 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 = shadowMatrix * ubuf.lightMvp * position;
vLCVertPos = ubuf.shadowBias * ubuf.lightMvp * position;
gl_Position = ubuf.mvp * position;
}
......@@ -86,7 +86,7 @@ struct {
QRhiGraphicsPipeline *shadowPs = nullptr;
} d;
const int UBLOCK_SIZE = 64 * 2 + 4;
const int UBLOCK_SIZE = 64 * 3 + 4;
const int SHADOW_UBLOCK_SIZE = 64 * 1;
const int UBUF_SLOTS = 4; // 2 objects * 2 passes with different cameras
......@@ -219,6 +219,23 @@ void Window::customRender()
const int oneRoundedUniformBlockSize = m_r->ubufAligned(UBLOCK_SIZE);
QMatrix4x4 shadowBias;
// fill it in column-major order to keep our sanity (ctor would take row-major)
float *sbp = shadowBias.data();
if (m_r->isClipDepthZeroToOne()) {
// convert x, y [-1, 1] -> [0, 1]
*sbp++ = 0.5f; *sbp++ = 0.0f; *sbp++ = 0.0f; *sbp++ = 0.0f;
*sbp++ = 0.0f; *sbp++ = 0.5f; *sbp++ = 0.0f; *sbp++ = 0.0f;
*sbp++ = 0.0f; *sbp++ = 0.0f; *sbp++ = 1.0f; *sbp++ = 0.0f;
*sbp++ = 0.5f; *sbp++ = 0.5f; *sbp++ = 0.0f; *sbp++ = 1.0f;
} else {
// convert x, y, z [-1, 1] -> [0, 1]
*sbp++ = 0.5f; *sbp++ = 0.0f; *sbp++ = 0.0f; *sbp++ = 0.0f;
*sbp++ = 0.0f; *sbp++ = 0.5f; *sbp++ = 0.0f; *sbp++ = 0.0f;
*sbp++ = 0.0f; *sbp++ = 0.0f; *sbp++ = 0.5f; *sbp++ = 0.0f;
*sbp++ = 0.5f; *sbp++ = 0.5f; *sbp++ = 0.5f; *sbp++ = 1.0f;
}
const QVector3D lightPos(5, 10, 10);
QMatrix4x4 lightViewProj = m_r->clipSpaceCorrMatrix();
lightViewProj.perspective(45.0f, 1, 0.1f, 100.0f);
......@@ -237,8 +254,9 @@ void Window::customRender()
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
const QMatrix4x4 shadowMvp = lightViewProj * m;
u->updateDynamicBuffer(d.ubuf, 64, 64, shadowMvp.constData());
u->updateDynamicBuffer(d.ubuf, 128, 64, shadowBias.constData());
qint32 useShadows = 1;
u->updateDynamicBuffer(d.ubuf, 128, 4, &useShadows);
u->updateDynamicBuffer(d.ubuf, 192, 4, &useShadows);
// for the shadow pass
u->updateDynamicBuffer(d.ubuf, 2 * oneRoundedUniformBlockSize, 64, shadowMvp.constData());
......@@ -257,8 +275,9 @@ void Window::customRender()
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize, 64, mvp.constData());
const QMatrix4x4 shadowMvp = lightViewProj * m;
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize + 64, 64, shadowMvp.constData());
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize + 128, 64, shadowBias.constData());
qint32 useShadows = 0;
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize + 128, 4, &useShadows);
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize + 192, 4, &useShadows);
// for the shadow pass
u->updateDynamicBuffer(d.ubuf, 3 * oneRoundedUniformBlockSize, 64, shadowMvp.constData());
......
......@@ -4127,6 +4127,20 @@ bool QRhi::isYUpInNDC() const
return d->isYUpInNDC();
}
/*!
\return \c true if the underlying graphics API uses depth 0 - 1 in clip
space.
In practice this is \c false for OpenGL only.
\note clipSpaceCorrMatrix() includes the corresponding adjustment in its
returned matrix.
*/
bool QRhi::isClipDepthZeroToOne() const
{
return d->isClipDepthZeroToOne();
}
/*!
\return a matrix that can be used to allow applications keep using
OpenGL-targeted vertex data and perspective projection matrices (such as,
......
......@@ -1330,6 +1330,7 @@ public:
bool isYUpInFramebuffer() const;
bool isYUpInNDC() const;
bool isClipDepthZeroToOne() const;
QMatrix4x4 clipSpaceCorrMatrix() const;
......
......@@ -130,6 +130,7 @@ public:
virtual int ubufAlignment() const = 0;
virtual bool isYUpInFramebuffer() const = 0;
virtual bool isYUpInNDC() const = 0;
virtual bool isClipDepthZeroToOne() const = 0;
virtual QMatrix4x4 clipSpaceCorrMatrix() const = 0;
virtual bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags) const = 0;
virtual bool isFeatureSupported(QRhi::Feature feature) const = 0;
......
......@@ -305,6 +305,11 @@ bool QRhiD3D11::isYUpInNDC() const
return true;
}
bool QRhiD3D11::isClipDepthZeroToOne() const
{
return true;
}
QMatrix4x4 QRhiD3D11::clipSpaceCorrMatrix() const
{
// Like with Vulkan, but Y is already good.
......
......@@ -532,6 +532,7 @@ public:
int ubufAlignment() const override;
bool isYUpInFramebuffer() const override;
bool isYUpInNDC() const override;
bool isClipDepthZeroToOne() const override;
QMatrix4x4 clipSpaceCorrMatrix() const override;
bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags) const override;
bool isFeatureSupported(QRhi::Feature feature) const override;
......
......@@ -430,6 +430,11 @@ bool QRhiGles2::isYUpInNDC() const
return true;
}
bool QRhiGles2::isClipDepthZeroToOne() const
{
return false;
}
QMatrix4x4 QRhiGles2::clipSpaceCorrMatrix() const
{
return QMatrix4x4(); // identity
......
......@@ -533,6 +533,7 @@ public:
int ubufAlignment() const override;
bool isYUpInFramebuffer() const override;
bool isYUpInNDC() const override;
bool isClipDepthZeroToOne() const override;
QMatrix4x4 clipSpaceCorrMatrix() const override;
bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags) const override;
bool isFeatureSupported(QRhi::Feature feature) const override;
......
......@@ -419,6 +419,11 @@ bool QRhiMetal::isYUpInNDC() const
return true;
}
bool QRhiMetal::isClipDepthZeroToOne() const
{
return true;
}
QMatrix4x4 QRhiMetal::clipSpaceCorrMatrix() const
{
// depth range 0..1
......
......@@ -354,6 +354,7 @@ public:
int ubufAlignment() const override;
bool isYUpInFramebuffer() const override;
bool isYUpInNDC() const override;
bool isClipDepthZeroToOne() const override;
QMatrix4x4 clipSpaceCorrMatrix() const override;
bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags) const override;
bool isFeatureSupported(QRhi::Feature feature) const override;
......
......@@ -107,7 +107,7 @@ int QRhiNull::ubufAlignment() const
bool QRhiNull::isYUpInFramebuffer() const
{
return true;
return false;
}
bool QRhiNull::isYUpInNDC() const
......@@ -115,6 +115,11 @@ bool QRhiNull::isYUpInNDC() const
return true;
}
bool QRhiNull::isClipDepthZeroToOne() const
{
return true;
}
QMatrix4x4 QRhiNull::clipSpaceCorrMatrix() const
{
return QMatrix4x4(); // identity
......
......@@ -240,6 +240,7 @@ public:
int ubufAlignment() const override;
bool isYUpInFramebuffer() const override;
bool isYUpInNDC() const override;
bool isClipDepthZeroToOne() const override;
QMatrix4x4 clipSpaceCorrMatrix() const override;
bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags) const override;
bool isFeatureSupported(QRhi::Feature feature) const override;
......
......@@ -2897,6 +2897,11 @@ bool QRhiVulkan::isYUpInNDC() const
return false;
}
bool QRhiVulkan::isClipDepthZeroToOne() const
{
return true;
}
QMatrix4x4 QRhiVulkan::clipSpaceCorrMatrix() const
{
// See https://matthewwellings.com/blog/the-new-vulkan-coordinate-system/
......
......@@ -425,6 +425,7 @@ public:
int ubufAlignment() const override;
bool isYUpInFramebuffer() const override;
bool isYUpInNDC() const override;
bool isClipDepthZeroToOne() const override;
QMatrix4x4 clipSpaceCorrMatrix() const override;
bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags) const override;
bool isFeatureSupported(QRhi::Feature feature) const override;
......
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