Commit bb04781d authored by Laszlo Agocs's avatar Laszlo Agocs

Add texture comparison op to sampler

parent ebeae5dc
......@@ -94,7 +94,7 @@ void Window::customInit()
d.initialUpdates->uploadTexture(d.tex, desc);
d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat);
QRhiSampler::Repeat, QRhiSampler::Repeat);
d.releasePool << d.sampler;
d.sampler->build();
......
......@@ -2013,15 +2013,31 @@ bool QRhiTexture::buildFrom(const QRhiNativeHandles *src)
\value MirrorOnce
*/
/*!
\enum QRhiSampler::CompareOp
Specifies the texture comparison function.
\value Never (default)
\value Less
\value Equal
\value LessOrEqual
\value Greater
\value NotEqual
\value GreaterOrEqual
\value Always
*/
/*!
\internal
*/
QRhiSampler::QRhiSampler(QRhiImplementation *rhi,
Filter magFilter_, Filter minFilter_, Filter mipmapMode_,
AddressMode u_, AddressMode v_, AddressMode w_)
AddressMode u_, AddressMode v_)
: QRhiResource(rhi),
m_magFilter(magFilter_), m_minFilter(minFilter_), m_mipmapMode(mipmapMode_),
m_addressU(u_), m_addressV(v_), m_addressW(w_)
m_addressU(u_), m_addressV(v_),
m_addressW(QRhiSampler::ClampToEdge),
m_compareOp(QRhiSampler::Never)
{
}
......@@ -4251,16 +4267,16 @@ QRhiTexture *QRhi::newTexture(QRhiTexture::Format format,
/*!
\return a new sampler with the specified magnification filter \a magFilter,
minification filter \a minFilter, mipmapping mode \a mipmapMpde, and S/T/R
addressing modes \a u, \a v, and \a w.
minification filter \a minFilter, mipmapping mode \a mipmapMpde, and S/T
addressing modes \a u and \a v.
\sa QRhiResource::release(), QRhiResource::releaseAndDestroy()
*/
QRhiSampler *QRhi::newSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
QRhiSampler::Filter mipmapMode,
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w)
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v)
{
return d->createSampler(magFilter, minFilter, mipmapMode, u, v, w);
return d->createSampler(magFilter, minFilter, mipmapMode, u, v);
}
/*!
......
......@@ -722,6 +722,17 @@ public:
MirrorOnce
};
enum CompareOp {
Never,
Less,
Equal,
LessOrEqual,
Greater,
NotEqual,
GreaterOrEqual,
Always
};
Filter magFilter() const { return m_magFilter; }
void setMagFilter(Filter f) { m_magFilter = f; }
......@@ -740,18 +751,22 @@ public:
AddressMode addressW() const { return m_addressW; }
void setAddressW(AddressMode mode) { m_addressW = mode; }
CompareOp textureCompareOp() const { return m_compareOp; }
void setTextureCompareOp(CompareOp op) { m_compareOp = op; }
virtual bool build() = 0;
protected:
QRhiSampler(QRhiImplementation *rhi,
Filter magFilter_, Filter minFilter_, Filter mipmapMode_,
AddressMode u_, AddressMode v_, AddressMode w_);
AddressMode u_, AddressMode v_);
Filter m_magFilter;
Filter m_minFilter;
Filter m_mipmapMode;
AddressMode m_addressU;
AddressMode m_addressV;
AddressMode m_addressW;
CompareOp m_compareOp;
Q_DECL_UNUSED_MEMBER quint64 m_reserved;
};
......@@ -1289,7 +1304,7 @@ public:
QRhiSampler *newSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
QRhiSampler::Filter mipmapMode,
QRhiSampler::AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w = QRhiSampler::ClampToEdge);
QRhiSampler::AddressMode u, QRhiSampler::AddressMode v);
QRhiTextureRenderTarget *newTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
QRhiTextureRenderTarget::Flags flags = QRhiTextureRenderTarget::Flags());
......
......@@ -78,7 +78,7 @@ public:
QRhiTexture::Flags flags) = 0;
virtual QRhiSampler *createSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
QRhiSampler::Filter mipmapMode,
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w) = 0;
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v) = 0;
virtual QRhiTextureRenderTarget *createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
QRhiTextureRenderTarget::Flags flags) = 0;
......
......@@ -399,9 +399,9 @@ QRhiTexture *QRhiD3D11::createTexture(QRhiTexture::Format format, const QSize &p
QRhiSampler *QRhiD3D11::createSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
QRhiSampler::Filter mipmapMode,
QRhiSampler::AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w)
QRhiSampler::AddressMode u, QRhiSampler::AddressMode v)
{
return new QD3D11Sampler(this, magFilter, minFilter, mipmapMode, u, v, w);
return new QD3D11Sampler(this, magFilter, minFilter, mipmapMode, u, v);
}
QRhiTextureRenderTarget *QRhiD3D11::createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
......@@ -2155,8 +2155,8 @@ const QRhiNativeHandles *QD3D11Texture::nativeHandles()
}
QD3D11Sampler::QD3D11Sampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w)
: QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v, w)
AddressMode u, AddressMode v)
: QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v)
{
}
......@@ -2223,6 +2223,31 @@ static inline D3D11_TEXTURE_ADDRESS_MODE toD3DAddressMode(QRhiSampler::AddressMo
}
}
static inline D3D11_COMPARISON_FUNC toD3DTextureComparisonFunc(QRhiSampler::CompareOp op)
{
switch (op) {
case QRhiSampler::Never:
return D3D11_COMPARISON_NEVER;
case QRhiSampler::Less:
return D3D11_COMPARISON_LESS;
case QRhiSampler::Equal:
return D3D11_COMPARISON_EQUAL;
case QRhiSampler::LessOrEqual:
return D3D11_COMPARISON_LESS_EQUAL;
case QRhiSampler::Greater:
return D3D11_COMPARISON_GREATER;
case QRhiSampler::NotEqual:
return D3D11_COMPARISON_NOT_EQUAL;
case QRhiSampler::GreaterOrEqual:
return D3D11_COMPARISON_GREATER_EQUAL;
case QRhiSampler::Always:
return D3D11_COMPARISON_ALWAYS;
default:
Q_UNREACHABLE();
return D3D11_COMPARISON_NEVER;
}
}
bool QD3D11Sampler::build()
{
if (samplerState)
......@@ -2235,6 +2260,7 @@ bool QD3D11Sampler::build()
desc.AddressV = toD3DAddressMode(m_addressV);
desc.AddressW = toD3DAddressMode(m_addressW);
desc.MaxAnisotropy = 1.0f;
desc.ComparisonFunc = toD3DTextureComparisonFunc(m_compareOp);
desc.MaxLOD = m_mipmapMode == None ? 0.0f : 1000.0f;
QRHI_RES_RHI(QRhiD3D11);
......
......@@ -106,7 +106,7 @@ struct QD3D11Texture : public QRhiTexture
struct QD3D11Sampler : public QRhiSampler
{
QD3D11Sampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w);
AddressMode u, AddressMode v);
void release() override;
bool build() override;
......@@ -479,7 +479,7 @@ public:
QRhiTexture::Flags flags) override;
QRhiSampler *createSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
QRhiSampler::Filter mipmapMode,
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w) override;
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v) override;
QRhiTextureRenderTarget *createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
QRhiTextureRenderTarget::Flags flags) override;
......
......@@ -576,9 +576,9 @@ QRhiTexture *QRhiGles2::createTexture(QRhiTexture::Format format, const QSize &p
QRhiSampler *QRhiGles2::createSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
QRhiSampler::Filter mipmapMode,
QRhiSampler::AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w)
QRhiSampler::AddressMode u, QRhiSampler::AddressMode v)
{
return new QGles2Sampler(this, magFilter, minFilter, mipmapMode, u, v, w);
return new QGles2Sampler(this, magFilter, minFilter, mipmapMode, u, v);
}
QRhiTextureRenderTarget *QRhiGles2::createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
......@@ -2225,8 +2225,8 @@ const QRhiNativeHandles *QGles2Texture::nativeHandles()
}
QGles2Sampler::QGles2Sampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w)
: QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v, w)
AddressMode u, AddressMode v)
: QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v)
{
}
......
......@@ -104,7 +104,7 @@ struct QGles2Texture : public QRhiTexture
struct QGles2Sampler : public QRhiSampler
{
QGles2Sampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w);
AddressMode u, AddressMode v);
void release() override;
bool build() override;
......@@ -479,7 +479,7 @@ public:
QRhiTexture::Flags flags) override;
QRhiSampler *createSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
QRhiSampler::Filter mipmapMode,
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w) override;
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v) override;
QRhiTextureRenderTarget *createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
QRhiTextureRenderTarget::Flags flags) override;
......
......@@ -519,7 +519,7 @@ QRhiTexture *QRhiMetal::createTexture(QRhiTexture::Format format, const QSize &p
QRhiSampler *QRhiMetal::createSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
QRhiSampler::Filter mipmapMode,
QRhiSampler::AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w)
QRhiSampler::AddressMode u, QRhiSampler::AddressMode v)
{
return new QMetalSampler(this, magFilter, minFilter, mipmapMode, u, v, w);
}
......@@ -2115,8 +2115,8 @@ const QRhiNativeHandles *QMetalTexture::nativeHandles()
}
QMetalSampler::QMetalSampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w)
: QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v, w),
AddressMode u, AddressMode v)
: QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v),
d(new QMetalSamplerData)
{
}
......@@ -2190,6 +2190,31 @@ static inline MTLSamplerAddressMode toMetalAddressMode(QRhiSampler::AddressMode
}
}
static inline MTLCompareFunction toMetalTextureCompareFunction(QRhiSampler::CompareOp op)
{
switch (op) {
case QRhiSampler::Never:
return MTLCompareFunctionNever;
case QRhiSampler::Less:
return MTLCompareFunctionLess;
case QRhiSampler::Equal:
return MTLCompareFunctionEqual;
case QRhiSampler::LessOrEqual:
return MTLCompareFunctionLessEqual;
case QRhiSampler::Greater:
return MTLCompareFunctionGreater;
case QRhiSampler::NotEqual:
return MTLCompareFunctionNotEqual;
case QRhiSampler::GreaterOrEqual:
return MTLCompareFunctionGreaterEqual;
case QRhiSampler::Always:
return MTLCompareFunctionAlways;
default:
Q_UNREACHABLE();
return MTLCompareFunctionNever;
}
}
bool QMetalSampler::build()
{
if (d->samplerState)
......@@ -2202,6 +2227,7 @@ bool QMetalSampler::build()
desc.sAddressMode = toMetalAddressMode(m_addressU);
desc.tAddressMode = toMetalAddressMode(m_addressV);
desc.rAddressMode = toMetalAddressMode(m_addressW);
desc.compareFunction = toMetalTextureCompareFunction(m_compareOp);
QRHI_RES_RHI(QRhiMetal);
d->samplerState = [rhiD->d->dev newSamplerStateWithDescriptor: desc];
......
......@@ -114,7 +114,7 @@ struct QMetalSamplerData;
struct QMetalSampler : public QRhiSampler
{
QMetalSampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w);
AddressMode u, AddressMode v);
~QMetalSampler();
void release() override;
bool build() override;
......@@ -301,7 +301,7 @@ public:
QRhiTexture::Flags flags) override;
QRhiSampler *createSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
QRhiSampler::Filter mipmapMode,
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w) override;
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v) override;
QRhiTextureRenderTarget *createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
QRhiTextureRenderTarget::Flags flags) override;
......
......@@ -167,9 +167,9 @@ QRhiTexture *QRhiNull::createTexture(QRhiTexture::Format format, const QSize &pi
QRhiSampler *QRhiNull::createSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
QRhiSampler::Filter mipmapMode,
QRhiSampler::AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w)
QRhiSampler::AddressMode u, QRhiSampler::AddressMode v)
{
return new QNullSampler(this, magFilter, minFilter, mipmapMode, u, v, w);
return new QNullSampler(this, magFilter, minFilter, mipmapMode, u, v);
}
QRhiTextureRenderTarget *QRhiNull::createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
......@@ -425,8 +425,8 @@ const QRhiNativeHandles *QNullTexture::nativeHandles()
}
QNullSampler::QNullSampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w)
: QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v, w)
AddressMode u, AddressMode v)
: QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v)
{
}
......
......@@ -77,7 +77,7 @@ struct QNullTexture : public QRhiTexture
struct QNullSampler : public QRhiSampler
{
QNullSampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w);
AddressMode u, AddressMode v);
void release() override;
bool build() override;
};
......@@ -187,7 +187,7 @@ public:
QRhiTexture::Flags flags) override;
QRhiSampler *createSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
QRhiSampler::Filter mipmapMode,
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w) override;
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v) override;
QRhiTextureRenderTarget *createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
QRhiTextureRenderTarget::Flags flags) override;
......
......@@ -3022,9 +3022,9 @@ QRhiTexture *QRhiVulkan::createTexture(QRhiTexture::Format format, const QSize &
QRhiSampler *QRhiVulkan::createSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
QRhiSampler::Filter mipmapMode,
QRhiSampler::AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w)
QRhiSampler::AddressMode u, QRhiSampler::AddressMode v)
{
return new QVkSampler(this, magFilter, minFilter, mipmapMode, u, v, w);
return new QVkSampler(this, magFilter, minFilter, mipmapMode, u, v);
}
QRhiTextureRenderTarget *QRhiVulkan::createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
......@@ -3692,6 +3692,31 @@ static inline VkShaderStageFlags toVkShaderStageFlags(QRhiShaderResourceBinding:
return VkShaderStageFlags(s);
}
static inline VkCompareOp toVkTextureCompareOp(QRhiSampler::CompareOp op)
{
switch (op) {
case QRhiSampler::Never:
return VK_COMPARE_OP_NEVER;
case QRhiSampler::Less:
return VK_COMPARE_OP_LESS;
case QRhiSampler::Equal:
return VK_COMPARE_OP_EQUAL;
case QRhiSampler::LessOrEqual:
return VK_COMPARE_OP_LESS_OR_EQUAL;
case QRhiSampler::Greater:
return VK_COMPARE_OP_GREATER;
case QRhiSampler::NotEqual:
return VK_COMPARE_OP_NOT_EQUAL;
case QRhiSampler::GreaterOrEqual:
return VK_COMPARE_OP_GREATER_OR_EQUAL;
case QRhiSampler::Always:
return VK_COMPARE_OP_ALWAYS;
default:
Q_UNREACHABLE();
return VK_COMPARE_OP_NEVER;
}
}
QVkBuffer::QVkBuffer(QRhiImplementation *rhi, Type type, UsageFlags usage, int size)
: QRhiBuffer(rhi, type, usage, size)
{
......@@ -4107,8 +4132,8 @@ const QRhiNativeHandles *QVkTexture::nativeHandles()
}
QVkSampler::QVkSampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w)
: QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v, w)
AddressMode u, AddressMode v)
: QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v)
{
}
......@@ -4144,6 +4169,8 @@ bool QVkSampler::build()
samplerInfo.addressModeV = toVkAddressMode(m_addressV);
samplerInfo.addressModeW = toVkAddressMode(m_addressW);
samplerInfo.maxAnisotropy = 1.0f;
samplerInfo.compareEnable = m_compareOp != Never;
samplerInfo.compareOp = toVkTextureCompareOp(m_compareOp);
samplerInfo.maxLod = m_mipmapMode == None ? 0.25f : 1000.0f;
QRHI_RES_RHI(QRhiVulkan);
......
......@@ -130,7 +130,7 @@ struct QVkTexture : public QRhiTexture
struct QVkSampler : public QRhiSampler
{
QVkSampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w);
AddressMode u, AddressMode v);
void release() override;
bool build() override;
......@@ -372,7 +372,7 @@ public:
QRhiTexture::Flags flags) override;
QRhiSampler *createSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
QRhiSampler::Filter mipmapMode,
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w) override;
QRhiSampler:: AddressMode u, QRhiSampler::AddressMode v) override;
QRhiTextureRenderTarget *createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
QRhiTextureRenderTarget::Flags flags) 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