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
|
|
|
|
2023-08-24 22:30:20 +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()
|
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_init();
|
2023-08-19 12:49:23 +00:00
|
|
|
|
2023-08-24 22:30:20 +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
|
2023-08-24 22:30:20 +00:00
|
|
|
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();
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_str8 shaderPath = oc_path_executable_relative(oc_scratch(), OC_STR8("triangle_shader.metallib"));
|
|
|
|
const char* shaderPathCString = oc_str8_to_cstring(oc_scratch(), 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;
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// start app
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_window_bring_to_front(window);
|
|
|
|
oc_window_focus(window);
|
2023-08-19 12:49:23 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
while(!oc_should_quit())
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_pump_events(0);
|
|
|
|
oc_event* event = 0;
|
|
|
|
while((event = oc_next_event(oc_scratch())) != 0)
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
switch(event->type)
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
case OC_EVENT_WINDOW_CLOSE:
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_request_quit();
|
2023-08-19 12:49:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-08-24 22:30:20 +00:00
|
|
|
|
|
|
|
oc_arena_clear(oc_scratch());
|
2023-08-19 12:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vector_uint2 viewportSize;
|
|
|
|
viewportSize.x = 800;
|
|
|
|
viewportSize.y = 600;
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
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];
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_surface_present(surface);
|
2023-08-19 12:49:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_terminate();
|
2023-08-19 12:49:23 +00:00
|
|
|
|
|
|
|
return (0);
|
2022-12-26 17:33:31 +00:00
|
|
|
}
|