implementing queries
This commit is contained in:
@@ -45,6 +45,7 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
|
||||
|
||||
interface.dispatch_table = &.{
|
||||
.begin = begin,
|
||||
.beginQuery = beginQuery,
|
||||
.beginRenderPass = beginRenderPass,
|
||||
.bindDescriptorSets = bindDescriptorSets,
|
||||
.bindPipeline = bindPipeline,
|
||||
@@ -58,6 +59,7 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
|
||||
.copyBufferToImage = copyBufferToImage,
|
||||
.copyImage = copyImage,
|
||||
.copyImageToBuffer = copyImageToBuffer,
|
||||
.copyQueryPoolResults = copyQueryPoolResults,
|
||||
.dispatch = dispatch,
|
||||
.dispatchIndirect = dispatchIndirect,
|
||||
.draw = draw,
|
||||
@@ -65,6 +67,7 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
|
||||
.drawIndexedIndirect = drawIndexedIndirect,
|
||||
.drawIndirect = drawIndirect,
|
||||
.end = end,
|
||||
.endQuery = endQuery,
|
||||
.endRenderPass = endRenderPass,
|
||||
.executeCommands = executeCommands,
|
||||
.fillBuffer = fillBuffer,
|
||||
@@ -72,6 +75,7 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
|
||||
.pipelineBarrier = pipelineBarrier,
|
||||
.pushConstants = pushConstants,
|
||||
.reset = reset,
|
||||
.resetQueryPool = resetQueryPool,
|
||||
.resetEvent = resetEvent,
|
||||
.resolveImage = resolveImage,
|
||||
.setEvent = setEvent,
|
||||
@@ -134,6 +138,116 @@ pub fn reset(interface: *Interface, _: vk.CommandBufferResetFlags) VkError!void
|
||||
|
||||
// Commands ====================================================================================================
|
||||
|
||||
pub fn beginQuery(interface: *Interface, pool: *base.QueryPool, query: u32, _: vk.QueryControlFlags) VkError!void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
const allocator = self.command_allocator.allocator();
|
||||
|
||||
const CommandImpl = struct {
|
||||
const Impl = @This();
|
||||
|
||||
pool: *base.QueryPool,
|
||||
query: u32,
|
||||
|
||||
pub fn execute(context: *anyopaque, device: *ExecutionDevice) VkError!void {
|
||||
const impl: *Impl = @ptrCast(@alignCast(context));
|
||||
try impl.pool.begin(impl.query);
|
||||
if (impl.pool.query_type == .occlusion) {
|
||||
for (device.active_occlusion_queries.items) |active| {
|
||||
if (active.pool == impl.pool and active.query == impl.query)
|
||||
return;
|
||||
}
|
||||
device.active_occlusion_queries.append(device.renderer.device.device_allocator.allocator(), .{
|
||||
.pool = impl.pool,
|
||||
.query = impl.query,
|
||||
}) catch return VkError.OutOfDeviceMemory;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const cmd = allocator.create(CommandImpl) catch return VkError.OutOfHostMemory;
|
||||
errdefer allocator.destroy(cmd);
|
||||
cmd.* = .{
|
||||
.pool = pool,
|
||||
.query = query,
|
||||
};
|
||||
self.commands.append(allocator, .{ .ptr = cmd, .vtable = &.{ .execute = CommandImpl.execute } }) catch return VkError.OutOfHostMemory;
|
||||
}
|
||||
|
||||
pub fn endQuery(interface: *Interface, pool: *base.QueryPool, query: u32) VkError!void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
const allocator = self.command_allocator.allocator();
|
||||
|
||||
const CommandImpl = struct {
|
||||
const Impl = @This();
|
||||
|
||||
pool: *base.QueryPool,
|
||||
query: u32,
|
||||
|
||||
pub fn execute(context: *anyopaque, device: *ExecutionDevice) VkError!void {
|
||||
const impl: *Impl = @ptrCast(@alignCast(context));
|
||||
try impl.pool.end(impl.query);
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < device.active_occlusion_queries.items.len) {
|
||||
const active = device.active_occlusion_queries.items[i];
|
||||
if (active.pool == impl.pool and active.query == impl.query) {
|
||||
_ = device.active_occlusion_queries.swapRemove(i);
|
||||
continue;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const cmd = allocator.create(CommandImpl) catch return VkError.OutOfHostMemory;
|
||||
errdefer allocator.destroy(cmd);
|
||||
cmd.* = .{
|
||||
.pool = pool,
|
||||
.query = query,
|
||||
};
|
||||
self.commands.append(allocator, .{ .ptr = cmd, .vtable = &.{ .execute = CommandImpl.execute } }) catch return VkError.OutOfHostMemory;
|
||||
}
|
||||
|
||||
pub fn resetQueryPool(interface: *Interface, pool: *base.QueryPool, first: u32, count: u32) VkError!void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
const allocator = self.command_allocator.allocator();
|
||||
|
||||
const CommandImpl = struct {
|
||||
const Impl = @This();
|
||||
|
||||
pool: *base.QueryPool,
|
||||
first: u32,
|
||||
count: u32,
|
||||
|
||||
pub fn execute(context: *anyopaque, device: *ExecutionDevice) VkError!void {
|
||||
const impl: *Impl = @ptrCast(@alignCast(context));
|
||||
try impl.pool.reset(impl.first, impl.count);
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < device.active_occlusion_queries.items.len) {
|
||||
const active = device.active_occlusion_queries.items[i];
|
||||
if (active.pool == impl.pool and
|
||||
active.query >= impl.first and
|
||||
active.query < impl.first + impl.count)
|
||||
{
|
||||
_ = device.active_occlusion_queries.swapRemove(i);
|
||||
continue;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const cmd = allocator.create(CommandImpl) catch return VkError.OutOfHostMemory;
|
||||
errdefer allocator.destroy(cmd);
|
||||
cmd.* = .{
|
||||
.pool = pool,
|
||||
.first = first,
|
||||
.count = count,
|
||||
};
|
||||
self.commands.append(allocator, .{ .ptr = cmd, .vtable = &.{ .execute = CommandImpl.execute } }) catch return VkError.OutOfHostMemory;
|
||||
}
|
||||
|
||||
pub fn beginRenderPass(interface: *Interface, render_pass: *base.RenderPass, framebuffer: *base.Framebuffer, render_area: vk.Rect2D, clear_values: ?[]const vk.ClearValue) VkError!void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
const allocator = self.command_allocator.allocator();
|
||||
@@ -627,6 +741,45 @@ pub fn copyImageToBuffer(interface: *Interface, src: *base.Image, src_layout: vk
|
||||
self.commands.append(allocator, .{ .ptr = cmd, .vtable = &.{ .execute = CommandImpl.execute } }) catch return VkError.OutOfHostMemory;
|
||||
}
|
||||
|
||||
pub fn copyQueryPoolResults(interface: *Interface, pool: *base.QueryPool, first: u32, count: u32, dst: *base.Buffer, offset: vk.DeviceSize, stride: vk.DeviceSize, flags: vk.QueryResultFlags) VkError!void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
const allocator = self.command_allocator.allocator();
|
||||
|
||||
const CommandImpl = struct {
|
||||
const Impl = @This();
|
||||
|
||||
pool: *base.QueryPool,
|
||||
dst: *SoftBuffer,
|
||||
first: u32,
|
||||
count: u32,
|
||||
offset: vk.DeviceSize,
|
||||
stride: vk.DeviceSize,
|
||||
flags: vk.QueryResultFlags,
|
||||
|
||||
pub fn execute(context: *anyopaque, _: *ExecutionDevice) VkError!void {
|
||||
const impl: *Impl = @ptrCast(@alignCast(context));
|
||||
const value_size: vk.DeviceSize = if (impl.flags.@"64_bit") 8 else 4;
|
||||
const item_size = value_size * (1 + @as(vk.DeviceSize, @intFromBool(impl.flags.with_availability_bit)));
|
||||
const byte_size = if (impl.count == 0) 0 else (impl.count - 1) * impl.stride + item_size;
|
||||
const map = try impl.dst.mapAsSliceWithAddedOffset(u8, impl.offset, byte_size);
|
||||
try impl.pool.copyResults(impl.first, impl.count, map, impl.stride, impl.flags);
|
||||
}
|
||||
};
|
||||
|
||||
const cmd = allocator.create(CommandImpl) catch return VkError.OutOfHostMemory;
|
||||
errdefer allocator.destroy(cmd);
|
||||
cmd.* = .{
|
||||
.pool = pool,
|
||||
.dst = @alignCast(@fieldParentPtr("interface", dst)),
|
||||
.first = first,
|
||||
.count = count,
|
||||
.offset = offset,
|
||||
.stride = stride,
|
||||
.flags = flags,
|
||||
};
|
||||
self.commands.append(allocator, .{ .ptr = cmd, .vtable = &.{ .execute = CommandImpl.execute } }) catch return VkError.OutOfHostMemory;
|
||||
}
|
||||
|
||||
pub fn dispatch(interface: *Interface, group_count_x: u32, group_count_y: u32, group_count_z: u32) VkError!void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
const allocator = self.command_allocator.allocator();
|
||||
|
||||
+1
-241
@@ -378,228 +378,6 @@ fn writeImageInt4(context: *anyopaque, dim: spv.SpvDim, x: i32, y: i32, z: i32,
|
||||
}
|
||||
}
|
||||
|
||||
const CubeCoordinate = struct {
|
||||
face: u32,
|
||||
u: f32,
|
||||
v: f32,
|
||||
w: f32 = 0.0,
|
||||
};
|
||||
|
||||
fn resolveCubeCoordinate(x: f32, y: f32, z: f32) CubeCoordinate {
|
||||
const ax = @abs(x);
|
||||
const ay = @abs(y);
|
||||
const az = @abs(z);
|
||||
|
||||
var face: u32 = 0;
|
||||
var sc: f32 = 0.0;
|
||||
var tc: f32 = 0.0;
|
||||
var ma: f32 = 1.0;
|
||||
|
||||
if (ax >= ay and ax >= az) {
|
||||
ma = ax;
|
||||
if (x >= 0.0) {
|
||||
face = 0;
|
||||
sc = -z;
|
||||
tc = -y;
|
||||
} else {
|
||||
face = 1;
|
||||
sc = z;
|
||||
tc = -y;
|
||||
}
|
||||
} else if (ay >= ax and ay >= az) {
|
||||
ma = ay;
|
||||
if (y >= 0.0) {
|
||||
face = 2;
|
||||
sc = x;
|
||||
tc = z;
|
||||
} else {
|
||||
face = 3;
|
||||
sc = x;
|
||||
tc = -z;
|
||||
}
|
||||
} else {
|
||||
ma = az;
|
||||
if (z >= 0.0) {
|
||||
face = 4;
|
||||
sc = x;
|
||||
tc = -y;
|
||||
} else {
|
||||
face = 5;
|
||||
sc = -x;
|
||||
tc = -y;
|
||||
}
|
||||
}
|
||||
|
||||
const inv_ma = if (ma == 0.0) 0.0 else 1.0 / ma;
|
||||
return .{
|
||||
.face = face,
|
||||
.u = (sc * inv_ma + 1.0) * 0.5,
|
||||
.v = (tc * inv_ma + 1.0) * 0.5,
|
||||
};
|
||||
}
|
||||
|
||||
fn cubeDirection(face: u32, u: f32, v: f32) struct { x: f32, y: f32, z: f32 } {
|
||||
const sc = u * 2.0 - 1.0;
|
||||
const tc = v * 2.0 - 1.0;
|
||||
|
||||
return switch (face) {
|
||||
0 => .{ .x = 1.0, .y = -tc, .z = -sc },
|
||||
1 => .{ .x = -1.0, .y = -tc, .z = sc },
|
||||
2 => .{ .x = sc, .y = 1.0, .z = tc },
|
||||
3 => .{ .x = sc, .y = -1.0, .z = -tc },
|
||||
4 => .{ .x = sc, .y = -tc, .z = 1.0 },
|
||||
5 => .{ .x = -sc, .y = -tc, .z = -1.0 },
|
||||
else => .{ .x = 0.0, .y = 0.0, .z = 0.0 },
|
||||
};
|
||||
}
|
||||
|
||||
fn readSampledFloat4(
|
||||
image: *SoftImage,
|
||||
image_view: *SoftImageView,
|
||||
sampler: *SoftSampler,
|
||||
dim: spv.SpvDim,
|
||||
coord: CubeCoordinate,
|
||||
ix: i32,
|
||||
iy: i32,
|
||||
iz: i32,
|
||||
) VkError!zm.F32x4 {
|
||||
const range = image_view.interface.subresource_range;
|
||||
const extent = image.getMipLevelExtent(range.base_mip_level);
|
||||
const width_f: f32 = @floatFromInt(extent.width);
|
||||
const height_f: f32 = @floatFromInt(extent.height);
|
||||
|
||||
const texel = if (dim == .Cube) blk: {
|
||||
const dir = cubeDirection(
|
||||
coord.face,
|
||||
(@as(f32, @floatFromInt(ix)) + 0.5) / width_f,
|
||||
(@as(f32, @floatFromInt(iy)) + 0.5) / height_f,
|
||||
);
|
||||
break :blk resolveCubeCoordinate(dir.x, dir.y, dir.z);
|
||||
} else coord;
|
||||
|
||||
const z: i32, const layer: u32 = switch (image_view.interface.view_type) {
|
||||
.@"1d_array" => .{ 0, range.base_array_layer + @as(u32, @intCast(sampleAddress(@intFromFloat(coord.v), viewLayerCount(image, range), .clamp_to_edge))) },
|
||||
.@"2d_array", .cube_array => .{ 0, range.base_array_layer + @as(u32, @intCast(sampleAddress(@intFromFloat(coord.w), viewLayerCount(image, range), .clamp_to_edge))) },
|
||||
.@"3d" => .{ sampleAddressOrBorder(iz, extent.depth, sampler.interface.address_mode_w) orelse return samplerBorderColor(sampler), range.base_array_layer },
|
||||
.cube => .{ 0, range.base_array_layer + texel.face },
|
||||
else => .{ 0, range.base_array_layer },
|
||||
};
|
||||
|
||||
const sx = if (dim == .Cube)
|
||||
std.math.clamp(@as(i32, @intFromFloat(texel.u * width_f)), 0, @as(i32, @intCast(extent.width)) - 1)
|
||||
else
|
||||
sampleAddressOrBorder(ix, extent.width, sampler.interface.address_mode_u) orelse return samplerBorderColor(sampler);
|
||||
const sy = if (dim == .Cube)
|
||||
std.math.clamp(@as(i32, @intFromFloat(texel.v * height_f)), 0, @as(i32, @intCast(extent.height)) - 1)
|
||||
else
|
||||
sampleAddressOrBorder(iy, extent.height, sampler.interface.address_mode_v) orelse return samplerBorderColor(sampler);
|
||||
|
||||
const result = try image.readFloat4(
|
||||
.{
|
||||
.x = sx,
|
||||
.y = sy,
|
||||
.z = z,
|
||||
},
|
||||
.{
|
||||
.aspect_mask = range.aspect_mask,
|
||||
.mip_level = range.base_mip_level,
|
||||
.array_layer = layer,
|
||||
},
|
||||
image_view.interface.format,
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
fn sampleAddress(coord: i32, extent: u32, mode: vk.SamplerAddressMode) i32 {
|
||||
return sampleAddressOrBorder(coord, extent, mode).?;
|
||||
}
|
||||
|
||||
fn sampleAddressOrBorder(coord: i32, extent: u32, mode: vk.SamplerAddressMode) ?i32 {
|
||||
const extent_i: i32 = @intCast(extent);
|
||||
return switch (mode) {
|
||||
.repeat => @mod(coord, extent_i),
|
||||
.mirrored_repeat => blk: {
|
||||
const period = extent_i * 2;
|
||||
const mirrored = @mod(coord, period);
|
||||
break :blk if (mirrored < extent_i) mirrored else period - mirrored - 1;
|
||||
},
|
||||
.clamp_to_border => if (coord < 0 or coord >= extent_i) null else coord,
|
||||
else => std.math.clamp(coord, 0, extent_i - 1),
|
||||
};
|
||||
}
|
||||
|
||||
fn samplerBorderColor(sampler: *SoftSampler) zm.F32x4 {
|
||||
return switch (sampler.interface.border_color) {
|
||||
.float_opaque_white, .int_opaque_white => .{ 1.0, 1.0, 1.0, 1.0 },
|
||||
.float_opaque_black, .int_opaque_black => .{ 0.0, 0.0, 0.0, 1.0 },
|
||||
else => .{ 0.0, 0.0, 0.0, 0.0 },
|
||||
};
|
||||
}
|
||||
|
||||
fn viewLayerCount(image: *SoftImage, range: vk.ImageSubresourceRange) u32 {
|
||||
return if (range.layer_count == vk.REMAINING_ARRAY_LAYERS)
|
||||
image.interface.array_layers - range.base_array_layer
|
||||
else
|
||||
range.layer_count;
|
||||
}
|
||||
|
||||
fn sampleNearestFloat4(image: *SoftImage, image_view: *SoftImageView, sampler: *SoftSampler, dim: spv.SpvDim, coord: CubeCoordinate) VkError!zm.F32x4 {
|
||||
const extent = image.getMipLevelExtent(image_view.interface.subresource_range.base_mip_level);
|
||||
const width_f: f32 = @floatFromInt(extent.width);
|
||||
const height_f: f32 = @floatFromInt(extent.height);
|
||||
return readSampledFloat4(
|
||||
image,
|
||||
image_view,
|
||||
sampler,
|
||||
dim,
|
||||
coord,
|
||||
@intFromFloat(coord.u * width_f),
|
||||
@intFromFloat(coord.v * height_f),
|
||||
@intFromFloat(coord.w * @as(f32, @floatFromInt(extent.depth))),
|
||||
);
|
||||
}
|
||||
|
||||
fn sampleLinearFloat4(image: *SoftImage, image_view: *SoftImageView, sampler: *SoftSampler, dim: spv.SpvDim, coord: CubeCoordinate) VkError!zm.F32x4 {
|
||||
const extent = image.getMipLevelExtent(image_view.interface.subresource_range.base_mip_level);
|
||||
const width_f: f32 = @floatFromInt(extent.width);
|
||||
const height_f: f32 = @floatFromInt(extent.height);
|
||||
const x = coord.u * width_f - 0.5;
|
||||
const y = coord.v * height_f - 0.5;
|
||||
const z = coord.w * @as(f32, @floatFromInt(extent.depth)) - 0.5;
|
||||
const x0: i32 = @intFromFloat(@floor(x));
|
||||
const y0: i32 = @intFromFloat(@floor(y));
|
||||
const z0: i32 = @intFromFloat(@floor(z));
|
||||
const x1 = x0 + 1;
|
||||
const y1 = y0 + 1;
|
||||
const z1 = z0 + 1;
|
||||
const wx = x - @as(f32, @floatFromInt(x0));
|
||||
const wy = y - @as(f32, @floatFromInt(y0));
|
||||
const wz = z - @as(f32, @floatFromInt(z0));
|
||||
|
||||
const p000 = try readSampledFloat4(image, image_view, sampler, dim, coord, x0, y0, z0);
|
||||
const p100 = try readSampledFloat4(image, image_view, sampler, dim, coord, x1, y0, z0);
|
||||
const p010 = try readSampledFloat4(image, image_view, sampler, dim, coord, x0, y1, z0);
|
||||
const p110 = try readSampledFloat4(image, image_view, sampler, dim, coord, x1, y1, z0);
|
||||
|
||||
const row00 = p000 * zm.f32x4s(1.0 - wx) + p100 * zm.f32x4s(wx);
|
||||
const row10 = p010 * zm.f32x4s(1.0 - wx) + p110 * zm.f32x4s(wx);
|
||||
const slice0 = row00 * zm.f32x4s(1.0 - wy) + row10 * zm.f32x4s(wy);
|
||||
|
||||
if (image_view.interface.view_type != .@"3d")
|
||||
return slice0;
|
||||
|
||||
const p001 = try readSampledFloat4(image, image_view, sampler, dim, coord, x0, y0, z1);
|
||||
const p101 = try readSampledFloat4(image, image_view, sampler, dim, coord, x1, y0, z1);
|
||||
const p011 = try readSampledFloat4(image, image_view, sampler, dim, coord, x0, y1, z1);
|
||||
const p111 = try readSampledFloat4(image, image_view, sampler, dim, coord, x1, y1, z1);
|
||||
|
||||
const row01 = p001 * zm.f32x4s(1.0 - wx) + p101 * zm.f32x4s(wx);
|
||||
const row11 = p011 * zm.f32x4s(1.0 - wx) + p111 * zm.f32x4s(wx);
|
||||
const slice1 = row01 * zm.f32x4s(1.0 - wy) + row11 * zm.f32x4s(wy);
|
||||
|
||||
return slice0 * zm.f32x4s(1.0 - wz) + slice1 * zm.f32x4s(wz);
|
||||
}
|
||||
|
||||
fn sampleImageFloat4(context: *anyopaque, context2: *anyopaque, dim: spv.SpvDim, x: f32, y: f32, z: f32) SpvRuntimeError!spv.Runtime.Vec4(f32) {
|
||||
var pixel = zm.f32x4s(0.0);
|
||||
|
||||
@@ -613,25 +391,7 @@ fn sampleImageFloat4(context: *anyopaque, context2: *anyopaque, dim: spv.SpvDim,
|
||||
const image: *SoftImage = @alignCast(@fieldParentPtr("interface", image_view.interface.image));
|
||||
|
||||
const sampler: *SoftSampler = @ptrCast(@alignCast(context2));
|
||||
|
||||
if (dim == .Cube) {
|
||||
const coord = resolveCubeCoordinate(x, y, z);
|
||||
pixel = switch (sampler.interface.mag_filter) {
|
||||
.linear => sampleLinearFloat4(image, image_view, sampler, dim, coord),
|
||||
else => sampleNearestFloat4(image, image_view, sampler, dim, coord),
|
||||
} catch return SpvRuntimeError.Unknown;
|
||||
} else {
|
||||
const coord: CubeCoordinate = .{
|
||||
.u = x,
|
||||
.v = y,
|
||||
.w = z,
|
||||
.face = 0,
|
||||
};
|
||||
pixel = switch (sampler.interface.mag_filter) {
|
||||
.linear => sampleLinearFloat4(image, image_view, sampler, dim, coord),
|
||||
else => sampleNearestFloat4(image, image_view, sampler, dim, coord),
|
||||
} catch return SpvRuntimeError.Unknown;
|
||||
}
|
||||
pixel = SoftSampler.sampleImageFloat4(image, image_view, sampler, dim, x, y, z) catch return SpvRuntimeError.Unknown;
|
||||
}
|
||||
|
||||
return .{
|
||||
|
||||
@@ -28,5 +28,6 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
|
||||
|
||||
pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
allocator.free(interface.queries);
|
||||
allocator.destroy(self);
|
||||
}
|
||||
|
||||
@@ -96,6 +96,7 @@ fn taskRunner(self: *Self, info: Interface.SubmitInfo, p_fence: ?*base.Fence, ru
|
||||
|
||||
var execution_device: ExecutionDevice = undefined;
|
||||
execution_device.setup(soft_device);
|
||||
defer execution_device.deinit(soft_device.device_allocator.allocator());
|
||||
|
||||
for (info.command_buffers.items) |command_buffer| {
|
||||
const soft_command_buffer: *SoftCommandBuffer = @alignCast(@fieldParentPtr("interface", command_buffer));
|
||||
|
||||
@@ -1,13 +1,34 @@
|
||||
const std = @import("std");
|
||||
const vk = @import("vulkan");
|
||||
const base = @import("base");
|
||||
const spv = @import("spv");
|
||||
const zm = base.zm;
|
||||
|
||||
const VkError = base.VkError;
|
||||
const Device = base.Device;
|
||||
const F32x4 = zm.F32x4;
|
||||
|
||||
const SoftImage = @import("SoftImage.zig");
|
||||
const SoftImageView = @import("SoftImageView.zig");
|
||||
|
||||
const Self = @This();
|
||||
pub const Interface = base.Sampler;
|
||||
|
||||
const CubeCoordinate = struct {
|
||||
face: u32,
|
||||
u: f32,
|
||||
v: f32,
|
||||
w: f32 = 0.0,
|
||||
};
|
||||
|
||||
const ImageSamplingContext = struct {
|
||||
image: *SoftImage,
|
||||
image_view: *SoftImageView,
|
||||
sampler: *Self,
|
||||
dim: spv.SpvDim,
|
||||
coord: CubeCoordinate,
|
||||
};
|
||||
|
||||
interface: Interface,
|
||||
|
||||
pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const vk.SamplerCreateInfo) VkError!*Self {
|
||||
@@ -30,3 +51,286 @@ pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
allocator.destroy(self);
|
||||
}
|
||||
|
||||
fn resolveCubeCoordinate(x: f32, y: f32, z: f32) CubeCoordinate {
|
||||
const ax = @abs(x);
|
||||
const ay = @abs(y);
|
||||
const az = @abs(z);
|
||||
|
||||
var face: u32 = 0;
|
||||
var sc: f32 = 0.0;
|
||||
var tc: f32 = 0.0;
|
||||
var ma: f32 = 1.0;
|
||||
|
||||
if (ax >= ay and ax >= az) {
|
||||
ma = ax;
|
||||
if (x >= 0.0) {
|
||||
face = 0;
|
||||
sc = -z;
|
||||
tc = -y;
|
||||
} else {
|
||||
face = 1;
|
||||
sc = z;
|
||||
tc = -y;
|
||||
}
|
||||
} else if (ay >= ax and ay >= az) {
|
||||
ma = ay;
|
||||
if (y >= 0.0) {
|
||||
face = 2;
|
||||
sc = x;
|
||||
tc = z;
|
||||
} else {
|
||||
face = 3;
|
||||
sc = x;
|
||||
tc = -z;
|
||||
}
|
||||
} else {
|
||||
ma = az;
|
||||
if (z >= 0.0) {
|
||||
face = 4;
|
||||
sc = x;
|
||||
tc = -y;
|
||||
} else {
|
||||
face = 5;
|
||||
sc = -x;
|
||||
tc = -y;
|
||||
}
|
||||
}
|
||||
|
||||
const inv_ma = if (ma == 0.0) 0.0 else 1.0 / ma;
|
||||
return .{
|
||||
.face = face,
|
||||
.u = (sc * inv_ma + 1.0) * 0.5,
|
||||
.v = (tc * inv_ma + 1.0) * 0.5,
|
||||
};
|
||||
}
|
||||
|
||||
fn cubeDirection(face: u32, u: f32, v: f32) struct { x: f32, y: f32, z: f32 } {
|
||||
const sc = u * 2.0 - 1.0;
|
||||
const tc = v * 2.0 - 1.0;
|
||||
|
||||
return switch (face) {
|
||||
0 => .{ .x = 1.0, .y = -tc, .z = -sc },
|
||||
1 => .{ .x = -1.0, .y = -tc, .z = sc },
|
||||
2 => .{ .x = sc, .y = 1.0, .z = tc },
|
||||
3 => .{ .x = sc, .y = -1.0, .z = -tc },
|
||||
4 => .{ .x = sc, .y = -tc, .z = 1.0 },
|
||||
5 => .{ .x = -sc, .y = -tc, .z = -1.0 },
|
||||
else => .{ .x = 0.0, .y = 0.0, .z = 0.0 },
|
||||
};
|
||||
}
|
||||
|
||||
inline fn sampleAddress(coord: i32, extent: u32, mode: vk.SamplerAddressMode) i32 {
|
||||
return sampleAddressOrBorder(coord, extent, mode).?;
|
||||
}
|
||||
|
||||
fn sampleAddressOrBorder(coord: i32, extent: u32, mode: vk.SamplerAddressMode) ?i32 {
|
||||
const extent_i: i32 = @intCast(extent);
|
||||
return switch (mode) {
|
||||
.repeat => @mod(coord, extent_i),
|
||||
.mirrored_repeat => blk: {
|
||||
const period = extent_i * 2;
|
||||
const mirrored = @mod(coord, period);
|
||||
break :blk if (mirrored < extent_i) mirrored else period - mirrored - 1;
|
||||
},
|
||||
.clamp_to_border => if (coord < 0 or coord >= extent_i) null else coord,
|
||||
else => std.math.clamp(coord, 0, extent_i - 1),
|
||||
};
|
||||
}
|
||||
|
||||
fn samplerBorderColor(sampler: *Self) F32x4 {
|
||||
return switch (sampler.interface.border_color) {
|
||||
.float_opaque_white, .int_opaque_white => .{ 1.0, 1.0, 1.0, 1.0 },
|
||||
.float_opaque_black, .int_opaque_black => .{ 0.0, 0.0, 0.0, 1.0 },
|
||||
else => .{ 0.0, 0.0, 0.0, 0.0 },
|
||||
};
|
||||
}
|
||||
|
||||
fn viewLayerCount(image: *SoftImage, range: vk.ImageSubresourceRange) u32 {
|
||||
return if (range.layer_count == vk.REMAINING_ARRAY_LAYERS)
|
||||
image.interface.array_layers - range.base_array_layer
|
||||
else
|
||||
range.layer_count;
|
||||
}
|
||||
|
||||
fn sampleArrayLayer(coord: f32, layer_count: u32) u32 {
|
||||
const layer_coord: i32 = @intFromFloat(@floor(coord + 0.5));
|
||||
return @intCast(sampleAddress(layer_coord, layer_count, .clamp_to_edge));
|
||||
}
|
||||
|
||||
fn readSampledFloat4(
|
||||
image: *SoftImage,
|
||||
image_view: *SoftImageView,
|
||||
sampler: *Self,
|
||||
dim: spv.SpvDim,
|
||||
coord: CubeCoordinate,
|
||||
ix: i32,
|
||||
iy: i32,
|
||||
iz: i32,
|
||||
) VkError!F32x4 {
|
||||
const range = image_view.interface.subresource_range;
|
||||
const extent = image.getMipLevelExtent(range.base_mip_level);
|
||||
const width_f: f32 = @floatFromInt(extent.width);
|
||||
const height_f: f32 = @floatFromInt(extent.height);
|
||||
|
||||
const texel = if (dim == .Cube) blk: {
|
||||
const dir = cubeDirection(
|
||||
coord.face,
|
||||
(@as(f32, @floatFromInt(ix)) + 0.5) / width_f,
|
||||
(@as(f32, @floatFromInt(iy)) + 0.5) / height_f,
|
||||
);
|
||||
break :blk resolveCubeCoordinate(dir.x, dir.y, dir.z);
|
||||
} else coord;
|
||||
|
||||
const z: i32, const layer: u32 = switch (image_view.interface.view_type) {
|
||||
.@"1d_array" => .{ 0, range.base_array_layer + sampleArrayLayer(coord.v, viewLayerCount(image, range)) },
|
||||
.@"2d_array" => .{ 0, range.base_array_layer + sampleArrayLayer(coord.w, viewLayerCount(image, range)) },
|
||||
.cube_array => .{ 0, range.base_array_layer + sampleArrayLayer(coord.w, @divTrunc(viewLayerCount(image, range), 6)) * 6 + texel.face },
|
||||
.@"3d" => .{ sampleAddressOrBorder(iz, extent.depth, sampler.interface.address_mode_w) orelse return samplerBorderColor(sampler), range.base_array_layer },
|
||||
.cube => .{ 0, range.base_array_layer + texel.face },
|
||||
else => .{ 0, range.base_array_layer },
|
||||
};
|
||||
|
||||
const sx = if (dim == .Cube)
|
||||
std.math.clamp(@as(i32, @intFromFloat(texel.u * width_f)), 0, @as(i32, @intCast(extent.width)) - 1)
|
||||
else
|
||||
sampleAddressOrBorder(ix, extent.width, sampler.interface.address_mode_u) orelse return samplerBorderColor(sampler);
|
||||
const sy = if (dim == .Cube)
|
||||
std.math.clamp(@as(i32, @intFromFloat(texel.v * height_f)), 0, @as(i32, @intCast(extent.height)) - 1)
|
||||
else
|
||||
sampleAddressOrBorder(iy, extent.height, sampler.interface.address_mode_v) orelse return samplerBorderColor(sampler);
|
||||
|
||||
return image.readFloat4(
|
||||
.{
|
||||
.x = sx,
|
||||
.y = sy,
|
||||
.z = z,
|
||||
},
|
||||
.{
|
||||
.aspect_mask = range.aspect_mask,
|
||||
.mip_level = range.base_mip_level,
|
||||
.array_layer = layer,
|
||||
},
|
||||
image_view.interface.format,
|
||||
);
|
||||
}
|
||||
|
||||
fn readSampledFloat4At(context: *const ImageSamplingContext, ix: i32, iy: i32, iz: i32) VkError!F32x4 {
|
||||
return readSampledFloat4(
|
||||
context.image,
|
||||
context.image_view,
|
||||
context.sampler,
|
||||
context.dim,
|
||||
context.coord,
|
||||
ix,
|
||||
iy,
|
||||
iz,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn sampleImageFloat4(image: *SoftImage, image_view: *SoftImageView, sampler: *Self, dim: spv.SpvDim, x: f32, y: f32, z: f32) VkError!F32x4 {
|
||||
const extent = image.getMipLevelExtent(image_view.interface.subresource_range.base_mip_level);
|
||||
const coord: CubeCoordinate = switch (image_view.interface.view_type) {
|
||||
.@"1d_array" => .{
|
||||
.u = x,
|
||||
.v = y,
|
||||
.face = 0,
|
||||
},
|
||||
.@"1d" => .{
|
||||
.u = x,
|
||||
.v = 0.0,
|
||||
.face = 0,
|
||||
},
|
||||
.@"2d_array" => .{
|
||||
.u = x,
|
||||
.v = y,
|
||||
.w = z,
|
||||
.face = 0,
|
||||
},
|
||||
.cube, .cube_array => resolveCubeCoordinate(x, y, z),
|
||||
else => .{
|
||||
.u = x,
|
||||
.v = y,
|
||||
.w = z,
|
||||
.face = 0,
|
||||
},
|
||||
};
|
||||
const context: ImageSamplingContext = .{
|
||||
.image = image,
|
||||
.image_view = image_view,
|
||||
.sampler = sampler,
|
||||
.dim = dim,
|
||||
.coord = coord,
|
||||
};
|
||||
|
||||
return sampleFloat4(
|
||||
*const ImageSamplingContext,
|
||||
&context,
|
||||
zm.f32x4(
|
||||
coord.u * @as(f32, @floatFromInt(extent.width)),
|
||||
coord.v * @as(f32, @floatFromInt(extent.height)),
|
||||
coord.w * @as(f32, @floatFromInt(extent.depth)),
|
||||
0.0,
|
||||
),
|
||||
switch (sampler.interface.mag_filter) {
|
||||
.linear => .linear,
|
||||
else => .nearest,
|
||||
},
|
||||
image_view.interface.view_type == .@"3d",
|
||||
readSampledFloat4At,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn sampleFloat4(
|
||||
comptime Context: type,
|
||||
context: Context,
|
||||
pos: F32x4,
|
||||
filter: vk.Filter,
|
||||
filter_3D: bool,
|
||||
comptime read: fn (Context, i32, i32, i32) VkError!F32x4,
|
||||
) VkError!F32x4 {
|
||||
if (filter == .nearest) {
|
||||
return read(
|
||||
context,
|
||||
@intFromFloat(pos[0]),
|
||||
@intFromFloat(pos[1]),
|
||||
@intFromFloat(pos[2]),
|
||||
);
|
||||
}
|
||||
|
||||
const x = pos[0] - 0.5;
|
||||
const y = pos[1] - 0.5;
|
||||
const z = pos[2] - 0.5;
|
||||
const x0: i32 = @intFromFloat(@floor(x));
|
||||
const y0: i32 = @intFromFloat(@floor(y));
|
||||
const z0: i32 = @intFromFloat(@floor(z));
|
||||
const x1 = x0 + 1;
|
||||
const y1 = y0 + 1;
|
||||
const z1 = z0 + 1;
|
||||
const wx = x - @as(f32, @floatFromInt(x0));
|
||||
const wy = y - @as(f32, @floatFromInt(y0));
|
||||
const wz = z - @as(f32, @floatFromInt(z0));
|
||||
|
||||
const p000 = try read(context, x0, y0, z0);
|
||||
const p100 = try read(context, x1, y0, z0);
|
||||
const p010 = try read(context, x0, y1, z0);
|
||||
const p110 = try read(context, x1, y1, z0);
|
||||
|
||||
const row00 = p000 * zm.f32x4s(1.0 - wx) + p100 * zm.f32x4s(wx);
|
||||
const row10 = p010 * zm.f32x4s(1.0 - wx) + p110 * zm.f32x4s(wx);
|
||||
const slice0 = row00 * zm.f32x4s(1.0 - wy) + row10 * zm.f32x4s(wy);
|
||||
|
||||
if (!filter_3D)
|
||||
return slice0;
|
||||
|
||||
const p001 = try read(context, x0, y0, z1);
|
||||
const p101 = try read(context, x1, y0, z1);
|
||||
const p011 = try read(context, x0, y1, z1);
|
||||
const p111 = try read(context, x1, y1, z1);
|
||||
|
||||
const row01 = p001 * zm.f32x4s(1.0 - wx) + p101 * zm.f32x4s(wx);
|
||||
const row11 = p011 * zm.f32x4s(1.0 - wx) + p111 * zm.f32x4s(wx);
|
||||
const slice1 = row01 * zm.f32x4s(1.0 - wy) + row11 * zm.f32x4s(wy);
|
||||
|
||||
return slice0 * zm.f32x4s(1.0 - wz) + slice1 * zm.f32x4s(wz);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,11 @@ pub const GRAPHICS_PIPELINE_STATE = 0;
|
||||
pub const COMPUTE_PIPELINE_STATE = 1;
|
||||
pub const MAX_DYNAMIC_DESCRIPTORS_PER_SET = 64;
|
||||
|
||||
pub const ActiveOcclusionQuery = struct {
|
||||
pool: *base.QueryPool,
|
||||
query: u32,
|
||||
};
|
||||
|
||||
pub const PipelineState = struct {
|
||||
pipeline: ?*SoftPipeline,
|
||||
sets: [base.VULKAN_MAX_DESCRIPTOR_SETS]?*SoftDescriptorSet,
|
||||
@@ -39,6 +44,7 @@ compute: ComputeDispatcher,
|
||||
renderer: Renderer,
|
||||
|
||||
pipeline_states: [2]PipelineState,
|
||||
active_occlusion_queries: std.ArrayList(ActiveOcclusionQuery),
|
||||
|
||||
/// Initializating an execution device and
|
||||
/// not creating one to avoid dangling pointers
|
||||
@@ -61,8 +67,13 @@ pub fn setup(self: *Self, device: *SoftDevice) void {
|
||||
},
|
||||
};
|
||||
}
|
||||
self.active_occlusion_queries = .empty;
|
||||
self.compute = .init(device, &self.pipeline_states[@intFromEnum(vk.PipelineBindPoint.compute)]);
|
||||
self.renderer = .init(device, &self.pipeline_states[@intFromEnum(vk.PipelineBindPoint.graphics)]);
|
||||
self.renderer = .init(device, &self.pipeline_states[@intFromEnum(vk.PipelineBindPoint.graphics)], &self.active_occlusion_queries);
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
|
||||
self.active_occlusion_queries.deinit(allocator);
|
||||
}
|
||||
|
||||
pub fn writeDescriptorSets(state: *PipelineState, rt: *spv.Runtime) !void {
|
||||
|
||||
@@ -74,7 +74,6 @@ pub const DrawCall = struct {
|
||||
render_pass: *SoftRenderPass,
|
||||
framebuffer: *SoftFramebuffer,
|
||||
|
||||
allocator_mutex: std.Io.Mutex,
|
||||
rasterizer_wait_group: std.Io.Group,
|
||||
|
||||
stats: struct {
|
||||
@@ -94,7 +93,6 @@ pub const DrawCall = struct {
|
||||
.depth_attachment = if (render_pass.interface.subpasses[renderer.subpass_index].depth_stencil_attachments) |desc| framebuffer.interface.attachments[desc.attachment] else null,
|
||||
.render_pass = render_pass,
|
||||
.framebuffer = framebuffer,
|
||||
.allocator_mutex = .init,
|
||||
.rasterizer_wait_group = .init,
|
||||
.stats = .{
|
||||
.polygons_drawn = 0,
|
||||
@@ -132,8 +130,9 @@ framebuffer: ?*SoftFramebuffer,
|
||||
dynamic_state: DynamicState,
|
||||
|
||||
subpass_index: usize,
|
||||
active_occlusion_queries: *std.ArrayList(ExecutionDevice.ActiveOcclusionQuery),
|
||||
|
||||
pub fn init(device: *SoftDevice, state: *PipelineState) Self {
|
||||
pub fn init(device: *SoftDevice, state: *PipelineState, active_occlusion_queries: *std.ArrayList(ExecutionDevice.ActiveOcclusionQuery)) Self {
|
||||
return .{
|
||||
.device = device,
|
||||
.state = state,
|
||||
@@ -152,6 +151,7 @@ pub fn init(device: *SoftDevice, state: *PipelineState) Self {
|
||||
.stencil_back_reference = null,
|
||||
},
|
||||
.subpass_index = 0,
|
||||
.active_occlusion_queries = active_occlusion_queries,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -574,7 +574,12 @@ pub fn readFloat4(map: []const u8, src_format: vk.Format) F32x4 {
|
||||
=> c[0] = normalizedI8(map[0]),
|
||||
|
||||
.r16_snorm => c[0] = normalizedI16(std.mem.bytesToValue(u16, map)),
|
||||
.r16_unorm => c[0] = @as(f32, @floatFromInt(std.mem.bytesToValue(u16, map))) / std.math.maxInt(u16),
|
||||
.r16_unorm,
|
||||
.d16_unorm,
|
||||
=> c[0] = @as(f32, @floatFromInt(std.mem.bytesToValue(u16, map))) / std.math.maxInt(u16),
|
||||
.x8_d24_unorm_pack32,
|
||||
.d24_unorm_s8_uint,
|
||||
=> c[0] = @as(f32, @floatFromInt(std.mem.bytesToValue(u32, map) & 0x00ff_ffff)) / @as(f32, @floatFromInt(0x00ff_ffff)),
|
||||
|
||||
.r8g8b8a8_sint,
|
||||
.r8g8b8a8_uint,
|
||||
@@ -856,6 +861,14 @@ pub fn writeFloat4(c: F32x4, map: []u8, dst_format: vk.Format) void {
|
||||
.d16_unorm,
|
||||
=> std.mem.bytesAsValue(u16, map).* = @intFromFloat(@round(color[0] * std.math.maxInt(u16))),
|
||||
|
||||
.x8_d24_unorm_pack32,
|
||||
.d24_unorm_s8_uint,
|
||||
=> {
|
||||
const depth: u32 = @intFromFloat(@round(color[0] * @as(f32, @floatFromInt(0x00ff_ffff))));
|
||||
const preserved: u32 = std.mem.bytesToValue(u32, map) & 0xff00_0000;
|
||||
std.mem.bytesAsValue(u32, map).* = preserved | depth;
|
||||
},
|
||||
|
||||
.r16_sfloat => std.mem.bytesAsValue(f16, map).* = @floatCast(color[0]),
|
||||
|
||||
.r32_sint,
|
||||
|
||||
@@ -72,9 +72,12 @@ pub fn shaderInvocation(
|
||||
|
||||
rt.callEntryPoint(allocator, entry) catch |err| switch (err) {
|
||||
// Some errors can be safely ignored
|
||||
SpvRuntimeError.OutOfBounds,
|
||||
SpvRuntimeError.Killed,
|
||||
=> {},
|
||||
SpvRuntimeError.OutOfBounds => {},
|
||||
SpvRuntimeError.Killed => {
|
||||
try rt.flushDescriptorSets(allocator);
|
||||
freeOwnedInputs(allocator, fragment_inputs);
|
||||
return undefined; // FIXME
|
||||
},
|
||||
else => return err,
|
||||
};
|
||||
|
||||
|
||||
@@ -173,6 +173,16 @@ inline fn run(data: RunData) !void {
|
||||
};
|
||||
}
|
||||
|
||||
try common.writeToTargets(outputs, data.draw_call, data.color_attachment_access, data.depth_attachment_access, data.stencil_attachment_access, true, @intCast(pixel_x), @intCast(pixel_y), z);
|
||||
try common.writeToTargets(
|
||||
outputs,
|
||||
data.draw_call,
|
||||
data.color_attachment_access,
|
||||
data.depth_attachment_access,
|
||||
data.stencil_attachment_access,
|
||||
true,
|
||||
@intCast(pixel_x),
|
||||
@intCast(pixel_y),
|
||||
z,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,13 +98,7 @@ fn updateStencilValue(stencil: *RenderTargetAccess, offset: usize, state: vk.Ste
|
||||
blitter.writeInt4(@splat(new_value), stencil.base[offset..], stencil.format);
|
||||
}
|
||||
|
||||
pub fn stencilTestAndUpdate(
|
||||
stencil: *RenderTargetAccess,
|
||||
x: usize,
|
||||
y: usize,
|
||||
state: vk.StencilOpState,
|
||||
depth_passed: ?bool,
|
||||
) bool {
|
||||
pub fn stencilTestAndUpdate(stencil: *RenderTargetAccess, x: usize, y: usize, state: vk.StencilOpState, depth_passed: ?bool) bool {
|
||||
const offset = targetOffset(stencil.*, x, y) orelse return false;
|
||||
const current = blitter.readInt4(stencil.base[offset..], stencil.format)[0] & std.math.maxInt(u8);
|
||||
const reference = state.reference & std.math.maxInt(u8);
|
||||
@@ -132,15 +126,27 @@ fn stencilTest(stencil: *RenderTargetAccess, offset: usize, state: vk.StencilOpS
|
||||
return compare(u32, state.compare_op, reference & compare_mask, current & compare_mask);
|
||||
}
|
||||
|
||||
fn quantizeDepthForFormat(format: vk.Format, z: f32) f32 {
|
||||
const clamped = std.math.clamp(z, 0.0, 1.0);
|
||||
return switch (format) {
|
||||
.d16_unorm => @as(f32, @floatFromInt(@as(u16, @intFromFloat(@round(clamped * std.math.maxInt(u16)))))) / std.math.maxInt(u16),
|
||||
.x8_d24_unorm_pack32,
|
||||
.d24_unorm_s8_uint,
|
||||
=> @as(f32, @floatFromInt(@as(u32, @intFromFloat(@round(clamped * @as(f32, @floatFromInt(0x00ff_ffff))))))) / @as(f32, @floatFromInt(0x00ff_ffff)),
|
||||
else => z,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn depthTestAndUpdate(depth: *RenderTargetAccess, x: usize, y: usize, z: f32, state: vk.PipelineDepthStencilStateCreateInfo) bool {
|
||||
if (state.depth_test_enable == .false)
|
||||
return true;
|
||||
|
||||
const offset = targetOffset(depth.*, x, y) orelse return false;
|
||||
const reference = quantizeDepthForFormat(depth.format, z);
|
||||
const depth_value = blitter.readFloat4(depth.base[offset..], depth.format);
|
||||
const passed = compare(f32, state.depth_compare_op, z, depth_value[0]);
|
||||
const passed = compare(f32, state.depth_compare_op, reference, depth_value[0]);
|
||||
if (passed and state.depth_write_enable == .true)
|
||||
blitter.writeFloat4(zm.f32x4s(z), depth.base[offset..], depth.format);
|
||||
blitter.writeFloat4(zm.f32x4s(reference), depth.base[offset..], depth.format);
|
||||
return passed;
|
||||
}
|
||||
|
||||
@@ -312,6 +318,9 @@ pub fn writeToTargets(
|
||||
const io = draw_call.renderer.device.interface.io();
|
||||
const depth_stencil_state = draw_call.renderer.state.pipeline.?.interface.mode.graphics.depth_stencil;
|
||||
|
||||
if (x >= draw_call.framebuffer.interface.width or y >= draw_call.framebuffer.interface.height)
|
||||
return;
|
||||
|
||||
var stencil_state: ?vk.StencilOpState = null;
|
||||
var stencil_offset: ?usize = null;
|
||||
if (stencil_attachment_access) |stencil| {
|
||||
@@ -361,6 +370,10 @@ pub fn writeToTargets(
|
||||
}
|
||||
}
|
||||
|
||||
for (draw_call.renderer.active_occlusion_queries.items) |active| {
|
||||
try active.pool.addSamples(active.query, 1);
|
||||
}
|
||||
|
||||
for (color_attachment_access, 0..) |maybe_color, location| {
|
||||
const color = maybe_color orelse continue;
|
||||
const color_offset = targetOffset(color, x, y) orelse continue;
|
||||
|
||||
@@ -191,7 +191,17 @@ inline fn run(data: RunData) !void {
|
||||
};
|
||||
}
|
||||
|
||||
try common.writeToTargets(outputs, data.draw_call, data.color_attachment_access, data.depth_attachment_access, data.stencil_attachment_access, data.front_face, @intCast(x), @intCast(y), z);
|
||||
try common.writeToTargets(
|
||||
outputs,
|
||||
data.draw_call,
|
||||
data.color_attachment_access,
|
||||
data.depth_attachment_access,
|
||||
data.stencil_attachment_access,
|
||||
data.front_face,
|
||||
@intCast(x),
|
||||
@intCast(y),
|
||||
z,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,16 +39,18 @@ pub fn runWrapper(data: RunData) void {
|
||||
|
||||
inline fn run(data: RunData) !void {
|
||||
const shader = data.pipeline.stages.getPtrAssertContains(.vertex);
|
||||
const rt = &shader.runtimes[data.batch_id].rt;
|
||||
const runtime = &shader.runtimes[data.batch_id];
|
||||
const mutex = &runtime.mutex;
|
||||
const rt = &runtime.rt;
|
||||
|
||||
const io = data.draw_call.renderer.device.interface.io();
|
||||
mutex.lock(io) catch return VkError.DeviceLost;
|
||||
defer mutex.unlock(io);
|
||||
|
||||
const entry = try rt.getEntryPointByName(shader.entry);
|
||||
|
||||
var invocation_index: usize = data.batch_id;
|
||||
while (invocation_index < data.vertex_count) : (invocation_index += data.batch_size) {
|
||||
const io = data.draw_call.renderer.device.interface.io();
|
||||
data.draw_call.allocator_mutex.lock(io) catch return VkError.DeviceLost;
|
||||
defer data.draw_call.allocator_mutex.unlock(io);
|
||||
|
||||
rt.resetInvocation(data.allocator);
|
||||
try rt.populatePushConstants(data.draw_call.renderer.state.push_constant_blob[0..]);
|
||||
|
||||
@@ -78,9 +80,11 @@ inline fn run(data: RunData) !void {
|
||||
|
||||
rt.callEntryPoint(data.allocator, entry) catch |err| switch (err) {
|
||||
// Some errors can be safely ignored
|
||||
SpvRuntimeError.OutOfBounds,
|
||||
SpvRuntimeError.Killed,
|
||||
=> {},
|
||||
SpvRuntimeError.OutOfBounds => {},
|
||||
SpvRuntimeError.Killed => {
|
||||
try rt.flushDescriptorSets(data.allocator);
|
||||
return;
|
||||
},
|
||||
else => return err,
|
||||
};
|
||||
|
||||
@@ -95,7 +99,7 @@ inline fn run(data: RunData) !void {
|
||||
};
|
||||
const memory_size = try rt.getResultMemorySize(result_word);
|
||||
output.outputs[location][component] = .{
|
||||
.interpolation_type = if (rt.hasResultDecoration(result_word, .Flat) or resultIsInteger(rt, result_word)) .flat else .smooth, // TODO : handle noperspective
|
||||
.interpolation_type = if (rt.hasResultDecoration(result_word, .Flat) or rt.resultIsInteger(result_word)) .flat else .smooth, // TODO : handle noperspective
|
||||
.blob = data.allocator.alloc(u8, memory_size + INTERFACE_BLOB_PADDING) catch return VkError.OutOfDeviceMemory,
|
||||
.size = memory_size,
|
||||
};
|
||||
@@ -116,32 +120,7 @@ fn setupBuiltins(rt: *spv.Runtime, vertex_index: usize, instance_index: usize) !
|
||||
try rt.writeBuiltIn(std.mem.asBytes(&instance_index_u32), .InstanceIndex);
|
||||
}
|
||||
|
||||
fn resultIsInteger(rt: *spv.Runtime, result_word: spv.SpvWord) bool {
|
||||
const value = rt.results[result_word].getConstValue() catch return false;
|
||||
return switch (value.*) {
|
||||
.Int,
|
||||
.Vector2i32,
|
||||
.Vector3i32,
|
||||
.Vector4i32,
|
||||
.Vector2u32,
|
||||
.Vector3u32,
|
||||
.Vector4u32,
|
||||
=> true,
|
||||
.Vector => |lanes| lanes.len != 0 and switch (lanes[0]) {
|
||||
.Int => true,
|
||||
else => false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
fn writeVertexInput(
|
||||
rt: *spv.Runtime,
|
||||
allocator: std.mem.Allocator,
|
||||
raw_input: []const u8,
|
||||
format: vk.Format,
|
||||
location: u32,
|
||||
) !void {
|
||||
fn writeVertexInput(rt: *spv.Runtime, allocator: std.mem.Allocator, raw_input: []const u8, format: vk.Format, location: u32) !void {
|
||||
var has_split_components = false;
|
||||
for (1..4) |component| {
|
||||
_ = rt.getResultByLocationComponent(location, @intCast(component), .input) catch |err| switch (err) {
|
||||
|
||||
Reference in New Issue
Block a user