pass a uniform

This commit is contained in:
David Allemang
2024-07-08 10:51:42 -04:00
parent 29cd7fa5e5
commit 53d063246b
3 changed files with 57 additions and 12 deletions

View File

@@ -44,14 +44,9 @@ pub const Vertex = extern struct {
}; };
pub const Uniform = extern struct { pub const Uniform = extern struct {
proj: [16]f32 = .{ proj: [16]f32,
0.5, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
},
const DescriptorLayoutInfo = vk.DescriptorSetLayoutCreateInfo{ pub const DescriptorLayoutInfo = vk.DescriptorSetLayoutCreateInfo{
.flags = .{}, .flags = .{},
.binding_count = 1, .binding_count = 1,
.p_bindings = &.{ .p_bindings = &.{

View File

@@ -23,7 +23,14 @@ const vertices = [_]Uber.Vertex{
const indices = [_]Uber.Index{ 4, 5, 6, 6, 5, 7 }; const indices = [_]Uber.Index{ 4, 5, 6, 6, 5, 7 };
const uniform = Uber.Uniform{}; const uniform = Uber.Uniform{
.proj = .{
0.5, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
},
};
const Frame = struct { const Frame = struct {
pub fn init() !Frame { pub fn init() !Frame {
@@ -41,9 +48,11 @@ const Frame = struct {
view: vk.ImageView, view: vk.ImageView,
scissor: vk.Rect2D, scissor: vk.Rect2D,
pipeline: vk.Pipeline, pipeline: vk.Pipeline,
layout: vk.PipelineLayout,
vertex_buffer: vk.Buffer, vertex_buffer: vk.Buffer,
index_buffer: vk.Buffer, index_buffer: vk.Buffer,
uniform_buffer: vk.Buffer, uniform_buffer: vk.Buffer,
descriptor_set: vk.DescriptorSet,
) !void { ) !void {
_ = self; _ = self;
@@ -103,6 +112,7 @@ const Frame = struct {
cmd.beginRendering(&info); cmd.beginRendering(&info);
cmd.bindDescriptorSets(.graphics, layout, 0, 1, &.{descriptor_set}, 0, null);
cmd.bindPipeline(.graphics, pipeline); cmd.bindPipeline(.graphics, pipeline);
cmd.bindVertexBuffers(0, 1, &.{vertex_buffer}, &.{0}); cmd.bindVertexBuffers(0, 1, &.{vertex_buffer}, &.{0});
cmd.bindIndexBuffer(index_buffer, 0, .uint16); cmd.bindIndexBuffer(index_buffer, 0, .uint16);
@@ -282,6 +292,8 @@ pub fn main() !void {
defer au.D.unmapMemory(index_memory); defer au.D.unmapMemory(index_memory);
index_data.* = indices; index_data.* = indices;
// todo ring buffer for frames in flight. need to use an offset when binding
// use dynamic offset - descriptor type VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
const uniform_buffer = try au.D.createBuffer(&vk.BufferCreateInfo{ const uniform_buffer = try au.D.createBuffer(&vk.BufferCreateInfo{
.size = @sizeOf(@TypeOf(uniform)), .size = @sizeOf(@TypeOf(uniform)),
.usage = .{ .uniform_buffer_bit = true }, .usage = .{ .uniform_buffer_bit = true },
@@ -299,6 +311,38 @@ pub fn main() !void {
defer au.D.unmapMemory(uniform_memory); defer au.D.unmapMemory(uniform_memory);
uniform_data.* = uniform; uniform_data.* = uniform;
var descriptorSet: vk.DescriptorSet = undefined;
try au.D.allocateDescriptorSets(&vk.DescriptorSetAllocateInfo{
.descriptor_pool = descriptorPool,
.descriptor_set_count = 1,
.p_set_layouts = &.{uber.set_layout},
}, @ptrCast(&descriptorSet));
defer au.D.freeDescriptorSets(descriptorPool, 1, &.{descriptorSet}) catch unreachable; // todo handle this?
au.D.updateDescriptorSets(
1,
&.{
vk.WriteDescriptorSet{
.dst_set = descriptorSet,
.dst_binding = 0,
.dst_array_element = 0,
.descriptor_type = .uniform_buffer,
.descriptor_count = 1,
.p_image_info = &[0]vk.DescriptorImageInfo{},
.p_texel_buffer_view = &[0]vk.BufferView{},
.p_buffer_info = &.{
vk.DescriptorBufferInfo{
.buffer = uniform_buffer,
.offset = 0,
.range = vk.WHOLE_SIZE,
},
},
},
},
0,
null,
);
var prng = std.Random.Sfc64.init(std.crypto.random.int(u64)); var prng = std.Random.Sfc64.init(std.crypto.random.int(u64));
const rand = prng.random(); const rand = prng.random();
@@ -350,9 +394,11 @@ pub fn main() !void {
view, view,
vk.Rect2D{ .offset = .{ .x = 0, .y = 0 }, .extent = sc.cinfo.image_extent }, vk.Rect2D{ .offset = .{ .x = 0, .y = 0 }, .extent = sc.cinfo.image_extent },
uber.pipeline, uber.pipeline,
uber.layout,
vertex_buffer, vertex_buffer,
index_buffer, index_buffer,
uniform_buffer, uniform_buffer,
descriptorSet,
); );
for (vertex_data) |*v| { for (vertex_data) |*v| {

View File

@@ -1,11 +1,15 @@
#version 450 #version 450
layout(location = 0) in vec4 a_pos; layout (set = 0, binding = 0) uniform CameraBuffer {
layout(location = 1) in vec3 a_color; mat4 viewproj;
} cam;
layout(location = 0) out vec3 v_color; layout (location = 0) in vec4 a_pos;
layout (location = 1) in vec3 a_color;
layout (location = 0) out vec3 v_color;
void main() { void main() {
gl_Position = a_pos; gl_Position = a_pos * cam.viewproj;
v_color = a_color; v_color = a_color;
} }