summaryrefslogtreecommitdiff
path: root/src/gpu/Executor.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpu/Executor.zig')
-rw-r--r--src/gpu/Executor.zig41
1 files changed, 32 insertions, 9 deletions
diff --git a/src/gpu/Executor.zig b/src/gpu/Executor.zig
index 412d0ed..460b2d5 100644
--- a/src/gpu/Executor.zig
+++ b/src/gpu/Executor.zig
@@ -155,6 +155,8 @@ const Target = struct {
msaa_fresh: bool = true,
stencil_samples: u8 = 0,
stencil: ?*c.SDL_GPUTexture = null,
+ depth_valid: bool = false,
+ depth_sampleable: bool = false,
};
pub fn init(gpa: std.mem.Allocator, device: *c.SDL_GPUDevice) Gpu {
@@ -229,25 +231,41 @@ pub fn run(g: *Gpu, pass: abi.gpu.Pass) !void {
/// Records into the caller's command buffer; the caller submits it.
/// Uploads cycle their buffers, allowing several passes in one frame.
pub fn record(g: *Gpu, cmd: *c.SDL_GPUCommandBuffer, pass: abi.gpu.Pass) !void {
- return g.recordWithInput(cmd, pass, null);
+ return g.recordWithInput(cmd, pass, &.{});
+}
+
+pub fn runPostProcess(g: *Gpu, source: abi.Image, target: abi.Image, post: abi.gpu.PostProcess) !void {
+ const start = g.startTiming();
+ const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer;
+ errdefer _ = c.SDL_CancelGPUCommandBuffer(cmd);
+ try g.postProcess(cmd, source, target, post);
+ try g.submit(cmd, "postprocess", start);
}
/// A second pass on the same command buffer; input and output must differ.
pub fn postProcess(g: *Gpu, cmd: *c.SDL_GPUCommandBuffer, source: abi.Image, target: abi.Image, post: abi.gpu.PostProcess) !void {
if (source == target) return error.FeedbackLoop;
- const input = g.output(source) orelse return error.MissingImage;
+ const input = g.targets.get(source) orelse return error.MissingImage;
+ var textures: [2]*c.SDL_GPUTexture = undefined;
+ textures[0] = input.texture;
+ if (post.sample_depth) {
+ if (!input.depth_valid or input.stencil == null) return error.MissingDepthAttachment;
+ if (input.stencil_samples != 1) return error.MultisampleDepthUnsupported;
+ if (!input.depth_sampleable) return error.DepthSamplingUnsupported;
+ textures[1] = input.stencil.?;
+ }
try g.recordWithInput(cmd, .{
.image = target,
.width = input.width,
.height = input.height,
.pipelines = &.{.{ .vertex = post.vertex, .fragment = post.fragment, .stride = 0, .attributes = &.{}, .blend = .none }},
.draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = post.fragment_uniforms }},
- }, input.texture);
+ }, textures[0 .. 1 + @as(usize, @intFromBool(post.sample_depth))]);
}
-fn recordWithInput(g: *Gpu, cmd: *c.SDL_GPUCommandBuffer, pass: abi.gpu.Pass, input: ?*c.SDL_GPUTexture) !void {
+fn recordWithInput(g: *Gpu, cmd: *c.SDL_GPUCommandBuffer, pass: abi.gpu.Pass, input: []const *c.SDL_GPUTexture) !void {
if (pass.width == 0 or pass.height == 0 or pass.draws.len == 0 and pass.clear == null) return;
- if (input != null and g.post_sampler == null) {
+ if (input.len != 0 and g.post_sampler == null) {
g.post_sampler = c.SDL_CreateGPUSampler(g.device, &std.mem.zeroInit(c.SDL_GPUSamplerCreateInfo, .{
.min_filter = c.SDL_GPU_FILTER_NEAREST,
.mag_filter = c.SDL_GPU_FILTER_NEAREST,
@@ -269,7 +287,7 @@ fn recordWithInput(g: *Gpu, cmd: *c.SDL_GPUCommandBuffer, pass: abi.gpu.Pass, in
if (d.pipeline >= pass.pipelines.len) return error.BadPipeline;
const p = pass.pipelines[d.pipeline];
if ((p.depth_test != null or p.depth_write) and !pass.depth) return error.MissingDepthAttachment;
- if (built[d.pipeline] == null) built[d.pipeline] = try g.pipeline(pass.pipelines[d.pipeline], samples, pass.stencil or pass.depth, @intFromBool(input != null));
+ if (built[d.pipeline] == null) built[d.pipeline] = try g.pipeline(pass.pipelines[d.pipeline], samples, pass.stencil or pass.depth, @intCast(input.len));
}
// Every draw's vertices go up in one copy.
@@ -314,13 +332,14 @@ fn recordWithInput(g: *Gpu, cmd: *c.SDL_GPUCommandBuffer, pass: abi.gpu.Pass, in
depth.texture = t.stencil;
depth.load_op = if (pass.depth) c.SDL_GPU_LOADOP_CLEAR else c.SDL_GPU_LOADOP_DONT_CARE;
depth.clear_depth = 1;
- depth.store_op = c.SDL_GPU_STOREOP_DONT_CARE;
+ depth.store_op = if (pass.depth) c.SDL_GPU_STOREOP_STORE else c.SDL_GPU_STOREOP_DONT_CARE;
depth.stencil_load_op = c.SDL_GPU_LOADOP_CLEAR;
depth.stencil_store_op = c.SDL_GPU_STOREOP_DONT_CARE;
depth.clear_stencil = 0;
}
const rp = c.SDL_BeginGPURenderPass(cmd, &color, 1, if (pass.stencil or pass.depth) &depth else null) orelse return error.GpuRenderPass;
- if (input) |texture_| c.SDL_BindGPUFragmentSamplers(rp, 0, &.{ .texture = texture_, .sampler = g.post_sampler.? }, 1);
+ t.depth_valid = pass.depth;
+ for (input, 0..) |texture_, slot| c.SDL_BindGPUFragmentSamplers(rp, @intCast(slot), &.{ .texture = texture_, .sampler = g.post_sampler.? }, 1);
var bound: ?*c.SDL_GPUGraphicsPipeline = null;
// State already set in this pass, so draws that repeat it skip the call.
var vertex_uniforms: ?[]const u8 = null;
@@ -575,9 +594,13 @@ fn renderTarget(g: *Gpu, image: abi.Image, w: u32, h: u32, samples: u8, stencil:
t.msaa_fresh = true;
}
if (stencil and (t.stencil == null or t.stencil_samples != samples)) {
+ const sampled_usage = c.SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET | c.SDL_GPU_TEXTUREUSAGE_SAMPLER;
+ const sampleable = samples == 1 and c.SDL_GPUTextureSupportsFormat(g.device, g.stencil_format, c.SDL_GPU_TEXTURETYPE_2D, sampled_usage);
+ const replacement = try g.texture(w, h, g.stencil_format, if (sampleable) sampled_usage else c.SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET, samples);
if (t.stencil) |s| c.SDL_ReleaseGPUTexture(g.device, s);
- t.stencil = try g.texture(w, h, g.stencil_format, c.SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET, samples);
+ t.stencil = replacement;
t.stencil_samples = samples;
+ t.depth_sampleable = sampleable;
}
return t;
}