fix: issues with drawing

This commit is contained in:
Samuliak 2024-07-02 10:05:59 +02:00
parent c6704f9218
commit 4bc19e8e43
3 changed files with 26 additions and 7 deletions

View file

@ -33,6 +33,9 @@ class RendererMTL final : public Renderer {
// HACK // HACK
MTL::Texture* topScreenTexture; MTL::Texture* topScreenTexture;
// Helpers
MTL::SamplerState* basicSampler;
// Pipelines // Pipelines
MTL::RenderPipelineState* displayPipeline; MTL::RenderPipelineState* displayPipeline;
MTL::RenderPipelineState* drawPipeline; MTL::RenderPipelineState* drawPipeline;

View file

@ -28,11 +28,15 @@ void RendererMTL::display() {
MTL::RenderPassDescriptor* renderPassDescriptor = MTL::RenderPassDescriptor::alloc()->init(); MTL::RenderPassDescriptor* renderPassDescriptor = MTL::RenderPassDescriptor::alloc()->init();
MTL::RenderPassColorAttachmentDescriptor* colorAttachment = renderPassDescriptor->colorAttachments()->object(0); MTL::RenderPassColorAttachmentDescriptor* colorAttachment = renderPassDescriptor->colorAttachments()->object(0);
colorAttachment->setTexture(drawable->texture()); colorAttachment->setTexture(drawable->texture());
colorAttachment->setLoadAction(MTL::LoadActionClear); colorAttachment->setLoadAction(MTL::LoadActionDontCare);
colorAttachment->setClearColor(MTL::ClearColor{1.0f, 0.0f, 0.0f, 1.0f});
colorAttachment->setStoreAction(MTL::StoreActionStore); colorAttachment->setStoreAction(MTL::StoreActionStore);
MTL::RenderCommandEncoder* renderCommandEncoder = commandBuffer->renderCommandEncoder(renderPassDescriptor); MTL::RenderCommandEncoder* renderCommandEncoder = commandBuffer->renderCommandEncoder(renderPassDescriptor);
renderCommandEncoder->setRenderPipelineState(displayPipeline);
renderCommandEncoder->setFragmentTexture(topScreenTexture, 0);
renderCommandEncoder->setFragmentSamplerState(basicSampler, 0);
renderCommandEncoder->drawPrimitives(MTL::PrimitiveTypeTriangleStrip, NS::UInteger(0), NS::UInteger(4));
renderCommandEncoder->endEncoding(); renderCommandEncoder->endEncoding();
commandBuffer->presentDrawable(drawable); commandBuffer->presentDrawable(drawable);
@ -49,9 +53,18 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
commandQueue = device->newCommandQueue(); commandQueue = device->newCommandQueue();
// HACK // HACK
MTL::TextureDescriptor* descriptor = MTL::TextureDescriptor::texture2DDescriptor(MTL::PixelFormatRGBA8Unorm, 400, 240, false); MTL::TextureDescriptor* descriptor = MTL::TextureDescriptor::alloc()->init();
descriptor->setTextureType(MTL::TextureType2D);
descriptor->setPixelFormat(MTL::PixelFormatRGBA8Unorm);
descriptor->setWidth(400);
descriptor->setHeight(240);
descriptor->setUsage(MTL::TextureUsageRenderTarget | MTL::TextureUsageShaderRead);
topScreenTexture = device->newTexture(descriptor); topScreenTexture = device->newTexture(descriptor);
// Helpers
MTL::SamplerDescriptor* samplerDescriptor = MTL::SamplerDescriptor::alloc()->init();
basicSampler = device->newSamplerState(samplerDescriptor);
// -------- Pipelines -------- // -------- Pipelines --------
// Load shaders // Load shaders
@ -73,8 +86,8 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
displayPipelineDescriptor->setVertexFunction(vertexDisplayFunction); displayPipelineDescriptor->setVertexFunction(vertexDisplayFunction);
displayPipelineDescriptor->setFragmentFunction(fragmentDisplayFunction); displayPipelineDescriptor->setFragmentFunction(fragmentDisplayFunction);
// HACK // HACK
auto* colorAttachment = displayPipelineDescriptor->colorAttachments()->object(0); auto* displayColorAttachment = displayPipelineDescriptor->colorAttachments()->object(0);
colorAttachment->setPixelFormat(topScreenTexture->pixelFormat()); displayColorAttachment->setPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm_sRGB);
error = nullptr; error = nullptr;
displayPipeline = device->newRenderPipelineState(displayPipelineDescriptor, &error); displayPipeline = device->newRenderPipelineState(displayPipelineDescriptor, &error);
@ -90,7 +103,8 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
drawPipelineDescriptor->setVertexFunction(vertexDrawFunction); drawPipelineDescriptor->setVertexFunction(vertexDrawFunction);
drawPipelineDescriptor->setFragmentFunction(fragmentDrawFunction); drawPipelineDescriptor->setFragmentFunction(fragmentDrawFunction);
// HACK // HACK
colorAttachment->setPixelFormat(MTL::PixelFormatRGBA8Unorm); auto* drawColorAttachment = drawPipelineDescriptor->colorAttachments()->object(0);
drawColorAttachment->setPixelFormat(topScreenTexture->pixelFormat());
// Vertex descriptor // Vertex descriptor
// TODO: add all attributes // TODO: add all attributes
@ -131,6 +145,8 @@ void RendererMTL::textureCopy(u32 inputAddr, u32 outputAddr, u32 totalBytes, u32
void RendererMTL::drawVertices(PICA::PrimType primType, std::span<const PICA::Vertex> vertices) { void RendererMTL::drawVertices(PICA::PrimType primType, std::span<const PICA::Vertex> vertices) {
createCommandBufferIfNeeded(); createCommandBufferIfNeeded();
std::cout << "DRAWING " << vertices.size() << " VERTICES" << std::endl;
// TODO: don't begin a new render pass every time // TODO: don't begin a new render pass every time
MTL::RenderPassDescriptor* renderPassDescriptor = MTL::RenderPassDescriptor::alloc()->init(); MTL::RenderPassDescriptor* renderPassDescriptor = MTL::RenderPassDescriptor::alloc()->init();
MTL::RenderPassColorAttachmentDescriptor* colorAttachment = renderPassDescriptor->colorAttachments()->object(0); MTL::RenderPassColorAttachmentDescriptor* colorAttachment = renderPassDescriptor->colorAttachments()->object(0);

View file

@ -30,7 +30,7 @@ struct DrawVertexOut {
vertex DrawVertexOut vertexDraw(DrawVertexIn in [[stage_in]]) { vertex DrawVertexOut vertexDraw(DrawVertexIn in [[stage_in]]) {
DrawVertexOut out; DrawVertexOut out;
out.position = in.position; out.position = float4(in.position.xy, 0.0, 1.0); // HACK
out.color = in.color; out.color = in.color;
return out; return out;