Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions assets/cubyz/shaders/graphics/graph.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

layout(location = 0) out vec4 frag_color;

#ifdef OPEN_GL
layout(location = 5) uniform vec3 lineColor;
#else
layout(push_constant, std430) uniform _ {
vec2 start;
vec2 dimension;
vec2 screen;
int points;
int offset;
vec3 lineColor;
};
#endif

void main() {
frag_color = vec4(lineColor, 1);
Expand Down
11 changes: 11 additions & 0 deletions assets/cubyz/shaders/graphics/graph.vert
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
#version 460

#ifdef OPEN_GL
// in pixel
layout(location = 0) uniform vec2 start;
layout(location = 1) uniform vec2 dimension;
layout(location = 2) uniform vec2 screen;
layout(location = 3) uniform int points;
layout(location = 4) uniform int offset;
#else
layout(push_constant, std430) uniform _ {
vec2 start;
vec2 dimension;
vec2 screen;
int points;
int offset;
vec3 lineColor;
};
#endif

layout(std430, binding = 5) buffer _data
{
Expand Down
28 changes: 14 additions & 14 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,37 @@
.lazy = true,
},
.cubyz_deps_headers = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/15/cubyz_deps_headers.tar.gz",
.hash = "N-V-__8AAM-mgACe7tD-FszvbiOt6OT20kfflXo0U4W7Ocf2",
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/16/cubyz_deps_headers.tar.gz",
.hash = "N-V-__8AAGWugADL8wEBCVTt1693f2FFYlTOtARkDPfCA5IV",
},
.cubyz_deps_aarch64_macos = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/15/cubyz_deps_aarch64-macos-none.tar.gz",
.hash = "N-V-__8AAP4b0AUagCF3rHszwtcDGvqy1nmHFh6NKJEyW_sB",
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/16/cubyz_deps_aarch64-macos-none.tar.gz",
.hash = "N-V-__8AAI4c0AXTRMQ1oq7SmlzUdOMFrn27lkTCTgLOPbdX",
.lazy = true,
},
.cubyz_deps_aarch64_linux = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/15/cubyz_deps_aarch64-linux-musl.tar.gz",
.hash = "N-V-__8AAGB0CQMn4gzU9hxnpMoYao13RYpAYSgIQaWWWgAw",
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/16/cubyz_deps_aarch64-linux-musl.tar.gz",
.hash = "N-V-__8AAAp-CQNms25cSzNPSuLeREoJrUMrsDUg_rKVswqD",
.lazy = true,
},
.cubyz_deps_aarch64_windows = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/15/cubyz_deps_aarch64-windows-gnu.tar.gz",
.hash = "N-V-__8AAFIsLAMsvnDrhB3GbIWf_uaF4fiWs2xW4XICDDxC",
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/16/cubyz_deps_aarch64-windows-gnu.tar.gz",
.hash = "N-V-__8AAFI0LAOW7i-eoOvmZPbP3m_8RjBa0ApOZZVWcmuI",
.lazy = true,
},
.cubyz_deps_x86_64_macos = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/15/cubyz_deps_x86_64-macos-none.tar.gz",
.hash = "N-V-__8AACBhwgXczzGAqEqj60G8q1Z96x0BNBD8J5cbOgBc",
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/16/cubyz_deps_x86_64-macos-none.tar.gz",
.hash = "N-V-__8AALBhwgWlN6EDwCpwkGloSo4glB2Sy1BlTPp16p6L",
.lazy = true,
},
.cubyz_deps_x86_64_linux = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/15/cubyz_deps_x86_64-linux-musl.tar.gz",
.hash = "N-V-__8AACRdDAP1u3TIi39quNzYPCZXxZfeWtqVwP8cz7k6",
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/16/cubyz_deps_x86_64-linux-musl.tar.gz",
.hash = "N-V-__8AAP5mDAO07XbYWBftU0_TGnDRdtvLEC4LyjPnEJHU",
.lazy = true,
},
.cubyz_deps_x86_64_windows = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/15/cubyz_deps_x86_64-windows-gnu.tar.gz",
.hash = "N-V-__8AAJCyTQP3paHxSzDuz6tzj_BX7ImSiI6BVWAliH6j",
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/16/cubyz_deps_x86_64-windows-gnu.tar.gz",
.hash = "N-V-__8AAEC6TQMzg397LH97hse_0-Cag-pF0-oUHiCZ_k7m",
.lazy = true,
},
.cubyz_large_assets = .{
Expand Down
28 changes: 22 additions & 6 deletions src/graphics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,7 @@ pub const VertexArray = struct { // MARK: VertexArray

pub const SSBO = struct { // MARK: SSBO
bufferID: c_uint,
buffer: ?vulkan.Buffer = null,
pub fn init() SSBO {
var self = SSBO{.bufferID = undefined};
c.glGenBuffers(1, &self.bufferID);
Expand All @@ -1408,7 +1409,22 @@ pub const SSBO = struct { // MARK: SSBO
return self;
}

pub fn initDynamicSize(comptime T: type, len: usize) SSBO {
var self = SSBO{.bufferID = undefined};
c.glGenBuffers(1, &self.bufferID);
c.glBindBuffer(c.GL_SHADER_STORAGE_BUFFER, self.bufferID);
c.glBufferData(c.GL_SHADER_STORAGE_BUFFER, @intCast(len*@sizeOf(T)), null, c.GL_DYNAMIC_DRAW);
c.glBindBuffer(c.GL_SHADER_STORAGE_BUFFER, 0);
if (main.settings.launchConfig.vulkanTestingMode) {
self.buffer = .init(len*@sizeOf(T), .{.usage = c.VK_BUFFER_USAGE_TRANSFER_DST_BIT | c.VK_BUFFER_USAGE_STORAGE_BUFFER_BIT});
}
return self;
}

pub fn deinit(self: SSBO) void {
if (self.buffer) |buffer| {
buffer.deferredDeinit();
}
c.glDeleteBuffers(1, &self.bufferID);
}

Expand All @@ -1420,18 +1436,18 @@ pub const SSBO = struct { // MARK: SSBO
c.glBindBuffer(c.GL_SHADER_STORAGE_BUFFER, self.bufferID);
c.glBufferData(c.GL_SHADER_STORAGE_BUFFER, @intCast(data.len*@sizeOf(T)), data.ptr, c.GL_STATIC_DRAW);
c.glBindBuffer(c.GL_SHADER_STORAGE_BUFFER, 0);
if (self.buffer) |buffer| {
buffer.uploadData(0, std.mem.sliceAsBytes(data));
}
}

pub fn bufferSubData(self: SSBO, comptime T: type, data: []const T, length: usize) void {
c.glBindBuffer(c.GL_SHADER_STORAGE_BUFFER, self.bufferID);
c.glBufferSubData(c.GL_SHADER_STORAGE_BUFFER, 0, @intCast(length*@sizeOf(T)), data.ptr);
c.glBindBuffer(c.GL_SHADER_STORAGE_BUFFER, 0);
}

pub fn createDynamicBuffer(self: SSBO, comptime T: type, size: usize) void {
c.glBindBuffer(c.GL_SHADER_STORAGE_BUFFER, self.bufferID);
c.glBufferData(c.GL_SHADER_STORAGE_BUFFER, @intCast(size*@sizeOf(T)), null, c.GL_DYNAMIC_DRAW);
c.glBindBuffer(c.GL_SHADER_STORAGE_BUFFER, 0);
if (self.buffer) |buffer| {
buffer.uploadData(0, std.mem.sliceAsBytes(data));
}
}
};

Expand Down
43 changes: 43 additions & 0 deletions src/graphics/CommandBuffer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,49 @@ pub fn bindVertexArray(self: CommandBuffer, buffer: main.graphics.VertexArray) v
}
}

const DescriptorBindPoint = enum(c.VkPipelineBindPoint) {
graphics = c.VK_PIPELINE_BIND_POINT_GRAPHICS,
compute = c.VK_PIPELINE_BIND_POINT_COMPUTE,
};

const BindingInfo = union(enum) {
ssbo: struct {
binding: u32,
dynamic: bool = false,
ssbo: main.graphics.SSBO,
offset: usize = 0,
range: usize = c.VK_WHOLE_SIZE,
},
};

pub fn bindDescriptors(self: CommandBuffer, pipeline: main.graphics.Pipeline, bindPoint: DescriptorBindPoint, set: u32, bindings: []const BindingInfo) void {
const arena = main.stackAllocator.createArena();
defer main.stackAllocator.destroyArena(arena);
const writeInfo = arena.alloc(c.VkWriteDescriptorSet, bindings.len);
for (0..bindings.len) |i| {
writeInfo[i] = .{
.sType = c.VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstBinding = switch (bindings[i]) {
inline else => |b| b.binding,
},
.descriptorCount = 1,
};
switch (bindings[i]) {
.ssbo => |ssbo| {
writeInfo[i].descriptorType = if (ssbo.dynamic) c.VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC else c.VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
const bufferInfo = arena.create(c.VkDescriptorBufferInfo);
bufferInfo.* = .{
.buffer = ssbo.ssbo.buffer.?.handle,
.offset = ssbo.offset,
.range = ssbo.range,
};
writeInfo[i].pBufferInfo = bufferInfo;
},
}
}
c.vkCmdPushDescriptorSetKHR(self.handle, @intFromEnum(bindPoint), pipeline.pipelineLayout, set, @intCast(writeInfo.len), writeInfo.ptr);
}

pub fn pushConstants(self: CommandBuffer, pipeline: main.graphics.Pipeline, constants: anytype) void {
c.vkCmdPushConstants(self.handle, pipeline.pipelineLayout, c.VK_SHADER_STAGE_ALL, 0, @sizeOf(@TypeOf(constants.*)), constants);
}
Expand Down
1 change: 1 addition & 0 deletions src/graphics/pipelines.zig
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ pub const Pipeline = struct { // MARK: Pipeline

const descriptorSetLayoutInfo = c.VkDescriptorSetLayoutCreateInfo{
.sType = c.VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.flags = c.VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,
.bindingCount = @intCast(options.bindings.len),
.pBindings = @ptrCast(options.bindings.ptr),
};
Expand Down
1 change: 1 addition & 0 deletions src/graphics/vulkan.zig
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ pub fn createInstance() void {
const deviceExtensions = blk: {
const baseDeviceExtensions = [_][*:0]const u8{
c.VK_KHR_SWAPCHAIN_EXTENSION_NAME,
c.VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME,
};
if (builtin.target.os.tag == .macos) {
break :blk baseDeviceExtensions ++ [_][*:0]const u8{c.VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME};
Expand Down
30 changes: 29 additions & 1 deletion src/gui/windows/performance_graph.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const main = @import("main");
const graphics = main.graphics;
const draw = graphics.draw;
const Texture = graphics.Texture;
const vulkan = graphics.vulkan;
const Vec2f = main.vec.Vec2f;

const c = @import("c");
Expand Down Expand Up @@ -40,19 +41,30 @@ var uniforms: struct {
offset: c_int,
lineColor: c_int,
} = undefined;
const Uniforms = extern struct {
start: [2]f32 align(8),
dimension: [2]f32 align(8),
screen: [2]f32 align(8),
points: c_int,
offset: c_int,
lineColor: [3]f32 align(16),
};

pub fn init() void {
ssbo = graphics.SSBO.init();
ssbo = graphics.SSBO.initDynamicSize(f32, lastFrameTime.len);
pipeline = graphics.Pipeline.init(
"assets/cubyz/shaders/graphics/graph.vert",
"assets/cubyz/shaders/graphics/graph.frag",
"",
&uniforms,
graphics.VertexArray.EmptyVertex,
.{
.bindings = &.{.ssbo(5, .{.vertex = true})},
.rasterState = .{.cullMode = .none},
.depthStencilState = .{.depthTest = false, .depthWrite = false},
.blendState = .{.attachments = &.{.alphaBlending}, .formats = &.{.swapChain}},
.inputAssemblyState = .{.topology = .lineStrip},
.pushConstantSize = @sizeOf(Uniforms),
},
);
}
Expand Down Expand Up @@ -93,4 +105,20 @@ pub fn render() void {
ssbo.bufferData(f32, &lastFrameTime);
ssbo.bind(5);
c.glDrawArrays(c.GL_LINE_STRIP, 0, lastFrameTime.len);

if (main.settings.launchConfig.vulkanTestingMode) {
vulkan.currentFrame.guiCommands.bindPipeline(pipeline, null);
vulkan.currentFrame.guiCommands.bindDescriptors(pipeline, .graphics, 0, &.{
.{.ssbo = .{.binding = 5, .ssbo = ssbo}},
});
vulkan.currentFrame.guiCommands.pushConstants(pipeline, &Uniforms{
.start = pos,
.dimension = .{dim[0], draw.setScale(1)},
.screen = .{@floatFromInt(main.Window.width), @floatFromInt(main.Window.height)},
.points = lastFrameTime.len,
.offset = index,
.lineColor = .{1, 1, 1},
});
vulkan.currentFrame.guiCommands.draw(lastFrameTime.len, 0);
}
}
3 changes: 1 addition & 2 deletions src/particles.zig
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ pub const ParticleSystem = struct {
},
);

particlesSSBO = SSBO.init();
particlesSSBO.createDynamicBuffer(Particle, maxCapacity);
particlesSSBO = SSBO.initDynamicSize(Particle, maxCapacity);
particlesSSBO.bind(13);
}

Expand Down
Loading