diff --git a/assets/shaders/gradient.comp.spv b/assets/shaders/gradient.comp.spv index 7ca2085..4763e9c 100644 Binary files a/assets/shaders/gradient.comp.spv and b/assets/shaders/gradient.comp.spv differ diff --git a/assets/shaders/pbr.frag.spv b/assets/shaders/pbr.frag.spv index 33e0b49..22086e9 100644 Binary files a/assets/shaders/pbr.frag.spv and b/assets/shaders/pbr.frag.spv differ diff --git a/assets/shaders/pbr.vert.spv b/assets/shaders/pbr.vert.spv index 6b77a51..d66f785 100644 Binary files a/assets/shaders/pbr.vert.spv and b/assets/shaders/pbr.vert.spv differ diff --git a/dub.json b/dub.json index 9dcb5f0..ad9be9c 100644 --- a/dub.json +++ b/dub.json @@ -15,7 +15,7 @@ "libs-windows": [], "preGenerateCommands-linux": ["./build.sh"], "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"] }, { diff --git a/src/VulkanRenderer b/src/VulkanRenderer index 1290c5f..6021713 160000 --- a/src/VulkanRenderer +++ b/src/VulkanRenderer @@ -1 +1 @@ -Subproject commit 1290c5f45ac49cc5534817e8d6b0adae4ac058af +Subproject commit 6021713286482893ca0614c894419290c6f425d4 diff --git a/src/dlib b/src/dlib index a97309c..c83ffab 160000 --- a/src/dlib +++ b/src/dlib @@ -1 +1 @@ -Subproject commit a97309cb04e0e802047586a5bacbacc9b8e9649b +Subproject commit c83ffabce69071a3e7a0af3f26aa420082eeda1f diff --git a/src/game2.d b/src/game2.d new file mode 100644 index 0000000..8dafa55 --- /dev/null +++ b/src/game2.d @@ -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 = + +} diff --git a/src/gears/game.d b/src/gears/game.d index 639e587..2b08381 100644 --- a/src/gears/game.d +++ b/src/gears/game.d @@ -153,9 +153,9 @@ InitGame(PlatformWindow* window) g.ui_vertex_buf = g.ui_vertex_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.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.default_tex, 16, 16, 4, white_tex); diff --git a/src/gears/main.d b/src/gears/main.d index 0c0a676..0707b7c 100644 --- a/src/gears/main.d +++ b/src/gears/main.d @@ -5,15 +5,8 @@ import game; import core.simd; 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) { - InitFreeType(); - PlatformWindow window = CreateWindow("Video Game", 1920, 1080); Game g = InitGame(&window); diff --git a/src/shaders/gui.frag.glsl b/src/shaders/gui.frag.glsl deleted file mode 100644 index 93b5149..0000000 --- a/src/shaders/gui.frag.glsl +++ /dev/null @@ -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; -} diff --git a/src/shaders/gui.layout b/src/shaders/gui.layout deleted file mode 100644 index b7c9038..0000000 --- a/src/shaders/gui.layout +++ /dev/null @@ -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; diff --git a/src/shaders/gui.vert.glsl b/src/shaders/gui.vert.glsl deleted file mode 100644 index e30f3b6..0000000 --- a/src/shaders/gui.vert.glsl +++ /dev/null @@ -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); -} diff --git a/src/shaders/pbr.frag.glsl b/src/shaders/pbr.frag.glsl new file mode 100644 index 0000000..b05c6ac --- /dev/null +++ b/src/shaders/pbr.frag.glsl @@ -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 + } +} diff --git a/src/shaders/pbr.vert.glsl b/src/shaders/pbr.vert.glsl new file mode 100644 index 0000000..940b63b --- /dev/null +++ b/src/shaders/pbr.vert.glsl @@ -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; +} diff --git a/src/shaders/structures.layout b/src/shaders/structures.layout index 216e707..ee4045d 100644 --- a/src/shaders/structures.layout +++ b/src/shaders/structures.layout @@ -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 (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