Commit 52c77e47 authored by Laszlo Agocs's avatar Laszlo Agocs
Browse files

Clean up qrhi.h

parent 55b6fd85
......@@ -67,20 +67,20 @@ void QRhiResource::releaseAndDestroy()
QRhiBuffer::QRhiBuffer(QRhiImplementation *rhi, Type type_, UsageFlags usage_, int size_)
: QRhiResource(rhi),
type(type_), usage(usage_), size(size_)
m_type(type_), m_usage(usage_), m_size(size_)
{
}
QRhiRenderBuffer::QRhiRenderBuffer(QRhiImplementation *rhi, Type type_, const QSize &pixelSize_,
int sampleCount_, Hints hints_)
: QRhiResource(rhi),
type(type_), pixelSize(pixelSize_), sampleCount(sampleCount_), hints(hints_)
m_type(type_), m_pixelSize(pixelSize_), m_sampleCount(sampleCount_), m_hints(hints_)
{
}
QRhiTexture::QRhiTexture(QRhiImplementation *rhi, Format format_, const QSize &pixelSize_, Flags flags_)
: QRhiResource(rhi),
format(format_), pixelSize(pixelSize_), flags(flags_)
m_format(format_), m_pixelSize(pixelSize_), m_flags(flags_)
{
}
......@@ -88,8 +88,8 @@ QRhiSampler::QRhiSampler(QRhiImplementation *rhi,
Filter magFilter_, Filter minFilter_, Filter mipmapMode_,
AddressMode u_, AddressMode v_, AddressMode w_)
: QRhiResource(rhi),
magFilter(magFilter_), minFilter(minFilter_), mipmapMode(mipmapMode_),
addressU(u_), addressV(v_), addressW(w_)
m_magFilter(magFilter_), m_minFilter(minFilter_), m_mipmapMode(mipmapMode_),
m_addressU(u_), m_addressV(v_), m_addressW(w_)
{
}
......@@ -112,8 +112,8 @@ QRhiTextureRenderTarget::QRhiTextureRenderTarget(QRhiImplementation *rhi,
const QRhiTextureRenderTargetDescription &desc_,
Flags flags_)
: QRhiRenderTarget(rhi),
desc(desc_),
flags(flags_)
m_desc(desc_),
m_flags(flags_)
{
}
......@@ -122,10 +122,10 @@ QRhiShaderResourceBindings::QRhiShaderResourceBindings(QRhiImplementation *rhi)
{
}
QRhiShaderResourceBindings::Binding QRhiShaderResourceBindings::Binding::uniformBuffer(
QRhiShaderResourceBinding QRhiShaderResourceBinding::uniformBuffer(
int binding_, StageFlags stage_, QRhiBuffer *buf_)
{
Binding b;
QRhiShaderResourceBinding b;
b.binding = binding_;
b.stage = stage_;
b.type = UniformBuffer;
......@@ -135,11 +135,11 @@ QRhiShaderResourceBindings::Binding QRhiShaderResourceBindings::Binding::uniform
return b;
}
QRhiShaderResourceBindings::Binding QRhiShaderResourceBindings::Binding::uniformBuffer(
QRhiShaderResourceBinding QRhiShaderResourceBinding::uniformBuffer(
int binding_, StageFlags stage_, QRhiBuffer *buf_, int offset_, int size_)
{
Q_ASSERT(size_ > 0);
Binding b;
QRhiShaderResourceBinding b;
b.binding = binding_;
b.stage = stage_;
b.type = UniformBuffer;
......@@ -149,10 +149,10 @@ QRhiShaderResourceBindings::Binding QRhiShaderResourceBindings::Binding::uniform
return b;
}
QRhiShaderResourceBindings::Binding QRhiShaderResourceBindings::Binding::sampledTexture(
QRhiShaderResourceBinding QRhiShaderResourceBinding::sampledTexture(
int binding_, StageFlags stage_, QRhiTexture *tex_, QRhiSampler *sampler_)
{
Binding b;
QRhiShaderResourceBinding b;
b.binding = binding_;
b.stage = stage_;
b.type = SampledTexture;
......@@ -265,7 +265,7 @@ void QRhiResourceUpdateBatch::uploadStaticBuffer(QRhiBuffer *buf, const void *da
d->staticBufferUploads.append({ buf, data });
}
void QRhiResourceUpdateBatch::uploadTexture(QRhiTexture *tex, const TextureUploadDescription &desc)
void QRhiResourceUpdateBatch::uploadTexture(QRhiTexture *tex, const QRhiTextureUploadDescription &desc)
{
d->textureUploads.append({ tex, desc });
}
......
......@@ -48,8 +48,13 @@
QT_BEGIN_NAMESPACE
class QRhiImplementation;
class QWindow;
class QRhiImplementation;
class QRhiBuffer;
class QRhiRenderBuffer;
class QRhiTexture;
class QRhiSampler;
struct QRhiResourceUpdateBatchPrivate;
struct Q_RHI_EXPORT QRhiColorClearValue
{
......@@ -97,7 +102,6 @@ struct Q_RHI_EXPORT QRhiScissor
Q_DECLARE_TYPEINFO(QRhiScissor, Q_MOVABLE_TYPE);
// should be mappable to D3D12_INPUT_ELEMENT_DESC + D3D12_VERTEX_BUFFER_VIEW...
struct Q_RHI_EXPORT QRhiVertexInputLayout
{
struct Q_RHI_EXPORT Binding {
......@@ -109,8 +113,7 @@ struct Q_RHI_EXPORT QRhiVertexInputLayout
Binding(quint32 stride_, Classification cls = PerVertex)
: stride(stride_), classification(cls)
{ }
// stride must be a multiple of 4
quint32 stride; // if another api needs this in setVertexInput, make the cb store a ptr to the current ps and look up the stride via that
quint32 stride; // must be a multiple of 4
Classification classification;
};
......@@ -165,6 +168,99 @@ struct Q_RHI_EXPORT QRhiGraphicsShaderStage
Q_DECLARE_TYPEINFO(QRhiGraphicsShaderStage, Q_MOVABLE_TYPE);
struct Q_RHI_EXPORT QRhiShaderResourceBinding
{
enum Type {
UniformBuffer,
SampledTexture
};
enum StageFlag {
VertexStage = 1 << 0,
FragmentStage = 1 << 1,
GeometryStage = 1 << 2,
TessellationControlStage = 1 << 3,
TessellationEvaluationStage = 1 << 4
};
Q_DECLARE_FLAGS(StageFlags, StageFlag)
static QRhiShaderResourceBinding uniformBuffer(int binding_, StageFlags stage_, QRhiBuffer *buf_);
// Bind a region only. Up to the user to ensure offset is aligned to ubufAlignment.
static QRhiShaderResourceBinding uniformBuffer(int binding_, StageFlags stage_, QRhiBuffer *buf_, int offset_, int size_);
static QRhiShaderResourceBinding sampledTexture(int binding_, StageFlags stage_, QRhiTexture *tex_, QRhiSampler *sampler_);
int binding;
StageFlags stage;
Type type;
struct UniformBufferData {
QRhiBuffer *buf;
int offset;
int maybeSize;
};
struct SampledTextureData {
QRhiTexture *tex;
QRhiSampler *sampler;
};
union {
UniformBufferData ubuf;
SampledTextureData stex;
};
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiShaderResourceBinding::StageFlags)
Q_DECLARE_TYPEINFO(QRhiShaderResourceBinding, Q_MOVABLE_TYPE);
struct Q_RHI_EXPORT QRhiTextureRenderTargetDescription
{
struct Q_RHI_EXPORT ColorAttachment {
ColorAttachment() { }
ColorAttachment(QRhiTexture *texture_) : texture(texture_) { }
QRhiTexture *texture = nullptr;
int layer = 0; // face (0..5) for cubemaps
};
QRhiTextureRenderTargetDescription()
{ }
QRhiTextureRenderTargetDescription(const ColorAttachment &colorAttachment)
{ colorAttachments.append(colorAttachment); }
QRhiTextureRenderTargetDescription(const ColorAttachment &colorAttachment, QRhiRenderBuffer *depthStencilBuffer_)
: depthStencilBuffer(depthStencilBuffer_)
{ colorAttachments.append(colorAttachment); }
QRhiTextureRenderTargetDescription(const ColorAttachment &colorAttachment, QRhiTexture *depthTexture_)
: depthTexture(depthTexture_)
{ colorAttachments.append(colorAttachment); }
QVector<ColorAttachment> colorAttachments;
QRhiRenderBuffer *depthStencilBuffer = nullptr;
QRhiTexture *depthTexture = nullptr;
};
Q_DECLARE_TYPEINFO(QRhiTextureRenderTargetDescription::ColorAttachment, Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(QRhiTextureRenderTargetDescription, Q_MOVABLE_TYPE);
struct Q_RHI_EXPORT QRhiTextureUploadDescription
{
struct Q_RHI_EXPORT Layer {
struct Q_RHI_EXPORT MipLevel {
MipLevel() { }
MipLevel(const QImage &image_) : image(image_) { }
QImage image;
};
Layer() { }
Layer(const QVector<MipLevel> &mipImages_) : mipImages(mipImages_) { }
QVector<MipLevel> mipImages;
};
QRhiTextureUploadDescription() { }
QRhiTextureUploadDescription(const QVector<Layer> &layers_) : layers(layers_) { }
QVector<Layer> layers; // 6 layers for cubemaps, 1 otherwise
};
Q_DECLARE_TYPEINFO(QRhiTextureUploadDescription::Layer::MipLevel, Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(QRhiTextureUploadDescription::Layer, Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(QRhiTextureUploadDescription, Q_MOVABLE_TYPE);
class Q_RHI_EXPORT QRhiResource
{
public:
......@@ -194,14 +290,23 @@ public:
};
Q_DECLARE_FLAGS(UsageFlags, UsageFlag)
Type type;
UsageFlags usage;
int size; // no restrictions here, up to the backend to round up if needed
Type type() const { return m_type; }
void setType(Type t) { m_type = t; }
UsageFlags usage() const { return m_usage; }
void setUsage(UsageFlags u) { m_usage = u; }
int size() const { return m_size; }
void setSize(int sz) { m_size = sz; }
virtual bool build() = 0;
protected:
QRhiBuffer(QRhiImplementation *rhi, Type type_, UsageFlags usage_, int size_);
Type m_type;
UsageFlags m_usage;
int m_size; // no restrictions here, up to the backend to round up if needed
void *m_reserved;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiBuffer::UsageFlags)
......@@ -218,16 +323,28 @@ public:
};
Q_DECLARE_FLAGS(Hints, Hint)
Type type;
QSize pixelSize;
int sampleCount;
Hints hints;
Type type() const { return m_type; }
void setType(Type t) { m_type = t; }
QSize pixelSize() const { return m_pixelSize; }
void setPixelSize(const QSize &sz) { m_pixelSize = sz; }
int sampleCount() const { return m_sampleCount; }
void setSampleCount(int s) { m_sampleCount = s; }
Hints hints() const { return m_hints; }
void setHints(Hints h) { m_hints = h; }
virtual bool build() = 0;
protected:
QRhiRenderBuffer(QRhiImplementation *rhi, Type type_, const QSize &pixelSize_,
int sampleCount_, Hints hints_);
Type m_type;
QSize m_pixelSize;
int m_sampleCount;
Hints m_hints;
void *m_reserved;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiRenderBuffer::Hints)
......@@ -253,14 +370,23 @@ public:
D32
};
Format format;
QSize pixelSize;
Flags flags;
Format format() const { return m_format; }
void setFormat(Format fmt) { m_format = fmt; }
QSize pixelSize() const { return m_pixelSize; }
void setPixelSize(const QSize &sz) { m_pixelSize = sz; }
Flags flags() const { return m_flags; }
void setFlags(Flags f) { m_flags = f; }
virtual bool build() = 0;
protected:
QRhiTexture(QRhiImplementation *rhi, Format format_, const QSize &pixelSize_, Flags flags_);
Format m_format;
QSize m_pixelSize;
Flags m_flags;
void *m_reserved;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiTexture::Flags)
......@@ -282,12 +408,23 @@ public:
MirrorOnce
};
Filter magFilter;
Filter minFilter;
Filter mipmapMode;
AddressMode addressU;
AddressMode addressV;
AddressMode addressW;
Filter magFilter() const { return m_magFilter; }
void setMagFilter(Filter f) { m_magFilter = f; }
Filter minFilter() const { return m_minFilter; }
void setMinFilter(Filter f) { m_minFilter = f; }
Filter mipmapMode() const { return m_mipmapMode; }
void setMipmapMode(Filter f) { m_mipmapMode = f; }
AddressMode addressU() const { return m_addressU; }
void setAddressU(AddressMode mode) { m_addressU = mode; }
AddressMode addressV() const { return m_addressV; }
void setAddressV(AddressMode mode) { m_addressV = mode; }
AddressMode addressW() const { return m_addressW; }
void setAddressW(AddressMode mode) { m_addressW = mode; }
virtual bool build() = 0;
......@@ -295,12 +432,20 @@ protected:
QRhiSampler(QRhiImplementation *rhi,
Filter magFilter_, Filter minFilter_, Filter mipmapMode_,
AddressMode u_, AddressMode v_, AddressMode w_);
Filter m_magFilter;
Filter m_minFilter;
Filter m_mipmapMode;
AddressMode m_addressU;
AddressMode m_addressV;
AddressMode m_addressW;
void *m_reserved;
};
class Q_RHI_EXPORT QRhiRenderPass : public QRhiResource
{
protected:
QRhiRenderPass(QRhiImplementation *rhi);
void *m_reserved;
};
class Q_RHI_EXPORT QRhiRenderTarget : public QRhiResource
......@@ -317,6 +462,7 @@ public:
protected:
QRhiRenderTarget(QRhiImplementation *rhi);
void *m_reserved;
};
class Q_RHI_EXPORT QRhiReferenceRenderTarget : public QRhiRenderTarget
......@@ -325,34 +471,6 @@ protected:
QRhiReferenceRenderTarget(QRhiImplementation *rhi);
};
struct Q_RHI_EXPORT QRhiTextureRenderTargetDescription
{
struct Q_RHI_EXPORT ColorAttachment {
ColorAttachment() { }
ColorAttachment(QRhiTexture *texture_) : texture(texture_) { }
QRhiTexture *texture = nullptr;
int layer = 0; // face (0..5) for cubemaps
};
QRhiTextureRenderTargetDescription()
{ }
QRhiTextureRenderTargetDescription(const ColorAttachment &colorAttachment)
{ colorAttachments.append(colorAttachment); }
QRhiTextureRenderTargetDescription(const ColorAttachment &colorAttachment, QRhiRenderBuffer *depthStencilBuffer_)
: depthStencilBuffer(depthStencilBuffer_)
{ colorAttachments.append(colorAttachment); }
QRhiTextureRenderTargetDescription(const ColorAttachment &colorAttachment, QRhiTexture *depthTexture_)
: depthTexture(depthTexture_)
{ colorAttachments.append(colorAttachment); }
QVector<ColorAttachment> colorAttachments;
QRhiRenderBuffer *depthStencilBuffer = nullptr;
QRhiTexture *depthTexture = nullptr;
};
Q_DECLARE_TYPEINFO(QRhiTextureRenderTargetDescription::ColorAttachment, Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(QRhiTextureRenderTargetDescription, Q_MOVABLE_TYPE);
class Q_RHI_EXPORT QRhiTextureRenderTarget : public QRhiRenderTarget
{
public:
......@@ -361,13 +479,19 @@ public:
};
Q_DECLARE_FLAGS(Flags, Flag)
QRhiTextureRenderTargetDescription desc;
Flags flags;
QRhiTextureRenderTargetDescription description() const { return m_desc; }
void setDescription(const QRhiTextureRenderTargetDescription &desc) { m_desc = desc; }
Flags flags() const { return m_flags; }
void setFlags(Flags f) { m_flags = f; }
// as usual, textures in desc must be built before calling build() on the rt
virtual bool build() = 0;
protected:
QRhiTextureRenderTarget(QRhiImplementation *rhi, const QRhiTextureRenderTargetDescription &desc_, Flags flags_);
QRhiTextureRenderTargetDescription m_desc;
Flags m_flags;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiTextureRenderTarget::Flags)
......@@ -375,57 +499,17 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiTextureRenderTarget::Flags)
class Q_RHI_EXPORT QRhiShaderResourceBindings : public QRhiResource
{
public:
struct Q_RHI_EXPORT Binding {
enum Type {
UniformBuffer,
SampledTexture
};
enum StageFlag {
VertexStage = 1 << 0,
FragmentStage = 1 << 1,
GeometryStage = 1 << 2,
TessellationControlStage = 1 << 3,
TessellationEvaluationStage = 1 << 4
};
Q_DECLARE_FLAGS(StageFlags, StageFlag)
static Binding uniformBuffer(int binding_, StageFlags stage_, QRhiBuffer *buf_);
// Bind a region only. Up to the user to ensure offset is aligned to ubufAlignment.
static Binding uniformBuffer(int binding_, StageFlags stage_, QRhiBuffer *buf_, int offset_, int size_);
static Binding sampledTexture(int binding_, StageFlags stage_, QRhiTexture *tex_, QRhiSampler *sampler_);
int binding;
StageFlags stage;
Type type;
struct UniformBufferData {
QRhiBuffer *buf;
int offset;
int maybeSize;
};
struct SampledTextureData {
QRhiTexture *tex;
QRhiSampler *sampler;
};
union {
UniformBufferData ubuf;
SampledTextureData stex;
};
};
QVector<Binding> bindings;
QVector<QRhiShaderResourceBinding> bindings() const { return m_bindings; }
void setBindings(const QVector<QRhiShaderResourceBinding> &b) { m_bindings = b; }
virtual bool build() = 0;
protected:
QRhiShaderResourceBindings(QRhiImplementation *rhi);
QVector<QRhiShaderResourceBinding> m_bindings;
void *m_reserved;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiShaderResourceBindings::Binding::StageFlags)
Q_DECLARE_TYPEINFO(QRhiShaderResourceBindings::Binding, Q_MOVABLE_TYPE);
class Q_RHI_EXPORT QRhiGraphicsPipeline : public QRhiResource
{
public:
......@@ -533,31 +617,83 @@ public:
CompareOp compareOp = Always;
};
Flags flags;
Topology topology = Triangles;
CullMode cullMode = None;
FrontFace frontFace = CCW;
QVector<TargetBlend> targetBlends; // no blend when empty
bool depthTest = false;
bool depthWrite = false;
CompareOp depthOp = Less;
bool stencilTest = false;
StencilOpState stencilFront;
StencilOpState stencilBack;
// use the same read (compare) and write masks for both faces (see d3d).
// have the reference value dynamically settable.
quint32 stencilReadMask = 0xFF;
quint32 stencilWriteMask = 0xFF;
int sampleCount = 1; // MSAA, swapchain+depthstencil must match
QVector<QRhiGraphicsShaderStage> shaderStages;
QRhiVertexInputLayout vertexInputLayout;
QRhiShaderResourceBindings *shaderResourceBindings = nullptr; // must be built by the time ps' build() is called
const QRhiRenderPass *renderPass = nullptr;
Flags flags() const { return m_flags; }
void setFlags(Flags f) { m_flags = f; }
Topology topology() const { return m_topology; }
void setTopology(Topology t) { m_topology = t; }
CullMode cullMode() const { return m_cullMode; }
void setCullMode(CullMode mode) { m_cullMode = mode; }
FrontFace frontFace() const { return m_frontFace; }
void setFrontFace(FrontFace f) { m_frontFace = f; }
QVector<TargetBlend> targetBlends() const { return m_targetBlends; }
void setTargetBlends(const QVector<TargetBlend> &blends) { m_targetBlends = blends; }
bool hasDepthTest() const { return m_depthTest; }
void setDepthTest(bool enable) { m_depthTest = enable; }
bool hasDepthWrite() const { return m_depthWrite; }
void setDepthWrite(bool enable) { m_depthWrite = enable; }
CompareOp depthOp() const { return m_depthOp; }
void setDepthOp(CompareOp op) { m_depthOp = op; }
bool hasStencilTest() const { return m_stencilTest; }
void setStencilTest(bool enable) { m_stencilTest = enable; }
StencilOpState stencilFront() const { return m_stencilFront; }
void setStencilFront(const StencilOpState &state) { m_stencilFront = state; }
StencilOpState stencilBack() const { return m_stencilBack; }
void setStencilBack(const StencilOpState &state) { m_stencilBack = state; }
quint32 stencilReadMask() const { return m_stencilReadMask; }
void setStencilReadMask(quint32 mask) { m_stencilReadMask = mask; }
quint32 stencilWriteMask() const { return m_stencilWriteMask; }
void setStencilWriteMask(quint32 mask) { m_stencilWriteMask = mask; }
int sampleCount() const { return m_sampleCount; }
void setSampleCount(int s) { m_sampleCount = s; }
QVector<QRhiGraphicsShaderStage> shaderStages() const { return m_shaderStages; }
void setShaderStages(const QVector<QRhiGraphicsShaderStage> &stages) { m_shaderStages = stages; }
QRhiVertexInputLayout vertexInputLayout() const { return m_vertexInputLayout; }
void setVertexInputLayout(const QRhiVertexInputLayout &layout) { m_vertexInputLayout = layout; }
QRhiShaderResourceBindings *shaderResourceBindings() const { return m_shaderResourceBindings; }
void setShaderResourceBindings(QRhiShaderResourceBindings *srb) { m_shaderResourceBindings = srb; }
const QRhiRenderPass *renderPass() const { return m_renderPass; }
void setRenderPass(const QRhiRenderPass *pass) { m_renderPass = pass; }
virtual bool build() = 0;
protected:
QRhiGraphicsPipeline(QRhiImplementation *rhi);
Flags m_flags;
Topology m_topology = Triangles;
CullMode m_cullMode = None;
FrontFace m_frontFace = CCW;
QVector<TargetBlend> m_targetBlends; // no blend when empty
bool m_depthTest = false;
bool m_depthWrite = false;
CompareOp m_depthOp = Less;
bool m_stencilTest = false;
StencilOpState m_stencilFront;
StencilOpState m_stencilBack;
quint32 m_stencilReadMask = 0xFF; // applies to both faces
quint32 m_stencilWriteMask = 0xFF; // applies to both faces
int m_sampleCount = 1; // MSAA, swapchain+depthstencil must match
QVector<QRhiGraphicsShaderStage> m_shaderStages;
QRhiVertexInputLayout m_vertexInputLayout;
QRhiShaderResourceBindings *m_shaderResourceBindings = nullptr; // must be built by the time ps' build() is called
const QRhiRenderPass *m_renderPass = nullptr;
void *m_reserved;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiGraphicsPipeline::Flags)
......@@ -568,6 +704,7 @@ class Q_RHI_EXPORT QRhiCommandBuffer : public QRhiResource
{
protected:
QRhiCommandBuffer(QRhiImplementation *rhi);
void *m_reserved;
};
class Q_RHI_EXPORT QRhiSwapChain : public QRhiResource
......@@ -598,31 +735,14 @@ public:
protected:
QRhiSwapChain(QRhiImplementation *rhi);
void *m_reserved;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiSwapChain::SurfaceImportFlags)
struct QRhiResourceUpdateBatchPrivate;
class Q_RHI_EXPORT QRhiResourceUpdateBatch // sort of a command buffer for copy type of operations
{
public:
struct TextureUploadDescription {
struct Layer {
struct MipLevel {
MipLevel() { }
MipLevel(const QImage &image_) : image(image_) { }
QImage image;
};
Layer() { }
Layer(const QVector<MipLevel> &mipImages_) : mipImages(mipImages_) { }
QVector<MipLevel> mipImages;
};
TextureUploadDescription() { }
TextureUploadDescription(const QVector<Layer> &layers_) : layers(layers_) { }
QVector<Layer> layers; // 6 layers for cubemaps, 1 otherwise
};
~QRhiResourceUpdateBatch();