Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions plume_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,15 @@ namespace plume {
}
}

void D3D12CommandList::setBlendConstants(const float rgba[4]) {
d3d->OMSetBlendFactor(rgba);
}

void D3D12CommandList::setStencilReference(uint32_t reference) {
d3d->OMSetStencilRef(reference);
activeStencilRef = reference;
}

void D3D12CommandList::setDepthBias(float depthBias, float depthBiasClamp, float slopeScaledDepthBias) {
# ifdef PLUME_D3D12_AGILITY_SDK_ENABLED
assert(d3dV9 != nullptr);
Expand Down Expand Up @@ -2472,6 +2481,10 @@ namespace plume {
assert(activeGraphicsPipeline->type == D3D12Pipeline::Type::Graphics);

const D3D12GraphicsPipeline *graphicsPipeline = static_cast<const D3D12GraphicsPipeline *>(activeGraphicsPipeline);
if (graphicsPipeline->dynamicStencilRef) {
return;
}

if (activeStencilRef != graphicsPipeline->stencilRef) {
d3d->OMSetStencilRef(graphicsPipeline->stencilRef);
activeStencilRef = graphicsPipeline->stencilRef;
Expand Down Expand Up @@ -3086,6 +3099,7 @@ namespace plume {
assert(desc.pipelineLayout != nullptr);

topology = toD3D12(desc.primitiveTopology);
dynamicStencilRef = desc.dynamicStencilReferenceEnabled;

const D3D12PipelineLayout *pipelineLayout = static_cast<const D3D12PipelineLayout *>(desc.pipelineLayout);
const D3D12Shader *vertexShader = static_cast<const D3D12Shader *>(desc.vertexShader);
Expand Down
3 changes: 3 additions & 0 deletions plume_d3d12.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ namespace plume {
void setScissors(const RenderRect *scissorRects, uint32_t count) override;
void setFramebuffer(const RenderFramebuffer *framebuffer) override;
void setDepthBias(float depthBias, float depthBiasClamp, float slopeScaledDepthBias) override;
void setBlendConstants(const float rgba[4]) override;
void setStencilReference(uint32_t reference) override;
void clearColor(uint32_t attachmentIndex, RenderColor colorValue, const RenderRect *clearRects, uint32_t clearRectsCount) override;
void clearDepthStencil(bool clearDepth, bool clearStencil, float depthValue, uint32_t stencilValue, const RenderRect *clearRects, uint32_t clearRectsCount) override;
void copyBufferRegion(RenderBufferReference dstBuffer, RenderBufferReference srcBuffer, uint64_t size) override;
Expand Down Expand Up @@ -397,6 +399,7 @@ namespace plume {
std::vector<RenderInputSlot> inputSlots;
D3D12_PRIMITIVE_TOPOLOGY topology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
uint32_t stencilRef = 0;
bool dynamicStencilRef = false;

D3D12GraphicsPipeline(D3D12Device *device, const RenderGraphicsPipelineDesc &desc);
~D3D12GraphicsPipeline() override;
Expand Down
24 changes: 21 additions & 3 deletions plume_metal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,7 @@ namespace plume {
state.renderPipelineState = device->mtl->newRenderPipelineState(descriptor, &error);
state.primitiveType = mapPrimitiveType(desc.primitiveTopology);
state.stencilReference = desc.stencilEnabled ? desc.stencilReference : 0;
state.dynamicStencilReferenceEnabled = desc.dynamicStencilReferenceEnabled;

if (desc.dynamicDepthBiasEnabled) {
state.dynamicDepthBiasEnabled = true;
Expand Down Expand Up @@ -2750,6 +2751,16 @@ namespace plume {
}
}

void MetalCommandList::setBlendConstants(const float rgba[4]) {
std::copy(rgba, rgba + 4, dynamicBlendConstants);
dirtyGraphicsState.blendConstants = 1;
}

void MetalCommandList::setStencilReference(uint32_t reference) {
dynamicStencilReference = reference;
dirtyGraphicsState.depthStencil = 1;
}

void MetalCommandList::setDepthBias(float depthBias, float depthBiasClamp, float slopeScaledDepthBias) {
dynamicDepthBias.depthBias = depthBias;
dynamicDepthBias.depthBiasClamp = depthBiasClamp;
Expand Down Expand Up @@ -3444,9 +3455,10 @@ namespace plume {
activeRenderEncoder->setDepthStencilState(activeRenderState->depthStencilState);
stateCache.lastDepthStencilState = activeRenderState->depthStencilState;
}
if (activeRenderState->stencilReference != stateCache.lastStencilReference) {
activeRenderEncoder->setStencilReferenceValue(activeRenderState->stencilReference);
stateCache.lastStencilReference = activeRenderState->stencilReference;
const uint32_t stencilReference = activeRenderState->dynamicStencilReferenceEnabled ? dynamicStencilReference : activeRenderState->stencilReference;
if (stencilReference != stateCache.lastStencilReference) {
activeRenderEncoder->setStencilReferenceValue(stencilReference);
stateCache.lastStencilReference = stencilReference;
}
}
dirtyGraphicsState.depthStencil = 0;
Expand All @@ -3471,6 +3483,12 @@ namespace plume {
dirtyGraphicsState.rasterizer = 0;
}

// Blend constants
if (dirtyGraphicsState.blendConstants) {
activeRenderEncoder->setBlendColor(dynamicBlendConstants[0], dynamicBlendConstants[1], dynamicBlendConstants[2], dynamicBlendConstants[3]);
dirtyGraphicsState.blendConstants = 0;
}

// Depth bias
if (dirtyGraphicsState.depthBias) {
float newDepthBias, newSlopeScale, newClamp;
Expand Down
9 changes: 8 additions & 1 deletion plume_metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ namespace plume {
uint32_t depthBias : 1;
uint32_t depthStencil : 1;
uint32_t rasterizer : 1;
uint32_t blendConstants : 1;
};
uint32_t value;
};
Expand All @@ -129,7 +130,7 @@ namespace plume {
uint32_t vertexBufferSlots;

void setAll() {
value = 0x1FF; // All 9 bits set
value = 0x3FF; // All 10 bits set
descriptorSetDirtyIndex = 0;
vertexBufferSlots = (1U << MAX_VERTEX_BUFFER_BINDINGS) - 1;
}
Expand Down Expand Up @@ -197,6 +198,7 @@ namespace plume {
float depthBiasClamp;
float depthBiasSlopeFactor;
bool dynamicDepthBiasEnabled;
bool dynamicStencilReferenceEnabled = false;
};

struct ExtendedRenderTexture : RenderTexture {
Expand Down Expand Up @@ -428,6 +430,9 @@ namespace plume {
float slopeScaledDepthBias;
} dynamicDepthBias;

uint32_t dynamicStencilReference = 0;
float dynamicBlendConstants[4] = {};

struct {
uint32_t updateDirtyBits = ~0;
int update[MetalBarrierStage::COUNT] = {};
Expand Down Expand Up @@ -481,6 +486,8 @@ namespace plume {
void setScissors(const RenderRect *scissorRects, uint32_t count) override;
void setFramebuffer(const RenderFramebuffer *framebuffer) override;
void setDepthBias(float depthBias, float depthBiasClamp, float slopeScaledDepthBias) override;
void setBlendConstants(const float rgba[4]) override;
void setStencilReference(uint32_t reference) override;
void clearColor(uint32_t attachmentIndex, RenderColor colorValue, const RenderRect *clearRects, uint32_t clearRectsCount) override;
void clearDepthStencil(bool clearDepth, bool clearStencil, float depthValue, uint32_t stencilValue, const RenderRect *clearRects, uint32_t clearRectsCount) override;
void copyBufferRegion(RenderBufferReference dstBuffer, RenderBufferReference srcBuffer, uint64_t size) override;
Expand Down
13 changes: 13 additions & 0 deletions plume_render_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ namespace plume {
virtual void setScissors(const RenderRect *scissorRects, uint32_t count) = 0;
virtual void setFramebuffer(const RenderFramebuffer *framebuffer) = 0;
virtual void setDepthBias(float depthBias, float depthBiasClamp, float slopeScaledDepthBias) = 0;
// Blend constants (BLEND_FACTOR / INV_BLEND_FACTOR) for pipelines created with dynamicBlendConstantsEnabled.
virtual void setBlendConstants(const float rgba[4]) = 0;
// Stencil reference (front and back) for pipelines created with dynamicStencilReferenceEnabled.
// Like other dynamic state, set it again after binding a pipeline that has it static.
virtual void setStencilReference(uint32_t reference) = 0;
virtual void clearColor(uint32_t attachmentIndex = 0, RenderColor colorValue = RenderColor(), const RenderRect *clearRects = nullptr, uint32_t clearRectsCount = 0) = 0;
virtual void clearDepthStencil(bool clearDepth = true, bool clearStencil = true, float depthValue = 1.0f, uint32_t stencilValue = 0, const RenderRect *clearRects = nullptr, uint32_t clearRectsCount = 0) = 0;
virtual void copyBufferRegion(RenderBufferReference dstBuffer, RenderBufferReference srcBuffer, uint64_t size) = 0;
Expand All @@ -150,6 +155,10 @@ namespace plume {
virtual void discardTexture(const RenderTexture* texture) = 0; // D3D12 only.
virtual void resetQueryPool(const RenderQueryPool *queryPool, uint32_t queryFirstIndex, uint32_t queryCount) = 0;
virtual void writeTimestamp(const RenderQueryPool *queryPool, uint32_t queryIndex) = 0;
// Occlusion queries (samples that pass the depth and stencil tests) in a pool from
// RenderDevice::createOcclusionQueryPool.
virtual void beginQuery(const RenderQueryPool *queryPool, uint32_t queryIndex) { (void)queryPool; (void)queryIndex; }
virtual void endQuery(const RenderQueryPool *queryPool, uint32_t queryIndex) { (void)queryPool; (void)queryIndex; }

// Concrete implementation shortcuts.
inline void barriers(RenderBarrierStages stages, const RenderBufferBarrier &barrier) {
Expand Down Expand Up @@ -221,6 +230,8 @@ namespace plume {
virtual void queryResults() = 0;
virtual const uint64_t *getResults() const = 0;
virtual uint32_t getCount() const = 0;
// Results of queries [first, first + count) without waiting; false if any is not available yet.
virtual bool getRawResults(uint32_t first, uint32_t count, uint64_t *out) { (void)first; (void)count; (void)out; return false; }
};

struct RenderDevice {
Expand All @@ -241,6 +252,8 @@ namespace plume {
virtual std::unique_ptr<RenderCommandSemaphore> createCommandSemaphore() = 0;
virtual std::unique_ptr<RenderFramebuffer> createFramebuffer(const RenderFramebufferDesc &desc) = 0;
virtual std::unique_ptr<RenderQueryPool> createQueryPool(uint32_t queryCount) = 0;
// nullptr if the backend has no occlusion queries (only Vulkan has them so far).
virtual std::unique_ptr<RenderQueryPool> createOcclusionQueryPool(uint32_t queryCount) { (void)queryCount; return nullptr; }
virtual void setBottomLevelASBuildInfo(RenderBottomLevelASBuildInfo &buildInfo, const RenderBottomLevelASMesh *meshes, uint32_t meshCount, bool preferFastBuild = true, bool preferFastTrace = false) = 0;
virtual void setTopLevelASBuildInfo(RenderTopLevelASBuildInfo &buildInfo, const RenderTopLevelASInstance *instances, uint32_t instanceCount, bool preferFastBuild = true, bool preferFastTrace = false) = 0;
virtual void setShaderBindingTableInfo(RenderShaderBindingTableInfo &tableInfo, const RenderShaderBindingGroups &groups, const RenderPipeline *pipeline, RenderDescriptorSet **descriptorSets, uint32_t descriptorSetCount) = 0;
Expand Down
50 changes: 50 additions & 0 deletions plume_render_interface_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ namespace plume {
BC7_TYPELESS,
BC7_UNORM,
BC7_UNORM_SRGB,
R16G16_SSCALED,
R16G16B16A16_SSCALED,
// Vertex formats: USCALED/SSCALED are unnormalized integers read as floats.
R16G16B16A16_USCALED,
R8G8B8A8_USCALED,
R8G8B8A8_SSCALED,
R16G16_USCALED,
R8G8_USCALED,
R8G8_SSCALED,
R16_USCALED,
R16_SSCALED,
R8_USCALED,
R8_SSCALED,
A2B10G10R10_UNORM_PACK32,
A2B10G10R10_SNORM_PACK32,
MAX
};

Expand Down Expand Up @@ -597,6 +612,24 @@ namespace plume {
case RenderFormat::BC7_UNORM:
case RenderFormat::BC7_UNORM_SRGB:
return 16;
case RenderFormat::R16G16B16A16_SSCALED:
case RenderFormat::R16G16B16A16_USCALED:
return 8;
case RenderFormat::A2B10G10R10_UNORM_PACK32:
case RenderFormat::A2B10G10R10_SNORM_PACK32:
case RenderFormat::R8G8B8A8_USCALED:
case RenderFormat::R8G8B8A8_SSCALED:
case RenderFormat::R16G16_SSCALED:
case RenderFormat::R16G16_USCALED:
return 4;
case RenderFormat::R8G8_USCALED:
case RenderFormat::R8G8_SSCALED:
case RenderFormat::R16_USCALED:
case RenderFormat::R16_SSCALED:
return 2;
case RenderFormat::R8_USCALED:
case RenderFormat::R8_SSCALED:
return 1;
default:
assert(false && "Unknown format.");
return 1;
Expand Down Expand Up @@ -681,6 +714,21 @@ namespace plume {
case RenderFormat::BC7_UNORM:
case RenderFormat::BC7_UNORM_SRGB:
return 4;
case RenderFormat::R16G16B16A16_SSCALED:
case RenderFormat::R16G16_SSCALED:
case RenderFormat::R16G16B16A16_USCALED:
case RenderFormat::R8G8B8A8_USCALED:
case RenderFormat::R8G8B8A8_SSCALED:
case RenderFormat::R16G16_USCALED:
case RenderFormat::R8G8_USCALED:
case RenderFormat::R8G8_SSCALED:
case RenderFormat::R16_USCALED:
case RenderFormat::R16_SSCALED:
case RenderFormat::R8_USCALED:
case RenderFormat::R8_SSCALED:
case RenderFormat::A2B10G10R10_UNORM_PACK32:
case RenderFormat::A2B10G10R10_SNORM_PACK32:
return 1;
default:
assert(false && "Unknown format.");
return 1;
Expand Down Expand Up @@ -1286,6 +1334,8 @@ namespace plume {
float depthBiasClamp = 0.0f;
float slopeScaledDepthBias = 0.0f;
bool dynamicDepthBiasEnabled = false;
bool dynamicBlendConstantsEnabled = false; // Blend constants come from RenderCommandList::setBlendConstants.
bool dynamicStencilReferenceEnabled = false; // The stencil reference comes from RenderCommandList::setStencilReference.
bool depthEnabled = false;
bool depthWriteEnabled = false;
bool stencilEnabled = false;
Expand Down
Loading