Fixing command resource binding

This commit is contained in:
Dynamitos committed 2025-09-05 07:10:27 +02:00
1 parent 5be15b4929
commit e97beda2bc
13 files changed
+73 -57

No files matched your search

+1 -1
View File
@@ -12,7 +12,7 @@ using namespace Seele;
using namespace Seele::Metal;
BufferAllocation::BufferAllocation(PGraphics graphics, const std::string& name, uint64 size, MTL::ResourceOptions options)
: CommandBoundResource(graphics) {
: CommandBoundResource(graphics, name) {
buffer = graphics->getDevice()->newBuffer(size, options);
buffer->setLabel(NS::String::string(name.c_str(), NS::ASCIIStringEncoding));
}
+8 -8
View File
@@ -147,7 +147,7 @@ void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet) {
createEncoder(boundPipeline->vertexFunction, boundPipeline->vertexSets);
createEncoder(boundPipeline->fragmentFunction, boundPipeline->fragmentSets);
if(setHandle->argumentBuffer == nullptr) {
setHandle->argumentBuffer = new BufferAllocation(setHandle->graphics, "ArgumentBuffer", setHandle->encoder->encodedLength());
setHandle->argumentBuffer = new BufferAllocation(metalSet->graphics, "ArgumentBuffer", setHandle->encoder->encodedLength());
setHandle->encoder->setArgumentBuffer(setHandle->argumentBuffer->buffer, 0);
}
setHandle->argumentBuffer->bind();
@@ -198,18 +198,18 @@ void RenderCommand::bindIndexBuffer(Gfx::PIndexBuffer gfxIndexBuffer) {
}
void RenderCommand::pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) {
uint pushIndex = boundPipeline->getPipelineLayout()->findParameter("pOffsets");
uint pushIndex = boundPipeline->getPipelineLayout()->findParameter(boundPipeline->getPipelineLayout()->getPushConstantName(offset));
if (stage & Gfx::SE_SHADER_STAGE_VERTEX_BIT) {
encoder->setVertexBytes(data, size, pushIndex); // TODO: hardcoded
encoder->setVertexBytes(data, size, pushIndex);
}
if (stage & Gfx::SE_SHADER_STAGE_FRAGMENT_BIT) {
encoder->setFragmentBytes(data, size, pushIndex); // TODO: hardcoded
encoder->setFragmentBytes(data, size, pushIndex);
}
if (stage & Gfx::SE_SHADER_STAGE_TASK_BIT_EXT) {
encoder->setObjectBytes(data, size, pushIndex); // TODO: hardcoded
encoder->setObjectBytes(data, size, pushIndex);
}
if (stage & Gfx::SE_SHADER_STAGE_MESH_BIT_EXT) {
encoder->setMeshBytes(data, size, pushIndex); // TODO: hardcoded
encoder->setMeshBytes(data, size, pushIndex);
}
}
@@ -271,7 +271,7 @@ void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet) {
};
createEncoder(boundPipeline->computeFunction);
if(setHandle->argumentBuffer == nullptr) {
setHandle->argumentBuffer = new BufferAllocation(setHandle->graphics, "ArgumentBuffer", setHandle->encoder->encodedLength());
setHandle->argumentBuffer = new BufferAllocation(metalSet->graphics, "ArgumentBuffer", setHandle->encoder->encodedLength());
setHandle->encoder->setArgumentBuffer(setHandle->argumentBuffer->buffer, 0);
}
setHandle->argumentBuffer->bind();
@@ -366,6 +366,7 @@ void CommandQueue::submitCommands(PEvent signalSemaphore) {
for (auto it = pendingCommands.begin(); it != pendingCommands.end(); it++) {
if ((*it)->getHandle() == cmdBuffer) {
auto& cmd = *it;
std::cout << "Command completed" << std::endl;
cmd->reset();
readyCommands.add(std::move(*it));
pendingCommands.erase(it);
@@ -375,7 +376,6 @@ void CommandQueue::submitCommands(PEvent signalSemaphore) {
}));
PEvent prevCmdEvent = activeCommand->getCompletedEvent();
activeCommand->end(signalSemaphore);
activeCommand->waitDeviceIdle();
pendingCommands.add(std::move(activeCommand));
if (!readyCommands.empty()) {
activeCommand = std::move(readyCommands.front());
-1
View File
@@ -58,7 +58,6 @@ public:
void updateTexture(const std::string& name, uint32 index, Gfx::PTextureView texture);
void updateAccelerationStructure(const std::string& name, uint32 index, Gfx::PTopLevelAS as);
PGraphics graphics;
PDescriptorPool owner;
OBufferAllocation argumentBuffer = nullptr;
MTL::ArgumentEncoder* encoder = nullptr;
+2 -1
View File
@@ -59,7 +59,7 @@ void DescriptorLayout::create() {
MTL::ArgumentEncoder* DescriptorLayout::createEncoder() { return graphics->getDevice()->newArgumentEncoder(arguments); }
DescriptorSetHandle::DescriptorSetHandle(PGraphics graphics, PDescriptorPool owner, const std::string& name)
: CommandBoundResource(graphics), owner(owner)
: CommandBoundResource(graphics, name), owner(owner)
{
}
@@ -143,6 +143,7 @@ void DescriptorSetHandle::updateTexture(const std::string& name, uint32 index, G
.access = owner->getLayout()->variableMapping[name].access,
});
boundResources.add(tex);
boundResources.add(tex->getSource());
}
void DescriptorSetHandle::updateAccelerationStructure(const std::string& , uint32 , Gfx::PTopLevelAS ){}
+4 -4
View File
@@ -56,17 +56,17 @@ void RenderPass::updateRenderPass() {
for (size_t i = 0; i < layout.colorAttachments.size(); ++i) {
const auto& color = layout.colorAttachments[i];
auto desc = renderPass->colorAttachments()->object(i);
desc->setTexture(color.getTextureView().cast<TextureBase>()->getImage());
desc->setTexture(color.getTextureView().cast<TextureView>()->getHandle());
if (!layout.resolveAttachments.empty()) {
const auto& resolve = layout.resolveAttachments[i];
desc->setResolveTexture(resolve.getTextureView().cast<TextureBase>()->getImage());
desc->setResolveTexture(resolve.getTextureView().cast<TextureView>()->getHandle());
}
}
if (layout.depthAttachment.getTextureView() != nullptr) {
auto depth = renderPass->depthAttachment();
depth->setTexture(layout.depthAttachment.getTextureView().cast<TextureBase>()->getImage());
depth->setTexture(layout.depthAttachment.getTextureView().cast<TextureView>()->getHandle());
if (layout.depthResolveAttachment.getTextureView() != nullptr) {
depth->setResolveTexture(layout.depthResolveAttachment.getTextureView().cast<TextureBase>()->getImage());
depth->setResolveTexture(layout.depthResolveAttachment.getTextureView().cast<TextureView>()->getHandle());
}
}
}
+13 -4
View File
@@ -5,6 +5,7 @@
#include <Foundation/Foundation.hpp>
#include <Metal/Metal.hpp>
#include <QuartzCore/QuartzCore.hpp>
#include <iostream>
namespace Seele {
namespace Metal {
@@ -25,18 +26,26 @@ class DestructionManager {
DEFINE_REF(DestructionManager)
class CommandBoundResource {
public:
CommandBoundResource(PGraphics graphics) : graphics(graphics) {}
public:
CommandBoundResource(PGraphics graphics, const std::string& name) : graphics(graphics), name(name) {}
virtual ~CommandBoundResource() {
if (isCurrentlyBound())
abort();
}
constexpr bool isCurrentlyBound() const { return bindCount > 0; }
constexpr void bind() { bindCount++; }
constexpr void unbind() { bindCount--; }
void bind() { bindCount++;
std::cout << "Bind " << bindCount << " " << name << std::endl;
}
void unbind() { bindCount--;
std::cout << "Unbind " << bindCount << " " << name << std::endl;
}
const std::string& getName() const {
return name;
}
protected:
PGraphics graphics;
std::string name;
uint64 bindCount = 0;
};
DEFINE_REF(CommandBoundResource)
+11 -4
View File
@@ -13,7 +13,7 @@ using namespace Seele;
using namespace Seele::Metal;
TextureView::TextureView(PGraphics graphics, PTextureHandle source, uint32 width, uint32 height, uint32 numLayers, uint32 numMipLevels, MTL::Texture* view)
: CommandBoundResource(graphics), width(width), height(height), numLayers(numLayers), numMipLevels(numMipLevels), source(source), view(view) {}
: CommandBoundResource(graphics, source->getName()), width(width), height(height), numLayers(numLayers), numMipLevels(numMipLevels), source(source), view(view) {}
TextureView::~TextureView() {}
@@ -43,15 +43,22 @@ void TextureView::changeLayout(Gfx::SeImageLayout newLayout, Gfx::SeAccessFlags
Gfx::OTextureView TextureHandle::createTextureView(uint32 baseMipLevel, uint32 viewLevelCount, uint32 baseArrayLayer,
uint32 viewLayerCount) {
MTL::Texture* viewTexture =
texture->newTextureView(cast(format), type, NS::Range(baseMipLevel, viewLevelCount), NS::Range(baseArrayLayer, viewLayerCount));
MTL::Texture* viewTexture;
if(viewLevelCount > 1)
{
viewTexture = texture->newTextureView(cast(format), type, NS::Range(baseMipLevel, viewLevelCount), NS::Range(baseArrayLayer, viewLayerCount));
}
else
{
viewTexture = texture->newTextureView(cast(format));
}
uint32 viewWidth = width * std::pow(0.5, baseMipLevel);
uint32 viewHeight = height * std::pow(0.5, baseMipLevel);
return new TextureView(graphics, this, viewWidth, viewHeight, viewLayerCount, viewLevelCount, viewTexture);
}
TextureHandle::TextureHandle(PGraphics graphics, MTL::TextureType type, const TextureCreateInfo& createInfo, MTL::Texture* existingImage)
: CommandBoundResource(graphics), texture(existingImage), type(type), width(createInfo.width), height(createInfo.height),
: CommandBoundResource(graphics, createInfo.name), texture(existingImage), type(type), width(createInfo.width), height(createInfo.height),
depth(createInfo.depth), arrayCount(createInfo.elements), mipLevels(1), samples(createInfo.samples), format(createInfo.format),
usage(createInfo.usage), layout(Gfx::SE_IMAGE_LAYOUT_UNDEFINED), ownsImage(existingImage == nullptr) {
if (createInfo.useMip) {