Bake fixes

This commit is contained in:
luboslenco
2025-08-21 16:47:23 +02:00
parent ebcfca4f16
commit 8867cbc0ab
6 changed files with 40 additions and 10 deletions
+4
View File
@@ -878,6 +878,9 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) {
.dynamicRendering = VK_TRUE,
};
VkPhysicalDeviceFeatures enabled_features = {};
enabled_features.independentBlend = VK_TRUE;
VkDeviceCreateInfo deviceinfo = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.pNext = &dynamic_rendering_features,
@@ -887,6 +890,7 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) {
.ppEnabledLayerNames = (const char *const *)wanted_device_layers,
.enabledExtensionCount = wanted_device_extension_count,
.ppEnabledExtensionNames = (const char *const *)wanted_device_extensions,
.pEnabledFeatures = &enabled_features,
};
VkPhysicalDeviceRayTracingPipelineFeaturesKHR raytracing_pipeline_ext = {0};
+16
View File
@@ -191,6 +191,9 @@ typedef enum spirv_opcode {
SPIRV_OPCODE_MEMBER_DECORATE = 72,
SPIRV_OPCODE_COMPOSITE_CONSTRUCT = 80,
SPIRV_OPCODE_COMPOSITE_EXTRACT = 81,
////
SPIRV_OPCODE_TRANSPOSE = 84,
////
SPIRV_OPCODE_SAMPLED_IMAGE = 86,
SPIRV_OPCODE_IMAGE_SAMPLE_IMPLICIT_LOD = 87,
SPIRV_OPCODE_IMAGE_SAMPLE_EXPLICIT_LOD = 88,
@@ -1501,6 +1504,14 @@ static spirv_id write_op_image_fetch(instructions_buffer *instructions, spirv_id
return result;
}
static spirv_id write_op_transpose(instructions_buffer *instructions, spirv_id type, spirv_id operand) {
spirv_id result = allocate_index();
uint32_t operands[] = {type.id, result.id, operand.id};
write_instruction(instructions, WORD_COUNT(operands), SPIRV_OPCODE_TRANSPOSE, operands);
return result;
}
////
static spirv_id write_op_ext_inst(instructions_buffer *instructions, spirv_id result_type, spirv_id set, uint32_t instruction, spirv_id operand) {
@@ -2582,6 +2593,11 @@ static void write_function(instructions_buffer *instructions, function *f, spirv
spirv_id id = write_op_ext_inst3(instructions, spirv_float4_type, glsl_import, SPIRV_GLSL_STD_FMIX, operand1, operand2, operand3);
hmput(index_map, o->op_call.var.index, id);
}
else if (func == add_name("transpose")) {
spirv_id operand = get_var(instructions, o->op_call.parameters[0]);
spirv_id id = write_op_transpose(instructions, spirv_float3x3_type, operand);
hmput(index_map, o->op_call.var.index, id);
}
////
+15
View File
@@ -97,6 +97,20 @@ static void add_func_float4_float4_float4_float(char *name) {
f->block = NULL;
}
static void add_func_float3x3_float3x3(char *name) {
function_id func = add_function(add_name(name));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("float3x3"));
f->return_type.type = find_type_by_ref(&f->return_type);
f->parameter_names[0] = add_name("a");
init_type_ref(&f->parameter_types[0], add_name("float3x3"));
f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);
f->parameters_size = 1;
f->block = NULL;
}
////
static void add_func_int(char *name) {
@@ -874,6 +888,7 @@ void functions_init(void) {
add_func_float3_float3("frac3");
add_func_float3_float3_float3_float("lerp3");
add_func_float4_float4_float4_float("lerp4");
add_func_float3x3_float3x3("transpose");
////
+1 -1
View File
@@ -425,7 +425,7 @@ function context_create(): context_t {
c.brush_lazy_y = 0.0;
c.brush_paint = uv_type_t.UVMAP;
c.brush_angle_reject_dot = 0.5;
c.bake_type = bake_type_t.AO;
c.bake_type = bake_type_t.CURVATURE;
c.bake_axis = bake_axis_t.XYZ;
c.bake_up_axis = bake_up_axis_t.Z;
c.bake_samples = 128;
-4
View File
@@ -262,10 +262,6 @@ fun cotangent_frame(n: float3, p: float3, tex_coord: float2): float3x3 { \
} \
";
let str_transpose: string = "\
fun _transpose(m: float3x3): float3x3 { return float3x3(m[0][0], m[1][0], m[2][0], m[0][1], m[1][1], m[2][1], m[0][2], m[1][2], m[2][2]); }\
";
let str_octahedron_wrap: string = "\
fun octahedron_wrap(v: float2): float2 { \
var a: float2; \
+4 -5
View File
@@ -24,8 +24,7 @@ function make_bake_run(con: node_shader_context_t, kong: node_shader_t) {
node_shader_add_texture(kong, "texpaint_undo", "_texpaint_undo"); // Baked high-poly normals
node_shader_write_frag(kong, "var n0: float3 = sample_lod(texpaint_undo, sampler_linear, tex_coord, 0.0).rgb * float3(2.0, 2.0, 2.0) - float3(1.0, 1.0, 1.0);");
node_shader_add_function(kong, str_cotangent_frame);
node_shader_add_function(kong, str_transpose);
node_shader_write_frag(kong, "var invTBN: float3x3 = _transpose(cotangent_frame(n, n, tex_coord));");
node_shader_write_frag(kong, "var invTBN: float3x3 = transpose(cotangent_frame(n, n, tex_coord));");
node_shader_write_frag(kong, "var res: float3 = normalize(invTBN * n0) * float3(0.5, 0.5, 0.5) + float3(0.5, 0.5, 0.5);");
node_shader_write_frag(kong, "output[0] = float4(res, 1.0);");
}
@@ -47,9 +46,9 @@ function make_bake_run(con: node_shader_context_t, kong: node_shader_t) {
node_shader_add_texture(kong, "texpaint_undo", "_texpaint_undo"); // Baked height
node_shader_write_frag(kong, "var tex_dx: float2 = ddx2(tex_coord);");
node_shader_write_frag(kong, "var tex_dy: float2 = ddy2(tex_coord);");
node_shader_write_frag(kong, "var h0: float = sample_lod(texpaint_undo, sampler_linear, tex_coord, 0.0).r * 100;");
node_shader_write_frag(kong, "var h1: float = sample_lod(texpaint_undo, sampler_linear, tex_coord + tex_dx, 0.0).r * 100;");
node_shader_write_frag(kong, "var h2: float = sample_lod(texpaint_undo, sampler_linear, tex_coord + tex_dy, 0.0).r * 100;");
node_shader_write_frag(kong, "var h0: float = sample_lod(texpaint_undo, sampler_linear, tex_coord, 0.0).r * 100.0;");
node_shader_write_frag(kong, "var h1: float = sample_lod(texpaint_undo, sampler_linear, tex_coord + tex_dx, 0.0).r * 100.0;");
node_shader_write_frag(kong, "var h2: float = sample_lod(texpaint_undo, sampler_linear, tex_coord + tex_dy, 0.0).r * 100.0;");
node_shader_write_frag(kong, "output[0] = float4((h1 - h0) * 0.5 + 0.5, (h2 - h0) * 0.5 + 0.5, 0.0, 1.0);");
}
else if (context_raw.bake_type == bake_type_t.POSITION) {