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
MTL::Texture* topScreenTexture;
// Helpers
MTL::SamplerState* basicSampler;
// Pipelines
MTL::RenderPipelineState* displayPipeline;
MTL::RenderPipelineState* drawPipeline;

View file

@ -28,11 +28,15 @@ void RendererMTL::display() {
MTL::RenderPassDescriptor* renderPassDescriptor = MTL::RenderPassDescriptor::alloc()->init();
MTL::RenderPassColorAttachmentDescriptor* colorAttachment = renderPassDescriptor->colorAttachments()->object(0);
colorAttachment->setTexture(drawable->texture());
colorAttachment->setLoadAction(MTL::LoadActionClear);
colorAttachment->setClearColor(MTL::ClearColor{1.0f, 0.0f, 0.0f, 1.0f});
colorAttachment->setLoadAction(MTL::LoadActionDontCare);
colorAttachment->setStoreAction(MTL::StoreActionStore);
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();
commandBuffer->presentDrawable(drawable);
@ -49,9 +53,18 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
commandQueue = device->newCommandQueue();
// 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);
// Helpers
MTL::SamplerDescriptor* samplerDescriptor = MTL::SamplerDescriptor::alloc()->init();
basicSampler = device->newSamplerState(samplerDescriptor);
// -------- Pipelines --------
// Load shaders
@ -73,8 +86,8 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
displayPipelineDescriptor->setVertexFunction(vertexDisplayFunction);
displayPipelineDescriptor->setFragmentFunction(fragmentDisplayFunction);
// HACK
auto* colorAttachment = displayPipelineDescriptor->colorAttachments()->object(0);
colorAttachment->setPixelFormat(topScreenTexture->pixelFormat());
auto* displayColorAttachment = displayPipelineDescriptor->colorAttachments()->object(0);
displayColorAttachment->setPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm_sRGB);
error = nullptr;
displayPipeline = device->newRenderPipelineState(displayPipelineDescriptor, &error);
@ -90,7 +103,8 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
drawPipelineDescriptor->setVertexFunction(vertexDrawFunction);
drawPipelineDescriptor->setFragmentFunction(fragmentDrawFunction);
// HACK
colorAttachment->setPixelFormat(MTL::PixelFormatRGBA8Unorm);
auto* drawColorAttachment = drawPipelineDescriptor->colorAttachments()->object(0);
drawColorAttachment->setPixelFormat(topScreenTexture->pixelFormat());
// Vertex descriptor
// 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) {
createCommandBufferIfNeeded();
std::cout << "DRAWING " << vertices.size() << " VERTICES" << std::endl;
// TODO: don't begin a new render pass every time
MTL::RenderPassDescriptor* renderPassDescriptor = MTL::RenderPassDescriptor::alloc()->init();
MTL::RenderPassColorAttachmentDescriptor* colorAttachment = renderPassDescriptor->colorAttachments()->object(0);

View file

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