begin rework of engine for 3d rendering

This commit is contained in:
matthew 2025-10-27 07:02:07 +11:00
parent 35985df611
commit 20caf5ea4d
15 changed files with 271 additions and 98 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -15,7 +15,7 @@
"libs-windows": [], "libs-windows": [],
"preGenerateCommands-linux": ["./build.sh"], "preGenerateCommands-linux": ["./build.sh"],
"preGenerateCommands-windows": [], "preGenerateCommands-windows": [],
"dflags": ["-Xcc=-mno-sse", "-P-I/usr/include/freetype2", "-vgc"], "dflags": ["-Xcc=-mno-sse", "-P-I/usr/include/freetype2", "-vgc", "-Jbuild", "-Jassets/fonts"],
"dflags-dmd": ["-P=-DSTBI_NO_SIMD"] "dflags-dmd": ["-P=-DSTBI_NO_SIMD"]
}, },
{ {

@ -1 +1 @@
Subproject commit 1290c5f45ac49cc5534817e8d6b0adae4ac058af Subproject commit 6021713286482893ca0614c894419290c6f425d4

@ -1 +1 @@
Subproject commit a97309cb04e0e802047586a5bacbacc9b8e9649b Subproject commit c83ffabce69071a3e7a0af3f26aa420082eeda1f

60
src/game2.d Normal file
View File

@ -0,0 +1,60 @@
import dlib;
import vulkan;
struct GameState
{
RenderState rds;
}
struct RenderState
{
Renderer rd;
struct
{
Pipeline pbr;
} pipelines;
struct
{
DescSetLayout globals;
DescSetLayout resources;
} desc_layouts;
struct
{
DescSet pbr;
} desc_sets;
struct
{
PipelineLayout pbr;
} layouts;
}
GameState
InitGame(PlatformWindow* window)
{
GameState g;
Init(&g.rds);
}
void
Init(RendererState* rds, PlatformWindow* window)
{
version(linux)
{
PlatformHandles handles = {
conn: window.conn,
window: window.window,
};
}
rds.rd = InitRenderer(handles, MB(24), MB(32));
DescLayoutBinding[5] bindings = [
{ binding: 0, descriptorType: DT.Image, descriptorCount: 1, stageFlags: SS.All },
{ binding: 1, descriptorType: DT.Storage, descriptorCount: 1, stageFlags: SS.All },
{ binding: 2, descriptorType: DT.Uniform, descriptorCount: 1, stageFlags: SS.All },
];
rds.desc_layouts.pbr =
}

View File

@ -153,9 +153,9 @@ InitGame(PlatformWindow* window)
g.ui_vertex_buf = g.ui_vertex_mapped_buf.data; g.ui_vertex_buf = g.ui_vertex_mapped_buf.data;
g.ui_index_buf = g.ui_index_mapped_buf.data; g.ui_index_buf = g.ui_index_mapped_buf.data;
u8[] font_data = LoadAssetData(&g.arena, "fonts/NuberNextCondensed-DemiBold.otf"); u8[] font_data = CastStr!(u8)(import("NuberNextCondensed-DemiBold.otf"));
g.font = OpenFont(font_data); g.font = OpenFont(font_data);
g.atlas_buf = CreateAtlas(&g.arena, g.font, 32, 256); g.atlas_buf = CreateAtlas(&g.arena, g.font, 32, 512);
CreateImageView(&g.rd, &g.font_tex, g.atlas_buf.atlas.width, g.atlas_buf.atlas.height, 4, g.atlas_buf.data); CreateImageView(&g.rd, &g.font_tex, g.atlas_buf.atlas.width, g.atlas_buf.atlas.height, 4, g.atlas_buf.data);
CreateImageView(&g.rd, &g.default_tex, 16, 16, 4, white_tex); CreateImageView(&g.rd, &g.default_tex, 16, 16, 4, white_tex);

View File

@ -5,15 +5,8 @@ import game;
import core.simd; import core.simd;
import core.stdc.string : memcpy; import core.stdc.string : memcpy;
// TODO:
// 1. Determine how to better handle inputs
// 2. Set up multisampling
// 3. Replace standard library calls with nogc replacements
void main(string[] argv) void main(string[] argv)
{ {
InitFreeType();
PlatformWindow window = CreateWindow("Video Game", 1920, 1080); PlatformWindow window = CreateWindow("Video Game", 1920, 1080);
Game g = InitGame(&window); Game g = InitGame(&window);

View File

@ -1,19 +0,0 @@
#version 460
#extension GL_GOOGLE_include_directive : require
#include "structures.layout"
#include "gui.layout"
layout (location = 0) in struct FragDataIn {
vec4 color;
vec2 uv;
} FragData;
layout (location = 0) out vec4 FragColor;
void main()
{
vec4 tex_color = texture(sampler2D(SpriteAtlas, SamplerNearest), FragData.uv);
FragColor = FragData.color * tex_color;
}

View File

@ -1,9 +0,0 @@
layout (set = 1, binding = 0) uniform texture2D SpriteAtlas;
layout (set = 1, binding = 1) uniform Scale {
vec2 factor;
} S;
layout (push_constant) uniform Constants {
vec2 res;
} PC;

View File

@ -1,57 +0,0 @@
#version 460
#extension GL_GOOGLE_include_directive : require
#include "structures.layout"
#include "gui.layout"
layout (location = 0) in vec2 in_dst_start;
layout (location = 1) in vec2 in_dst_end;
layout (location = 2) in vec2 in_src_start;
layout (location = 3) in vec2 in_src_end;
layout (location = 4) in vec4 in_col;
layout (location = 0) out struct FragDataOut {
vec4 color;
vec2 uv;
} FragData;
vec2 Vertices[4] = vec2[4](
vec2(-1.0, +1.0),
vec2(-1.0, -1.0),
vec2(+1.0, +1.0),
vec2(+1.0, -1.0)
);
vec2 rotate(vec2 coords, float theta)
{
return mat2(cos(theta), sin(theta), -sin(theta), cos(theta)) * coords;
}
void main()
{
ivec2 tex_size = textureSize(sampler2D(SpriteAtlas, SamplerNearest), 0);
vec2 dst_half_size = (in_dst_end - in_dst_start) / 2;
vec2 dst_center = (in_dst_end + in_dst_start) / 2;
vec2 dst_pos = (Vertices[gl_VertexIndex] * dst_half_size + dst_center);
vec2 src_half_size = (in_src_end - in_src_start) / 2;
vec2 src_center = (in_src_end + in_src_start) / 2;
vec2 src_pos = (Vertices[gl_VertexIndex] * src_half_size + src_center);
vec2 uvs[4] = vec2[4](
vec2(in_src_start.x, in_src_start.y),
vec2(in_src_start.x, in_src_end.y),
vec2(in_src_end.x, in_src_start.y),
vec2(in_src_end.x, in_src_end.y)
);
FragData.color = in_col;
FragData.uv = uvs[gl_VertexIndex] / tex_size;
gl_Position = vec4((2 * dst_pos.x / PC.res.x - 1) * S.factor.x,
(2 * dst_pos.y / PC.res.y - 1) * S.factor.y,
0,
1);
}

78
src/shaders/pbr.frag.glsl Normal file
View File

@ -0,0 +1,78 @@
#version 460
#extension GL_GOOGLE_include_directive : require
#include "structures.layout"
layout (location = 0) in struct FragDataIn {
vec3 normal;
vec2 uv;
} FragData;
layout (location = 0, index = 0) out vec4 FragColor;
vec4 CalculateDirectionalLight(vec4 diff_col, vec4 diff_samp, vec4 light_col, vec3 dir, vec3 normal)
{
float diffuse_factor = max(dot(normal, -dir), 0.0);
vec4 ambient = vec4(vec3(G.ambient_color * diff_col), diff_samp.a);
vec4 diffuse = vec4(vec3(light_col * diffuse_factor), diff_samp.a);
diffuse *= diff_samp;
ambient *= diff_samp;
return (ambient + diffuse);
}
float UnPreMultLinearToSRGB(float col)
{
return col < 0.0031308f ? col * 12.92f : (pow(col, 1.0f / 2.4f) * 1.055f) - 0.055f;
}
vec4 UnPreMultLinearToSRGB(vec4 col)
{
col.r = UnPreMultLinearToSRGB(col.x);
col.g = UnPreMultLinearToSRGB(col.x);
col.b = UnPreMultLinearToSRGB(col.x);
return col;
}
void main()
{
ivec2 coord = ivec2(gl_FragCoord.xy);
int store_mask = 0;
int view_size = int(G.res.x) * int(G.res.y);
int sample_id = 0;
int list_pos = view_size * OIT_LAYERS * sample_id + coord.y * int(G.res.x) + coord.x;
vec4 col = ModelDiffuse;
vec4 tex_col = texture(sampler2D(AlbedoTex, SamplerNearest), FragData.uv);
vec4 out_col = CalculateDirectionalLight(col, tex_col, G.light_color, G.light_direction, FragData.normal);
out_col.a = 1.0;// Materials[nonuniformEXT(PC.mat_id)].alpha;
if(ALPHA_TEX)
{
vec4 alpha_col = texture(sampler2D(AlphaTex, SamplerNearest), FragData.uv);
out_col.a = alpha_col.a;
}
//FragColor = out_col;
//TODO: set up OIT again
vec4 srgb_col = out_col;// UnPreMultLinearToSRGB(out_col);
uvec4 store_value = uvec4(packUnorm4x8(srgb_col), floatBitsToUint(gl_FragCoord.z), store_mask, 0);
uint old_counter = imageAtomicAdd(ImageAux, coord, 1u);
if (old_counter < OIT_LAYERS)
{
imageStore(ImageABuffer, list_pos + int(old_counter) * view_size, store_value);
FragColor = vec4(0.0);
}
else
{
FragColor = vec4(0.0); //vec4(out_col.rgb * out_col.a, out_col.a); // Change to vec4(0.0) to disable tail blending
}
}

54
src/shaders/pbr.vert.glsl Normal file
View File

@ -0,0 +1,54 @@
#version 460
#extension GL_GOOGLE_include_directive : require
#extension GL_EXT_nonuniform_qualifier : require
#include "structures.layout"
/*
layout (set = 1, binding = 0) uniform texture2D AlbedoTexture;
layout (set = 1, binding = 1) uniform texture2D AmbientTexture;
layout (set = 1, binding = 2) uniform texture2D SpecularTexture;
layout (set = 1, binding = 3) uniform texture2D AlphaTexture;
layout (set = 1, binding = 4) uniform Material {
vec4 ambient;
vec4 diffuse;
vec4 specular;
bool albedo_has_texture;
bool ambient_has_texture;
bool specular_has_texture;
bool alpha_has_texture;
float shininess;
float alpha;
} Materials[];
*/
layout (location = 0) in vec4 in_col;
layout (location = 1) in vec4 in_tangent;
layout (location = 2) in vec3 in_pos;
layout (location = 3) in vec3 in_normal;
layout (location = 4) in vec2 in_uv;
layout (location = 0) out struct FragDataOut {
vec3 normal;
vec2 uv;
} FragData;
mat4 y_matrix = mat4(
1.0, 0.0, 0.0, 0.0,
0.0, -1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
);
void main()
{
gl_Position = G.projection * G.view * ModelMatrix * vec4(in_pos, 1.0);
FragData.uv = in_uv;
FragData.normal = mat3(ModelMatrix) * in_normal;
}

View File

@ -1,4 +1,77 @@
// DEFINES
#define OIT_LAYERS 16
#define IMG_MAX 100
#define BUF_MAX 25
#define UNI_MAX 50
// SPEC CONSTANTS
layout (constant_id = 0) const bool ALBEDO_TEX = false;
layout (constant_id = 1) const bool AMBIENT_TEX = false;
layout (constant_id = 2) const bool SPECULAR_TEX = false;
layout (constant_id = 3) const bool ALPHA_TEX = false;
// SET 0
layout (rgba16f, set = 0, binding = 0) uniform image2D DrawImage; layout (rgba16f, set = 0, binding = 0) uniform image2D DrawImage;
layout (set = 0, binding = 1) uniform sampler SamplerNearest; layout (set = 0, binding = 1) uniform sampler SamplerNearest;
layout (set = 0, binding = 0) uniform Globals {
mat4 view;
mat4 projection;
vec4 light_color;
vec4 ambient_color;
vec3 light_direction;
vec2 res;
} G;
layout (set = 0, binding = 1, rg32ui) uniform coherent uimageBuffer ImageABuffer;
layout (set = 0, binding = 2, r32ui) uniform coherent uimage2D ImageAux;
// SET 2
layout (set = 2, binding = 0) uniform texture2D Textures[IMG_MAX];
layout (set = 2, binding = 1) uniform Material {
vec4 ambient;
vec4 diffuse;
vec4 specular;
bool albedo_has_texture;
bool ambient_has_texture;
bool specular_has_texture;
bool alpha_has_texture;
float shininess;
float alpha;
} Materials[BUF_MAX];
layout (set = 2, binding = 2) uniform ModelState {
mat4 model_matrix;
} State[UNI_MAX];
// PUSH CONSTANTS
layout (push_constant) uniform PushConstants {
uint t0;
uint t1;
uint t2;
uint t3;
uint m0;
uint s0;
} PC;
// ALIAS MACROS
#define ModelMatrix State[PC.s0].model_matrix
#define AlbedoTex Textures[PC.t0]
#define AmbientTex Textures[PC.t1]
#define SpecularTex Textures[PC.t2]
#define AlphaTex Textures[PC.t3]
#define ModelAmbient Materials[PC.m0].ambient
#define ModelDiffuse Materials[PC.m0].diffuse
#define ModelSpecular Materials[PC.m0].specular
#define ModelShininess Materials[PC.m0].shininess
#define ModelAlpha Materials[PC.m0].alpha