Move viewport matrix multiplication out of vertex shader
This commit is contained in:
parent
319bad9659
commit
97622d0bf7
@ -75,8 +75,7 @@ internal void render_triangle(const Triangle *triangle, const Model *model,
|
|||||||
};
|
};
|
||||||
|
|
||||||
for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
|
for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
|
||||||
vertices[i] =
|
vertices[i] = run_vertex_shader(shader, &vertices[i]);
|
||||||
run_vertex_shader(shader, &vertices[i], (Buffer *)&render->img);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (render_type == RENDER_TYPE_WIREFRAME) {
|
if (render_type == RENDER_TYPE_WIREFRAME) {
|
||||||
@ -102,11 +101,17 @@ internal void fill_triangle(Render *render, ShaderID shader,
|
|||||||
const Model *model, RenderType type) {
|
const Model *model, RenderType type) {
|
||||||
Image *img = &(render->img);
|
Image *img = &(render->img);
|
||||||
Depth *depth = &(render->depth);
|
Depth *depth = &(render->depth);
|
||||||
TriangleBBox bbox = get_triangle_bbox(img, vertices);
|
|
||||||
|
|
||||||
V3f vert0 = vertices[0];
|
V3f vp_verts[TRIANGLE_VERTICES] = {0};
|
||||||
V3f vert1 = vertices[1];
|
for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
|
||||||
V3f vert2 = vertices[2];
|
vp_verts[i] = get_viewport_vertex(&vertices[i], img);
|
||||||
|
}
|
||||||
|
|
||||||
|
TriangleBBox bbox = get_triangle_bbox(img, vp_verts);
|
||||||
|
|
||||||
|
V3f vert0 = vp_verts[0];
|
||||||
|
V3f vert1 = vp_verts[1];
|
||||||
|
V3f vert2 = vp_verts[2];
|
||||||
V3f point;
|
V3f point;
|
||||||
|
|
||||||
V3f coords;
|
V3f coords;
|
||||||
@ -140,12 +145,12 @@ internal void fill_triangle(Render *render, ShaderID shader,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
px = vertices[0].x * coords.x + vertices[1].x * coords.y +
|
px = vp_verts[0].x * coords.x + vp_verts[1].x * coords.y +
|
||||||
vertices[2].x * coords.z;
|
vp_verts[2].x * coords.z;
|
||||||
py = vertices[0].y * coords.x + vertices[1].y * coords.y +
|
py = vp_verts[0].y * coords.x + vp_verts[1].y * coords.y +
|
||||||
vertices[2].y * coords.z;
|
vp_verts[2].y * coords.z;
|
||||||
pz = vertices[0].z * coords.x + vertices[1].z * coords.y +
|
pz = vp_verts[0].z * coords.x + vp_verts[1].z * coords.y +
|
||||||
vertices[2].z * coords.z;
|
vp_verts[2].z * coords.z;
|
||||||
position = (V3f){px, py, pz};
|
position = (V3f){px, py, pz};
|
||||||
normalise_v3(position);
|
normalise_v3(position);
|
||||||
data.position = position;
|
data.position = position;
|
||||||
@ -207,5 +212,10 @@ internal V3f get_barycentric_coords(V3f a, V3f b, V3f c, V3f p) {
|
|||||||
internal V3f get_viewport_vertex(const V3f *vertex, const Image *img) {
|
internal V3f get_viewport_vertex(const V3f *vertex, const Image *img) {
|
||||||
V4f vh = {.x = vertex->x, .y = 0.0f - vertex->y, .z = vertex->z, .w = 1.0f};
|
V4f vh = {.x = vertex->x, .y = 0.0f - vertex->y, .z = vertex->z, .w = 1.0f};
|
||||||
vh = mat4x4_mul_vec4(viewport(vh.x, vh.y, img->width, img->height), vh);
|
vh = mat4x4_mul_vec4(viewport(vh.x, vh.y, img->width, img->height), vh);
|
||||||
return project_vec4(vh);
|
|
||||||
|
V3f output = project_vec4(vh);
|
||||||
|
output.x = clamp(output.x, 0.0f, img->width);
|
||||||
|
output.y = clamp(output.y, 0.0f, img->height);
|
||||||
|
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
@ -28,10 +28,8 @@ ShaderID register_shader(void *shader, VertexShader *vertex,
|
|||||||
return (ShaderID){g_repository.count};
|
return (ShaderID){g_repository.count};
|
||||||
}
|
}
|
||||||
|
|
||||||
V3f run_vertex_shader(ShaderID shader, const V3f *vertex,
|
V3f run_vertex_shader(ShaderID shader, const V3f *vertex) {
|
||||||
const Buffer *out_buf) {
|
if (IS_INVALID_SHADER(shader) || shader.id > g_repository.count || !vertex) {
|
||||||
if (IS_INVALID_SHADER(shader) || shader.id > g_repository.count || !vertex ||
|
|
||||||
!out_buf) {
|
|
||||||
return (V3f){0};
|
return (V3f){0};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,7 +40,7 @@ V3f run_vertex_shader(ShaderID shader, const V3f *vertex,
|
|||||||
return (V3f){0};
|
return (V3f){0};
|
||||||
}
|
}
|
||||||
|
|
||||||
return vertex_func(shader_obj, vertex, out_buf);
|
return vertex_func(shader_obj, vertex);
|
||||||
}
|
}
|
||||||
|
|
||||||
FragmentResult run_fragment_shader(ShaderID shader, const FragmentData *data,
|
FragmentResult run_fragment_shader(ShaderID shader, const FragmentData *data,
|
||||||
|
@ -29,16 +29,14 @@ struct fragment_data {
|
|||||||
#define DISCARDED_FRAGMENT ((FragmentResult){.discard = true})
|
#define DISCARDED_FRAGMENT ((FragmentResult){.discard = true})
|
||||||
#define DISCARD_FRAGMENT(RESULT) (RESULT.discard)
|
#define DISCARD_FRAGMENT(RESULT) (RESULT.discard)
|
||||||
|
|
||||||
typedef V3f(VertexShader)(void *shader, const V3f *vertex,
|
typedef V3f(VertexShader)(void *shader, const V3f *vertex);
|
||||||
const Buffer *out_buf);
|
|
||||||
typedef FragmentResult(FragmentShader)(void *shader, const FragmentData *data,
|
typedef FragmentResult(FragmentShader)(void *shader, const FragmentData *data,
|
||||||
const Colour *colour,
|
const Colour *colour,
|
||||||
const Model *model);
|
const Model *model);
|
||||||
|
|
||||||
ShaderID register_shader(void *shader, VertexShader *vertex,
|
ShaderID register_shader(void *shader, VertexShader *vertex,
|
||||||
FragmentShader *fragment);
|
FragmentShader *fragment);
|
||||||
V3f run_vertex_shader(ShaderID shader, const V3f *vertex,
|
V3f run_vertex_shader(ShaderID shader, const V3f *vertex);
|
||||||
const Buffer *out_buf);
|
|
||||||
FragmentResult run_fragment_shader(ShaderID shader, const FragmentData *data,
|
FragmentResult run_fragment_shader(ShaderID shader, const FragmentData *data,
|
||||||
const Colour *colour, const Model *model);
|
const Colour *colour, const Model *model);
|
||||||
|
|
||||||
|
@ -32,8 +32,7 @@ internal V3f g_target = {0};
|
|||||||
internal V3f g_up = {0.0f, 1.0f, 0.0f};
|
internal V3f g_up = {0.0f, 1.0f, 0.0f};
|
||||||
internal M4x4f g_cam_matrix = mat4x4_identity;
|
internal M4x4f g_cam_matrix = mat4x4_identity;
|
||||||
|
|
||||||
internal V3f general_shader_vertex(void *shader, const V3f *vertex,
|
internal V3f general_shader_vertex(void *shader, const V3f *vertex);
|
||||||
const Buffer *out_buf);
|
|
||||||
internal FragmentResult phong_shader_fragment(void *shader,
|
internal FragmentResult phong_shader_fragment(void *shader,
|
||||||
const FragmentData *data,
|
const FragmentData *data,
|
||||||
const Colour *colour,
|
const Colour *colour,
|
||||||
@ -66,22 +65,14 @@ void load_shaders(void) {
|
|||||||
albedo_shader_fragment);
|
albedo_shader_fragment);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal V3f general_shader_vertex(void *shader, const V3f *vertex,
|
internal V3f general_shader_vertex(void *shader, const V3f *vertex) {
|
||||||
const Buffer *out_buf) {
|
|
||||||
Shader *shader_ptr = (Shader *)shader;
|
Shader *shader_ptr = (Shader *)shader;
|
||||||
|
|
||||||
V4f vh = {.x = vertex->x, .y = vertex->y, .z = vertex->z, .w = 1.0f};
|
V4f vh = {.x = vertex->x, .y = vertex->y, .z = vertex->z, .w = 1.0f};
|
||||||
vh = mat4x4_mul_vec4(shader_ptr->projection,
|
vh = mat4x4_mul_vec4(shader_ptr->projection,
|
||||||
mat4x4_mul_vec4(shader_ptr->model_view, vh));
|
mat4x4_mul_vec4(shader_ptr->model_view, vh));
|
||||||
vh.y = 0.0 - vh.y;
|
|
||||||
vh = mat4x4_mul_vec4(viewport(vh.x, vh.y, out_buf->width, out_buf->height),
|
|
||||||
vh);
|
|
||||||
|
|
||||||
V3f output = project_vec4(vh);
|
return project_vec4(vh);
|
||||||
output.x = clamp(output.x, 0.0f, out_buf->width);
|
|
||||||
output.y = clamp(output.y, 0.0f, out_buf->height);
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal FragmentResult phong_shader_fragment(void *shader,
|
internal FragmentResult phong_shader_fragment(void *shader,
|
||||||
|
Loading…
Reference in New Issue
Block a user