orca/sketches/triangleMetal/main.m

129 lines
4.4 KiB
Mathematica
Raw Normal View History

2023-09-07 12:51:48 +00:00
/*************************************************************************
2022-12-26 17:33:31 +00:00
*
2023-09-07 12:51:48 +00:00
* Orca
* Copyright 2023 Martin Fouilleul and the Orca project contributors
* See LICENSE.txt for licensing information
2022-12-26 17:33:31 +00:00
*
2023-09-07 12:51:48 +00:00
**************************************************************************/
2023-08-19 12:49:23 +00:00
#include <stdlib.h>
#include <string.h>
2022-12-26 17:33:31 +00:00
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
2023-08-19 12:49:23 +00:00
#include <math.h>
2022-12-26 17:33:31 +00:00
#include "orca.h"
#include "graphics/mtl_surface.h"
2022-12-26 17:33:31 +00:00
2023-08-19 12:49:23 +00:00
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>
2022-12-26 17:33:31 +00:00
2023-08-19 12:49:23 +00:00
#include "vertex.h"
2022-12-26 17:33:31 +00:00
2023-08-19 12:49:23 +00:00
static const my_vertex triangle[3] = { { { 250, -250 }, { 1, 0, 0, 1 } },
{ { -250, -250 }, { 0, 1, 0, 1 } },
{ { 0, 250 }, { 0, 0, 1, 1 } } };
2022-12-26 17:33:31 +00:00
int main()
{
oc_init();
2023-08-19 12:49:23 +00:00
oc_rect rect = { .x = 100, .y = 100, .w = 800, .h = 600 };
oc_window window = oc_window_create(rect, OC_STR8("test"), 0);
2023-08-19 12:49:23 +00:00
//NOTE: create surface
oc_surface surface = oc_surface_create_for_window(window, OC_METAL);
2023-08-19 12:49:23 +00:00
//NOTE(martin): load the library
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
oc_arena_scope* scratch = oc_scratch_begin();
oc_str8 shaderPath = oc_path_executable_relative(scratch.arena, OC_STR8("triangle_shader.metallib"));
const char* shaderPathCString = oc_str8_to_cstring(scratch.arena, shaderPath);
2023-08-19 12:49:23 +00:00
NSString* metalFileName = [[NSString alloc] initWithCString:shaderPathCString encoding:NSUTF8StringEncoding];
NSError* err = 0;
id<MTLLibrary> library = [device newLibraryWithFile:metalFileName error:&err];
if(err != nil)
{
const char* errStr = [[err localizedDescription] UTF8String];
printf("error : %s\n", errStr);
return (-1);
}
id<MTLFunction> vertexFunction = [library newFunctionWithName:@"VertexShader"];
id<MTLFunction> fragmentFunction = [library newFunctionWithName:@"FragmentShader"];
//NOTE(martin): create a render pipeline
MTLRenderPipelineDescriptor* pipelineStateDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
pipelineStateDescriptor.label = @"My simple pipeline";
pipelineStateDescriptor.vertexFunction = vertexFunction;
pipelineStateDescriptor.fragmentFunction = fragmentFunction;
CAMetalLayer* layer = oc_mtl_surface_layer(surface);
2023-08-19 12:49:23 +00:00
pipelineStateDescriptor.colorAttachments[0].pixelFormat = layer.pixelFormat;
id<MTLRenderPipelineState> pipelineState = [device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:&err];
if(err != nil)
{
const char* errStr = [[err localizedDescription] UTF8String];
printf("error : %s\n", errStr);
return (-1);
}
oc_scratch_end(scrathc);
2023-08-19 12:49:23 +00:00
// start app
oc_window_bring_to_front(window);
oc_window_focus(window);
2023-08-19 12:49:23 +00:00
while(!oc_should_quit())
2023-08-19 12:49:23 +00:00
{
scratch = oc_scratch_begin();
oc_pump_events(0);
oc_event* event = 0;
while((event = oc_next_event(scratch.arena)) != 0)
2023-08-19 12:49:23 +00:00
{
switch(event->type)
2023-08-19 12:49:23 +00:00
{
case OC_EVENT_WINDOW_CLOSE:
2023-08-19 12:49:23 +00:00
{
oc_request_quit();
2023-08-19 12:49:23 +00:00
}
break;
default:
break;
}
}
vector_uint2 viewportSize;
viewportSize.x = 800;
viewportSize.y = 600;
oc_surface_select(surface);
id<CAMetalDrawable> drawable = oc_mtl_surface_drawable(surface);
id<MTLCommandBuffer> commandBuffer = oc_mtl_surface_command_buffer(surface);
2023-08-19 12:49:23 +00:00
MTLRenderPassDescriptor* renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor];
renderPassDescriptor.colorAttachments[0].texture = drawable.texture;
renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
id<MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
//Set the pipeline state
[encoder setRenderPipelineState:pipelineState];
//Send data to the shader and add draw call
[encoder setVertexBytes:triangle length:sizeof(triangle) atIndex:vertexInputIndexVertices];
[encoder setVertexBytes:&viewportSize length:sizeof(viewportSize) atIndex:vertexInputIndexViewportSize];
[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3];
[encoder endEncoding];
oc_surface_present(surface);
oc_scratch_end(scratch);
2023-08-19 12:49:23 +00:00
}
oc_terminate();
2023-08-19 12:49:23 +00:00
return (0);
2022-12-26 17:33:31 +00:00
}