This commit is contained in:
2025-07-01 19:23:50 +01:00
parent 7f89bc4d62
commit 3331e12441
7 changed files with 149 additions and 78 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
glslangValidator --spirv-val --spirv-dis -g0 -S frag --target-env vulkan1.4 --quiet src/trace.frag.glsl > src/trace.spv-dis
#sed -i '/%11 =/,/OpFunctionEnd/d' src/trace.spv-dis
#sed -i 's/%11\b/%1000/g' src/trace.spv-dis
sed -i '/%11 =/,/OpFunctionEnd/d' src/trace.spv-dis
sed -i 's/%11\b/%1000/g' src/trace.spv-dis
spirv-as --preserve-numeric-ids --target-env vulkan1.4 -o src/recompiled.spv src/trace.spv-dis
rm frag.spv
# rm src/trace.spv-dis
+21 -5
View File
@@ -4,23 +4,39 @@
layout(input_attachment_index = 0, set = 1, binding = 0) uniform subpassInput u_color;
layout(input_attachment_index = 1, set = 1, binding = 1) uniform subpassInput u_normals;
layout(input_attachment_index = 2, set = 1, binding = 2) uniform subpassInput u_depth;
layout(location = 0) in vec2 v_pos;
layout(location = 0) out vec4 f_color;
vec3 shading(vec3 normal, vec4 position)
vec3 WorldPosFromDepth(float depth) {
vec4 clipSpacePosition = vec4(v_pos, depth, 1.0);
vec4 worldSpacePosition = inverse(camera_uniforms.proj * camera_uniforms.view) * clipSpacePosition;
return worldSpacePosition.xyz /= worldSpacePosition.w;
}
vec3 shading(vec3 normal, vec3 position)
{
vec3 accum = vec3(0., 0., 0.);
mat3 rotation = mat3(pc.world[0].xyz, pc.world[1].xyz, pc.world[2].xyz);
//vec3 position=pc.world[3].xyz;
for (int i = 0; (i < light_uniforms.light_count) && (i < 32); i++)
{
accum += light_uniforms.col[i].xyz * ((dot(normalize(rotation * normal), normalize(light_uniforms.pos[i].xyz - position.xyz)) * .5) + .5);
accum += light_uniforms.col[i].xyz * ((dot(normal, normalize(light_uniforms.pos[i].xyz - position)) * .5) + .5);
}
//return vec3(int(position.x * 256.0) ^ int(position.y * 256.0) ^ int(position.z * 256.0));
return accum;
}
void main() {
f_color = vec4(subpassLoad(u_color).rgb, 1.0);
vec3 col = subpassLoad(u_color).xyz;
vec3 normal = subpassLoad(u_normals).xyz;
float depth = subpassLoad(u_depth).x;
if (depth == 1.0) {
discard;
}
vec3 position = WorldPosFromDepth(depth);
f_color = vec4(col * shading(normal, position), 1.0);
}
+10 -2
View File
@@ -2,8 +2,16 @@
#extension GL_GOOGLE_include_directive:require
#include "include.glsl"
const vec2 full_screen_triangle[3] = {vec2(-1., -1.), vec2(1., -1.), vec2(-1., 1.)};
layout(location = 0) out vec2 v_pos;
const vec2 full_screen_triangle[3] = {
vec2(0., 0.),
vec2(0., 2.),
vec2(2, 0.)
};
void main() {
gl_Position = vec4(full_screen_triangle[gl_VertexIndex], 0.0, 1.0);
vec2 tri = (full_screen_triangle[gl_VertexIndex] * 2.0) - 1.0;
v_pos = tri;
gl_Position = vec4(v_pos, 0.5, 1.0);
}
+33 -9
View File
@@ -82,6 +82,7 @@ use winit::{
event::{DeviceEvent, ElementState, MouseButton, WindowEvent},
event_loop::{ActiveEventLoop, EventLoop},
keyboard::{KeyCode, PhysicalKey},
platform::{wayland::EventLoopBuilderExtWayland, x11::EventLoopBuilderExtX11},
window::Window,
};
@@ -116,7 +117,7 @@ fn main() -> Result<(), impl Error> {
])
.unwrap();
let event_loop = EventLoop::new().unwrap();
let event_loop = EventLoop::builder().with_x11().build().unwrap();
let mut app = App::new(&event_loop);
event_loop.run_app(&mut app)
@@ -209,6 +210,7 @@ struct RenderContext {
framebuffers: Vec<Arc<Framebuffer>>,
color_buffer: Arc<ImageView>,
normal_buffer: Arc<ImageView>,
depth_buffer: Arc<ImageView>,
shader_modules: ShaderModules,
implicit_pipeline: Arc<GraphicsPipeline>,
lighting_pipeline: Arc<GraphicsPipeline>,
@@ -790,7 +792,7 @@ impl ApplicationHandler for App {
{
color: [final_color],
depth_stencil: {},
input: [color, normals],
input: [color, normals, depth],
},
{
color: [final_color],
@@ -806,7 +808,7 @@ impl ApplicationHandler for App {
depth_range: 0.0..=1.0,
};
let (framebuffers, color_buffer, normal_buffer) =
let (framebuffers, color_buffer, normal_buffer, depth_buffer) =
framebuffer_generation(&images, &render_pass, &self.memory_allocator);
let (implicit_pipeline, lighting_pipeline) = deferred_pipeline_recompile(
@@ -840,6 +842,7 @@ impl ApplicationHandler for App {
framebuffers,
color_buffer,
normal_buffer,
depth_buffer,
shader_modules,
implicit_pipeline,
lighting_pipeline,
@@ -991,8 +994,12 @@ impl App {
.expect("Failed to recreate swapchain");
rcx.swapchain = new_swapchain;
(rcx.framebuffers, rcx.color_buffer, rcx.normal_buffer) =
framebuffer_generation(&new_images, &rcx.render_pass, &self.memory_allocator);
(
rcx.framebuffers,
rcx.color_buffer,
rcx.normal_buffer,
rcx.depth_buffer,
) = framebuffer_generation(&new_images, &rcx.render_pass, &self.memory_allocator);
rcx.viewport.extent[0] = window_size.width as f32;
rcx.viewport.extent[1] = window_size.height as f32 * -1.;
@@ -1175,9 +1182,16 @@ impl App {
)
.unwrap();
assert!(rcx.lighting_pipeline.layout().set_layouts().len() == 2);
//assert!(rcx.lighting_pipeline.layout().set_layouts().len() == 2);
let lighting_layout1 = &rcx.lighting_pipeline.layout().set_layouts()[0];
//println!(
// "Descriptor counts: {:?}",
// lighting_layout1
// .descriptor_counts()
// .iter()
// .collect::<Vec<_>>()
//);
let lighting_set1 = DescriptorSet::new(
self.descriptor_set_allocator.clone(),
lighting_layout1.clone(),
@@ -1196,6 +1210,7 @@ impl App {
[
WriteDescriptorSet::image_view(0, rcx.color_buffer.clone()),
WriteDescriptorSet::image_view(1, rcx.normal_buffer.clone()),
WriteDescriptorSet::image_view(2, rcx.depth_buffer.clone()),
],
[],
)
@@ -1314,6 +1329,8 @@ impl App {
)
.unwrap();
unsafe { builder.draw(3, 1, 0, 0) }.unwrap();
builder
.next_subpass(
Default::default(),
@@ -1368,7 +1385,12 @@ fn framebuffer_generation(
images: &[Arc<Image>],
render_pass: &Arc<RenderPass>,
allocator: &Arc<StandardMemoryAllocator>,
) -> (Vec<Arc<Framebuffer>>, Arc<ImageView>, Arc<ImageView>) {
) -> (
Vec<Arc<Framebuffer>>,
Arc<ImageView>,
Arc<ImageView>,
Arc<ImageView>,
) {
let depth_buffer = ImageView::new_default(
Image::new(
allocator.clone(),
@@ -1376,7 +1398,9 @@ fn framebuffer_generation(
image_type: ImageType::Dim2d,
format: Format::D16_UNORM,
extent: images[0].extent(),
usage: ImageUsage::DEPTH_STENCIL_ATTACHMENT | ImageUsage::TRANSIENT_ATTACHMENT,
usage: ImageUsage::DEPTH_STENCIL_ATTACHMENT
| ImageUsage::INPUT_ATTACHMENT
| ImageUsage::TRANSIENT_ATTACHMENT,
..Default::default()
},
AllocationCreateInfo::default(),
@@ -1441,7 +1465,7 @@ fn framebuffer_generation(
})
.collect::<Vec<_>>();
(framebuffers, color_buffer, normal_buffer)
(framebuffers, color_buffer, normal_buffer, depth_buffer)
}
fn deferred_pipeline_recompile(
Binary file not shown.
+3 -2
View File
@@ -57,14 +57,15 @@ void main() {
vec3 n = getNormal(p, td.y);
f_color = vec4(0.5, 0.5, 0.5, 1.0);
f_normal = vec4(n, 1.0);
mat3 rotation = mat3(pc.world[0].xyz, pc.world[1].xyz, pc.world[2].xyz);
f_normal = vec4(normalize(rotation * n), 1.0);
vec4 tpoint = camera_uniforms.proj * camera_uniforms.view * pc.world * vec4(p, 1);
gl_FragDepth = (tpoint.z / tpoint.w);
}
else
{
//f_color = vec4(raydir, 1.0);
discard;
}
+79 -57
View File
@@ -1,12 +1,12 @@
; SPIR-V
; Version: 1.6
; Generator: Khronos Glslang Reference Front End; 11
; Bound: 260
; Bound: 289
; Schema: 0
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %4 "main" %27 %35 %171 %182 %217 %220 %gl_FragDepth %259
OpEntryPoint Fragment %4 "main" %27 %35 %171 %182 %217 %246 %gl_FragDepth %288
OpExecutionMode %4 OriginUpperLeft
OpExecutionMode %4 DepthReplacing
OpExecutionMode %4 DepthGreater
@@ -30,16 +30,16 @@
OpDecorate %182 Binding 1
OpDecorate %182 DescriptorSet 0
OpDecorate %217 Location 0
OpDecorate %220 Location 1
OpDecorate %246 Location 1
OpDecorate %gl_FragDepth BuiltIn FragDepth
OpDecorate %_arr_v4float_uint_32 ArrayStride 16
OpDecorate %_arr_v4float_uint_32_0 ArrayStride 16
OpDecorate %_struct_257 Block
OpMemberDecorate %_struct_257 0 Offset 0
OpMemberDecorate %_struct_257 1 Offset 512
OpMemberDecorate %_struct_257 2 Offset 1024
OpDecorate %259 Binding 0
OpDecorate %259 DescriptorSet 0
OpDecorate %_struct_286 Block
OpMemberDecorate %_struct_286 0 Offset 0
OpMemberDecorate %_struct_286 1 Offset 512
OpMemberDecorate %_struct_286 2 Offset 1024
OpDecorate %288 Binding 0
OpDecorate %288 DescriptorSet 0
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
@@ -89,7 +89,10 @@
%217 = OpVariable %_ptr_Output_v4float Output
%float_0_5 = OpConstant %float 0.5
%219 = OpConstantComposite %v4float %float_0_5 %float_0_5 %float_0_5 %float_1
%220 = OpVariable %_ptr_Output_v4float Output
%mat3v3float = OpTypeMatrix %v3float 3
%_ptr_Function_mat3v3float = OpTypePointer Function %mat3v3float
%_ptr_PushConstant_v4float = OpTypePointer PushConstant %v4float
%246 = OpVariable %_ptr_Output_v4float Output
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Uniform_mat4v4float = OpTypePointer Uniform %mat4v4float
%_ptr_Output_float = OpTypePointer Output %float
@@ -98,9 +101,9 @@
%uint_32 = OpConstant %uint 32
%_arr_v4float_uint_32 = OpTypeArray %v4float %uint_32
%_arr_v4float_uint_32_0 = OpTypeArray %v4float %uint_32
%_struct_257 = OpTypeStruct %_arr_v4float_uint_32 %_arr_v4float_uint_32_0 %uint
%_ptr_Uniform__struct_257 = OpTypePointer Uniform %_struct_257
%259 = OpVariable %_ptr_Uniform__struct_257 Uniform
%_struct_286 = OpTypeStruct %_arr_v4float_uint_32 %_arr_v4float_uint_32_0 %uint
%_ptr_Uniform__struct_286 = OpTypePointer Uniform %_struct_286
%288 = OpVariable %_ptr_Uniform__struct_286 Uniform
%4 = OpFunction %void None %3
%5 = OpLabel
%169 = OpVariable %_ptr_Function_v3float Function
@@ -113,7 +116,8 @@
%209 = OpVariable %_ptr_Function_v3float Function
%210 = OpVariable %_ptr_Function_v3float Function
%212 = OpVariable %_ptr_Function_float Function
%227 = OpVariable %_ptr_Function_v4float Function
%222 = OpVariable %_ptr_Function_mat3v3float Function
%256 = OpVariable %_ptr_Function_v4float Function
%39 = OpAccessChain %_ptr_PushConstant_v3float %35 %int_1
%40 = OpLoad %v3float %39
%42 = OpCompositeConstruct %v3float %float_8 %float_8 %float_8
@@ -150,7 +154,7 @@
%205 = OpLoad %float %204
%206 = OpFOrdLessThan %bool %205 %float_9_99999975en05
OpSelectionMerge %208 None
OpBranchConditional %206 %207 %251
OpBranchConditional %206 %207 %280
%207 = OpLabel
%211 = OpLoad %v3float %196
OpStore %210 %211
@@ -160,47 +164,65 @@
%215 = OpFunctionCall %v3float %17 %210 %212
OpStore %209 %215
OpStore %217 %219
%221 = OpLoad %v3float %209
%222 = OpCompositeExtract %float %221 0
%223 = OpCompositeExtract %float %221 1
%224 = OpCompositeExtract %float %221 2
%225 = OpCompositeConstruct %v4float %222 %223 %224 %float_1
OpStore %220 %225
%229 = OpAccessChain %_ptr_Uniform_mat4v4float %182 %int_1
%230 = OpLoad %mat4v4float %229
%231 = OpAccessChain %_ptr_Uniform_mat4v4float %182 %int_0
%232 = OpLoad %mat4v4float %231
%233 = OpMatrixTimesMatrix %mat4v4float %230 %232
%234 = OpAccessChain %_ptr_PushConstant_mat4v4float %35 %int_0
%235 = OpLoad %mat4v4float %234
%236 = OpMatrixTimesMatrix %mat4v4float %233 %235
%237 = OpLoad %v3float %196
%238 = OpCompositeExtract %float %237 0
%239 = OpCompositeExtract %float %237 1
%240 = OpCompositeExtract %float %237 2
%241 = OpCompositeConstruct %v4float %238 %239 %240 %float_1
%242 = OpMatrixTimesVector %v4float %236 %241
OpStore %227 %242
%245 = OpAccessChain %_ptr_Function_float %227 %uint_2
%246 = OpLoad %float %245
%248 = OpAccessChain %_ptr_Function_float %227 %uint_3
%249 = OpLoad %float %248
%250 = OpFDiv %float %246 %249
OpStore %gl_FragDepth %250
%224 = OpAccessChain %_ptr_PushConstant_v4float %35 %int_0 %int_0
%225 = OpLoad %v4float %224
%226 = OpVectorShuffle %v3float %225 %225 0 1 2
%227 = OpAccessChain %_ptr_PushConstant_v4float %35 %int_0 %int_1
%228 = OpLoad %v4float %227
%229 = OpVectorShuffle %v3float %228 %228 0 1 2
%230 = OpAccessChain %_ptr_PushConstant_v4float %35 %int_0 %int_2
%231 = OpLoad %v4float %230
%232 = OpVectorShuffle %v3float %231 %231 0 1 2
%233 = OpCompositeExtract %float %226 0
%234 = OpCompositeExtract %float %226 1
%235 = OpCompositeExtract %float %226 2
%236 = OpCompositeExtract %float %229 0
%237 = OpCompositeExtract %float %229 1
%238 = OpCompositeExtract %float %229 2
%239 = OpCompositeExtract %float %232 0
%240 = OpCompositeExtract %float %232 1
%241 = OpCompositeExtract %float %232 2
%242 = OpCompositeConstruct %v3float %233 %234 %235
%243 = OpCompositeConstruct %v3float %236 %237 %238
%244 = OpCompositeConstruct %v3float %239 %240 %241
%245 = OpCompositeConstruct %mat3v3float %242 %243 %244
OpStore %222 %245
%247 = OpLoad %mat3v3float %222
%248 = OpLoad %v3float %209
%249 = OpMatrixTimesVector %v3float %247 %248
%250 = OpExtInst %v3float %1 Normalize %249
%251 = OpCompositeExtract %float %250 0
%252 = OpCompositeExtract %float %250 1
%253 = OpCompositeExtract %float %250 2
%254 = OpCompositeConstruct %v4float %251 %252 %253 %float_1
OpStore %246 %254
%258 = OpAccessChain %_ptr_Uniform_mat4v4float %182 %int_1
%259 = OpLoad %mat4v4float %258
%260 = OpAccessChain %_ptr_Uniform_mat4v4float %182 %int_0
%261 = OpLoad %mat4v4float %260
%262 = OpMatrixTimesMatrix %mat4v4float %259 %261
%263 = OpAccessChain %_ptr_PushConstant_mat4v4float %35 %int_0
%264 = OpLoad %mat4v4float %263
%265 = OpMatrixTimesMatrix %mat4v4float %262 %264
%266 = OpLoad %v3float %196
%267 = OpCompositeExtract %float %266 0
%268 = OpCompositeExtract %float %266 1
%269 = OpCompositeExtract %float %266 2
%270 = OpCompositeConstruct %v4float %267 %268 %269 %float_1
%271 = OpMatrixTimesVector %v4float %265 %270
OpStore %256 %271
%274 = OpAccessChain %_ptr_Function_float %256 %uint_2
%275 = OpLoad %float %274
%277 = OpAccessChain %_ptr_Function_float %256 %uint_3
%278 = OpLoad %float %277
%279 = OpFDiv %float %275 %278
OpStore %gl_FragDepth %279
OpBranch %208
%251 = OpLabel
%280 = OpLabel
OpTerminateInvocation
%208 = OpLabel
OpReturn
OpFunctionEnd
%11 = OpFunction %float None %9
%10 = OpFunctionParameter %_ptr_Function_v3float
%12 = OpLabel
%45 = OpLoad %v3float %10
%46 = OpExtInst %float %1 Length %45
%48 = OpFSub %float %46 %float_1
OpReturnValue %48
OpFunctionEnd
%17 = OpFunction %v3float None %14
%15 = OpFunctionParameter %_ptr_Function_v3float
%16 = OpFunctionParameter %_ptr_Function_float
@@ -219,7 +241,7 @@
%62 = OpLoad %float %61
%63 = OpCompositeConstruct %v3float %56 %59 %62
OpStore %64 %63
%65 = OpFunctionCall %float %11 %64
%65 = OpFunctionCall %float %1000 %64
%66 = OpAccessChain %_ptr_Function_float %51 %uint_0
OpStore %66 %65
%67 = OpAccessChain %_ptr_Function_float %15 %uint_0
@@ -231,7 +253,7 @@
%73 = OpLoad %float %72
%74 = OpCompositeConstruct %v3float %68 %71 %73
OpStore %75 %74
%76 = OpFunctionCall %float %11 %75
%76 = OpFunctionCall %float %1000 %75
%77 = OpAccessChain %_ptr_Function_float %51 %uint_1
OpStore %77 %76
%78 = OpAccessChain %_ptr_Function_float %15 %uint_0
@@ -243,13 +265,13 @@
%84 = OpFAdd %float %83 %float_9_99999975en05
%85 = OpCompositeConstruct %v3float %79 %81 %84
OpStore %86 %85
%87 = OpFunctionCall %float %11 %86
%87 = OpFunctionCall %float %1000 %86
%88 = OpAccessChain %_ptr_Function_float %51 %uint_2
OpStore %88 %87
%89 = OpLoad %v3float %51
%91 = OpLoad %v3float %15
OpStore %90 %91
%92 = OpFunctionCall %float %11 %90
%92 = OpFunctionCall %float %1000 %90
%93 = OpCompositeConstruct %v3float %92 %92 %92
%94 = OpFSub %v3float %89 %93
%95 = OpExtInst %v3float %1 Normalize %94
@@ -269,7 +291,7 @@
OpStore %23 %102
%104 = OpLoad %v3float %23
OpStore %103 %104
%105 = OpFunctionCall %float %11 %103
%105 = OpFunctionCall %float %1000 %103
%107 = OpFMul %float %105 %float_0_899999976
%108 = OpAccessChain %_ptr_Function_float %99 %uint_1
OpStore %108 %107
@@ -319,7 +341,7 @@
%125 = OpLabel
%148 = OpLoad %v3float %23
OpStore %147 %148
%149 = OpFunctionCall %float %11 %147
%149 = OpFunctionCall %float %1000 %147
%150 = OpFMul %float %149 %float_0_899999976
%151 = OpAccessChain %_ptr_Function_float %99 %uint_1
OpStore %151 %150