diff --git a/gpu/src/NativeInstance.zig b/gpu/src/NativeInstance.zig index cfe8a5c2..dffe534b 100644 --- a/gpu/src/NativeInstance.zig +++ b/gpu/src/NativeInstance.zig @@ -393,42 +393,44 @@ inline fn convertRenderPipelineDescriptor( tmp_depth_stencil: *c.WGPUDepthStencilState, tmp_fragment_state: *c.WGPUFragmentState, ) c.WGPURenderPipelineDescriptor { - tmp_depth_stencil.* = c.WGPUDepthStencilState{ - .nextInChain = null, - .format = @enumToInt(d.depth_stencil.format), - .depthWriteEnabled = d.depth_stencil.depth_write_enabled, - .depthCompare = @enumToInt(d.depth_stencil.depth_compare), - .stencilFront = @bitCast(c.WGPUStencilFaceState, d.depth_stencil.stencil_front), - .stencilBack = @bitCast(c.WGPUStencilFaceState, d.depth_stencil.stencil_back), - .stencilReadMask = d.depth_stencil.stencil_read_mask, - .stencilWriteMask = d.depth_stencil.stencil_write_mask, - .depthBias = d.depth_stencil.depth_bias, - .depthBiasSlopeScale = d.depth_stencil.depth_bias_slope_scale, - .depthBiasClamp = d.depth_stencil.depth_bias_clamp, - }; + if (d.depth_stencil) |ds| { + tmp_depth_stencil.* = c.WGPUDepthStencilState{ + .nextInChain = null, + .format = @enumToInt(ds.format), + .depthWriteEnabled = ds.depth_write_enabled, + .depthCompare = @enumToInt(ds.depth_compare), + .stencilFront = @bitCast(c.WGPUStencilFaceState, ds.stencil_front), + .stencilBack = @bitCast(c.WGPUStencilFaceState, ds.stencil_back), + .stencilReadMask = ds.stencil_read_mask, + .stencilWriteMask = ds.stencil_write_mask, + .depthBias = ds.depth_bias, + .depthBiasSlopeScale = ds.depth_bias_slope_scale, + .depthBiasClamp = ds.depth_bias_clamp, + }; + } tmp_fragment_state.* = c.WGPUFragmentState{ .nextInChain = null, .module = @ptrCast(c.WGPUShaderModule, d.fragment.module.ptr), .entryPoint = d.vertex.entry_point, - .constantCount = @intCast(u32, d.fragment.constants.len), - .constants = @ptrCast(*const c.WGPUConstantEntry, &d.fragment.constants[0]), - .targetCount = @intCast(u32, d.fragment.targets.len), - .targets = @ptrCast(*const c.WGPUColorTargetState, &d.fragment.targets[0]), + .constantCount = if (d.fragment.constants) |v| @intCast(u32, v.len) else 0, + .constants = if (d.fragment.constants) |v| @ptrCast(*const c.WGPUConstantEntry, &v[0]) else null, + .targetCount = if (d.fragment.targets) |v| @intCast(u32, v.len) else 0, + .targets = if (d.fragment.targets) |v| @ptrCast(*const c.WGPUColorTargetState, &v[0]) else null, }; return c.WGPURenderPipelineDescriptor{ .nextInChain = null, .label = d.label, - .layout = @ptrCast(c.WGPUPipelineLayout, d.layout.ptr), + .layout = if (d.layout) |v| @ptrCast(c.WGPUPipelineLayout, v.ptr) else null, .vertex = c.WGPUVertexState{ .nextInChain = null, .module = @ptrCast(c.WGPUShaderModule, d.vertex.module.ptr), .entryPoint = d.vertex.entry_point, - .constantCount = @intCast(u32, d.vertex.constants.len), - .constants = @ptrCast(*const c.WGPUConstantEntry, &d.vertex.constants[0]), - .bufferCount = @intCast(u32, d.vertex.buffers.len), - .buffers = @ptrCast(*const c.WGPUVertexBufferLayout, &d.vertex.buffers[0]), + .constantCount = if (d.vertex.constants) |v| @intCast(u32, v.len) else 0, + .constants = if (d.vertex.constants) |v| @ptrCast(*const c.WGPUConstantEntry, &v[0]) else null, + .bufferCount = if (d.vertex.buffers) |v| @intCast(u32, v.len) else 0, + .buffers = if (d.vertex.buffers) |v| @ptrCast(*const c.WGPUVertexBufferLayout, &v[0]) else null, }, .primitive = c.WGPUPrimitiveState{ .nextInChain = null, @@ -437,7 +439,7 @@ inline fn convertRenderPipelineDescriptor( .frontFace = @enumToInt(d.primitive.front_face), .cullMode = @enumToInt(d.primitive.cull_mode), }, - .depthStencil = tmp_depth_stencil, + .depthStencil = if (d.depth_stencil != null) tmp_depth_stencil else null, .multisample = c.WGPUMultisampleState{ .nextInChain = null, .count = d.multisample.count, diff --git a/gpu/src/RenderPipeline.zig b/gpu/src/RenderPipeline.zig index 4d08f4ae..556c2e4c 100644 --- a/gpu/src/RenderPipeline.zig +++ b/gpu/src/RenderPipeline.zig @@ -34,10 +34,10 @@ pub inline fn setLabel(pipeline: RenderPipeline, label: [:0]const u8) void { pub const Descriptor = struct { label: ?[*:0]const u8 = null, - layout: PipelineLayout, + layout: ?PipelineLayout, vertex: VertexState, primitive: PrimitiveState, - depth_stencil: *const DepthStencilState, + depth_stencil: ?*const DepthStencilState, multisample: MultisampleState, fragment: *const FragmentState, }; diff --git a/gpu/src/structs.zig b/gpu/src/structs.zig index cc88a0f9..e2dbc51c 100644 --- a/gpu/src/structs.zig +++ b/gpu/src/structs.zig @@ -79,7 +79,7 @@ pub const ProgrammableStageDescriptor = struct { label: ?[*:0]const u8 = null, module: ShaderModule, entry_point: [*:0]const u8, - constants: []const ConstantEntry, + constants: ?[]const ConstantEntry, }; pub const ComputePassTimestampWrite = struct { @@ -119,15 +119,15 @@ pub const RenderPassColorAttachment = struct { pub const VertexState = struct { module: ShaderModule, entry_point: [*:0]const u8, - constants: []const ConstantEntry, - buffers: []const VertexBufferLayout, + constants: ?[]const ConstantEntry = null, + buffers: ?[]const VertexBufferLayout = null, }; pub const FragmentState = struct { module: ShaderModule, entry_point: [*:0]const u8, - constants: []const ConstantEntry, - targets: []const ColorTargetState, + constants: ?[]const ConstantEntry = null, + targets: ?[]const ColorTargetState = null, }; pub const ColorTargetState = extern struct {