Compare commits

..

10 Commits

29 changed files with 1771 additions and 535 deletions

1
.gitattributes vendored
View File

@@ -1,2 +1,3 @@
*.hdr filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.ttf filter=lfs diff=lfs merge=lfs -text

View File

@@ -1,22 +1,28 @@
{
"walls": {
"positive_x": {
"material": "OakLogTop.json"
"material": "OakLogTop.json",
"transform": "identity"
},
"negative_x": {
"material": "OakLogTop.json"
"material": "OakLogTop.json",
"transform": "mirror"
},
"positive_y": {
"material": "OakLog.json"
"material": "OakLog.json",
"transform": "rotate_ccw"
},
"negative_y": {
"material": "OakLog.json"
"material": "OakLog.json",
"transform": "rotate_cw"
},
"positive_z": {
"material": "OakLog.json"
"material": "OakLog.json",
"transform": "rotate_cw"
},
"negative_z": {
"material": "OakLog.json"
"material": "OakLog.json",
"transform": "rotate_cw"
}
}
}

View File

@@ -1,22 +1,28 @@
{
"walls": {
"positive_x": {
"material": "OakLog.json"
"material": "OakLog.json",
"transform": "rotate_cw"
},
"negative_x": {
"material": "OakLog.json"
"material": "OakLog.json",
"transform": "rotate_ccw"
},
"positive_y": {
"material": "OakLogTop.json"
"material": "OakLogTop.json",
"transform": "identity"
},
"negative_y": {
"material": "OakLogTop.json"
"material": "OakLogTop.json",
"transform": "mirror"
},
"positive_z": {
"material": "OakLog.json"
"material": "OakLog.json",
"transform": "identity"
},
"negative_z": {
"material": "OakLog.json"
"material": "OakLog.json",
"transform": "rotate_180"
}
}
}

View File

@@ -1,22 +1,28 @@
{
"walls": {
"positive_x": {
"material": "OakLog.json"
"material": "OakLog.json",
"transform": "identity"
},
"negative_x": {
"material": "OakLog.json"
"material": "OakLog.json",
"transform": "identity"
},
"positive_y": {
"material": "OakLog.json"
"material": "OakLog.json",
"transform": "identity"
},
"negative_y": {
"material": "OakLog.json"
"material": "OakLog.json",
"transform": "identity"
},
"positive_z": {
"material": "OakLogTop.json"
"material": "OakLogTop.json",
"transform": "identity"
},
"negative_z": {
"material": "OakLogTop.json"
"material": "OakLogTop.json",
"transform": "mirror_rotate_180"
}
}
}

BIN
assets/fonts/EBGaramond-Regular.ttf (Stored with Git LFS) Normal file

Binary file not shown.

BIN
assets/fonts/FiraGO-Regular.ttf (Stored with Git LFS) Normal file

Binary file not shown.

BIN
assets/fonts/FiraMono-Regular.ttf (Stored with Git LFS) Normal file

Binary file not shown.

View File

@@ -1,3 +1,4 @@
import "includes/math";
import "includes/structs";
struct Varyings {
@@ -48,6 +49,6 @@ Varyings vertexMain(uint vertexID : SV_VulkanVertexID, uint instanceID : SV_Vulk
[shader("fragment")]
float4 fragmentMain(Varyings frag) : SV_Target {
let image = _Images[frag.instance];
let texel = _Textures[image.texture].Sample(_Sampler, frag.texCoord);
let texel = texture2DAA(_Textures[image.texture], _Sampler, frag.texCoord);
return texel * image.tint;
}

View File

@@ -3,15 +3,17 @@ import "includes/structs";
struct Varyings {
float4 positionCS : SV_Position;
nointerpolation uint instance;
float2 texCoordEM : U_TEXCOORD;
float2 texCoordEM;
}
struct GuiText {
float4 color;
// Index into _GlyphData
uint glyph;
float2 bottomLeftSSPX;
float fontSizePX;
// Index into _GlyphData[font]
uint16_t glyph;
// Index into _GlyphData, _ControlPoints and _BandData
uint16_t font;
}
struct GlyphData {
@@ -28,10 +30,9 @@ struct GlyphData {
[[vk::binding(0, 0)]] ConstantBuffer<GlobalUniforms> _Global;
[[vk::binding(1, 0)]] StructuredBuffer<GuiText> _Instances;
[[vk::binding(0, 1)]] StructuredBuffer<GlyphData> _GlyphData;
[[vk::binding(1, 1)]] StructuredBuffer<half2> _ControlPoints;
[[vk::binding(2, 1)]] StructuredBuffer<uint> _BandData;
[[vk::binding(2, 0)]] StructuredBuffer<GlyphData> _GlyphData[];
[[vk::binding(3, 0)]] StructuredBuffer<half2> _ControlPoints[];
[[vk::binding(4, 0)]] StructuredBuffer<uint> _BandData[];
static const float2 VERTICES[4] = {
float2(0, 1),
@@ -70,7 +71,7 @@ uint classifyQuadCurve(float y0, float y1, float y2)
let class = (sign0 << 0) | (sign1 << 1) | (sign2 << 2);
let shift = 2 * class;
return (0x2E74U >> shift) & 0b11U;
return (0x1DB8 >> shift) & 0b11U;
}
// Find intersections between a quadratic Bézier curve and `y = 0` line by
@@ -156,12 +157,12 @@ Varyings vertexMain(uint vertexID : SV_VulkanVertexID, uint instanceID : SV_Vulk
let vertex = VERTICES[vertexID];
let instance = _Instances[instanceID];
let glyph = _GlyphData[instance.glyph];
let glyph = _GlyphData[instance.font][instance.glyph];
let normal = 2.0 * vertex - 1.0;
let sizePX = instance.fontSizePX * glyph.sizeEM;
let positionSSPX = vertex * sizePX + instance.bottomLeftSSPX;
let positionSSPX = vertex * float2(sizePX.x, -sizePX.y) + instance.bottomLeftSSPX;
let dilatedPositionSSPX = positionSSPX + 0.5 * normal;
let dilatedPositionCS = float4(mul(_Global.matrixSSPXtoCS, float3(positionSSPX, 1.0)), 0.0, 1.0);
@@ -177,7 +178,9 @@ Varyings vertexMain(uint vertexID : SV_VulkanVertexID, uint instanceID : SV_Vulk
[shader("fragment")]
float4 fragmentMain(Varyings frag) : SV_Target {
let instance = _Instances[frag.instance];
let glyph = _GlyphData[instance.glyph];
let glyph = _GlyphData[instance.font][instance.glyph];
let bandData = _BandData[instance.font];
let controlPoints = _ControlPoints[instance.font];
let emPerPX = fwidth(frag.texCoordEM);
let pxPerEM = 1.0 / emPerPX;
@@ -191,17 +194,17 @@ float4 fragmentMain(Varyings frag) : SV_Target {
var xWeight = 0.0;
let horizontalBandPtr = glyph.bandPtr.y + band.y;
let horizontalBandData = _BandData[horizontalBandPtr];
let horizontalBandData = bandData[horizontalBandPtr];
let horizontalBandCurvePtr = horizontalBandPtr + (horizontalBandData >> 16);
let horizontalBandCurveCount = horizontalBandData & 0xFFFF;
for (uint horizontalBandCurveID = 0; horizontalBandCurveID < horizontalBandCurveCount; horizontalBandCurveID++) {
let cpPtr = _BandData[horizontalBandCurvePtr + horizontalBandCurveID];
let cp0 = float2(_ControlPoints[cpPtr]) - frag.texCoordEM;
let cp1 = float2(_ControlPoints[cpPtr + 1]) - frag.texCoordEM;
let cp2 = float2(_ControlPoints[cpPtr + 2]) - frag.texCoordEM;
let cpPtr = bandData[horizontalBandCurvePtr + horizontalBandCurveID];
let cp0 = float2(controlPoints[cpPtr + 0]) - frag.texCoordEM;
let cp1 = float2(controlPoints[cpPtr + 1]) - frag.texCoordEM;
let cp2 = float2(controlPoints[cpPtr + 2]) - frag.texCoordEM;
if (max(max(cp0.x, cp1.x), cp2.x) * pxPerEM.x < -0.5) break;
if (max(max(cp0.x, cp1.x), cp2.x) * pxPerEM.x < -0.5) break; // XXX
let rootFlags = classifyQuadCurve(cp0.y, cp1.y, cp2.y);
if (rootFlags != 0U) {
@@ -228,17 +231,17 @@ float4 fragmentMain(Varyings frag) : SV_Target {
var yWeight = 0.0;
let verticalBandPtr = glyph.bandPtr.x + band.x;
let verticalBandData = _BandData[verticalBandPtr];
let verticalBandData = bandData[verticalBandPtr];
let verticalBandCurvePtr = verticalBandPtr + (verticalBandData >> 16);
let verticalBandCurveCount = verticalBandData & 0xFFFF;
for (uint verticalBandCurveID = 0; verticalBandCurveID < verticalBandCurveCount; verticalBandCurveID++) {
let cpPtr = _BandData[verticalBandCurvePtr + verticalBandCurveID];
let cp0 = float2(_ControlPoints[cpPtr]) - frag.texCoordEM;
let cp1 = float2(_ControlPoints[cpPtr + 1]) - frag.texCoordEM;
let cp2 = float2(_ControlPoints[cpPtr + 2]) - frag.texCoordEM;
let cpPtr = bandData[verticalBandCurvePtr + verticalBandCurveID];
let cp0 = float2(controlPoints[cpPtr + 0]) - frag.texCoordEM;
let cp1 = float2(controlPoints[cpPtr + 1]) - frag.texCoordEM;
let cp2 = float2(controlPoints[cpPtr + 2]) - frag.texCoordEM;
if (max(max(cp0.y, cp1.y), cp2.y) * pxPerEM.y < -0.5) break;
if (max(max(cp0.y, cp1.y), cp2.y) * pxPerEM.y < -0.5) break; // XXX
let rootFlags = classifyQuadCurve(cp0.x, cp1.x, cp2.x);
if (rootFlags != 0U) {

View File

@@ -1,5 +1,5 @@
struct GlobalUniforms {
float4x4 matrixWStoVS;
float3x4 matrixWStoVS;
float4x4 matrixVStoCS;
float2x3 matrixSSPXtoCS;
@@ -12,10 +12,10 @@ struct PointLight {
float3 positionWS;
float3 color;
IncomingLight getIncoming(float4x4 matrixWStoVS, float3 positionVS) {
IncomingLight getIncoming(float3x4 matrixWStoVS, float3 positionVS) {
IncomingLight light;
let lightPositionVS = mul(matrixWStoVS, float4(positionWS, 1.0)).xyz;
let lightPositionVS = mul(matrixWStoVS, float4(positionWS, 1.0));
let lightDirectionVS = normalize(lightPositionVS - positionVS);
let lightDistance = distance(positionVS, lightPositionVS);
let lightAttenuation = 1.0 / (lightDistance * lightDistance);
@@ -31,11 +31,11 @@ struct DirectionalLight {
float3 directionWS;
float3 color;
IncomingLight getIncoming(float4x4 matrixWStoVS) {
IncomingLight getIncoming(float3x4 matrixWStoVS) {
IncomingLight light;
light.radiance = color;
light.directionVS = normalize(mul(matrixWStoVS, float4(-directionWS, 0.0)).xyz);
light.directionVS = normalize(mul(matrixWStoVS, float4(-directionWS, 0.0)));
return light;
}
}
@@ -56,8 +56,9 @@ struct Material {
}
struct ObjectUniforms {
float4x4 matrixOStoWS;
float4x4 matrixOStoWSNormal;
// Limited to a composition of translation and rotation
float3x4 matrixOStoWS;
float2x3 texCoordTransform;
uint16_t material;
}

View File

@@ -33,22 +33,24 @@ Varyings vertexMain(Vertex vert, uint instanceID : SV_VulkanInstanceID) {
let object = _Objects[instanceID];
let positionWS = mul(object.matrixOStoWS, float4(vert.positionOS, 1.0)).xyz;
let positionVS = mul(_Global.matrixWStoVS, float4(positionWS, 1.0)).xyz;
let positionWS = mul(object.matrixOStoWS, float4(vert.positionOS, 1.0));
let positionVS = mul(_Global.matrixWStoVS, float4(positionWS, 1.0));
let positionCS = mul(_Global.matrixVStoCS, float4(positionVS, 1.0));
let normalWS = normalize(mul(object.matrixOStoWSNormal, float4(vert.normalOS, 0.0)).xyz);
let normalVS = normalize(mul(_Global.matrixWStoVS, float4(normalWS, 0.0)).xyz);
let texCoord = mul(object.texCoordTransform, float3(vert.texCoord, 1.0));
let tangentWS = normalize(mul(object.matrixOStoWSNormal, float4(vert.tangentOS.xyz, 0.0)).xyz);
let tangentVS = normalize(mul(_Global.matrixWStoVS, float4(tangentWS, 0.0)).xyz);
let normalWS = normalize(mul(object.matrixOStoWS, float4(vert.normalOS, 0.0)));
let normalVS = normalize(mul(_Global.matrixWStoVS, float4(normalWS, 0.0)));
let tangentWS = normalize(mul(object.matrixOStoWS, float4(vert.tangentOS.xyz, 0.0)));
let tangentVS = normalize(mul(_Global.matrixWStoVS, float4(tangentWS, 0.0)));
let bitangentVS = vert.tangentOS.w * normalize(cross(normalVS, tangentVS));
out.positionCS = positionCS;
out.instance = instanceID;
out.positionVS = positionVS;
out.texCoord = vert.texCoord;
out.texCoord = texCoord;
out.normalVS = normalVS;
out.tangentVS = tangentVS;
out.bitangentVS = bitangentVS;

View File

@@ -18,7 +18,7 @@ struct Varyings {
Varyings vertexMain(Vertex vert) {
Varyings out;
let directionVS = mul(_Global.matrixWStoVS, float4(vert.directionWS, 0.0)).xyz;
let directionVS = mul(_Global.matrixWStoVS, float4(vert.directionWS, 0.0));
let directionCS = mul(_Global.matrixVStoCS, float4(directionVS, 0.0));
out.positionCS = float4(directionCS.xy, 0.0, directionCS.w);

2
castle

Submodule castle updated: dc839e098c...99b8cf1d8e

View File

@@ -10,6 +10,7 @@ const media = @import("media");
const glfw = @import("zglfw");
const Atoms = @import("engine/Atoms.zig");
const Fonts = @import("engine/Fonts.zig");
const Engine = @import("engine/Engine.zig");
const Gui = @import("engine/Gui.zig");
const Materials = @import("engine/Materials.zig");
@@ -23,6 +24,7 @@ pub var allocator_persistent: std.mem.Allocator = undefined;
pub var io: std.Io = undefined;
pub var vk_allocator: *VkAllocator = undefined;
pub var stbi: *media.stbi = undefined;
pub var stbtt: *media.stbtt = undefined;
pub var window: *glfw.Window = undefined;
pub var atoms: *Atoms = undefined;
@@ -31,4 +33,5 @@ pub var swapchain: *Swapchain = undefined;
pub var textures: *Textures = undefined;
pub var materials: *Materials = undefined;
pub var fonts: *Fonts = undefined;
pub var gui: *Gui = undefined;

View File

@@ -1,6 +1,7 @@
const Chunk = @This();
const std = @import("std");
const c = @import("const.zig");
const ctx = @import("AppContext.zig");
const math = @import("math.zig");
const shaders = @import("shaders.zig");
@@ -15,9 +16,7 @@ const Game = @import("Game.zig");
const GenericBuffer = @import("engine/GenericBuffer.zig").GenericBuffer;
const Materials = @import("engine/Materials.zig");
const ObjectUniformsBuffer = GenericBuffer(void, shaders.ObjectUniforms);
pub const chunk_size = 16;
const chunk_size = c.vx_per_ck;
const initial_capacity = 256;
var next_chunk_id: std.atomic.Value(u64) = .init(0);
@@ -27,7 +26,7 @@ var next_chunk_id: std.atomic.Value(u64) = .init(0);
blocks: [chunk_size][chunk_size][chunk_size]Blocks.Id,
origin: vm.Vector3,
descriptor_set: vk.DescriptorSet,
object_buffer: ObjectUniformsBuffer,
object_buffer: shaders.ObjectUniformsBuffer,
object_count: u32,
/// For debug purposes, a unique "name" per chunk instance.
@@ -50,10 +49,10 @@ pub fn init(init_info: InitInfo) !Chunk {
});
errdefer engine.freeDescriptorSet(init_info.descriptor_pool, descriptor_set);
var object_buffer: ObjectUniformsBuffer = try .init(.{
var object_buffer: shaders.ObjectUniformsBuffer = try .init(.{
.usage = .storage,
.target_queue = .graphics,
.array_capacity = initial_capacity,
.capacity = initial_capacity,
});
errdefer object_buffer.deinit();
@@ -120,13 +119,18 @@ pub fn refresh(self: *Chunk, blocks: *const Blocks, neighbors: Neighbors) !void
inline for (@typeInfo(voxels.Orientation).@"enum".fields) |field| {
const side = @field(voxels.Orientation, field.name);
const material = @field(block.walls, field.name).material;
if (material != .empty and self.getOpposite(side, ix, iy, iz, blocks, neighbors) == .empty) {
const wall: Blocks.Definition.Wall = @field(block.walls, field.name);
if (wall.material != .empty and self.getOpposite(side, ix, iy, iz, blocks, neighbors) == .empty) {
const matrix = getMatrix(side, origin);
try uniforms.append(allocator_frame, .{
.matrixOStoWS = matrix,
.matrixOStoWSNormal = matrix,
.material = material,
.matrixOStoWS = .{
matrix.getIVersor().dropW(),
matrix.getJVersor().dropW(),
matrix.getKVersor().dropW(),
matrix.getTranslationVector().dropW(),
},
.texCoordTransform = wall.transform.getMatrix(),
.material = wall.material,
});
}
}
@@ -135,12 +139,12 @@ pub fn refresh(self: *Chunk, blocks: *const Blocks, neighbors: Neighbors) !void
}
const object_count: u32 = @intCast(uniforms.items.len);
if (self.object_buffer.array_capacity < object_count) {
if (self.object_buffer.capacity < object_count) {
const desired_capacity = std.math.ceilPowerOfTwoAssert(u32, object_count);
const new_object_buffer: ObjectUniformsBuffer = try .init(.{
const new_object_buffer: shaders.ObjectUniformsBuffer = try .init(.{
.usage = .storage,
.target_queue = .graphics,
.array_capacity = desired_capacity,
.capacity = desired_capacity,
});
try engine.updateDescriptorSets(.{
@@ -169,7 +173,7 @@ pub fn refresh(self: *Chunk, blocks: *const Blocks, neighbors: Neighbors) !void
engine.setObjectName(new_object_buffer.device_memory, "DM Chunk[{d}]", .{self.chunk_id});
}
try self.object_buffer.write(.{ .elements = uniforms.items });
try self.object_buffer.writeSlice(uniforms.items);
self.object_count = object_count;
}

View File

@@ -16,11 +16,11 @@ const Chunk = @import("Chunk.zig");
const Chunks = @import("Chunks.zig");
const CommandBuffer = @import("engine/CommandBuffer.zig");
const Engine = @import("engine/Engine.zig");
const Fonts = @import("engine/Fonts.zig");
const Iterator2 = math.Iterator2;
const Materials = @import("engine/Materials.zig");
const Player = @import("Player.zig");
const Skybox = @import("engine/Skybox.zig");
const StagingBuffer = @import("engine/StagingBuffer.zig");
const Swapchain = @import("engine/Swapchain.zig");
const Texture = @import("engine/Texture.zig");
const Textures = @import("engine/Textures.zig");
@@ -36,7 +36,8 @@ vertex_buffer: shaders.VertexBuffer,
index_buffer: shaders.IndexBuffer,
global_uniforms: shaders.GlobalUniformsBuffer,
global_uniforms_staging_buffer: StagingBuffer,
global_uniforms_staging_buffer: shaders.GlobalUniformsBuffer,
global_uniforms_staging_ptr: *shaders.GlobalUniforms,
global_uniforms_transfer_semaphores: []vk.Semaphore,
point_lights: shaders.PointLightBuffer,
directional_lights: shaders.DirectionalLightBuffer,
@@ -47,8 +48,8 @@ blocks: Blocks,
chunks: Chunks,
skybox: Skybox,
player: Player,
font: Fonts.Id,
const max_textures = 1024;
const max_point_lights = 1024;
const max_directional_lights = 4;
const chunk_descriptor_pool = 1024;
@@ -70,6 +71,7 @@ pub fn init() !Game {
const swapchain = ctx.swapchain;
const textures = ctx.textures;
const materials = ctx.materials;
const fonts = ctx.fonts;
const gui = ctx.gui;
var blocks = try Blocks.init();
@@ -84,9 +86,9 @@ pub fn init() !Game {
.mag_filter = .linear,
.min_filter = .linear,
.mipmap_mode = .linear,
.address_mode_u = .repeat,
.address_mode_v = .repeat,
.address_mode_w = .repeat,
.address_mode_u = .clamp_to_edge,
.address_mode_v = .clamp_to_edge,
.address_mode_w = .clamp_to_edge,
.mip_lod_bias = 0,
.anisotropy_enable = .false,
.max_anisotropy = 0,
@@ -135,9 +137,8 @@ pub fn init() !Game {
.{
.binding = 5,
.descriptor_type = .sampled_image,
.descriptor_count = max_textures,
.descriptor_count = Textures.max_textures,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
.flags = .{ .variable_descriptor_count_bit = true },
},
},
});
@@ -173,49 +174,45 @@ pub fn init() !Game {
var vertex_buffer = try shaders.VertexBuffer.init(.{
.usage = .vertex,
.target_queue = .graphics,
.array_capacity = 4,
.capacity = 4,
.name = "QuadVB",
});
errdefer vertex_buffer.deinit();
try vertex_buffer.write(.{
.elements = &.{
.init(
.init(0, 0, 0),
.init(0, 1),
.init(0, 0, 1),
.init(1, 0, 0, -1),
),
.init(
.init(1, 0, 0),
.init(1, 1),
.init(0, 0, 1),
.init(1, 0, 0, -1),
),
.init(
.init(0, 1, 0),
.init(0, 0),
.init(0, 0, 1),
.init(1, 0, 0, -1),
),
.init(
.init(1, 1, 0),
.init(1, 0),
.init(0, 0, 1),
.init(1, 0, 0, -1),
),
},
try vertex_buffer.writeSlice(&.{
.init(
.init(0, 0, 0),
.init(0, 1),
.init(0, 0, 1),
.init(1, 0, 0, -1),
),
.init(
.init(1, 0, 0),
.init(1, 1),
.init(0, 0, 1),
.init(1, 0, 0, -1),
),
.init(
.init(0, 1, 0),
.init(0, 0),
.init(0, 0, 1),
.init(1, 0, 0, -1),
),
.init(
.init(1, 1, 0),
.init(1, 0),
.init(0, 0, 1),
.init(1, 0, 0, -1),
),
});
var index_buffer = try shaders.IndexBuffer.init(.{
.usage = .index,
.target_queue = .graphics,
.array_capacity = 6,
.capacity = 6,
.name = "QuadIB",
});
errdefer index_buffer.deinit();
try index_buffer.write(.{
.elements = &.{ 0, 1, 2, 2, 1, 3 },
});
try index_buffer.writeSlice(&.{ 0, 1, 2, 2, 1, 3 });
var global_uniforms = try shaders.GlobalUniformsBuffer.init(.{
.usage = .uniform,
@@ -224,12 +221,17 @@ pub fn init() !Game {
});
errdefer global_uniforms.deinit();
var global_uniforms_staging_buffer = try StagingBuffer.init(.{
.capacity = @sizeOf(shaders.GlobalUniforms),
var global_uniforms_staging_buffer = try shaders.GlobalUniformsBuffer.init(.{
.usage = .staging,
.target_queue = .graphics,
.mappable = true,
.name = "GlobalUniforms Staging",
});
errdefer global_uniforms_staging_buffer.deinit();
const global_uniforms_staging_ptr = try global_uniforms_staging_buffer.map();
errdefer global_uniforms_staging_buffer.unmap();
const global_uniforms_transfer_semaphores = blk: {
var semaphores: std.ArrayList(vk.Semaphore) = try .initCapacity(allocator_general, swapchain.swapchain_images.len);
errdefer semaphores.deinit(allocator_general);
@@ -254,7 +256,7 @@ pub fn init() !Game {
var point_lights = try shaders.PointLightBuffer.init(.{
.usage = .storage,
.target_queue = .graphics,
.array_capacity = max_point_lights,
.capacity = max_point_lights,
.name = "PointLights",
});
errdefer point_lights.deinit();
@@ -262,7 +264,7 @@ pub fn init() !Game {
var directional_lights = try shaders.DirectionalLightBuffer.init(.{
.usage = .storage,
.target_queue = .graphics,
.array_capacity = max_directional_lights,
.capacity = max_directional_lights,
.name = "DirectionalLights",
});
errdefer directional_lights.deinit();
@@ -415,7 +417,7 @@ pub fn init() !Game {
},
.{
.type = .sampled_image,
.descriptor_count = max_textures,
.descriptor_count = Textures.max_textures,
},
.{
.type = .uniform_buffer,
@@ -433,7 +435,6 @@ pub fn init() !Game {
const global_descriptor_set = try engine.allocateDescriptorSet(.{
.descriptor_pool = descriptor_pool,
.set_layout = global_descriptor_set_layout,
.variable_descriptor_count = @intCast(textures.array.items.len),
});
engine.setObjectName(global_descriptor_set, "DS Global", .{});
@@ -442,6 +443,13 @@ pub fn init() !Game {
const block_stone = try blocks.getOrLoad("Stone.json");
const block_bedrock = try blocks.getOrLoad("Bedrock.json");
const font_id = try fonts.load("FiraMono-Regular.ttf", .{
.codepoint_ranges = &.{
.{ .start = 32, .end = 127 },
},
.name = "Fira Mono",
});
// VOLATILE Load all assets before this point
const descriptor_images = try allocator_frame.alloc(vk.DescriptorImageInfo, textures.array.items.len);
@@ -613,12 +621,14 @@ pub fn init() !Game {
});
}
try point_lights.write(.{ .elements = &point_lights_data });
try directional_lights.write(.{ .elements = &directional_lights_data });
try point_lights.writeSlice(&point_lights_data);
try directional_lights.writeSlice(&directional_lights_data);
var skybox = try Skybox.load("skybox.hdr", 512, global_uniforms.buffer);
errdefer skybox.deinit();
const font = &fonts.array.items[@intFromEnum(font_id)];
// TODO This coupling is not elegant - the game must provide the GUI with
// uniform buffer for globals. Maybe GUI should just use their own global
// uniform buffer not for any other reason than decoupling. After all, GUI
@@ -641,6 +651,88 @@ pub fn init() !Game {
},
},
},
.{
.dst_set = gui.text_descriptor_set,
.dst_binding = 0,
.dst_array_element = 0,
.descriptor_type = .uniform_buffer,
.descriptor_infos = .{
.buffer = &.{
.{
.buffer = global_uniforms.buffer,
.offset = 0,
.range = vk.WHOLE_SIZE,
},
},
},
},
.{
.dst_set = gui.text_descriptor_set,
.dst_binding = 2,
.dst_array_element = @intFromEnum(font_id),
.descriptor_type = .storage_buffer,
.descriptor_infos = .{
.buffer = &.{
.{
.buffer = font.glyph_data.buffer,
.offset = 0,
.range = vk.WHOLE_SIZE,
},
},
},
},
.{
.dst_set = gui.text_descriptor_set,
.dst_binding = 3,
.dst_array_element = @intFromEnum(font_id),
.descriptor_type = .storage_buffer,
.descriptor_infos = .{
.buffer = &.{
.{
.buffer = font.control_points.buffer,
.offset = 0,
.range = vk.WHOLE_SIZE,
},
},
},
},
.{
.dst_set = gui.text_descriptor_set,
.dst_binding = 4,
.dst_array_element = @intFromEnum(font_id),
.descriptor_type = .storage_buffer,
.descriptor_infos = .{
.buffer = &.{
.{
.buffer = font.band_data.buffer,
.offset = 0,
.range = vk.WHOLE_SIZE,
},
},
},
},
.{
.dst_set = gui.image_descriptor_set,
.dst_binding = 0,
.dst_array_element = 0,
.descriptor_type = .uniform_buffer,
.descriptor_infos = .{
.buffer = &.{
.{
.buffer = global_uniforms.buffer,
.offset = 0,
.range = vk.WHOLE_SIZE,
},
},
},
},
.{
.dst_set = gui.image_descriptor_set,
.dst_binding = 3,
.dst_array_element = 0,
.descriptor_type = .sampled_image,
.descriptor_infos = .{ .image = descriptor_images },
},
},
});
@@ -657,6 +749,7 @@ pub fn init() !Game {
.global_uniforms = global_uniforms,
.global_uniforms_staging_buffer = global_uniforms_staging_buffer,
.global_uniforms_staging_ptr = global_uniforms_staging_ptr,
.global_uniforms_transfer_semaphores = global_uniforms_transfer_semaphores,
.point_lights = point_lights,
.directional_lights = directional_lights,
@@ -667,6 +760,7 @@ pub fn init() !Game {
.chunks = .{ .chunks = chunks },
.skybox = skybox,
.player = .init(player_position_sv, 0, 0),
.font = font_id,
};
}
@@ -688,6 +782,7 @@ pub fn deinit(self: *Game) void {
self.skybox.deinit();
self.global_uniforms.deinit();
self.global_uniforms_staging_buffer.unmap();
self.global_uniforms_staging_buffer.deinit();
self.point_lights.deinit();
self.directional_lights.deinit();
@@ -709,37 +804,7 @@ pub fn deinit(self: *Game) void {
}
pub fn update(self: *Game, dt: f32) void {
const swapchain = ctx.swapchain;
const gui = ctx.gui;
self.player.update(dt, &self.chunks);
const extent = swapchain.extent;
const framebuffer_size = vm.Vector2.init(
@floatFromInt(extent.width),
@floatFromInt(extent.height),
);
const crosshair_half_extent_px = 8;
const crosshair_half_width_px = 1;
gui.pushBox(.{
.background_color = .init(1, 1, 1, 0.5),
.border_color = .init(0, 0, 0, 1),
.position_sspx = framebuffer_size.mulScalar(0.5).add(.init(-crosshair_half_extent_px, -crosshair_half_width_px)),
.size_px = .init(2.0 * crosshair_half_extent_px, 2.0 * crosshair_half_width_px),
.border_width_px = 0,
.border_radius_px = 0,
});
gui.pushBox(.{
.background_color = .init(1, 1, 1, 0.5),
.border_color = .init(0, 0, 0, 1),
.position_sspx = framebuffer_size.mulScalar(0.5).add(.init(-crosshair_half_width_px, -crosshair_half_extent_px)),
.size_px = .init(2.0 * crosshair_half_width_px, 2.0 * crosshair_half_extent_px),
.border_width_px = 0,
.border_radius_px = 0,
});
self.player.update(dt, self);
self.render() catch |err| {
std.log.err("Failed to render: {s}", .{@errorName(err)});
@@ -843,8 +908,13 @@ fn render(self: *Game) !void {
const ambient_light = vm.Vector3.init(0.01, 0.01, 0.01);
const global_uniforms_data: shaders.GlobalUniforms = .{
.matrixWStoVS = matrix_ws_to_vs,
self.global_uniforms_staging_ptr.* = .{
.matrixWStoVS = .{
matrix_ws_to_vs.getIVersor().dropW(),
matrix_ws_to_vs.getJVersor().dropW(),
matrix_ws_to_vs.getKVersor().dropW(),
matrix_ws_to_vs.getTranslationVector().dropW(),
},
.matrixVStoCS = matrix_vs_to_cs,
.matrixSSPXtoCS = matrix_sspx_to_cs,
@@ -852,10 +922,7 @@ fn render(self: *Game) !void {
.directionalLightCount = directional_lights_data.len,
.ambientLight = ambient_light,
};
const staging_memory = try self.global_uniforms_staging_buffer.map();
@memcpy(staging_memory, std.mem.asBytes(&global_uniforms_data));
self.global_uniforms_staging_buffer.unmap();
try self.global_uniforms_staging_buffer.flush(0, 1);
try self.deferred_command_buffers.ensureUnusedCapacity(allocator_general, 1);
const global_uniforms_transfer_command_buffer = try CommandBuffer.init(.transfer);

View File

@@ -2,6 +2,7 @@ const Player = @This();
const std = @import("std");
const c = @import("const.zig");
const ctx = @import("AppContext.zig");
const glfw = @import("zglfw");
const math = @import("math.zig");
const vm = @import("vecmath");
@@ -183,9 +184,9 @@ pub const blocks = [9][:0]const u8{
"Bricks.json",
"ChiseledStoneBricks.json",
"Cobblestone.json",
"DiamondBlock.json",
"GoldBlock.json",
"IronBlock.json",
"OakLogX.json",
"OakLogY.json",
"OakLogZ.json",
"OakPlanks.json",
"SmoothStone.json",
"StoneBricks.json",
@@ -231,7 +232,6 @@ pub fn onKeyDown(self: *Player, key: glfw.Key) void {
const key_code = @intFromEnum(key);
if (key_code >= @intFromEnum(glfw.Key.one) and key_code <= @intFromEnum(glfw.Key.nine)) {
self.block_index = @intCast(key_code - @intFromEnum(glfw.Key.one));
std.log.info("Picked block {s}", .{blocks[self.block_index]});
}
}
@@ -281,7 +281,11 @@ pub fn onMouseDown(self: *Player, button: glfw.MouseButton, game: *Game) void {
}
}
pub fn update(self: *Player, dt: f32, chunks: *const Chunks) void {
pub fn update(self: *Player, dt: f32, game: *Game) void {
const swapchain = ctx.swapchain;
const materials = ctx.materials;
const gui = ctx.gui;
defer self.resetAllButtons();
const raycast_origin_sv = self.position_sv
@@ -294,7 +298,7 @@ pub fn update(self: *Player, dt: f32, chunks: *const Chunks) void {
))
.round()
.asInt();
self.maybe_raycast_hit = chunks.raycast(raycast_origin_sv, raycast_ray_sv);
self.maybe_raycast_hit = game.chunks.raycast(raycast_origin_sv, raycast_ray_sv);
// --- GATHER INPUTS -------------------------------------------------------
@@ -343,7 +347,7 @@ pub fn update(self: *Player, dt: f32, chunks: *const Chunks) void {
var max_sv = position_sv.add(.init(collision_half_width_sv, collision_half_width_sv, collision_height_sv));
if (vertical_displacement_sv > 0) {
if (chunks.sweepCastUp(min_sv, max_sv, vertical_displacement_sv)) |hit| {
if (game.chunks.sweepCastUp(min_sv, max_sv, vertical_displacement_sv)) |hit| {
vertical_displacement_sv -= hit.projected_distance_sv;
}
position_sv = .add(position_sv, .init(0, 0, vertical_displacement_sv));
@@ -353,7 +357,7 @@ pub fn update(self: *Player, dt: f32, chunks: *const Chunks) void {
var i: usize = 0;
while (i < 2) : (i += 1) {
if (chunks.sweepCastHorizontal(min_sv, max_sv, horizontal_displacement_sv)) |hit| {
if (game.chunks.sweepCastHorizontal(min_sv, max_sv, horizontal_displacement_sv)) |hit| {
const adjustment = math.mulFrac2(hit.normal_frac.dropZ(), hit.projected_distance_sv);
horizontal_displacement_sv = .add(
horizontal_displacement_sv,
@@ -368,7 +372,7 @@ pub fn update(self: *Player, dt: f32, chunks: *const Chunks) void {
if (vertical_displacement_sv < 0.0) {
min_sv = .add(min_sv, horizontal_displacement_sv.withZ(0));
max_sv = .add(max_sv, horizontal_displacement_sv.withZ(0));
if (chunks.sweepCastDown(min_sv, max_sv, -vertical_displacement_sv)) |hit| {
if (game.chunks.sweepCastDown(min_sv, max_sv, -vertical_displacement_sv)) |hit| {
vertical_displacement_sv += hit.projected_distance_sv;
}
position_sv = .add(position_sv, .init(0, 0, vertical_displacement_sv));
@@ -377,6 +381,149 @@ pub fn update(self: *Player, dt: f32, chunks: *const Chunks) void {
self.position_sv = position_sv;
},
}
// --- GUI -----------------------------------------------------------------
const extent = swapchain.extent;
const framebuffer_size = vm.Vector2.init(
@floatFromInt(extent.width),
@floatFromInt(extent.height),
);
// --- CROSSHAIR ---
{
const crosshair_half_extent_px = 8;
const crosshair_half_width_px = 1;
gui.pushBox(.{
.background_color = .init(1, 1, 1, 0.5),
.border_color = .init(0, 0, 0, 1),
.position_sspx = framebuffer_size.mulScalar(0.5).add(.init(-crosshair_half_extent_px, -crosshair_half_width_px)),
.size_px = .init(2.0 * crosshair_half_extent_px, 2.0 * crosshair_half_width_px),
.border_width_px = 0,
.border_radius_px = 0,
});
gui.pushBox(.{
.background_color = .init(1, 1, 1, 0.5),
.border_color = .init(0, 0, 0, 1),
.position_sspx = framebuffer_size.mulScalar(0.5).add(.init(-crosshair_half_width_px, -crosshair_half_extent_px)),
.size_px = .init(2.0 * crosshair_half_width_px, 2.0 * crosshair_half_extent_px),
.border_width_px = 0,
.border_radius_px = 0,
});
}
// --- COORDINATES ---
{
const margin: f16 = 8;
const font_size: f32 = 16;
// NOTE We have to do the number formatting semi-manually, because zig's
// std fmt is weird with signed numbers
var number_buf: [9]u8 = undefined; // sign ('-' or ' ') and 8 hex digits
gui.pushBox(.{
.background_color = .init(0, 0, 0, 0.75),
.border_color = .init(0, 0, 0, 1),
.position_sspx = .init(0, 0),
.size_px = .init(164, 4 * margin + 3 * font_size),
.border_width_px = 0,
.border_radius_px = 0,
});
gui.printLine("X {s}", .{formatHexCoord(self.position_sv.x, &number_buf)}, .{
.position_sspx = .init(margin, margin + font_size),
.font_id = @enumFromInt(0),
.cap_height_px = font_size,
.color = .init(1, 1, 1, 1),
});
gui.printLine("Y {s}", .{formatHexCoord(self.position_sv.y, &number_buf)}, .{
.position_sspx = .init(margin, 2 * (margin + font_size)),
.font_id = @enumFromInt(0),
.cap_height_px = font_size,
.color = .init(1, 1, 1, 1),
});
gui.printLine("Z {s}", .{formatHexCoord(self.position_sv.z, &number_buf)}, .{
.position_sspx = .init(margin, 3 * (margin + font_size)),
.font_id = @enumFromInt(0),
.cap_height_px = font_size,
.color = .init(1, 1, 1, 1),
});
}
// --- HOTBAR ---
{
const block_selection_outline_width_px: f32 = 4;
const block_image_size_px: f32 = 48;
const block_image_gap_px: f32 = 8;
const block_image_margin_px: f32 = 8;
const hotbar_width_px: f32 = @as(f32, @floatFromInt(blocks.len)) * block_image_size_px + @as(f32, @floatFromInt(blocks.len - 1)) * block_image_gap_px;
const hotbar_left_px: f32 = 0.5 * (framebuffer_size.x - hotbar_width_px);
const hotbar_top_px: f32 = framebuffer_size.y - block_image_margin_px - block_image_size_px;
gui.pushBox(.{
.background_color = .init(0, 0, 0, 0.75),
.border_color = .init(0, 0, 0, 1),
.position_sspx = .init(hotbar_left_px - block_image_margin_px, hotbar_top_px - block_image_margin_px),
.size_px = .init(hotbar_width_px + 2 * block_image_margin_px, block_image_size_px + 2 * block_image_margin_px),
.border_width_px = 4,
.border_radius_px = 0,
});
var cx: f32 = hotbar_left_px;
for (blocks, 0..) |filename, i| {
defer cx += block_image_size_px + block_image_gap_px;
const id = game.blocks.getOrLoad(filename) catch |err| {
std.log.err("Error while rendering hotbar block {s}: {}", .{ filename, err });
continue;
};
const definition = &game.blocks.array.items[@intFromEnum(id)];
const material_id = definition.walls.positive_z.material;
const material = &materials.array.items[@intFromEnum(material_id)];
if (i == self.block_index) {
gui.pushBox(.{
.background_color = .initScalar(0),
.border_color = .init(1, 1, 1, 1),
.position_sspx = vm.Vector2.init(cx, hotbar_top_px).sub(.initScalar(block_selection_outline_width_px)),
.size_px = .initScalar(block_image_size_px + 2 * block_selection_outline_width_px),
.border_width_px = block_selection_outline_width_px,
.border_radius_px = block_selection_outline_width_px,
});
}
const texture_id = material.base_color_texture;
gui.pushImage(.{
.position_sspx = .init(cx, hotbar_top_px),
.size_px = .initScalar(block_image_size_px),
.texture_id = texture_id,
});
}
}
}
fn formatHexCoord(coord: i32, buf: *[9]u8) []const u8 {
buf[0] = ' ';
const coord_abs = @abs(coord);
_ = std.fmt.bufPrint(buf[1..9], "{X:>8}", .{coord_abs}) catch unreachable;
if (coord < 0) {
const i = std.mem.findScalarLast(u8, buf, ' ') orelse unreachable;
buf[i] = '-';
}
return buf[0..];
}
fn resetAllButtons(self: *Player) void {

View File

@@ -272,9 +272,9 @@ pub fn init() !*Engine {
var enabled_features_vulkan12: vk.PhysicalDeviceVulkan12Features = .{
.p_next = &enabled_features_vulkan11,
.shader_float_16 = .true,
.descriptor_indexing = .true,
.descriptor_binding_partially_bound = .true,
.descriptor_binding_variable_descriptor_count = .true,
.runtime_descriptor_array = .true,
.scalar_block_layout = .true,
.imageless_framebuffer = .true,
@@ -744,13 +744,11 @@ pub const DescriptorPoolCreateInfo = struct {
pub const DescriptorSetAllocateInfo = struct {
descriptor_pool: vk.DescriptorPool,
set_layout: vk.DescriptorSetLayout,
variable_descriptor_count: ?u32 = null,
};
pub const DescriptorSetsAllocateInfo = struct {
descriptor_pool: vk.DescriptorPool,
set_layouts: []const vk.DescriptorSetLayout,
variable_descriptor_counts: []const u32 = &.{},
};
pub const DescriptorSetLayoutBinding = struct {
@@ -968,28 +966,13 @@ pub fn allocateDescriptorSet(self: *Engine, allocate_info: DescriptorSetAllocate
try self.allocateDescriptorSets(.{
.descriptor_pool = allocate_info.descriptor_pool,
.set_layouts = &.{allocate_info.set_layout},
.variable_descriptor_counts = if (allocate_info.variable_descriptor_count) |x| &.{x} else &.{},
}, &descriptor_sets);
return descriptor_sets[0];
}
pub fn allocateDescriptorSets(self: *Engine, allocate_info: DescriptorSetsAllocateInfo, descriptor_sets: []vk.DescriptorSet) !void {
std.debug.assert(descriptor_sets.len >= allocate_info.set_layouts.len);
const has_variable_descriptor_counts = allocate_info.variable_descriptor_counts.len > 0;
var p_next: ?*const anyopaque = null;
if (has_variable_descriptor_counts) {
p_next = &vk.DescriptorSetVariableDescriptorCountAllocateInfo{
.p_next = p_next,
.descriptor_set_count = @intCast(allocate_info.variable_descriptor_counts.len),
.p_descriptor_counts = allocate_info.variable_descriptor_counts.ptr,
};
}
try self.device.allocateDescriptorSets(&.{
.p_next = p_next,
.descriptor_pool = allocate_info.descriptor_pool,
.descriptor_set_count = @intCast(allocate_info.set_layouts.len),
.p_set_layouts = allocate_info.set_layouts.ptr,

452
src/engine/Font.zig Normal file
View File

@@ -0,0 +1,452 @@
const Font = @This();
const std = @import("std");
const ctx = @import("../AppContext.zig");
const shaders = @import("../shaders.zig");
const vm = @import("vecmath");
const GenericBuffer = @import("GenericBuffer.zig").GenericBuffer;
pub const CodePointRange = struct {
/// inclusive
start: u21,
/// exclusive
end: u21,
};
pub const InitInfo = struct {
codepoint_ranges: []const CodePointRange = &.{
.{ .start = 32, .end = 127 },
},
/// [vertical, horizontal]
bands_per_em: vm.Vector2 = .init(8, 8),
name: ?[]const u8 = null,
};
const GlyphCpuData = struct {
/// `undefined` when `gpu_glyph_id == null`
bottom_left_em: vm.Vector2,
advance_em: f32,
left_side_bearing_em: f32,
/// If `null`, the glyph is empty (like a space). In such case, we only need
/// to advance the cursor by given `advance_em` and we must not emit any gpu
/// glyph instances.
gpu_glyph_id: ?u16,
};
cpu_glyph_data: []GlyphCpuData,
codepoint_glyph_id: std.hash_map.AutoHashMapUnmanaged(u21, u16),
kerning_em: std.hash_map.AutoHashMapUnmanaged([2]u16, f32),
cap_height_em: f32,
glyph_data: GenericBuffer(shaders.GlyphData),
control_points: GenericBuffer([2]f16),
band_data: GenericBuffer(u32),
const BandHelper = struct {
allocator: std.mem.Allocator,
bands: []std.ArrayList(u32),
fn init(allocator: std.mem.Allocator) BandHelper {
return .{
.allocator = allocator,
.bands = &.{},
};
}
fn ensureBandCount(self: *BandHelper, desired_band_count: usize) !void {
std.debug.assert(desired_band_count > 0);
const old_band_count = self.bands.len;
if (old_band_count >= desired_band_count) return;
const new_band_count = std.math.ceilPowerOfTwo(usize, desired_band_count) catch |err| switch (err) {
error.Overflow => return error.OutOfMemory,
};
self.bands = try self.allocator.realloc(self.bands, new_band_count);
for (old_band_count..new_band_count) |i| {
self.bands[i] = .empty;
}
}
fn clearBands(self: *BandHelper, band_count: usize) void {
for (self.bands[0..band_count]) |*array| {
array.clearRetainingCapacity();
}
}
fn insertControlPointPtr(self: *BandHelper, band: usize, ptr: u32) !void {
const array = &self.bands[band];
try array.append(self.allocator, ptr);
}
fn sortBands(self: *BandHelper, coordinate: u1, band_count: usize, control_points: []const [2]f16) void {
for (self.bands[0..band_count]) |*array| {
const sort_ctx = SortContext{
.coordinate = coordinate,
.control_points = control_points,
};
std.mem.sortUnstable(u32, array.items, &sort_ctx, SortContext.lessThan);
// std.debug.print(" BAND {d} sorted by coordinate {d}\n", .{ array - self.bands.ptr, coordinate });
// for (array.items) |cp_ptr| {
// const cp0 = control_points[cp_ptr + 0];
// const cp1 = control_points[cp_ptr + 1];
// const cp2 = control_points[cp_ptr + 2];
// const max = @max(cp0[coordinate], cp1[coordinate], cp2[coordinate]);
// std.debug.print(" max {d} | ({d}, {d}) → ({d}, {d}) → ({d}, {d})\n", .{ max, cp0[0], cp0[1], cp1[0], cp1[1], cp2[0], cp2[1] });
// }
}
}
fn write(self: *const BandHelper, band_count: usize, buffer: *std.ArrayList(u32), buffer_allocator: std.mem.Allocator) !void {
var band_data_ptr = buffer.items.len;
// --- HEADERS ---
band_data_ptr += band_count;
for (self.bands[0..band_count]) |*array| {
const band = array.items;
// Header is a self-relative forward-only pointer in high 16 bits
// and length in low 16 bits.
const ptr = std.math.cast(u16, band_data_ptr - buffer.items.len) orelse return error.Overflow;
const len = std.math.cast(u16, band.len) orelse return error.Overflow;
try buffer.append(buffer_allocator, (@as(u32, ptr) << 16) | len);
band_data_ptr += len;
}
// --- BAND DATA ---
for (self.bands[0..band_count]) |*array| {
const band = array.items;
try buffer.appendSlice(buffer_allocator, band);
}
std.debug.assert(band_data_ptr == buffer.items.len);
}
fn deinit(self: *BandHelper) void {
for (self.bands) |*array| {
array.deinit(self.allocator);
}
self.allocator.free(self.bands);
self.* = undefined;
}
};
const SortContext = struct {
coordinate: u1,
control_points: []const [2]f16,
fn lessThan(self: *const SortContext, lhs: u32, rhs: u32) bool {
const lhs_cp0 = self.control_points[lhs + 0];
const lhs_cp1 = self.control_points[lhs + 1];
const lhs_cp2 = self.control_points[lhs + 2];
const lhs_max = @max(
lhs_cp0[self.coordinate],
lhs_cp1[self.coordinate],
lhs_cp2[self.coordinate],
);
const rhs_cp0 = self.control_points[rhs + 0];
const rhs_cp1 = self.control_points[rhs + 1];
const rhs_cp2 = self.control_points[rhs + 2];
const rhs_max = @max(
rhs_cp0[self.coordinate],
rhs_cp1[self.coordinate],
rhs_cp2[self.coordinate],
);
return lhs_max >= rhs_max;
}
};
pub fn init(ttf_buffer: []const u8, init_info: InitInfo) !Font {
const allocator_general = ctx.allocator_general;
const allocator_frame = ctx.allocator_frame;
const stbtt = ctx.stbtt;
const ttf_font = try stbtt.initFont(ttf_buffer);
defer ttf_font.deinit();
const units_per_em = ttf_font.unitsPerEm();
const em_per_unit = 1 / @as(f32, @floatFromInt(units_per_em));
// std.debug.print("Units per EM: {d}\nEM per unit: {d}\n", .{ units_per_em, em_per_unit });
var glyph_count: u16 = 0;
for (init_info.codepoint_ranges) |range| {
const range_size = std.math.cast(u16, range.end - range.start) orelse return error.TooManyGlyphs;
glyph_count = std.math.add(u16, glyph_count, range_size) catch |err| switch (err) {
error.Overflow => return error.TooManyGlyphs,
};
}
var codepoint_glyph_id: std.hash_map.AutoHashMapUnmanaged(u21, u16) = .empty;
errdefer codepoint_glyph_id.deinit(allocator_general);
try codepoint_glyph_id.ensureTotalCapacity(allocator_general, glyph_count);
var external_glyph_id_to_cpu_glyph_id: std.hash_map.AutoHashMapUnmanaged(u16, u16) = .empty;
try external_glyph_id_to_cpu_glyph_id.ensureTotalCapacity(allocator_frame, glyph_count);
var kerning_em: std.hash_map.AutoHashMapUnmanaged([2]u16, f32) = .empty;
errdefer kerning_em.deinit(allocator_general);
const cap_height = ttf_font.getCapHeight() orelse return error.FontError;
const cap_height_em = @as(f32, @floatFromInt(cap_height)) * em_per_unit;
// std.debug.print("Cap height: {d} U | {d} EM\n", .{ cap_height, cap_height_em });
var cpu_glyph_data: std.ArrayList(GlyphCpuData) = try .initCapacity(allocator_general, glyph_count);
errdefer cpu_glyph_data.deinit(allocator_general);
var gpu_glyph_data: std.ArrayList(shaders.GlyphData) = try .initCapacity(allocator_frame, glyph_count);
var control_points_array: std.ArrayList([2]f16) = .empty;
defer control_points_array.deinit(allocator_general);
var band_data_array: std.ArrayList(u32) = .empty;
defer band_data_array.deinit(allocator_general);
var vertical_band_helper: BandHelper = .init(allocator_general);
defer vertical_band_helper.deinit();
var horizontal_band_helper: BandHelper = .init(allocator_general);
defer horizontal_band_helper.deinit();
for (init_info.codepoint_ranges) |range| {
var codepoint: u21 = range.start;
while (codepoint < range.end) : (codepoint += 1) {
// std.debug.print("Processing codepoint 0x{X} '{u}'\n", .{ codepoint, codepoint });
if (ttf_font.findGlyphIndex(codepoint)) |external_glyph_id| {
const external_glyph_id_to_cpu_glyph_id_get_or_put_result = external_glyph_id_to_cpu_glyph_id.getOrPutAssumeCapacity(external_glyph_id);
if (external_glyph_id_to_cpu_glyph_id_get_or_put_result.found_existing) {
codepoint_glyph_id.putAssumeCapacityNoClobber(codepoint, external_glyph_id_to_cpu_glyph_id_get_or_put_result.value_ptr.*);
continue;
} else {
const cpu_glyph_id = @as(u16, @intCast(cpu_glyph_data.items.len));
external_glyph_id_to_cpu_glyph_id_get_or_put_result.value_ptr.* = cpu_glyph_id;
codepoint_glyph_id.putAssumeCapacityNoClobber(codepoint, cpu_glyph_id);
}
const glyph_metrics = ttf_font.getGlyphMetrics(external_glyph_id);
// std.debug.print(" Advance: {d} U\n", .{glyph_metrics.advance_width});
// std.debug.print(" LSB: {d} U\n", .{glyph_metrics.left_side_bearing});
if (ttf_font.isGlyphEmpty(external_glyph_id)) {
// std.debug.print(" Glyph is empty\n", .{});
cpu_glyph_data.appendAssumeCapacity(.{
.bottom_left_em = undefined,
.left_side_bearing_em = @as(f32, @floatFromInt(glyph_metrics.left_side_bearing)) * em_per_unit,
.advance_em = @as(f32, @floatFromInt(glyph_metrics.advance_width)) * em_per_unit,
.gpu_glyph_id = null,
});
} else if (ttf_font.getGlyphBox(external_glyph_id)) |box| {
const bottom_left_em = vm.Vector2.init(
@floatFromInt(box.x_min),
@floatFromInt(box.y_min),
).mulScalar(em_per_unit);
const size_em = vm.Vector2.init(
@as(f32, @floatFromInt(box.x_max)) - @as(f32, @floatFromInt(box.x_min)),
@as(f32, @floatFromInt(box.y_max)) - @as(f32, @floatFromInt(box.y_min)),
).mulScalar(em_per_unit);
const gpu_glyph_id = @as(u16, @intCast(gpu_glyph_data.items.len));
cpu_glyph_data.appendAssumeCapacity(.{
.bottom_left_em = bottom_left_em,
.left_side_bearing_em = @as(f32, @floatFromInt(glyph_metrics.left_side_bearing)) * em_per_unit,
.advance_em = @as(f32, @floatFromInt(glyph_metrics.advance_width)) * em_per_unit,
.gpu_glyph_id = gpu_glyph_id,
});
const band_count = vm.Vector2.max(.round(.mul(size_em, init_info.bands_per_em)), .initScalar(1));
const vertical_band_count: u32 = @trunc(band_count.x);
const horizontal_band_count: u32 = @trunc(band_count.y);
try vertical_band_helper.ensureBandCount(vertical_band_count);
try horizontal_band_helper.ensureBandCount(horizontal_band_count);
vertical_band_helper.clearBands(vertical_band_count);
horizontal_band_helper.clearBands(horizontal_band_count);
const vertices = ttf_font.getGlyphShape(external_glyph_id);
defer ttf_font.freeShape(vertices);
// std.debug.print(" bottom left: {f} [EM]\n", .{bottom_left_em});
// std.debug.print(" size: {f} [EM]\n", .{size_em});
// std.debug.print(" band count: {f}\n", .{band_count});
// std.debug.print(" vertices: {d}\n", .{vertices.len});
var cp0: vm.Vector2 = undefined;
for (vertices) |vertex| {
var cp1: vm.Vector2 = undefined;
var cp2: vm.Vector2 = undefined;
switch (vertex.type) {
.STBTT_vmove => {
cp0.x = @as(f32, @floatFromInt(vertex.x)) * em_per_unit;
cp0.y = @as(f32, @floatFromInt(vertex.y)) * em_per_unit;
try control_points_array.append(
allocator_general,
.{ @floatCast(cp0.x), @floatCast(cp0.y) },
);
continue;
},
.STBTT_vline => {
cp1.x = @as(f32, @floatFromInt(vertex.x)) * em_per_unit;
cp1.y = @as(f32, @floatFromInt(vertex.y)) * em_per_unit;
cp2 = cp1;
try control_points_array.appendNTimes(
allocator_general,
.{ @floatCast(cp1.x), @floatCast(cp1.y) },
2,
);
},
.STBTT_vcurve => {
cp1.x = @as(f32, @floatFromInt(vertex.cx)) * em_per_unit;
cp1.y = @as(f32, @floatFromInt(vertex.cy)) * em_per_unit;
cp2.x = @as(f32, @floatFromInt(vertex.x)) * em_per_unit;
cp2.y = @as(f32, @floatFromInt(vertex.y)) * em_per_unit;
try control_points_array.append(allocator_general, .{ @floatCast(cp1.x), @floatCast(cp1.y) });
try control_points_array.append(allocator_general, .{ @floatCast(cp2.x), @floatCast(cp2.y) });
},
.STBTT_vcubic => unreachable,
}
defer cp0 = cp2;
const cp_min = cp0.min(cp1).min(cp2).sub(.initScalar(em_per_unit));
const cp_max = cp0.max(cp1).max(cp2).add(.initScalar(em_per_unit));
const min_band_f = band_count.mul(.sub(cp_min, bottom_left_em)).div(size_em)
.max(.initScalar(0))
.min(.sub(band_count, .initScalar(1)))
.floor();
const max_band_f = band_count.mul(.sub(cp_max, bottom_left_em)).div(size_em)
.max(.initScalar(0))
.min(.sub(band_count, .initScalar(1)))
.floor();
// std.debug.print(" {f} → {f} → {f} | bands: {f} → {f}\n", .{ cp0, cp1, cp2, min_band_f, max_band_f });
const vertical_band_start = @as(u32, @trunc(min_band_f.x));
const vertical_band_end = @as(u32, @trunc(max_band_f.x)) + 1;
const horizontal_band_start = @as(u32, @trunc(min_band_f.y));
const horizontal_band_end = @as(u32, @trunc(max_band_f.y)) + 1;
const cp_ptr = @as(u32, @intCast(control_points_array.items.len - 3));
for (vertical_band_start..vertical_band_end) |vband| {
try vertical_band_helper.insertControlPointPtr(vband, cp_ptr);
}
for (horizontal_band_start..horizontal_band_end) |hband| {
try horizontal_band_helper.insertControlPointPtr(hband, cp_ptr);
}
cp0 = cp2;
}
const vertical_band_ptr = @as(u32, @intCast(band_data_array.items.len));
vertical_band_helper.sortBands(1, vertical_band_count, control_points_array.items);
try vertical_band_helper.write(vertical_band_count, &band_data_array, allocator_general);
const horizontal_band_ptr = @as(u32, @intCast(band_data_array.items.len));
horizontal_band_helper.sortBands(0, horizontal_band_count, control_points_array.items);
try horizontal_band_helper.write(horizontal_band_count, &band_data_array, allocator_general);
// std.debug.print(" vband: [{d}] @ {d}\n hband: [{d}] @ {d}\n", .{ vertical_band_count, vertical_band_ptr, horizontal_band_count, horizontal_band_ptr });
gpu_glyph_data.appendAssumeCapacity(.{
.bottomLeftEM = bottom_left_em,
.sizeEM = size_em,
.verticalBandPtr = vertical_band_ptr,
.horizontalBandPtr = horizontal_band_ptr,
.verticalBandCount = vertical_band_count,
.horizontalBandCount = horizontal_band_count,
});
} else {
return error.FontError;
}
} else {
return error.MissingGlyph;
}
}
}
var kerning_it = ttf_font.kerningTableIterator();
while (kerning_it.next()) |entry| {
if (external_glyph_id_to_cpu_glyph_id.contains(entry.glyph_prev) and external_glyph_id_to_cpu_glyph_id.contains(entry.glyph_next)) {
const advance_em = @as(f32, @floatFromInt(entry.advance)) * em_per_unit;
try kerning_em.put(allocator_general, .{ entry.glyph_prev, entry.glyph_next }, advance_em);
}
}
// std.debug.print("Kerning table size: {d}\n", .{kerning_em.size});
var glyph_data: GenericBuffer(shaders.GlyphData) = try .init(.{
.usage = .storage,
.target_queue = .graphics,
.capacity = @intCast(gpu_glyph_data.items.len),
.name = "Glyph data",
});
errdefer glyph_data.deinit();
// std.debug.print("Glyph data size: {d} [B]\n", .{gpu_glyph_data.items.len * @sizeOf(shaders.GlyphData)});
var control_points: GenericBuffer([2]f16) = try .init(.{
.usage = .storage,
.target_queue = .graphics,
.capacity = @intCast(control_points_array.items.len),
.name = "Control points",
});
errdefer control_points.deinit();
// std.debug.print("Control points size: {d} [B]\n", .{control_points_array.items.len * @sizeOf([2]f16)});
var band_data: GenericBuffer(u32) = try .init(.{
.usage = .storage,
.target_queue = .graphics,
.capacity = @intCast(band_data_array.items.len),
.name = "Band data",
});
errdefer band_data.deinit();
// std.debug.print("Band data size: {d} [B]\n", .{band_data_array.items.len * @sizeOf(u32)});
try glyph_data.writeSlice(gpu_glyph_data.items);
try control_points.writeSlice(control_points_array.items);
try band_data.writeSlice(band_data_array.items);
return .{
.cpu_glyph_data = cpu_glyph_data.toOwnedSliceAssert(),
.codepoint_glyph_id = codepoint_glyph_id,
.kerning_em = kerning_em,
.cap_height_em = cap_height_em,
.glyph_data = glyph_data,
.control_points = control_points,
.band_data = band_data,
};
}
pub fn deinit(self: *Font) void {
const allocator_general = ctx.allocator_general;
self.band_data.deinit();
self.control_points.deinit();
self.glyph_data.deinit();
self.codepoint_glyph_id.deinit(allocator_general);
allocator_general.free(self.cpu_glyph_data);
}

111
src/engine/Fonts.zig Normal file
View File

@@ -0,0 +1,111 @@
const Fonts = @This();
const std = @import("std");
const ctx = @import("../AppContext.zig");
const Atom = @import("../engine/Atom.zig").Atom;
const Font = @import("Font.zig");
pub const Id = enum(u16) {
// VOLATILE Synchronize explicit values with `init` implementation.
_,
/// Cast an integer into an ID. This can produce an invalid ID.
pub fn fromInt(value: u16) Id {
return @enumFromInt(value);
}
/// Cast an index into an ID. This can produce an invalid ID. The caller
/// asserts that the index is not greater than the max ID value.
pub fn fromIndex(index: usize) Id {
std.debug.assert(index < max_fonts);
return @enumFromInt(@as(u16, @intCast(index)));
}
/// Cast an index into an ID. This can produce an invalid ID. Returns an
/// error if the index is greater than the max ID value.
pub fn fromIndexSafe(index: usize) error{Overflow}!Id {
if (index >= max_fonts) return error.Overflow;
return @enumFromInt(@as(u16, @intCast(index)));
}
/// Cast an ID into an integer.
pub fn toInt(self: Id) u16 {
return @intFromEnum(self);
}
};
/// Stores all `Font` structs and maps a font ID to a `Font` struct.
/// Preallocated with `allocator_persistent`.
array: std.ArrayList(Font),
pub const max_fonts = 256;
pub fn init() !*Fonts {
const allocator_persistent = ctx.allocator_persistent;
const fonts = try allocator_persistent.create(Fonts);
fonts.* = .{
.array = .empty,
};
errdefer {
for (fonts.array.items) |*font| {
font.deinit();
}
}
try fonts.array.ensureTotalCapacityPrecise(allocator_persistent, max_fonts);
// VOLATILE Synchronize with explicit values on top of `Id` type.
return fonts;
}
pub fn deinit(self: *Fonts) void {
std.log.scoped(.deinit).debug("Deinitializing {*}", .{self});
for (self.array.items) |*font| {
font.deinit();
}
self.* = undefined;
}
pub fn load(
self: *Fonts,
filename: []const u8,
init_info: Font.InitInfo,
) !Id {
const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) {
error.Overflow => return error.OutOfFonts,
};
const font = try loadFont(filename, init_info);
self.array.appendAssumeCapacity(font);
return id;
}
fn loadFont(
filename: []const u8,
init_info: Font.InitInfo,
) !Font {
const allocator_frame = ctx.allocator_frame;
const io = ctx.io;
std.log.debug("Loading font \"{s}\"...", .{filename});
const cwd = std.Io.Dir.cwd();
var dir = try cwd.openDir(io, "assets/fonts", .{});
defer dir.close(io);
const ttf_buffer = try dir.readFileAlloc(io, filename, allocator_frame, .unlimited);
var font = try Font.init(ttf_buffer, init_info);
errdefer font.deinit();
return font;
}

View File

@@ -5,7 +5,6 @@ const vk = @import("vulkan");
const CommandBuffer = @import("CommandBuffer.zig");
const Engine = @import("Engine.zig");
const StagingBuffer = @import("StagingBuffer.zig");
const TargetQueue = @import("TargetQueue.zig").TargetQueue;
pub const Usage = enum {
@@ -14,6 +13,7 @@ pub const Usage = enum {
index,
vertex,
indirect,
staging,
pub fn asBufferUsageFlags(self: Usage) vk.BufferUsageFlags {
return switch (self) {
@@ -22,6 +22,7 @@ pub const Usage = enum {
.index => .{ .transfer_dst_bit = true, .index_buffer_bit = true },
.vertex => .{ .transfer_dst_bit = true, .vertex_buffer_bit = true },
.indirect => .{ .transfer_dst_bit = true, .indirect_buffer_bit = true },
.staging => .{ .transfer_src_bit = true },
};
}
};
@@ -29,34 +30,27 @@ pub const Usage = enum {
pub const InitInfo = struct {
usage: Usage,
target_queue: TargetQueue,
array_capacity: u32 = 0,
mappable: bool = false,
capacity: u32 = 1,
name: ?[]const u8 = null,
};
pub fn GenericBuffer(comptime Header: type, comptime Element: type) type {
pub fn GenericBuffer(comptime T: type) type {
return struct {
const Self = @This();
buffer: vk.Buffer,
device_memory: vk.DeviceMemory,
mappable: bool,
target_queue: TargetQueue,
array_capacity: u32,
capacity: u32,
const header_size: u32 = @sizeOf(Header);
const element_size: u32 = @sizeOf(Element);
const array_offset: u32 = std.mem.alignForward(usize, header_size, @alignOf(Element));
pub const WriteInfo = struct {
header: ?Header = null,
element_offset: u32 = 0,
elements: []const Element = &.{},
};
const element_size: u32 = @sizeOf(T);
pub fn init(init_info: InitInfo) !Self {
const engine = ctx.engine;
const array_size = try std.math.mul(u32, init_info.array_capacity, element_size);
const size = try std.math.add(u32, array_offset, array_size);
const size = try std.math.mul(u32, init_info.capacity, element_size);
const target_queue_family = switch (init_info.target_queue) {
.graphics => engine.graphics_queue.allocation.family,
@@ -71,14 +65,17 @@ pub fn GenericBuffer(comptime Header: type, comptime Element: type) type {
});
errdefer engine.destroyBuffer(buffer);
if (init_info.name) |name| {
engine.setObjectName(buffer, "B {s} [{s}, {s}]", .{ name, @typeName(Header), @typeName(Element) });
engine.setObjectName(buffer, "B {s} [{s}]", .{ name, @typeName(T) });
}
const memory_requirements = engine.getBufferMemoryRequirements(buffer);
const device_memory = try engine.allocate(memory_requirements, .{ .device_local_bit = true });
const device_memory = try engine.allocate(memory_requirements, .{
.device_local_bit = !init_info.mappable,
.host_visible_bit = init_info.mappable,
});
errdefer engine.freeMemory(device_memory);
if (init_info.name) |name| {
engine.setObjectName(device_memory, "DM {s} [{s}, {s}]", .{ name, @typeName(Header), @typeName(Element) });
engine.setObjectName(device_memory, "DM {s} [{s}]", .{ name, @typeName(T) });
}
try engine.bindBufferMemory(buffer, device_memory, 0);
@@ -86,8 +83,9 @@ pub fn GenericBuffer(comptime Header: type, comptime Element: type) type {
return .{
.buffer = buffer,
.device_memory = device_memory,
.mappable = init_info.mappable,
.target_queue = init_info.target_queue,
.array_capacity = init_info.array_capacity,
.capacity = init_info.capacity,
};
}
@@ -102,120 +100,130 @@ pub fn GenericBuffer(comptime Header: type, comptime Element: type) type {
self.* = undefined;
}
pub fn write(self: Self, write_info: WriteInfo) !void {
const engine = ctx.engine;
const element_count = std.math.cast(u32, write_info.elements.len) orelse return error.Overflow;
std.debug.assert(write_info.element_offset + element_count <= self.array_capacity);
const header_write_size: u32 = if (write_info.header != null) header_size else 0;
const array_write_size = element_count * element_size;
const array_write_offset: u32 = if (header_write_size > 0) array_offset else 0;
const write_size = array_write_offset + array_write_size;
if (write_size == 0) {
return;
}
var regions_buffer: [2]vk.BufferCopy = undefined;
var regions: std.ArrayList(vk.BufferCopy) = .initBuffer(&regions_buffer);
// One continuous write
if (header_write_size > 0 and array_write_size > 0 and write_info.element_offset == 0) {
regions.appendAssumeCapacity(.{
.src_offset = 0,
.dst_offset = 0,
.size = write_size,
});
}
// Separate writes for header and array (if they exist)
else {
if (header_write_size > 0) {
regions.appendAssumeCapacity(.{
.src_offset = 0,
.dst_offset = 0,
.size = header_write_size,
});
}
if (array_write_size > 0) {
regions.appendAssumeCapacity(.{
.src_offset = array_write_offset,
.dst_offset = array_offset + write_info.element_offset * element_size,
.size = array_write_size,
});
}
std.debug.assert(regions.items.len > 0);
}
var staging_buffer: StagingBuffer = try .init(.{
.target_queue = self.target_queue,
.capacity = write_size,
});
defer staging_buffer.deinit();
const staging_memory = try staging_buffer.map();
if (write_info.header) |header| {
@memcpy(staging_memory[0..header_size], std.mem.asBytes(&header));
}
@memcpy(staging_memory[array_write_offset..write_size], std.mem.sliceAsBytes(write_info.elements));
staging_buffer.unmap();
var command_buffer: CommandBuffer = try .init(.transfer);
defer command_buffer.deinit();
try command_buffer.beginCommandBuffer();
command_buffer.copyBuffer(staging_buffer.buffer, self.buffer, regions.items);
try command_buffer.endCommandBuffer();
const fence = try engine.createFence(.{});
defer engine.destroyFence(fence);
try command_buffer.submit(.{ .fence = fence });
try engine.waitForFence(fence);
pub fn write(self: Self, value: T) !void {
return self.writeSliceAt(0, &.{value});
}
pub fn writeRaw(self: Self, data_offset: u32, data: []const u8) !void {
pub fn writeAt(self: Self, index: u32, value: T) !void {
return self.writeSliceAt(index, &.{value});
}
pub fn writeSlice(self: Self, values: []const T) !void {
return self.writeSliceAt(0, values);
}
pub fn writeSliceAt(self: Self, first_index: u32, values: []const T) !void {
const engine = ctx.engine;
const array_size = self.array_capacity * element_size;
const size = array_offset + array_size;
const element_count = std.math.cast(u32, values.len) orelse return error.Overflow;
std.debug.assert(first_index + element_count <= self.capacity);
const data_size = std.math.cast(u32, data.len) orelse return error.Overflow;
std.debug.assert(data_offset + data_size <= size);
const write_size = element_count * element_size;
if (element_count == 0) return;
var staging_buffer = try StagingBuffer.init(engine, .{
.target_queue = self.target_queue,
.capacity = data_size,
});
defer staging_buffer.deinit(engine);
if (self.mappable) {
const slice = try self.mapSliceAt(first_index, element_count);
defer self.unmap();
const staging_memory = try staging_buffer.map(engine);
@memcpy(staging_memory, data);
staging_buffer.unmap(engine);
@memcpy(slice, values);
try self.flush(first_index, element_count);
} else {
var staging_buffer: GenericBuffer(T) = try .init(.{
.usage = .staging,
.target_queue = self.target_queue,
.mappable = true,
.capacity = element_count,
});
defer staging_buffer.deinit();
var command_buffer = try CommandBuffer.init(engine, .transfer);
defer command_buffer.deinit(engine);
{
const staging_slice = try staging_buffer.mapSlice();
defer staging_buffer.unmap();
try command_buffer.beginCommandBuffer(.{});
command_buffer.copyBuffer(
staging_buffer.buffer,
self.buffer,
&.{
@memcpy(staging_slice, values);
try staging_buffer.flush(0, element_count);
}
var command_buffer: CommandBuffer = try .init(.transfer);
defer command_buffer.deinit();
try command_buffer.beginCommandBuffer();
const regions = [_]vk.BufferCopy{
.{
.src_offset = 0,
.dst_offset = data_offset,
.size = data_size,
.dst_offset = first_index * element_size,
.size = write_size,
},
};
command_buffer.copyBuffer(staging_buffer.buffer, self.buffer, &regions);
try command_buffer.endCommandBuffer();
const fence = try engine.createFence(.{});
defer engine.destroyFence(fence);
try command_buffer.submit(.{ .fence = fence });
try engine.waitForFence(fence);
}
}
pub fn map(self: Self) !*T {
std.debug.assert(self.mappable);
const slice = try self.mapSliceAt(0, 1);
return &slice[0];
}
pub fn mapAt(self: Self, index: u32) !*T {
std.debug.assert(self.mappable);
const slice = try self.mapSliceAt(index, 1);
return &slice[0];
}
pub fn mapSlice(self: Self) ![]T {
std.debug.assert(self.mappable);
return self.mapSliceAt(0, self.capacity);
}
pub fn mapSliceAt(self: Self, first_index: u32, count: u32) ![]T {
const engine = ctx.engine;
std.debug.assert(self.mappable);
std.debug.assert(first_index + count <= self.capacity);
const byte_offset = @as(u64, first_index) * @as(u64, element_size);
const byte_length = @as(u64, count) * @as(u64, element_size);
const mapped_memory = try engine.mapMemory(self.device_memory, byte_offset, byte_length, .{});
const slice = @as([*]T, @ptrCast(@alignCast(mapped_memory.ptr)))[0..count];
return slice;
}
pub fn flush(self: Self, first_index: u32, count: u32) !void {
const engine = ctx.engine;
std.debug.assert(self.mappable);
std.debug.assert(first_index + count <= self.capacity);
const byte_offset = @as(u64, first_index) * @as(u64, element_size);
const byte_length = @as(u64, count) * @as(u64, element_size);
try engine.device.flushMappedMemoryRanges(&.{
.{
.memory = self.device_memory,
.offset = byte_offset,
.size = byte_length,
},
);
try command_buffer.endCommandBuffer();
});
}
const fence = try engine.createFence(.{});
defer engine.destroyFence(fence);
pub fn unmap(self: Self) void {
const engine = ctx.engine;
try command_buffer.submit(engine, .{ .fence = fence });
try engine.waitForFence(fence);
std.debug.assert(self.mappable);
engine.unmapMemory(self.device_memory);
}
};
}

View File

@@ -8,6 +8,7 @@ const vm = @import("vecmath");
const CommandBuffer = @import("CommandBuffer.zig");
const Engine = @import("Engine.zig");
const Fonts = @import("Fonts.zig");
const GenericBuffer = @import("GenericBuffer.zig").GenericBuffer;
const Swapchain = @import("Swapchain.zig");
const Texture = @import("Texture.zig");
@@ -25,19 +26,18 @@ pub const Draw = struct {
pub const Text = extern struct {
color: vm.Vector4,
pos_min: vm.Vector2,
pos_max: vm.Vector2,
uv_min: vm.Vector2,
uv_max: vm.Vector2,
atlas_id: Textures.Id,
bottom_left_sspx: vm.Vector2,
font_size_px: f32,
glyph_id: u16,
font_id: Fonts.Id,
};
pub const Image = extern struct {
tint: vm.Vector4,
tint: vm.Vector4 = .initScalar(1),
position_sspx: vm.Vector2,
size_px: vm.Vector2,
uv_min: vm.Vector2,
uv_max: vm.Vector2,
uv_min: vm.Vector2 = .initScalar(0),
uv_max: vm.Vector2 = .initScalar(1),
texture_id: Textures.Id,
};
};
@@ -48,9 +48,9 @@ pub const Batch = struct {
instance_count: u32,
};
box_gpu_buffer: GenericBuffer(void, Draw.Box),
text_gpu_buffer: GenericBuffer(void, Draw.Text),
image_gpu_buffer: GenericBuffer(void, Draw.Image),
box_gpu_buffer: GenericBuffer(Draw.Box),
text_gpu_buffer: GenericBuffer(Draw.Text),
image_gpu_buffer: GenericBuffer(Draw.Image),
box_cpu_buffer: std.ArrayList(Draw.Box),
text_cpu_buffer: std.ArrayList(Draw.Text),
@@ -65,8 +65,18 @@ box_descriptor_set_layout: vk.DescriptorSetLayout,
box_pipeline_layout: vk.PipelineLayout,
box_pipeline: vk.Pipeline,
text_descriptor_set_layout: vk.DescriptorSetLayout,
text_pipeline_layout: vk.PipelineLayout,
text_pipeline: vk.Pipeline,
image_descriptor_set_layout: vk.DescriptorSetLayout,
image_pipeline_layout: vk.PipelineLayout,
image_pipeline: vk.Pipeline,
descriptor_pool: vk.DescriptorPool,
box_descriptor_set: vk.DescriptorSet,
text_descriptor_set: vk.DescriptorSet,
image_descriptor_set: vk.DescriptorSet,
pub const max_box_draws = 1024;
pub const max_image_draws = 1024;
@@ -81,26 +91,26 @@ pub fn init() !*Gui {
const gui = try allocator_persistent.create(Gui);
var box_gpu_buffer: GenericBuffer(void, Draw.Box) = try .init(.{
var box_gpu_buffer: GenericBuffer(Draw.Box) = try .init(.{
.usage = .storage,
.target_queue = .graphics,
.array_capacity = max_box_draws,
.capacity = max_box_draws,
.name = "GUI box draws",
});
errdefer box_gpu_buffer.deinit();
var text_gpu_buffer: GenericBuffer(void, Draw.Text) = try .init(.{
var text_gpu_buffer: GenericBuffer(Draw.Text) = try .init(.{
.usage = .storage,
.target_queue = .graphics,
.array_capacity = max_text_draws,
.capacity = max_text_draws,
.name = "GUI text draws",
});
errdefer text_gpu_buffer.deinit();
var image_gpu_buffer: GenericBuffer(void, Draw.Image) = try .init(.{
var image_gpu_buffer: GenericBuffer(Draw.Image) = try .init(.{
.usage = .storage,
.target_queue = .graphics,
.array_capacity = max_image_draws,
.capacity = max_image_draws,
.name = "GUI image draws",
});
errdefer image_gpu_buffer.deinit();
@@ -108,15 +118,24 @@ pub fn init() !*Gui {
const box_cpu_buffer: std.ArrayList(Draw.Box) = try .initCapacity(allocator_persistent, max_box_draws);
const text_cpu_buffer: std.ArrayList(Draw.Text) = try .initCapacity(allocator_persistent, max_text_draws);
const image_cpu_buffer: std.ArrayList(Draw.Image) = try .initCapacity(allocator_persistent, max_image_draws);
const batches: std.ArrayList(Batch) = try .initCapacity(allocator_persistent, max_batches);
var index_buffer = try shaders.IndexBuffer.init(.{
.usage = .index,
.target_queue = .graphics,
.capacity = 6,
.name = "QuadIB",
});
errdefer index_buffer.deinit();
try index_buffer.writeSlice(&.{ 0, 1, 2, 2, 1, 3 });
const sampler = try engine.createSampler(.{
.mag_filter = .linear,
.min_filter = .linear,
.mipmap_mode = .linear,
.address_mode_u = .repeat,
.address_mode_v = .repeat,
.address_mode_w = .repeat,
.address_mode_u = .clamp_to_edge,
.address_mode_v = .clamp_to_edge,
.address_mode_w = .clamp_to_edge,
.mip_lod_bias = 0,
.anisotropy_enable = .false,
.max_anisotropy = 0,
@@ -160,17 +179,6 @@ pub fn init() !*Gui {
defer engine.destroyShaderModule(box_shader);
engine.setObjectName(box_shader, "SM gui_box", .{});
var index_buffer = try shaders.IndexBuffer.init(.{
.usage = .index,
.target_queue = .graphics,
.array_capacity = 6,
.name = "QuadIB",
});
errdefer index_buffer.deinit();
try index_buffer.write(.{
.elements = &.{ 0, 1, 2, 2, 1, 3 },
});
const box_pipeline = try engine.createGraphicsPipeline(.{
.stages = &.{
.{
@@ -273,16 +281,325 @@ pub fn init() !*Gui {
errdefer engine.destroyPipeline(box_pipeline);
engine.setObjectName(box_pipeline, "P GUI Box", .{});
const text_descriptor_set_layout = try engine.createDescriptorSetLayout(.{
.bindings = &.{
.{
.binding = 0,
.descriptor_type = .uniform_buffer,
.descriptor_count = 1,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
},
.{
.binding = 1,
.descriptor_type = .storage_buffer,
.descriptor_count = 1,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
},
.{
.binding = 2,
.descriptor_type = .storage_buffer,
.descriptor_count = Fonts.max_fonts,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
},
.{
.binding = 3,
.descriptor_type = .storage_buffer,
.descriptor_count = Fonts.max_fonts,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
},
.{
.binding = 4,
.descriptor_type = .storage_buffer,
.descriptor_count = Fonts.max_fonts,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
},
},
});
errdefer engine.destroyDescriptorSetLayout(text_descriptor_set_layout);
engine.setObjectName(text_descriptor_set_layout, "DSL GUI Text", .{});
const text_pipeline_layout = try engine.createPipelineLayout(.{
.set_layouts = &.{
text_descriptor_set_layout,
},
});
errdefer engine.destroyPipelineLayout(text_pipeline_layout);
engine.setObjectName(text_pipeline_layout, "PL GUI Text", .{});
const text_shader = try engine.createShaderModule(.{ .code = &shaders.gui_text_spv });
defer engine.destroyShaderModule(text_shader);
engine.setObjectName(text_shader, "SM gui_text", .{});
const text_pipeline = try engine.createGraphicsPipeline(.{
.stages = &.{
.{
.stage = .{ .vertex_bit = true },
.module = text_shader,
.name = "vertexMain",
},
.{
.stage = .{ .fragment_bit = true },
.module = text_shader,
.name = "fragmentMain",
},
},
.vertex_input_state = .{},
.input_assembly_state = .{
.topology = .triangle_list,
.primitive_restart_enable = .false,
},
.viewport_state = .{
.viewports = &.{undefined}, // dynamic
.scissors = &.{undefined}, // dynamic
},
.rasterization_state = .{
.depth_clamp_enable = .false,
.rasterizer_discard_enable = .false,
.polygon_mode = .fill,
.cull_mode = .{},
.front_face = .counter_clockwise,
.depth_bias_enable = .false,
.depth_bias_constant_factor = 0,
.depth_bias_clamp = 0,
.depth_bias_slope_factor = 0,
.line_width = 1,
},
.multisample_state = .{
.rasterization_samples = .{ .@"1_bit" = true },
.sample_shading_enable = .false,
.min_sample_shading = 1,
.alpha_to_coverage_enable = .false,
.alpha_to_one_enable = .false,
},
.depth_stencil_state = .{
.depth_test_enable = .false,
.depth_write_enable = .false,
.depth_compare_op = .always,
.depth_bounds_test_enable = .false,
.stencil_test_enable = .false,
.front = .{
.fail_op = .keep,
.pass_op = .keep,
.depth_fail_op = .keep,
.compare_op = .never,
.compare_mask = 0,
.write_mask = 0,
.reference = 0,
},
.back = .{
.fail_op = .keep,
.pass_op = .keep,
.depth_fail_op = .keep,
.compare_op = .never,
.compare_mask = 0,
.write_mask = 0,
.reference = 0,
},
.min_depth_bounds = 0,
.max_depth_bounds = 1,
},
.color_blend_state = .{
.logic_op_enable = .false,
.logic_op = .copy,
.attachments = &.{
.{
.blend_enable = .true,
.src_color_blend_factor = .one,
.dst_color_blend_factor = .one_minus_src_alpha,
.color_blend_op = .add,
.src_alpha_blend_factor = .one,
.dst_alpha_blend_factor = .one_minus_src_alpha,
.alpha_blend_op = .add,
.color_write_mask = .{
.r_bit = true,
.g_bit = true,
.b_bit = true,
.a_bit = true,
},
},
},
.blend_constants = .{ 0, 0, 0, 0 },
},
.dynamic_state = .{
.dynamic_states = &.{ .viewport, .scissor },
},
.layout = text_pipeline_layout,
.view_mask = 0,
.color_attachment_formats = &.{swapchain.params.surface_format.format},
.depth_attachment_format = Texture.Usage.depth.vkFormat(),
.stencil_attachment_format = .undefined,
});
errdefer engine.destroyPipeline(text_pipeline);
engine.setObjectName(text_pipeline, "P GUI Box", .{});
const image_descriptor_set_layout = try engine.createDescriptorSetLayout(.{
.bindings = &.{
.{
.binding = 0,
.descriptor_type = .uniform_buffer,
.descriptor_count = 1,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
},
.{
.binding = 1,
.descriptor_type = .storage_buffer,
.descriptor_count = 1,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
},
.{
.binding = 2,
.descriptor_type = .sampler,
.descriptor_count = 1,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
.immutable_samplers = &.{sampler},
},
.{
.binding = 3,
.descriptor_type = .sampled_image,
.descriptor_count = Textures.max_textures,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
},
},
});
errdefer engine.destroyDescriptorSetLayout(image_descriptor_set_layout);
engine.setObjectName(box_descriptor_set_layout, "DSL GUI Image", .{});
const image_pipeline_layout = try engine.createPipelineLayout(.{
.set_layouts = &.{
image_descriptor_set_layout,
},
});
errdefer engine.destroyPipelineLayout(image_pipeline_layout);
engine.setObjectName(box_pipeline_layout, "PL GUI Image", .{});
const image_shader = try engine.createShaderModule(.{ .code = &shaders.gui_image_spv });
defer engine.destroyShaderModule(image_shader);
engine.setObjectName(text_shader, "SM gui_image", .{});
const image_pipeline = try engine.createGraphicsPipeline(.{
.stages = &.{
.{
.stage = .{ .vertex_bit = true },
.module = image_shader,
.name = "vertexMain",
},
.{
.stage = .{ .fragment_bit = true },
.module = image_shader,
.name = "fragmentMain",
},
},
.vertex_input_state = .{},
.input_assembly_state = .{
.topology = .triangle_list,
.primitive_restart_enable = .false,
},
.viewport_state = .{
.viewports = &.{undefined}, // dynamic
.scissors = &.{undefined}, // dynamic
},
.rasterization_state = .{
.depth_clamp_enable = .false,
.rasterizer_discard_enable = .false,
.polygon_mode = .fill,
.cull_mode = .{},
.front_face = .counter_clockwise,
.depth_bias_enable = .false,
.depth_bias_constant_factor = 0,
.depth_bias_clamp = 0,
.depth_bias_slope_factor = 0,
.line_width = 1,
},
.multisample_state = .{
.rasterization_samples = .{ .@"1_bit" = true },
.sample_shading_enable = .false,
.min_sample_shading = 1,
.alpha_to_coverage_enable = .false,
.alpha_to_one_enable = .false,
},
.depth_stencil_state = .{
.depth_test_enable = .false,
.depth_write_enable = .false,
.depth_compare_op = .always,
.depth_bounds_test_enable = .false,
.stencil_test_enable = .false,
.front = .{
.fail_op = .keep,
.pass_op = .keep,
.depth_fail_op = .keep,
.compare_op = .never,
.compare_mask = 0,
.write_mask = 0,
.reference = 0,
},
.back = .{
.fail_op = .keep,
.pass_op = .keep,
.depth_fail_op = .keep,
.compare_op = .never,
.compare_mask = 0,
.write_mask = 0,
.reference = 0,
},
.min_depth_bounds = 0,
.max_depth_bounds = 1,
},
.color_blend_state = .{
.logic_op_enable = .false,
.logic_op = .copy,
.attachments = &.{
.{
.blend_enable = .true,
.src_color_blend_factor = .one,
.dst_color_blend_factor = .one_minus_src_alpha,
.color_blend_op = .add,
.src_alpha_blend_factor = .one,
.dst_alpha_blend_factor = .one_minus_src_alpha,
.alpha_blend_op = .add,
.color_write_mask = .{
.r_bit = true,
.g_bit = true,
.b_bit = true,
.a_bit = true,
},
},
},
.blend_constants = .{ 0, 0, 0, 0 },
},
.dynamic_state = .{
.dynamic_states = &.{ .viewport, .scissor },
},
.layout = image_pipeline_layout,
.view_mask = 0,
.color_attachment_formats = &.{swapchain.params.surface_format.format},
.depth_attachment_format = Texture.Usage.depth.vkFormat(),
.stencil_attachment_format = .undefined,
});
errdefer engine.destroyPipeline(image_pipeline);
engine.setObjectName(image_pipeline, "P GUI Image", .{});
const descriptor_pool = try engine.createDescriptorPool(.{
.max_sets = 1,
.max_sets = 3,
.pool_sizes = &.{
.{
.type = .uniform_buffer,
.descriptor_count = 1,
.descriptor_count = 1 + // box
1 + // text
1, // image
},
.{
.type = .storage_buffer,
.descriptor_count = 1,
.descriptor_count = 1 + // box
(1 + 3 * Fonts.max_fonts) + // text
1, // image
},
.{
.type = .sampled_image,
.descriptor_count = Textures.max_textures, // image
},
.{
.type = .sampler,
.descriptor_count = 1, // image
},
},
});
@@ -295,6 +612,18 @@ pub fn init() !*Gui {
});
engine.setObjectName(box_descriptor_set, "DS GUI Box", .{});
const text_descriptor_set = try engine.allocateDescriptorSet(.{
.descriptor_pool = descriptor_pool,
.set_layout = text_descriptor_set_layout,
});
engine.setObjectName(text_descriptor_set, "DS GUI Text", .{});
const image_descriptor_set = try engine.allocateDescriptorSet(.{
.descriptor_pool = descriptor_pool,
.set_layout = image_descriptor_set_layout,
});
engine.setObjectName(image_descriptor_set, "DS GUI Image", .{});
try engine.updateDescriptorSets(.{
.writes = &.{
.{
@@ -312,6 +641,36 @@ pub fn init() !*Gui {
},
},
},
.{
.dst_set = text_descriptor_set,
.dst_binding = 1,
.dst_array_element = 0,
.descriptor_type = .storage_buffer,
.descriptor_infos = .{
.buffer = &.{
.{
.buffer = text_gpu_buffer.buffer,
.offset = 0,
.range = vk.WHOLE_SIZE,
},
},
},
},
.{
.dst_set = image_descriptor_set,
.dst_binding = 1,
.dst_array_element = 0,
.descriptor_type = .storage_buffer,
.descriptor_infos = .{
.buffer = &.{
.{
.buffer = image_gpu_buffer.buffer,
.offset = 0,
.range = vk.WHOLE_SIZE,
},
},
},
},
},
});
@@ -333,8 +692,18 @@ pub fn init() !*Gui {
.box_pipeline_layout = box_pipeline_layout,
.box_pipeline = box_pipeline,
.text_descriptor_set_layout = text_descriptor_set_layout,
.text_pipeline_layout = text_pipeline_layout,
.text_pipeline = text_pipeline,
.image_descriptor_set_layout = image_descriptor_set_layout,
.image_pipeline_layout = image_pipeline_layout,
.image_pipeline = image_pipeline,
.descriptor_pool = descriptor_pool,
.box_descriptor_set = box_descriptor_set,
.text_descriptor_set = text_descriptor_set,
.image_descriptor_set = image_descriptor_set,
};
return gui;
@@ -346,9 +715,15 @@ pub fn deinit(self: *Gui) void {
std.log.scoped(.deinit).debug("Deinitializing {*}", .{self});
engine.destroyDescriptorPool(self.descriptor_pool);
engine.destroyPipeline(self.image_pipeline);
engine.destroyPipeline(self.text_pipeline);
engine.destroyPipeline(self.box_pipeline);
self.index_buffer.deinit();
engine.destroyPipelineLayout(self.image_pipeline_layout);
engine.destroyPipelineLayout(self.text_pipeline_layout);
engine.destroyPipelineLayout(self.box_pipeline_layout);
engine.destroyDescriptorSetLayout(self.image_descriptor_set_layout);
engine.destroyDescriptorSetLayout(self.text_descriptor_set_layout);
engine.destroyDescriptorSetLayout(self.box_descriptor_set_layout);
engine.destroySampler(self.sampler);
@@ -379,6 +754,69 @@ pub fn pushText(self: *Gui, text: Draw.Text) void {
self.extendOrAddBatch(.Text, instance);
}
pub const PrintLineArgs = struct {
position_sspx: vm.Vector2,
font_id: Fonts.Id,
cap_height_px: f32,
color: vm.Vector4,
};
pub fn printLine(self: *Gui, comptime fmt: []const u8, fmt_args: anytype, print_args: PrintLineArgs) void {
const allocator_frame = ctx.allocator_frame;
const fonts = ctx.fonts;
const text = std.fmt.allocPrint(allocator_frame, fmt, fmt_args) catch |err| {
std.log.err("Failed formatting of string \"{s}\": {}", .{ fmt, err });
return;
};
const font = fonts.array.items[@intFromEnum(print_args.font_id)];
const font_size_px = print_args.cap_height_px / font.cap_height_em;
const utf8_view = std.unicode.Utf8View.init(text) catch {
std.log.err("Invalid UTF-8 sequence in GUI print line", .{});
return;
};
var cursor_sspx = print_args.position_sspx;
var maybe_prev_glyph: ?u16 = null;
var it = utf8_view.iterator();
while (it.nextCodepoint()) |codepoint| {
const glyph_id = font.codepoint_glyph_id.get(codepoint) orelse {
std.log.warn("Missing codepoint 0x{X} '{u}' in font #{d}", .{ codepoint, codepoint, print_args.font_id });
maybe_prev_glyph = null;
continue;
};
defer maybe_prev_glyph = glyph_id;
const glyph_data = &font.cpu_glyph_data[glyph_id];
var cursor_offset_em = glyph_data.bottom_left_em;
cursor_offset_em.y = -cursor_offset_em.y;
if (maybe_prev_glyph == null) {
cursor_offset_em.x += glyph_data.left_side_bearing_em;
}
if (maybe_prev_glyph) |prev_glyph| {
const kerning_em = font.kerning_em.get(.{ prev_glyph, glyph_id }) orelse 0;
cursor_sspx.x += kerning_em;
}
if (glyph_data.gpu_glyph_id) |gpu_glyph_id| {
self.pushText(.{
.color = print_args.color,
.bottom_left_sspx = cursor_sspx.add(cursor_offset_em.mulScalar(font_size_px)),
.font_size_px = font_size_px,
.glyph_id = gpu_glyph_id,
.font_id = print_args.font_id,
});
}
const advance_px = font_size_px * glyph_data.advance_em;
cursor_sspx.x += advance_px;
}
}
pub fn pushImage(self: *Gui, image: Draw.Image) void {
const instance: u32 = @intCast(self.image_cpu_buffer.items.len);
self.image_cpu_buffer.appendBounded(image) catch return;
@@ -402,9 +840,9 @@ fn extendOrAddBatch(self: *Gui, draw_type: std.meta.DeclEnum(Draw), instance: u3
pub fn draw(self: *const Gui, command_buffer: CommandBuffer) !void {
command_buffer.bindIndexBuffer(self.index_buffer.buffer, 0, .uint16);
try self.box_gpu_buffer.write(.{ .elements = self.box_cpu_buffer.items });
try self.text_gpu_buffer.write(.{ .elements = self.text_cpu_buffer.items });
try self.image_gpu_buffer.write(.{ .elements = self.image_cpu_buffer.items });
try self.box_gpu_buffer.writeSlice(self.box_cpu_buffer.items);
try self.text_gpu_buffer.writeSlice(self.text_cpu_buffer.items);
try self.image_gpu_buffer.writeSlice(self.image_cpu_buffer.items);
for (self.batches.items) |batch| {
switch (batch.draw_type) {
@@ -413,12 +851,12 @@ pub fn draw(self: *const Gui, command_buffer: CommandBuffer) !void {
command_buffer.bindDescriptorSet(.graphics, self.box_pipeline_layout, 0, self.box_descriptor_set, null);
},
.Text => {
std.log.warn("GUI Text not implemented; skipping batch", .{});
continue;
command_buffer.bindPipeline(.graphics, self.text_pipeline);
command_buffer.bindDescriptorSet(.graphics, self.text_pipeline_layout, 0, self.text_descriptor_set, null);
},
.Image => {
std.log.warn("GUI Image not implemented; skipping batch", .{});
continue;
command_buffer.bindPipeline(.graphics, self.image_pipeline);
command_buffer.bindDescriptorSet(.graphics, self.image_pipeline_layout, 0, self.image_descriptor_set, null);
},
}

View File

@@ -60,11 +60,14 @@ pub const Key = struct {
/// Maps a key value to a material ID. Preallocated with `allocator_persistent`.
map: std.AutoHashMapUnmanaged(Key, Id),
/// Stores all material data and maps a material ID to its data. Preallocated
/// with `allocator_persistent`.
///
/// Stores the same data as `material_buffer`, but is available on the CPU.
array: std.ArrayList(shaders.Material),
/// Stores all material data in a single contiguous storage buffer. Use the
/// material ID as an index into this buffer.
material_buffer: shaders.MaterialBuffer,
/// The amount of materials currently loaded.
material_count: usize,
/// With `@sizeOf(Material) == 52` and `max_materials == 4096`, the material
/// storage buffer should take 208 kiB in VRAM.
@@ -77,43 +80,43 @@ pub fn init() !*Materials {
materials.* = .{
.map = .empty,
.array = .empty,
.material_buffer = undefined,
.material_count = 0,
};
try materials.map.ensureTotalCapacity(allocator_persistent, max_materials);
try materials.array.ensureTotalCapacityPrecise(allocator_persistent, max_materials);
materials.material_buffer = try .init(.{
.usage = .storage,
.target_queue = .graphics,
.array_capacity = max_materials,
.capacity = max_materials,
.name = "Materials",
});
errdefer materials.material_buffer.deinit();
// VOLATILE Synchronize with explicit values on top of `Id` type.
try materials.material_buffer.write(.{
.element_offset = Id.empty.toInt(),
.elements = &.{
.{
.base_color = .zero,
.emissive = .init(1, 0, 1),
.ior = 1.45,
.metallic = 0,
.normal_scale = 1,
.occlusion_texture_strength = 1,
.roughness = 1,
.base_color_texture = .empty_base_color,
.emissive_texture = .empty_emissive,
.normal_texture = .empty_normal,
.occlusion_roughness_metallic_texture = .empty_occlusion_roughness_metallic,
},
const material_data = [_]shaders.Material{
.{
.base_color = .zero,
.emissive = .init(1, 0, 1),
.ior = 1.45,
.metallic = 0,
.normal_scale = 1,
.occlusion_texture_strength = 1,
.roughness = 1,
.base_color_texture = .empty_base_color,
.emissive_texture = .empty_emissive,
.normal_texture = .empty_normal,
.occlusion_roughness_metallic_texture = .empty_occlusion_roughness_metallic,
},
});
materials.material_count += 1;
};
std.debug.assert(materials.material_count == @typeInfo(Id).@"enum".fields.len);
materials.array.appendSliceAssumeCapacity(&material_data);
try materials.material_buffer.writeSlice(&material_data);
std.debug.assert(materials.array.items.len == @typeInfo(Id).@"enum".fields.len);
return materials;
}
@@ -176,13 +179,13 @@ pub fn getOrLoad(self: *Materials, maybe_filename: ?[]const u8) !Id {
if (self.map.get(key)) |id| {
return id;
} else {
const id = Id.fromIndexSafe(self.material_count) catch |err| switch (err) {
const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) {
error.Overflow => return error.OutOfMaterials,
};
try self.loadMaterial(filename, id.toInt());
const material = try self.loadMaterial(filename, id.toInt());
self.map.putAssumeCapacityNoClobber(key, id);
self.material_count += 1;
self.array.appendAssumeCapacity(material);
return id;
}
@@ -214,13 +217,13 @@ pub fn getOrLoadAtom(self: *Materials, filename: Atom) !Id {
if (self.map.get(key)) |id| {
return id;
} else {
const id = Id.fromIndexSafe(self.material_count) catch |err| switch (err) {
const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) {
error.Overflow => return error.OutOfMaterials,
};
try self.loadMaterial(filename.toString(), id.toInt());
const material = try self.loadMaterial(filename.toString(), id.toInt());
self.map.putAssumeCapacityNoClobber(key, id);
self.material_count += 1;
self.array.appendAssumeCapacity(material);
return id;
}
@@ -266,7 +269,7 @@ pub fn loadAll(self: *Materials) void {
}
}
fn loadMaterial(self: *Materials, filename: []const u8, index: u32) !void {
fn loadMaterial(self: *Materials, filename: []const u8, index: u32) !shaders.Material {
const allocator_frame = ctx.allocator_frame;
const io = ctx.io;
const textures = ctx.textures;
@@ -307,22 +310,20 @@ fn loadMaterial(self: *Materials, filename: []const u8, index: u32) !void {
.allocate = .alloc_if_needed,
});
try self.material_buffer.write(.{
.element_offset = index,
.elements = &.{
.{
.base_color = .initArray(material_json.baseColor),
.emissive = .initArray(material_json.emissive),
.ior = material_json.ior,
.metallic = material_json.metallic,
.normal_scale = material_json.normalScale,
.occlusion_texture_strength = material_json.occlusionTextureStrength,
.roughness = material_json.roughness,
.base_color_texture = try textures.getOrLoad(material_json.baseColorTexture, .base_color),
.emissive_texture = try textures.getOrLoad(material_json.emissiveTexture, .emissive),
.normal_texture = try textures.getOrLoad(material_json.normalTexture, .normal),
.occlusion_roughness_metallic_texture = try textures.getOrLoad(material_json.occlusionRoughnessMetallicTexture, .occlusion_roughness_metallic),
},
},
});
const material_data: shaders.Material = .{
.base_color = .initArray(material_json.baseColor),
.emissive = .initArray(material_json.emissive),
.ior = material_json.ior,
.metallic = material_json.metallic,
.normal_scale = material_json.normalScale,
.occlusion_texture_strength = material_json.occlusionTextureStrength,
.roughness = material_json.roughness,
.base_color_texture = try textures.getOrLoad(material_json.baseColorTexture, .base_color),
.emissive_texture = try textures.getOrLoad(material_json.emissiveTexture, .emissive),
.normal_texture = try textures.getOrLoad(material_json.normalTexture, .normal),
.occlusion_roughness_metallic_texture = try textures.getOrLoad(material_json.occlusionRoughnessMetallicTexture, .occlusion_roughness_metallic),
};
try self.material_buffer.writeAt(index, material_data);
return material_data;
}

View File

@@ -10,7 +10,6 @@ const vm = @import("vecmath");
const CommandBuffer = @import("CommandBuffer.zig");
const Engine = @import("Engine.zig");
const GenericBuffer = @import("GenericBuffer.zig").GenericBuffer;
const StagingBuffer = @import("StagingBuffer.zig");
const Swapchain = @import("Swapchain.zig");
const Texture = @import("Texture.zig");
@@ -18,7 +17,7 @@ image: vk.Image,
image_view: vk.ImageView,
device_memory: vk.DeviceMemory,
vertex_buffer: GenericBuffer(void, vm.Vector3),
vertex_buffer: GenericBuffer(vm.Vector3),
index_buffer: shaders.IndexBuffer,
sampler: vk.Sampler,
@@ -69,15 +68,21 @@ pub fn load(
// --- LOAD IMAGE INTO STAGING BUFFER --------------------------------------
var staging_buffer = try StagingBuffer.init(.{
.capacity = @intCast(img.width * img.height * @sizeOf(vm.ColorHdr)),
var staging_buffer = try GenericBuffer(vm.ColorHdr).init(.{
.usage = .staging,
.target_queue = .compute,
.mappable = true,
.capacity = img.width * img.height,
});
defer staging_buffer.deinit();
const staging_memory = try staging_buffer.map();
@memcpy(staging_memory, @as([*]const u8, @ptrCast(img.data)));
staging_buffer.unmap();
{
const staging_memory = try staging_buffer.mapSlice();
defer staging_buffer.unmap();
@memcpy(staging_memory, img.data);
try staging_buffer.flush(0, img.width * img.height);
}
// --- CREATE EQUIRECTANGULAR IMAGE ----------------------------------------
@@ -495,10 +500,10 @@ pub fn load(
// --- SKYBOX PIPELINE -----------------------------------------------------
var vertex_buffer = try GenericBuffer(void, vm.Vector3).init(.{
var vertex_buffer = try GenericBuffer(vm.Vector3).init(.{
.usage = .vertex,
.target_queue = .graphics,
.array_capacity = 8,
.capacity = 8,
});
errdefer vertex_buffer.deinit();
@@ -510,47 +515,43 @@ pub fn load(
// ┃╱ ┃╱ │╱
// 0━━━━1 O────X
try vertex_buffer.write(.{
.elements = &.{
.init(-1, -1, -1),
.init(1, -1, -1),
.init(-1, 1, -1),
.init(1, 1, -1),
.init(-1, -1, 1),
.init(1, -1, 1),
.init(-1, 1, 1),
.init(1, 1, 1),
},
try vertex_buffer.writeSlice(&.{
.init(-1, -1, -1),
.init(1, -1, -1),
.init(-1, 1, -1),
.init(1, 1, -1),
.init(-1, -1, 1),
.init(1, -1, 1),
.init(-1, 1, 1),
.init(1, 1, 1),
});
var index_buffer = try GenericBuffer(void, u16).init(.{
var index_buffer: shaders.IndexBuffer = try .init(.{
.usage = .index,
.target_queue = .graphics,
.array_capacity = 36,
.capacity = 36,
});
errdefer index_buffer.deinit();
try index_buffer.write(.{
.elements = &.{
// Positive X
3, 1, 7,
7, 1, 5,
// Negative X
0, 2, 4,
4, 2, 6,
// Positive Y
2, 3, 6,
6, 3, 7,
// Negative Y
1, 0, 5,
5, 0, 4,
// Positive Z
6, 7, 4,
4, 7, 5,
// Negative Z
0, 1, 2,
2, 1, 3,
},
try index_buffer.writeSlice(&.{
// Positive X
3, 1, 7,
7, 1, 5,
// Negative X
0, 2, 4,
4, 2, 6,
// Positive Y
2, 3, 6,
6, 3, 7,
// Negative Y
1, 0, 5,
5, 0, 4,
// Positive Z
6, 7, 4,
4, 7, 5,
// Negative Z
0, 1, 2,
2, 1, 3,
});
const descriptor_set_layout = try engine.createDescriptorSetLayout(.{

View File

@@ -1,83 +0,0 @@
const StagingBuffer = @This();
const std = @import("std");
const ctx = @import("../AppContext.zig");
const vk = @import("vulkan");
const Engine = @import("Engine.zig");
const TargetQueue = @import("TargetQueue.zig").TargetQueue;
buffer: vk.Buffer,
device_memory: vk.DeviceMemory,
capacity: u32,
pub const InitInfo = struct {
target_queue: TargetQueue,
capacity: u32,
};
pub fn init(init_info: InitInfo) !StagingBuffer {
const engine = ctx.engine;
const target_queue_family = switch (init_info.target_queue) {
.graphics => engine.graphics_queue.allocation.family,
.compute => engine.compute_queue.allocation.family,
};
const transfer_queue_family = engine.transfer_queue.allocation.family;
const buffer = try engine.createBuffer(.{
.size = init_info.capacity,
.usage = .{
.transfer_src_bit = true,
},
.queue_family_indices = &.{ target_queue_family, transfer_queue_family },
});
errdefer engine.destroyBuffer(buffer);
const memory_requirements = engine.getBufferMemoryRequirements(buffer);
const device_memory = try engine.allocate(
memory_requirements,
.{
.host_visible_bit = true,
.host_coherent_bit = true,
},
);
errdefer engine.freeMemory(device_memory);
try engine.bindBufferMemory(buffer, device_memory, 0);
return .{
.buffer = buffer,
.device_memory = device_memory,
.capacity = init_info.capacity,
};
}
pub fn deinit(self: *StagingBuffer) void {
const engine = ctx.engine;
std.log.scoped(.deinit).debug("Deinitializing {*}", .{self});
engine.freeMemory(self.device_memory);
engine.destroyBuffer(self.buffer);
self.* = undefined;
}
pub fn map(self: StagingBuffer) ![]u8 {
const mapped_memory = try self.mapPartial(0, self.capacity);
return mapped_memory;
}
pub fn mapPartial(self: StagingBuffer, offset: u32, len: u32) ![]u8 {
const engine = ctx.engine;
const mapped_memory = try engine.mapMemory(self.device_memory, offset, len, .{});
return mapped_memory;
}
pub fn unmap(self: StagingBuffer) void {
const engine = ctx.engine;
engine.unmapMemory(self.device_memory);
}

View File

@@ -7,7 +7,7 @@ const vk = @import("vulkan");
const CommandBuffer = @import("CommandBuffer.zig");
const DeviceAllocation = @import("DeviceAllocation.zig");
const Engine = @import("Engine.zig");
const StagingBuffer = @import("StagingBuffer.zig");
const GenericBuffer = @import("GenericBuffer.zig").GenericBuffer;
const TargetQueue = @import("TargetQueue.zig").TargetQueue;
pub const Usage = enum {
@@ -207,15 +207,21 @@ pub fn writeRaw(self: Texture, data: []const u8) !void {
std.debug.assert(data.len == byte_length);
std.debug.assert(self.usage != .depth);
var staging_buffer = try StagingBuffer.init(.{
.capacity = @intCast(byte_length),
var staging_buffer = try GenericBuffer(u8).init(.{
.usage = .staging,
.target_queue = self.target_queue,
.mappable = true,
.capacity = @intCast(byte_length),
});
defer staging_buffer.deinit();
const staging_memory = try staging_buffer.map();
@memcpy(staging_memory, data);
staging_buffer.unmap();
{
const staging_memory = try staging_buffer.mapSlice();
defer staging_buffer.unmap();
@memcpy(staging_memory, data);
try staging_buffer.flush(0, byte_length);
}
// --- TRANSITION TO TRANSFER_DST_OPTIMAL AND COPY -----------------

View File

@@ -41,6 +41,9 @@ pub fn main(init: std.process.Init) !void {
ctx.stbi = try ctx.allocator_persistent.create(media.stbi);
ctx.stbi.* = .init(ctx.allocator_general, ctx.io);
defer ctx.stbi.deinit();
ctx.stbtt = try ctx.allocator_persistent.create(media.stbtt);
ctx.stbtt.* = .init(ctx.allocator_general, ctx.io);
defer ctx.stbtt.deinit();
// --- INITIALIZE APP CONTEXT ---
@@ -105,6 +108,12 @@ pub fn main(init: std.process.Init) !void {
ctx.materials = undefined;
}
ctx.fonts = try .init();
defer {
ctx.fonts.deinit();
ctx.fonts = undefined;
}
ctx.gui = try .init();
defer {
ctx.gui.deinit();

View File

@@ -5,13 +5,13 @@ const Materials = @import("engine/Materials.zig");
const GenericBuffer = @import("engine/GenericBuffer.zig").GenericBuffer;
const Textures = @import("engine/Textures.zig");
pub const VertexBuffer = GenericBuffer(void, Vertex);
pub const IndexBuffer = GenericBuffer(void, Index);
pub const GlobalUniformsBuffer = GenericBuffer(GlobalUniforms, void);
pub const PointLightBuffer = GenericBuffer(void, PointLight);
pub const DirectionalLightBuffer = GenericBuffer(void, DirectionalLight);
pub const MaterialBuffer = GenericBuffer(void, Material);
pub const ObjectUniformsBuffer = GenericBuffer(void, ObjectUniforms);
pub const VertexBuffer = GenericBuffer(Vertex);
pub const IndexBuffer = GenericBuffer(Index);
pub const GlobalUniformsBuffer = GenericBuffer(GlobalUniforms);
pub const PointLightBuffer = GenericBuffer(PointLight);
pub const DirectionalLightBuffer = GenericBuffer(DirectionalLight);
pub const MaterialBuffer = GenericBuffer(Material);
pub const ObjectUniformsBuffer = GenericBuffer(ObjectUniforms);
pub const Vertex = extern struct {
positionOS: vm.Vector3,
@@ -44,7 +44,7 @@ pub const Vertex = extern struct {
pub const Index = u16;
pub const GlobalUniforms = extern struct {
matrixWStoVS: vm.Matrix4x4,
matrixWStoVS: [4]vm.Vector3,
matrixVStoCS: vm.Matrix4x4,
matrixSSPXtoCS: vm.Matrix3x2,
@@ -93,13 +93,24 @@ pub const Material = extern struct {
};
pub const ObjectUniforms = extern struct {
matrixOStoWS: vm.Matrix4x4,
matrixOStoWSNormal: vm.Matrix4x4,
// Limited to a composition of translation and rotation
matrixOStoWS: [4]vm.Vector3,
texCoordTransform: vm.Matrix3x2,
material: Materials.Id,
};
pub const GlyphData = extern struct {
bottomLeftEM: vm.Vector2,
sizeEM: vm.Vector2,
verticalBandPtr: u32,
horizontalBandPtr: u32,
verticalBandCount: u32,
horizontalBandCount: u32,
};
pub const equirect_to_cube_spv align(4) = @embedFile("shaders/equirect_to_cube").*;
pub const gui_box_spv align(4) = @embedFile("shaders/gui_box").*;
pub const gui_text_spv align(4) = @embedFile("shaders/gui_text").*;
pub const gui_image_spv align(4) = @embedFile("shaders/gui_image").*;
pub const main_spv align(4) = @embedFile("shaders/main").*;
pub const skybox_spv align(4) = @embedFile("shaders/skybox").*;

View File

@@ -59,6 +59,49 @@ pub const Transform = enum(u3) {
mirror_rotate_cw = 5,
mirror_rotate_ccw = 6,
mirror_rotate_180 = 7,
pub fn getMatrix(self: Transform) vm.Matrix3x2 {
return switch (self) {
// zig fmt: off
.identity => .identity,
.rotate_cw => .init(
0, -1,
1, 0,
0, 1,
),
.rotate_ccw => .init(
0, 1,
-1, 0,
1, 0,
),
.rotate_180 => .init(
-1, 0,
0, -1,
1, 1,
),
.mirror => .init(
-1, 0,
0, 1,
1, 0,
),
.mirror_rotate_cw => .init(
0, -1,
-1, 0,
1, 1,
),
.mirror_rotate_ccw => .init(
0, 1,
1, 0,
0, 0,
),
.mirror_rotate_180 => .init(
0, 1,
-1, 0,
1, 0,
),
// zig fmt: on
};
}
};
// ┌──────────────────── x