From 39c2eb67813715a179a659a18f752f917107eda6 Mon Sep 17 00:00:00 2001 From: copygirl Date: Wed, 27 Mar 2024 22:17:13 +0100 Subject: [PATCH] Add textures to rendered primitives --- build.zig | 11 ++--- build.zig.zon | 4 ++ src/gfx/default.png | Bin 0 -> 576 bytes src/primitives.zig | 99 ++++++++++++++++++++++---------------------- src/renderer.zig | 74 +++++++++++++++++++++++++++++++-- src/shader.wgsl | 13 ++++-- 6 files changed, 138 insertions(+), 63 deletions(-) create mode 100644 src/gfx/default.png diff --git a/build.zig b/build.zig index 1a3091a..7f6fbdf 100644 --- a/build.zig +++ b/build.zig @@ -6,20 +6,17 @@ pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const zmath_pkg = zmath.package(b, target, optimize, .{ - .options = .{ .enable_cross_platform_determinism = true }, - }); + const mach_core_dep = b.dependency("mach_core", .{ .target = target, .optimize = optimize }); + const zigimg_dep = b.dependency("zigimg", .{ .target = target, .optimize = optimize }); + const zmath_pkg = zmath.package(b, target, optimize, .{}); - const mach_core_dep = b.dependency("mach_core", .{ - .target = target, - .optimize = optimize, - }); const app = try mach_core.App.init(b, mach_core_dep.builder, .{ .name = "zig-bloxel-game", .src = "src/main.zig", .target = target, .optimize = optimize, .deps = &.{ + .{ .name = "zigimg", .module = zigimg_dep.module("zigimg") }, .{ .name = "zmath", .module = zmath_pkg.zmath }, }, }); diff --git a/build.zig.zon b/build.zig.zon index 43c92be..539df80 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -14,6 +14,10 @@ .url = "https://pkg.machengine.org/mach-core/6a62bcc90e0d072d632788a6575d77942bd09a19.tar.gz", .hash = "12209d39954fcda0be158461c10f64d14d5c7d097bd6d26785b332d75ffefa7dd7a0", }, + .zigimg = .{ + .url = "https://github.com/zigimg/zigimg/archive/ad6ad042662856f55a4d67499f1c4606c9951031.tar.gz", + .hash = "1220bf6b616ca219f95be1205b12aa8cdb7e09838fcebeae90b48b5ab0a030c5ab45", + }, .zmath = .{ .path = "libs/zig-gamedev/libs/zmath" }, }, } diff --git a/src/gfx/default.png b/src/gfx/default.png new file mode 100644 index 0000000000000000000000000000000000000000..da85e0223ccf8447ddc8255e8386cfb685412648 GIT binary patch literal 576 zcmV-G0>AxEX>4Tx04R}tkv&MmKpe$iQ$^8=gB?UVWT*~e7Zq`=RVYG*P%E_RU~=gfG-*gu zTpR`0f`cE6RRZ=T$6botEPx$ypT)#vvg<_Qpd2CnqBzuEw1KS{5* zwdfHrxD8xfw>4!CxZDATpA6ZQT`5RYC=`JAGy0|+Fmwy_t$Dq*_Hp_EWT>mu4RCM> zj1?(+-RIrCoxS~grq$mM-Dz^sowOe{00009a7bBm001r{001r{0eGc9b^rhX2XskI zMF;2t91AfdpvQ!Y0000PbVXQnLvL+uWo~o;Lvm$dbY)~9cWHEJAV*0}P*;Ht7XSbN zDoI2^R4C75{Qv(y12zzI9;+||69b80R0A=9@$1SRSS7f^+($!+ei{Hr(Fa5+LCFIE O0000 |pixels| { + core.queue.writeTexture(&.{ .texture = texture }, &data_layout, &img_size, pixels); + }, + .rgb24 => |pixels_rgb24| { + const pixels = try rgb24ToRgba32(allocator, pixels_rgb24); + defer allocator.free(pixels); + core.queue.writeTexture(&.{ .texture = texture }, &data_layout, &img_size, pixels); + }, + else => std.debug.panic("Unsupported image color format {s}", .{@tagName(img.pixels)}), + } + + return texture.createView(&.{}); +} + +/// Converts a raw 24-bit RGB pixel buffer to 32-bit RGBA. +fn rgb24ToRgba32(allocator: std.mem.Allocator, in: []Rgb24) ![]Rgba32 { + const out = try allocator.alloc(Rgba32, in.len); + for (in, out) |src, *dest| + dest.* = .{ .r = src.r, .g = src.g, .b = src.b, .a = 255 }; + return out; +} + /// Creates a depth texture. This is used to ensure that when things are /// rendered, an object behind another won't draw over one in front, simply /// because it was rendered at a later point in time. diff --git a/src/shader.wgsl b/src/shader.wgsl index 30463f5..1e06d6a 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -1,15 +1,20 @@ @group(0) @binding(0) var view_proj_matrix: mat4x4; -@group(1) @binding(0) var model_matrix: mat4x4; -@group(1) @binding(1) var model_color: vec3; + +@group(1) @binding(0) var texture_sampler: sampler; +@group(1) @binding(1) var model_texture: texture_2d; +@group(1) @binding(2) var model_matrix: mat4x4; +@group(1) @binding(3) var model_color: vec3; struct VertexInput { @location(0) position: vec3, + @location(1) uv: vec2, }; struct VertexOutput { @builtin(position) position: vec4, @location(0) color: vec4, + @location(1) frag_uv: vec2, }; struct FragmentOutput { @@ -21,11 +26,13 @@ struct FragmentOutput { let mvp = model_matrix * view_proj_matrix; out.position = vec4(in.position, 1.0) * mvp; out.color = vec4(model_color, 1.0); + out.frag_uv = in.uv; return out; } @fragment fn frag_main(in: VertexOutput) -> FragmentOutput { var out: FragmentOutput; - out.pixel_color = in.color; + let texture_color = textureSample(model_texture, texture_sampler, in.frag_uv); + out.pixel_color = texture_color * in.color; return out; }