fix rdtsc, example for rdtsc -> real time, start work on lighting

This commit is contained in:
matthew 2025-07-23 08:16:27 +10:00
parent 6c5beac00b
commit fd03759f57
13 changed files with 84 additions and 35 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -14,6 +14,25 @@ void main()
r.Renderer rd = r.Init(&window); r.Renderer rd = r.Init(&window);
scope(exit) r.Destroy(&rd); scope(exit) r.Destroy(&rd);
u64 os_freq = OSTimeFreq();
u64 cpu_start = RDTSC();
u64 os_start = OSTime();
u64 os_end = 0;
u64 os_elapsed = 0;
while (os_elapsed < os_freq)
{
os_end = OSTime();
os_elapsed = os_end - os_start;
}
u64 cpu_end = RDTSC();
u64 cpu_elapsed = cpu_end - cpu_start;
writefln(" OS Timer: %s -> %s = %s elapsed", os_start, os_end, os_elapsed);
writefln("OS Seconds: %.4f", cast(f64)(os_elapsed)/cast(f64)(os_freq));
writefln(" CPU Timer: %s -> %s = %s elapsed", cpu_start, cpu_end, cpu_elapsed);
while (true) while (true)
{ {
p.HandleEvents(&window); p.HandleEvents(&window);

View File

@ -49,6 +49,24 @@ enum BufferType : int
alias BT = BufferType; alias BT = BufferType;
struct GlobalUniforms
{
Mat4 view_matrix;
Mat4 projection_matrix;
Vec2 res;
}
struct ShaderUniforms
{
f32 placeholder;
}
struct PushConst
{
Mat4 model_matrix;
u32 mat_id;
}
enum PipelineType : int enum PipelineType : int
{ {
Graphics, Graphics,
@ -109,25 +127,15 @@ struct Renderer
struct Camera struct Camera
{ {
Vec3 pos = Vec3(0.0); Vec3 pos = Vec3(0.0, 0.0, 3.0);
Vec3 target = Vec3(0.0); Vec3 front = Vec3(0.0, 0.0, -1.0);
} Vec3 up = Vec3(0.0, 1.0, 0.0);
struct GlobalUniforms
{
Mat4 world_matrix;
Vec2 res;
}
struct ShaderUniforms
{
f32 placeholder;
} }
extern(C) struct Material extern(C) struct Material
{ {
Vec4 ambient; Vec4 ambient;
Vec4 diffuse; Vec4 albedo;
Vec4 specular; Vec4 specular;
u32 albedo_texture; u32 albedo_texture;
u32 ambient_texture; u32 ambient_texture;
@ -227,9 +235,9 @@ Init(p.Window* window)
rd.ui_pipeline = BuildGfxPipeline(&rd, &ui_info); rd.ui_pipeline = BuildGfxPipeline(&rd, &ui_info);
rd.compute_pipeline = BuildCompPipeline(&rd, &gradient_info); rd.compute_pipeline = BuildCompPipeline(&rd, &gradient_info);
rd.model = LoadModel(&rd, "models/Tree01.m3d"); rd.model = LoadModel(&rd, "models/yoda.m3d");
ReadModel(&rd, "models/Tree01.m3d"); ReadModel(&rd, "models/yoda.m3d");
return rd; return rd;
} }
@ -239,11 +247,7 @@ Cycle(Renderer* rd)
{ {
rd.ui_count = 0; rd.ui_count = 0;
f32 radius = 10.0; rd.globals.view_matrix = Mat4(1.0);
f32 cam_x = sin(RDTSC()) * radius * 0.000000000001;
f32 cam_z = cos(RDTSC()) * radius * 0.000000000001;
rd.globals.world_matrix = Mat4(1.0);
BeginFrame(rd); BeginFrame(rd);
@ -460,6 +464,7 @@ LoadModel(Renderer* rd, string name)
{ {
switch (m3d.material[i].prop[j].type) switch (m3d.material[i].prop[j].type)
{ {
case m3dp_Kd: ConvertColor(&mats[i].albedo, m3d.material[i].prop[j].value.color); break;
case m3dp_Ka: ConvertColor(&mats[i].ambient, m3d.material[i].prop[j].value.color); break; case m3dp_Ka: ConvertColor(&mats[i].ambient, m3d.material[i].prop[j].value.color); break;
case m3dp_Ks: ConvertColor(&mats[i].specular, m3d.material[i].prop[j].value.color); break; case m3dp_Ks: ConvertColor(&mats[i].specular, m3d.material[i].prop[j].value.color); break;
case m3dp_Ns: mats[i].shininess = m3d.material[i].prop[j].value.fnum; break; case m3dp_Ns: mats[i].shininess = m3d.material[i].prop[j].value.fnum; break;
@ -478,7 +483,7 @@ LoadModel(Renderer* rd, string name)
mats[i].specular_texture = tex_lookup[m3d.material[i].prop[j].value.textureid]; mats[i].specular_texture = tex_lookup[m3d.material[i].prop[j].value.textureid];
mats[i].specular_has_texture = true; mats[i].specular_has_texture = true;
} break; } break;
default: break; default: Logf("Unimplemented: %s", m3d.material[i].prop[j].type); break;
} }
} }

View File

@ -91,11 +91,6 @@ enum DescType : u32
alias DT = DescType; alias DT = DescType;
struct PushConst
{
u32 mat_id;
}
struct Image struct Image
{ {
VkImage image; VkImage image;
@ -1269,6 +1264,7 @@ CreateGraphicsPipeline(Vulkan* vk, GfxPipelineInfo* build_info)
VkPipelineRasterizationStateCreateInfo rasterization_info = { VkPipelineRasterizationStateCreateInfo rasterization_info = {
sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
cullMode: VK_CULL_MODE_BACK_BIT,
polygonMode: VK_POLYGON_MODE_FILL, polygonMode: VK_POLYGON_MODE_FILL,
lineWidth: 1.0, lineWidth: 1.0,
frontFace: VK_FRONT_FACE_COUNTER_CLOCKWISE, frontFace: VK_FRONT_FACE_COUNTER_CLOCKWISE,

View File

@ -24,12 +24,14 @@ void main()
{ {
gl_Position = in_pos * y_matrix; gl_Position = in_pos * y_matrix;
vec4 col = Materials[nonuniformEXT(PC.mat_id)].diffuse;
if (Materials[nonuniformEXT(PC.mat_id)].albedo_has_texture) if (Materials[nonuniformEXT(PC.mat_id)].albedo_has_texture)
{ {
out_col = texture(sampler2D(Textures[nonuniformEXT(Materials[nonuniformEXT(PC.mat_id)].albedo_texture)], SamplerNearest), in_uv); vec4 tex_col = texture(sampler2D(Textures[nonuniformEXT(Materials[nonuniformEXT(PC.mat_id)].albedo_texture)], SamplerNearest), in_uv);
} col = mix(col, tex_col, 0.5);
else
{
out_col = Materials[nonuniformEXT(PC.mat_id)].diffuse;
} }
out_col = col;
out_uv = in_uv;
} }

View File

@ -3,7 +3,8 @@
// **************************************************************************** // ****************************************************************************
layout (set = 0, binding = 0) uniform GlobalUniforms { layout (set = 0, binding = 0) uniform GlobalUniforms {
mat4 world_matrix; mat4 view_matrix;
mat4 projection_matrix;
vec2 res; vec2 res;
} G; } G;
@ -31,5 +32,6 @@ layout (set = 2, binding = 0) uniform Material {
} Materials[]; } Materials[];
layout (push_constant) uniform Constants { layout (push_constant) uniform Constants {
mat4 model_matrix;
uint mat_id; uint mat_id;
} PC; } PC;

View File

@ -359,7 +359,7 @@ Hash(string str)
return xxh3_64bits_withSeed(str.ptr, str.length, HASH_SEED); return xxh3_64bits_withSeed(str.ptr, str.length, HASH_SEED);
} }
u64 pragma(inline): u64
RDTSC() RDTSC()
{ {
union u64_split union u64_split
@ -367,8 +367,8 @@ RDTSC()
u64 full; u64 full;
struct struct
{ {
u32 upper;
u32 lower; u32 lower;
u32 upper;
}; };
}; };
@ -386,3 +386,28 @@ RDTSC()
return val.full; return val.full;
} }
pragma(inline): u64
OSTimeFreq()
{
version (linux)
{
u64 freq = 1000000;
}
return freq;
}
pragma(inline): u64
OSTime()
{
version(linux)
{
import core.sys.linux.sys.time;
timeval value;
gettimeofday(&value, null);
u64 time = OSTimeFreq() * cast(u64)(value.tv_sec) + cast(u64)(value.tv_usec);
}
return time;
}