diff --git a/entropy/render.nelua b/entropy/render.nelua index 700a2a6..7997f55 100644 --- a/entropy/render.nelua +++ b/entropy/render.nelua @@ -20,7 +20,7 @@ function Render:request_adapter_device(surface: WGPUSurface): (WGPUAdapter, WGPU &(@WGPURequestAdapterOptions){ powerPreference = WGPUPowerPreference_HighPerformance, compatibleSurface = surface, - forceFallbackAdapter = false, + forceFallbackAdapter = 0, }, function (status: WGPURequestAdapterStatus, received: WGPUAdapter, message: cstring, userdata: pointer): void print("adapter status: ", tostring(status), " | message: ", tostring(message)) diff --git a/entropy/render/context/glfw.nelua b/entropy/render/context/glfw.nelua index 6074888..0a776a3 100644 --- a/entropy/render/context/glfw.nelua +++ b/entropy/render/context/glfw.nelua @@ -8,9 +8,8 @@ local Event = require "entropy.defs.event" require "list" local GLFWContext: type = @record{ - width: uint32, - height: uint32, vsync: boolean, + surface_config: WGPUSurfaceConfiguration, user_pointer: pointer, resize_callback: function(pointer, uint32, uint32): void, @@ -21,38 +20,23 @@ local GLFWContext: type = @record{ adapter: WGPUAdapter, device: WGPUDevice, - format: WGPUTextureFormat, - - swapchain: WGPUSwapChain, - event_queue: list(Event), } -function GLFWContext:create_swapchain(): void - local present_mode: WGPUPresentMode +function GLFWContext:configure_surface(): void if self.vsync then - present_mode = WGPUPresentMode_Fifo + self.surface_config.presentMode = WGPUPresentMode_Fifo else - present_mode = WGPUPresentMode_Mailbox + self.surface_config.presentMode = WGPUPresentMode_Mailbox end - self.swapchain = wgpuDeviceCreateSwapChain( - self.device, - self.surface, - &(@WGPUSwapChainDescriptor){ - usage = WGPUTextureUsage_RenderAttachment, - format = self.format, - width = self.width, - height = self.height, - presentMode = present_mode, - } - ) + wgpuSurfaceConfigure(self.surface, self.surface_config) end function GLFWContext:resize(width: uint32, height: uint32): void - self.width = width - self.height = height - self:create_swapchain() + self.surface_config.width = width + self.surface_config.height = height + self:configure_surface() end function GLFWContext:init(title: string, width: uint32, height: uint32, vsync: boolean, render: *Render): void @@ -64,8 +48,10 @@ function GLFWContext:init(title: string, width: uint32, height: uint32, vsync: b -- create glfw window self.window = glfwCreateWindow(width, height, title, nilptr, nilptr) assert(self.window, "failed to create window!") - self.width = width - self.height = height + self.surface_config = (@WGPUSurfaceConfiguration){ + width = width, + height = height, + } -- set user pointer to ourselves glfwSetWindowUserPointer(self.window, self) @@ -158,12 +144,18 @@ function GLFWContext:init(title: string, width: uint32, height: uint32, vsync: b -- request adapter and device self.adapter, self.device = render:request_adapter_device(self.surface) - -- get surface format - self.format = wgpuSurfaceGetPreferredFormat(self.surface, self.adapter) + -- get supported surface capabilities + local supported_surface_capabilities: WGPUSurfaceCapabilities + wgpuSurfaceGetCapabilities(self.surface, self.adapter, &supported_surface_capabilities) + defer wgpuSurfaceCapabilitiesFreeMembers(supported_surface_capabilities) end + self.surface_config.device = self.device + self.surface_config.usage = WGPUTextureUsage_RenderAttachment + self.surface_config.format = (@*[0]WGPUTextureFormat)(supported_surface_capabilities.formats)[0] + self.surface_config.alphaMode = (@*[0]WGPUCompositeAlphaMode)(supported_surface_capabilities.alphaModes)[0] - -- create initial swapchain + -- initial surface configuration self.vsync = vsync - self:create_swapchain() + self:configure_surface() end function GLFWContext:cleanup(): void @@ -171,11 +163,16 @@ function GLFWContext:cleanup(): void end function GLFWContext:view(): WGPUTextureView - return wgpuSwapChainGetCurrentTextureView(self.swapchain) + local surface_texture: WGPUSurfaceTexture + wgpuSurfaceGetCurrentTexture(self.surface, &surface_texture) + if surface_texture == nilptr then + return nilptr + end + return wgpuTextureCreateView(surface_texture.texture, nilptr) end function GLFWContext:present(): void - wgpuSwapChainPresent(self.swapchain) + wgpuSurfacePresent(self.surface) end function GLFWContext:should_close(): boolean diff --git a/entropy/render/impl/mesh.nelua b/entropy/render/impl/mesh.nelua index 6c8ba85..5a62346 100644 --- a/entropy/render/impl/mesh.nelua +++ b/entropy/render/impl/mesh.nelua @@ -122,7 +122,7 @@ function Impl:init(renderer: a_renderer): void visibility = WGPUShaderStage_Vertex, buffer = (@WGPUBufferBindingLayout){ type = WGPUBufferBindingType_Uniform, - hasDynamicOffset = false, + hasDynamicOffset = 0, minBindingSize = #Globals, }, }, @@ -140,7 +140,7 @@ function Impl:init(renderer: a_renderer): void label = "mesh globals buffer", usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst, size = #Globals, - mappedAtCreation = false, + mappedAtCreation = 0, } ) self.globals_bind_group = wgpuDeviceCreateBindGroup( @@ -198,11 +198,11 @@ function Impl:init(renderer: a_renderer): void multisample = (@WGPUMultisampleState){ count = 1, mask = 0xffffffff, - alphaToCoverageEnabled = false, + alphaToCoverageEnabled = 0, }, depthStencil = &(@WGPUDepthStencilState){ format = WGPUTextureFormat_Depth32Float, - depthWriteEnabled = true, + depthWriteEnabled = 1, depthCompare = WGPUCompareFunction_Less, stencilFront = (@WGPUStencilFaceState){ compare = WGPUCompareFunction_Always, diff --git a/entropy/render/renderer.nelua b/entropy/render/renderer.nelua index 750ee03..a99d1a2 100644 --- a/entropy/render/renderer.nelua +++ b/entropy/render/renderer.nelua @@ -126,11 +126,11 @@ local World = require "entropy.world" depthLoadOp = WGPULoadOp_Clear, depthStoreOp = WGPUStoreOp_Store, depthClearValue = 1.0, - depthReadOnly = false, + depthReadOnly = 0, stencilLoadOp = WGPULoadOp_Clear, stencilStoreOp = WGPUStoreOp_Store, stencilClearValue = 0, - stencilReadOnly = false, + stencilReadOnly = 0, }, } ) diff --git a/entropy/scene/mesh.nelua b/entropy/scene/mesh.nelua index 3d99838..b8c86db 100644 --- a/entropy/scene/mesh.nelua +++ b/entropy/scene/mesh.nelua @@ -141,7 +141,7 @@ function Mesh:prepare(renderer: a_renderer) label = "mesh vertex buffer", usage = WGPUBufferUsage_Vertex, size = #Mesh.Vertex * #self.vertices, - mappedAtCreation = true, + mappedAtCreation = 1, } ) local vertex_buffer_mapped = (@*[0]Mesh.Vertex)(wgpuBufferGetMappedRange(self.vertex_buffer, 0, #Mesh.Vertex * #self.vertices)) @@ -157,7 +157,7 @@ function Mesh:prepare(renderer: a_renderer) label = "mesh index buffer", usage = WGPUBufferUsage_Index, size = #uint32 * #self.indices, - mappedAtCreation = true, + mappedAtCreation = 1, } ) local index_buffer_mapped = (@*[0]uint32)(wgpuBufferGetMappedRange(self.index_buffer, 0, #uint32 * #self.indices)) @@ -173,7 +173,7 @@ function Mesh:prepare(renderer: a_renderer) label = "mesh instance buffer", usage = WGPUBufferUsage_Vertex | WGPUBufferUsage_CopyDst, size = #Mesh.Instance * #self.instances, - mappedAtCreation = true, + mappedAtCreation = 1, } ) local instance_buffer_mapped = (@*[0]Mesh.Instance)(wgpuBufferGetMappedRange(self.instance_buffer, 0, #Mesh.Instance * #self.instances)) diff --git a/main.nelua b/main.nelua index 01c9a8d..439a8fd 100644 --- a/main.nelua +++ b/main.nelua @@ -132,7 +132,7 @@ local Impl = ComposeImpl({ ]] local ImplT = #[Impl]# local renderer: Renderer(ImplT) -renderer:init(context.device, context.format, context.width, context.height) +renderer:init(context.device, context.surface_config.format, context.surface_config.width, context.surface_config.height) -- setup resize callback context.user_pointer = &renderer