This commit is contained in:
Tuomas Katajisto 2025-07-21 22:47:31 +03:00
parent f6e754ff73
commit e90a4495e7
11 changed files with 1131 additions and 236 deletions

View File

@ -4,5 +4,5 @@ cd src/shaders/
./compile_shaders.sh
cd ..
cd ..
jai-linux -x64 first.jai
jai -x64 first.jai
./first

BIN
resources/lut.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

View File

@ -61,21 +61,22 @@ arb_tri_add :: (tri: Arb_Tri) {
array_add(*arbTriState.trilist, tri);
}
transform_to_screen_x :: (coord: float) -> float {
w, h := get_window_size();
return (coord / cast(float) w) * 2.0 - 1.0;
}
transform_to_screen_y :: (coord: float) -> float {
w, h := get_window_size();
return (coord / cast(float) h) * 2.0 - 1.0;
}
arb_tri_flush :: () {
if !arbTriState.active {
return;
}
transform_to_screen_x :: (coord: float) -> float {
w, h := get_window_size();
return (coord / cast(float) w) * 2.0 - 1.0;
}
transform_to_screen_y :: (coord: float) -> float {
w, h := get_window_size();
return (coord / cast(float) h) * 2.0 - 1.0;
}
arbTriState.active = false;

View File

@ -69,6 +69,8 @@ init_profiler :: () {
iprof_conf.text_width = text_width;
iprof_conf.draw_rectangle = draw_rectangle_prof;
iprof_conf.draw_line = draw_line;
iprof_conf.graph_begin = graph_begin;
iprof_conf.graph_end = graph_end;
}
draw_profiler :: () {

View File

@ -240,7 +240,12 @@ draw_trile :: () {
mvp := create_viewproj(*cam);
vs_params : Vs_Params;
vs_params.mvp = mvp.floats;
vs_params.camera = cam.position.component;
world_conf : Trixel_World_Config;
wc : *World_Config = *(World_Config.{});
world_config_to_shader_type(wc, *world_conf);
trixels : [4096]Position_Color;
min_distance : float = 999.0;
@ -272,8 +277,6 @@ draw_trile :: () {
for y: 0..15 {
for z: 0..15 {
if current_trile.trixels[x][y][z].empty then continue;
hit := does_ray_hit_cube(ray, .{ .{x * TRIXEL_SIZE, y * TRIXEL_SIZE, z * TRIXEL_SIZE}, .{TRIXEL_SIZE, TRIXEL_SIZE, TRIXEL_SIZE}});
trixels[trixel_count].pos.x = x * (1.0 / 16.0) + TRIXEL_SIZE_HALF;
trixels[trixel_count].pos.y = y * (1.0 / 16.0) + TRIXEL_SIZE_HALF;
@ -287,7 +290,7 @@ draw_trile :: () {
trixel_color = .{1.0, 0.0, 0.0};
}
trixels[trixel_count].col = .{trixel_color.x, trixel_color.y, trixel_color.z, 1.0};
trixels[trixel_count].col = .{trixel_color.x, trixel_color.y, trixel_color.z, material_encode_to_float(current_trile.trixels[x][y][z].material)};
trixel_count += 1;
}
}
@ -301,6 +304,7 @@ draw_trile :: () {
sg_apply_pipeline(gPipelines.trixel.pipeline);
sg_apply_bindings(*gPipelines.trixel.bind);
sg_apply_uniforms(UB_vs_params, *(sg_range.{ ptr = *vs_params, size = size_of(type_of(vs_params)) }));
sg_apply_uniforms(UB_trixel_world_config, *(sg_range.{ptr = *world_conf, size = size_of(type_of(world_conf))}));
sg_draw(0, 36, trixel_count);
}

View File

@ -1,15 +1,14 @@
MAX_FILE_SIZE :: 200_000;
buf : [MAX_FILE_SIZE]u8;
mandatory_done : bool = false;
mandatory_loads_left : s32 = 0;
init_after_mandatory_done : bool = false;
mandatory_loads_done :: () -> bool {
return mandatory_done;
return mandatory_loads_left <= 0;
}
init_font_loads :: () {
print("SENDING LOAD!!!!\n");
sfetch_send(*(sfetch_request_t.{
path = "./resources/DroidSerif-Regular.ttf".data,
callback = fontcb,
@ -22,7 +21,6 @@ init_font_loads :: () {
fontcb :: (res: *sfetch_response_t) #c_call {
push_context,defer_pop default_context;
print("RDY! Finished? % Fetched? % \n", res.fetched, res.finished);
state.font_default.fons_font = fonsAddFontMem(state.fons, "sans", res.data.ptr, xx res.data.size, 0);
ui_init_font_fields(*state.font_default);
mandatory_done = true;

View File

@ -1,4 +1,4 @@
#import "Basic";
#import "Basic"()(MEMORY_DEBUGGER=true);
#import "Math";
#import "Input";
#import "Hash_Table";
@ -131,7 +131,7 @@ frame :: () {
input_per_frame_event_and_flag_update();
memory_visualizer_per_frame_update();
// memory_visualizer_per_frame_update();
profiler_update();
reset_temporary_storage();

File diff suppressed because it is too large Load Diff

View File

@ -7,12 +7,13 @@ in vec4 inst_col;
layout(binding=0) uniform vs_params {
mat4 mvp;
vec3 camera;
};
out vec4 color;
out vec4 fnormal;
out vec4 pos;
out vec3 cam;
void main() {
vec3 instancepos = inst.xyz;
@ -20,30 +21,107 @@ void main() {
fnormal = normal;
color = inst_col;
pos = gl_Position;
cam = camera;
}
@end
@fs fs_trixel
layout(binding=1) uniform trixel_world_config {
vec3 skyBase;
vec3 skyTop;
vec3 sunDisk;
vec3 horizonHalo;
vec3 sunHalo;
vec3 sunLightColor;
vec3 sunPosition;
float sunIntensity;
int hasClouds;
int hasPlane;
float planeHeight;
int planeType;
float time;
};
in vec4 color;
in vec4 fnormal;
in vec4 pos;
in vec3 cam;
out vec4 frag_color;
const float PI = 3.1412854;
float DistributionGGX(vec3 N, vec3 H, float roughness) {
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH;
float num = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return num / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness) {
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float num = NdotV;
float denom = NdotV * (1.0 - k) + k;
return num / denom;
}
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) {
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
vec3 fresnelSchlick(float cosTheta, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
void main() {
// 2 lights.
vec3 light1 = vec3(5.0, 5.0, 2.0);
vec3 light2 = vec3(-5.0, -2.0, -2.0);
// Get the material info.
vec3 albedo = color.xyz;
vec3 light = 0.3 * albedo;
int packedMaterial = int(round(color.w*255.0));
float emittance = float((packedMaterial >> 1) & 0x3) / 3.0;
int roughnessInt = (packedMaterial >> 5) & 0x7;
float roughness = max(float(roughnessInt) / 7.0, 0.05);
float metallic = float((packedMaterial >> 3) & 0x3) / 3.0;
vec3 light1dir = normalize(light1 - pos.xyz);
vec3 light2dir = normalize(light2 - pos.xyz);
light += max(0.0, dot(light1dir, fnormal.xyz)) * albedo * 0.5;
light += max(0.0, dot(light2dir, fnormal.xyz)) * albedo * 0.3 * vec3(1.0, 0.7, 0.7);
frag_color = vec4(light, 1.0);
// Ambient light.
vec3 light = 0.3 * albedo;
// // Make emitting things look bright.
// if(emittance > 0.01) return vec4(albedo, 1.0);
vec3 N = normalize(fnormal.xyz);
vec3 V = normalize(cam - pos.xyz);
vec3 L = normalize(sunPosition);
vec3 H = normalize(V + L);
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metallic);
vec3 F = fresnelSchlick(max(dot(H,V), 0.0), F0);
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
vec3 specular = numerator / denominator;
float NdotL = max(dot(N, L), 0.0);
vec3 kD = vec3(1.0) - F;
kD *= 1.0 - metallic;
light += (kD * albedo / PI + specular) * NdotL * vec3(1.0, 1.0, 1.0);
frag_color = vec4(vec3(light), 1.0);
}
@end

View File

@ -94,7 +94,7 @@ material_to_rgba :: (mat: Material) -> (r: u8, g: u8, b: u8, a: u8) {
r : u8 = cast(u8) (mat.color.x * 255.0);
g : u8 = cast(u8) (mat.color.y * 255.0);
b : u8 = cast(u8) (mat.color.z * 255.0);
a : u8 = material_encode_to_char(mat); // @ToDo: Do actual material value encode here.
a : u8 = material_encode_to_char(mat);
return r,g,b,a;
}

View File

@ -5,23 +5,29 @@ World_Config :: struct {
horizonHalo : Vector3 = .{1.0, 1.0, 1.0};
sunHalo : Vector3 = .{1.0, 1.0, 1.0};
sunLightColor : Vector3 = .{1.0, 1.0, 1.0};
sunPosition : Vector3 = #run normalize(Vector3.{0.5, 0.5, 0.5});
sunPosition : Vector3 = #run normalize(Vector3.{0.2, 0.3, 0.4});
sunIntensity : float = 2.0;
hasClouds : int = 1;
hasClouds : s32 = 1;
hasPlane : int = 0;
hasPlane : s32 = 0;
planeHeight : float = 0.0;
planeType : int = 0;
planeType : s32 = 0;
}
// Copies over all the fields of our world config into a given shader type.
// Requires that the shader type has all of the fields the world config has.
world_config_to_shader_type :: (wc: *World_Config, data: *$T) {
data.skyBase = wc.skyBase.component;
data.skyTop = wc.skyTop.component;
data.sunDisk = wc.sunDisk.component;
data.horizonHalo = wc.horizonHalo.component;
data.sunHalo = wc.sunHalo.component;
data.time = cast(float) get_time();
generate_copy_code :: () -> string {
builder : String_Builder;
ti := type_info(World_Config);
for ti.members {
if it.type == type_info(Vector3) then print_to_builder(*builder, "data.% = wc.%.component;\n", it.name, it.name);
else print_to_builder(*builder, "data.% = wc.%;\n", it.name, it.name);
}
return builder_to_string(*builder);
}
#insert #run generate_copy_code();
}
IVector3 :: struct {