Skip to content

Commit

Permalink
fix: fixes for wgpu-native changes
Browse files Browse the repository at this point in the history
  • Loading branch information
wozeparrot committed Dec 3, 2023
1 parent 2350e74 commit fa2087f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 43 deletions.
2 changes: 1 addition & 1 deletion entropy/render.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
61 changes: 29 additions & 32 deletions entropy/render/context/glfw.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -158,24 +144,35 @@ 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
glfwTerminate()
end

function GLFWContext:view(): WGPUTextureView <inline>
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 <inline>
wgpuSwapChainPresent(self.swapchain)
wgpuSurfacePresent(self.surface)
end

function GLFWContext:should_close(): boolean <inline>
Expand Down
8 changes: 4 additions & 4 deletions entropy/render/impl/mesh.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions entropy/render/renderer.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
)
Expand Down
6 changes: 3 additions & 3 deletions entropy/scene/mesh.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion main.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fa2087f

Please sign in to comment.