I HAVE AQUIRED GPUTRACE

This commit is contained in:
Dynamitos
2024-04-19 22:44:00 +02:00
parent 194a0e8b91
commit a27e280ab8
26 changed files with 169 additions and 182 deletions
+4 -2
View File
@@ -68,9 +68,10 @@ public:
virtual void drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) override;
private:
PGraphicsPipeline boundPipeline;
PIndexBuffer boundIndexBuffer;
MTL::Buffer* argumentBuffer;
MTL::RenderCommandEncoder* encoder;
PIndexBuffer boundIndexBuffer;
PGraphicsPipeline boundPipeline;
std::string name;
};
DEFINE_REF(RenderCommand)
@@ -90,6 +91,7 @@ private:
PComputePipeline boundPipeline;
MTL::CommandBuffer* commandBuffer;
MTL::ComputeCommandEncoder* encoder;
MTL::Buffer* argumentBuffer;
std::string name;
};
DEFINE_REF(ComputeCommand)
+5 -3
View File
@@ -11,7 +11,6 @@
#include "Pipeline.h"
#include "Resources.h"
#include "Window.h"
#include <Metal/Metal.h>
using namespace Seele;
using namespace Seele::Metal;
@@ -123,7 +122,7 @@ void RenderCommand::drawIndexed(uint32 indexCount, uint32 instanceCount, int32 f
void RenderCommand::drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) {
// TODO:
encoder->drawMeshThreadgroups(MTL::Size(groupX, groupY, groupZ), MTL::Size(128, 128, 128), MTL::Size(32, 32, 32));
encoder->drawMeshThreadgroups(MTL::Size(groupX, groupY, groupZ), MTL::Size(128, 1, 1), MTL::Size(32, 1, 1));
}
ComputeCommand::ComputeCommand(MTL::CommandBuffer* cmdBuffer, const std::string& name)
@@ -139,13 +138,15 @@ void ComputeCommand::end() {
void ComputeCommand::bindPipeline(Gfx::PComputePipeline pipeline) {
boundPipeline = pipeline.cast<ComputePipeline>();
encoder->setComputePipelineState(boundPipeline->getHandle());
argumentBuffer = boundPipeline->graphics->getDevice()->newBuffer(sizeof(uint64) * boundPipeline->getPipelineLayout()->getLayouts().size(), MTL::ResourceOptionCPUCacheModeDefault);
}
void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet set) {
auto metalSet = set.cast<DescriptorSet>();
metalSet->bind();
uint32 parameterIndex = boundPipeline->getPipelineLayout()->findParameter(set->getLayout()->getName());
encoder->setBuffer(metalSet->getBuffer(), 0, parameterIndex);
uint64* topLevelTable = (uint64*)argumentBuffer->contents();
topLevelTable[parameterIndex] = metalSet->getBuffer()->gpuAddress();
auto bindings =metalSet->getLayout()->getBindings();
for(size_t i = 0; i < bindings.size(); ++i)
{
@@ -190,6 +191,7 @@ void ComputeCommand::pushConstants(Gfx::PPipelineLayout, Gfx::SeShaderStageFlags
void ComputeCommand::dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) {
// TODO
encoder->setBuffer(argumentBuffer, 0, 2);
encoder->dispatchThreadgroups(MTL::Size(threadX, threadY, threadZ), MTL::Size(32, 32, 1));
}
+1 -1
View File
@@ -62,7 +62,7 @@ public:
private:
PGraphics graphics;
PDescriptorPool owner;
MTL::Buffer* buffer;
MTL::Buffer* buffer = nullptr;
MTL::ArgumentEncoder* encoder;
Array<MTL::Resource*> boundResources;
uint32 bindCount;
+15 -11
View File
@@ -116,16 +116,14 @@ void DescriptorPool::reset() {
DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner)
: Gfx::DescriptorSet(owner->getLayout()), graphics(graphics), owner(owner), bindCount(0), currentlyInUse(false) {
// auto desc = (MTL::ArgumentDescriptor*)owner->getArguments()->object(0);
// std::cout << desc->access() << " " << desc->arrayLength() << " " << desc->index() << " " << desc->textureType() <<
// " " << desc->dataType() << std::endl;
if (owner->getArguments()->count() == 0) {
buffer = graphics->getDevice()->newBuffer(0, MTL::ResourceOptionCPUCacheModeDefault);
} else {
boundResources.resize(owner->getArguments()->count());
encoder = graphics->getDevice()->newArgumentEncoder(owner->getArguments());
buffer = graphics->getDevice()->newBuffer(encoder->encodedLength(), MTL::ResourceOptionCPUCacheModeDefault);
encoder->setArgumentBuffer(buffer, 0);
if (owner->getArguments()->count() > 0) {
boundResources.resize(owner->getArguments()->count());
encoder = graphics->getDevice()->newArgumentEncoder(owner->getArguments());
buffer = graphics->getDevice()->newBuffer(encoder->encodedLength(), MTL::ResourceOptionCPUCacheModeDefault);
encoder->setArgumentBuffer(buffer, 0);
} else
{
buffer = graphics->getDevice()->newBuffer(8, MTL::ResourceOptionCPUCacheModeDefault);
}
}
@@ -149,7 +147,13 @@ void DescriptorSet::updateSampler(uint32_t binding, Gfx::PSampler samplerState)
}
void DescriptorSet::updateTexture(uint32_t binding, Gfx::PTexture texture, Gfx::PSampler samplerState) {
PTextureBase base = texture.cast<TextureBase>();
encoder->setTexture(base->getTexture(), binding);
if(layout->getBindings()[binding].access == Gfx::SE_DESCRIPTOR_ACCESS_READ_ONLY_BIT)
{
encoder->setTexture(base->getTexture(), binding);
}else{
encoder->setBuffer(base->getTexture()->buffer(), 0, binding);
}
boundResources[binding] = base->getTexture();
}
+1 -1
View File
@@ -31,7 +31,7 @@ void Graphics::init(GraphicsInitializer)
queue = new CommandQueue(this);
ioQueue = new IOCommandQueue(this);
cache = new PipelineCache(this, "pipelines.metal");
meshShadingEnabled = false;
meshShadingEnabled = true;
}
Gfx::OWindow Graphics::createWindow(const WindowCreateInfo &createInfo)
+1 -1
View File
@@ -31,8 +31,8 @@ public:
virtual ~ComputePipeline();
constexpr MTL::ComputePipelineState* getHandle() const { return state; }
private:
PGraphics graphics;
private:
MTL::ComputePipelineState* state;
};
DEFINE_REF(ComputePipeline)
+75 -69
View File
@@ -1,4 +1,5 @@
#include "Shader.h"
#include "Descriptor.h"
#include "Foundation/NSError.hpp"
#include "Foundation/NSString.hpp"
#include "Graphics.h"
@@ -6,7 +7,6 @@
#include "Graphics/slang-compile.h"
#include "Metal/MTLDevice.hpp"
#include "Metal/MTLLibrary.hpp"
#include "Descriptor.h"
#include <fstream>
#include <iostream>
#include <metal_irconverter/metal_irconverter.h>
@@ -25,75 +25,83 @@ Shader::~Shader() {
}
void Shader::create(const ShaderCreateInfo& createInfo) {
Map<std::string, uint32> test;
std::cout << "Compiling " << createInfo.name << std::endl;
Map<std::string, uint32> test;
Slang::ComPtr<slang::IBlob> kernelBlob = generateShader(createInfo, SLANG_DXIL, test);
hash = CRC::Calculate(kernelBlob->getBufferPointer(), kernelBlob->getBufferSize(), CRC::CRC_32());
IRCompiler* pCompiler = IRCompilerCreate();
IRCompilerSetMinimumGPUFamily(pCompiler, IRGPUFamilyMetal3);
IRCompilerIgnoreRootSignature(pCompiler, true);
IRCompilerSetMinimumGPUFamily(pCompiler, IRGPUFamilyMetal3);
IRCompilerIgnoreRootSignature(pCompiler, true);
IRCompilerSetEntryPointName(pCompiler, "main");
IRCompilerSetValidationFlags(pCompiler, IRCompilerValidationFlagAll);
IRObject* pDXIL = IRObjectCreateFromDXIL((const uint8*)kernelBlob->getBufferPointer(), kernelBlob->getBufferSize(),
IRBytecodeOwnershipNone);
IRVersionedRootSignatureDescriptor descriptor;
descriptor.version = IRRootSignatureVersion_1_1;
Map<std::string, Gfx::PDescriptorLayout> layouts =createInfo.rootSignature->getLayouts();
descriptor.desc_1_1.NumParameters = layouts.size();
descriptor.desc_1_1.NumStaticSamplers = 0;
descriptor.desc_1_1.pStaticSamplers = nullptr;
Array<IRRootParameter1> parameters;
// each layout has an array for its bindings
Array<Array<IRDescriptorRange1>> ranges;
for(const auto& [name, layout] : layouts)
{
uint32 textureReg = 0; // SRV
uint32 samplerReg = 0; // Sampler
uint32 uavReg = 0; // UAV
uint32 constantReg = 0; // CBV
auto& bindingRanges = ranges.add();
for(auto binding : layout->getBindings())
{
IRDescriptorRangeType type;
uint32 reg = 0;
switch(binding.descriptorType)
{
case Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER:
type = IRDescriptorRangeTypeSRV;
reg = textureReg++;
break;
case Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
case Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
type = IRDescriptorRangeTypeCBV;
reg = constantReg++;
break;
case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE:
type = IRDescriptorRangeTypeUAV;
reg = uavReg++;
break;
default: throw std::logic_error("Not implemented");
};
bindingRanges.add() = IRDescriptorRange1{
.BaseShaderRegister = reg,
.NumDescriptors = binding.descriptorCount,
.RangeType = type,
.RegisterSpace = createInfo.rootSignature->findParameter(name),
};
IRVersionedRootSignatureDescriptor descriptor;
descriptor.version = IRRootSignatureVersion_1_1;
Map<std::string, Gfx::PDescriptorLayout> layouts = createInfo.rootSignature->getLayouts();
descriptor.desc_1_1.NumParameters = layouts.size();
descriptor.desc_1_1.NumStaticSamplers = 0;
descriptor.desc_1_1.pStaticSamplers = nullptr;
Array<IRRootParameter1> parameters;
// each layout has an array for its bindings
Array<Array<IRDescriptorRange1>> ranges;
for (const auto& [name, layout] : layouts) {
uint32 textureReg = 0; // SRV
uint32 samplerReg = 0; // Sampler
uint32 uavReg = 0; // UAV
uint32 constantReg = 0; // CBV
auto& bindingRanges = ranges.add();
for (auto binding : layout->getBindings()) {
IRDescriptorRangeType type;
uint32 reg = 0;
switch (binding.descriptorType) {
case Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
type = IRDescriptorRangeTypeSRV;
reg = textureReg++;
break;
case Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
case Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
type = IRDescriptorRangeTypeCBV;
reg = constantReg++;
break;
case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE:
case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER:
if (binding.access == Gfx::SE_DESCRIPTOR_ACCESS_READ_ONLY_BIT) {
type = IRDescriptorRangeTypeSRV;
reg = textureReg++;
break;
} else {
type = IRDescriptorRangeTypeUAV;
reg = uavReg++;
break;
}
parameters.add() = IRRootParameter1{
.ParameterType = IRRootParameterTypeDescriptorTable,
.DescriptorTable = IRRootDescriptorTable1{ .NumDescriptorRanges = (uint32)(bindingRanges.size()), bindingRanges.data() },
.ShaderVisibility = IRShaderVisibilityAll,
};
default:
throw std::logic_error("Not implemented");
};
bindingRanges.add() = IRDescriptorRange1{
.BaseShaderRegister = reg,
.NumDescriptors = binding.descriptorCount,
.RangeType = type,
.RegisterSpace = createInfo.rootSignature->findParameter(name),
.Flags = IRDescriptorRangeFlagDescriptorsStaticKeepingBufferBoundsChecks,
};
}
IRError* signatureError = nullptr;
descriptor.desc_1_1.NumParameters = parameters.size();
descriptor.desc_1_1.pParameters = parameters.data();
IRRootSignature* rootSignature = IRRootSignatureCreateFromDescriptor(&descriptor, &signatureError);
assert(rootSignature);
IRCompilerSetGlobalRootSignature(pCompiler, rootSignature);
// Compile DXIL to Metal IR:
parameters.add() = IRRootParameter1{
.ParameterType = IRRootParameterTypeDescriptorTable,
.DescriptorTable =
IRRootDescriptorTable1{.NumDescriptorRanges = (uint32)(bindingRanges.size()), bindingRanges.data()},
.ShaderVisibility = IRShaderVisibilityAll,
};
}
IRError* signatureError = nullptr;
descriptor.desc_1_1.NumParameters = parameters.size();
descriptor.desc_1_1.pParameters = parameters.data();
IRRootSignature* rootSignature = IRRootSignatureCreateFromDescriptor(&descriptor, &signatureError);
assert(rootSignature);
IRCompilerSetGlobalRootSignature(pCompiler, rootSignature);
// Compile DXIL to Metal IR:
IRError* pError = nullptr;
IRObject* pOutIR = IRCompilerAllocCompileAndLink(pCompiler, NULL, pDXIL, &pError);
@@ -124,18 +132,16 @@ void Shader::create(const ShaderCreateInfo& createInfo) {
IRObjectGetMetalLibBinary(pOutIR, irStage, pMetallib);
dispatch_data_t data = IRMetalLibGetBytecodeData(pMetallib);
IRShaderReflection* reflection = IRShaderReflectionCreate();
IRObjectGetReflection(pOutIR, irStage, reflection);
std::cout << " NumRes: " << IRShaderReflectionGetResourceCount(reflection) << std::endl;
std::cout << IRShaderReflectionAllocStringAndSerialize(reflection) << std::endl;
IRShaderReflectionDestroy(reflection);
IRShaderReflection* reflection = IRShaderReflectionCreate();
IRObjectGetReflection(pOutIR, irStage, reflection);
std::cout << IRShaderReflectionAllocStringAndSerialize(reflection) << std::endl;
IRShaderReflectionDestroy(reflection);
// Store the metallib to custom format or disk, or use to create a MTLLibrary.
NS::Error* error;
library = graphics->getDevice()->newLibrary(data, &error);
function = library->newFunction(NS::String::string("main", NS::ASCIIStringEncoding));
if(!function)
{
if (!function) {
assert(false);
}
IRMetalLibBinaryDestroy(pMetallib);
-1
View File
@@ -51,7 +51,6 @@ private:
CAMetalLayer* metalLayer;
CA::MetalDrawable* drawable;
OTexture2D backBuffer;
MTL::CaptureDescriptor* capture = nullptr;
std::function<void(KeyCode, InputAction, KeyModifier)> keyCallback;
std::function<void(double, double)> mouseMoveCallback;
-12
View File
@@ -94,17 +94,6 @@ Window::~Window() { glfwDestroyWindow(static_cast<GLFWwindow*>(windowHandle)); }
void Window::pollInput() { glfwPollEvents(); }
void Window::beginFrame() {
auto captureManager = MTL::CaptureManager::sharedCaptureManager();
capture = MTL::CaptureDescriptor::alloc()->init();
capture->setCaptureObject((__bridge id<MTLDevice>)graphics->getDevice());
capture->setDestination(MTL::CaptureDestinationDeveloperTools);
capture->setOutputURL(NS::URL::fileURLWithPath(NS::String::string(fmt::format("frame{0}.trace", Gfx::getCurrentFrameIndex()).c_str(), NS::ASCIIStringEncoding)));
NS::Error* error;
captureManager->startCapture(capture, &error);
if(error)
{
std::cout << error->localizedDescription()->cString(NS::ASCIIStringEncoding) << std::endl;
}
drawable = (__bridge CA::MetalDrawable*)[metalLayer nextDrawable];
createBackBuffer();
}
@@ -112,7 +101,6 @@ void Window::beginFrame() {
void Window::endFrame() {
graphics->getQueue()->getCommands()->present(drawable);
graphics->getQueue()->submitCommands();
MTL::CaptureManager::sharedCaptureManager()->stopCapture();
currentFrameIndex++;
}