redo device creation
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "metal_output_devices.h"
|
||||
#include "core/instance/metal_instance.h"
|
||||
#include "core/instance/gryphn_instance.h"
|
||||
#include <core/debugger/gryphn_debugger.h>
|
||||
|
||||
gnReturnCode gnCreateOutputDeviceFn(gnOutputDevice* outputDevice, gnInstance* instance, struct gnOutputDeviceInfo_t deviceInfo) {
|
||||
outputDevice->outputDevice = malloc(sizeof(gnPlatformOutputDevice));
|
||||
@@ -13,6 +14,79 @@ gnReturnCode gnCreateOutputDeviceFn(gnOutputDevice* outputDevice, gnInstance* in
|
||||
outputDevice->outputDevice->queues[i] = outputDevice->outputDevice->device.newCommandQueue;
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
NSError* error = nil;
|
||||
MTLCompileOptions* options = nil;
|
||||
NSString *shaderSource = @"#include <metal_stdlib>\
|
||||
using namespace metal;\
|
||||
struct VertexOut {\
|
||||
float4 position [[position]];\
|
||||
float2 uv;\
|
||||
};\
|
||||
vertex VertexOut vs_main(uint vertexID [[vertex_id]]) {\
|
||||
float2 positions[4] = {\
|
||||
{-1.0, -1.0},\
|
||||
{ 1.0, -1.0},\
|
||||
{-1.0, 1.0},\
|
||||
{ 1.0, 1.0}\
|
||||
};\
|
||||
float2 uvs[4] = {\
|
||||
{0.0, 1.0},\
|
||||
{1.0, 1.0},\
|
||||
{0.0, 0.0},\
|
||||
{1.0, 0.0}\
|
||||
};\
|
||||
VertexOut out;\
|
||||
out.position = float4(positions[vertexID], 0.0, 1.0);\
|
||||
out.uv = uvs[vertexID];\
|
||||
return out;\
|
||||
}\
|
||||
fragment float4 fs_main(VertexOut in [[stage_in]],\
|
||||
texture2d<float> colorTex [[texture(0)]],\
|
||||
sampler samp [[sampler(0)]]) {\
|
||||
return colorTex.sample(samp, in.uv);\
|
||||
}\
|
||||
";
|
||||
id<MTLLibrary> library = [outputDevice->outputDevice->device newLibraryWithSource:shaderSource options:nil error:&error];
|
||||
if (!library) {
|
||||
gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
|
||||
.message = gnCreateString("Failed to compile framebuffer shader")
|
||||
});
|
||||
return GN_FAILED_TO_CREATE_DEVICE;
|
||||
}
|
||||
// id<MTLFunction> vs = library->newFunction(NS::String::string("vs_main", NS::UTF8StringEncoding));
|
||||
id<MTLFunction> vs = [library newFunctionWithName:@"vs_main"];
|
||||
id<MTLFunction> fs = [library newFunctionWithName:@"fs_main"];
|
||||
|
||||
if (vs == nil) {
|
||||
gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
|
||||
.message = gnCreateString("Failed to load frambuffer vertex shader")
|
||||
});
|
||||
return GN_FAILED_TO_CREATE_DEVICE;
|
||||
}
|
||||
|
||||
if (fs == nil) {
|
||||
gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
|
||||
.message = gnCreateString("Failed to load frambuffer fragment shader")
|
||||
});
|
||||
return GN_FAILED_TO_CREATE_DEVICE;
|
||||
}
|
||||
|
||||
MTLRenderPipelineDescriptor* pipelineDesc =[[MTLRenderPipelineDescriptor alloc] init];;
|
||||
pipelineDesc.vertexFunction = vs;
|
||||
pipelineDesc.fragmentFunction = fs;
|
||||
pipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
|
||||
|
||||
outputDevice->outputDevice->framebuffer = [outputDevice->outputDevice->device newRenderPipelineStateWithDescriptor:pipelineDesc error:&error];
|
||||
if (!outputDevice->outputDevice->framebuffer) {
|
||||
gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
|
||||
.message = gnCreateString("Failed to create frambuffer render pipeline")
|
||||
});
|
||||
return GN_FAILED_TO_CREATE_DEVICE;
|
||||
}
|
||||
}
|
||||
|
||||
return GN_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -27,57 +101,3 @@ void gnDestroyOutputDeviceFn(gnOutputDevice* device) {
|
||||
// float x, y;
|
||||
// float u, v;
|
||||
// };
|
||||
|
||||
// GN_EXPORT gnReturnCode gnRegisterOutputDeviceFn(gnOutputDevice* outputDevice, const gnInstance& instance, const gnPhysicalOutputDevice& physicalDevice) {
|
||||
// if (outputDevice->outputDevice == nullptr) outputDevice->outputDevice = new gnPlatformOutputDevice();
|
||||
// outputDevice->physicalOutputDevice = const_cast<gnPhysicalOutputDevice*>(&physicalDevice);
|
||||
|
||||
// // instance.instance->metalLayer->setDevice(physicalDevice.physicalOutputDevice->device);
|
||||
|
||||
// // outputDevice->outputDevice->contentView = instance.instance->metalContentView->retain();
|
||||
// outputDevice->outputDevice->device = physicalDevice.physicalOutputDevice->device->retain();
|
||||
// outputDevice->outputDevice->commandQueue = outputDevice->outputDevice->device->newCommandQueue();
|
||||
// outputDevice->outputDevice->instance = const_cast<gnInstance*>(&instance);
|
||||
|
||||
// {
|
||||
|
||||
// NS::Error* error = nullptr;
|
||||
// MTL::CompileOptions* options = nullptr;
|
||||
// MTL::Library* library = physicalDevice.physicalOutputDevice->device->newLibrary(NS::String::string(shaderSrc, NS::UTF8StringEncoding), options, &error);
|
||||
// if (!library) {
|
||||
// return gnReturnError(GN_FAILED_CREATE_DEVICE, error->localizedDescription()->utf8String());
|
||||
// }
|
||||
// MTL::Function* vs = library->newFunction(NS::String::string("vs_main", NS::UTF8StringEncoding));
|
||||
// MTL::Function* fs = library->newFunction(NS::String::string("fs_main", NS::UTF8StringEncoding));
|
||||
|
||||
// MTL::RenderPipelineDescriptor* pipelineDesc = MTL::RenderPipelineDescriptor::alloc()->init();
|
||||
// pipelineDesc->setVertexFunction(vs);
|
||||
// pipelineDesc->setFragmentFunction(fs);
|
||||
// pipelineDesc->colorAttachments()->object(0)->setPixelFormat(MTL::PixelFormatBGRA8Unorm);
|
||||
|
||||
// instance.instance->framebufferRenderer = outputDevice->outputDevice->device->newRenderPipelineState(pipelineDesc, &error);
|
||||
// if (!instance.instance->framebufferRenderer) {
|
||||
// return gnReturnError(GN_FAILED_CREATE_DEVICE, error->localizedDescription()->utf8String());
|
||||
// }
|
||||
// }
|
||||
|
||||
// return GN_SUCCESS;
|
||||
// }
|
||||
|
||||
// GN_EXPORT void gnWaitForDeviceFn(const gnOutputDevice& device) {
|
||||
// NS::AutoreleasePool* pool = NS::AutoreleasePool::alloc()->init();
|
||||
|
||||
// auto mtlDevice = device.physicalOutputDevice->physicalOutputDevice->device;
|
||||
|
||||
// auto commandBuffer = device.outputDevice->commandQueue->commandBuffer();
|
||||
|
||||
// commandBuffer->commit();
|
||||
// commandBuffer->waitUntilCompleted();
|
||||
|
||||
// pool->release();
|
||||
// }
|
||||
|
||||
// GN_EXPORT void gnDestroyOutputDeviceFn(gnOutputDevice& device) {
|
||||
// device.outputDevice->commandQueue->release();
|
||||
// device.physicalOutputDevice->physicalOutputDevice->device->release();
|
||||
// }
|
||||
|
||||
@@ -14,4 +14,6 @@ struct gnPlatformOutputDevice_t {
|
||||
|
||||
int queueCount;
|
||||
id<MTLCommandQueue>* queues;
|
||||
|
||||
id<MTLRenderPipelineState> framebuffer;
|
||||
} gnPlatformOutputDevice;
|
||||
|
||||
Reference in New Issue
Block a user