wined3d: Factor out compute_normal_matrix() function.

Signed-off-by: Paul Gofman <gofmanp@gmail.com>
Signed-off-by: Henri Verbeet <hverbeet@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Paul Gofman 2019-05-15 18:06:52 +03:00 committed by Alexandre Julliard
parent 0d79d3625e
commit 57bb929e8a
3 changed files with 301 additions and 287 deletions

View File

@ -1296,281 +1296,6 @@ static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps
GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
}
/* Taken and adapted from Mesa. */
static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
{
float pos, neg, t, det;
struct wined3d_matrix temp;
/* Calculate the determinant of upper left 3x3 submatrix and
* determine if the matrix is singular. */
pos = neg = 0.0f;
t = in->_11 * in->_22 * in->_33;
if (t >= 0.0f)
pos += t;
else
neg += t;
t = in->_21 * in->_32 * in->_13;
if (t >= 0.0f)
pos += t;
else
neg += t;
t = in->_31 * in->_12 * in->_23;
if (t >= 0.0f)
pos += t;
else
neg += t;
t = -in->_31 * in->_22 * in->_13;
if (t >= 0.0f)
pos += t;
else
neg += t;
t = -in->_21 * in->_12 * in->_33;
if (t >= 0.0f)
pos += t;
else
neg += t;
t = -in->_11 * in->_32 * in->_23;
if (t >= 0.0f)
pos += t;
else
neg += t;
det = pos + neg;
if (fabsf(det) < 1e-25f)
return FALSE;
det = 1.0f / det;
temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
*out = temp;
return TRUE;
}
static void swap_rows(float **a, float **b)
{
float *tmp = *a;
*a = *b;
*b = tmp;
}
static BOOL invert_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
{
float wtmp[4][8];
float m0, m1, m2, m3, s;
float *r0, *r1, *r2, *r3;
r0 = wtmp[0];
r1 = wtmp[1];
r2 = wtmp[2];
r3 = wtmp[3];
r0[0] = m->_11;
r0[1] = m->_12;
r0[2] = m->_13;
r0[3] = m->_14;
r0[4] = 1.0f;
r0[5] = r0[6] = r0[7] = 0.0f;
r1[0] = m->_21;
r1[1] = m->_22;
r1[2] = m->_23;
r1[3] = m->_24;
r1[5] = 1.0f;
r1[4] = r1[6] = r1[7] = 0.0f;
r2[0] = m->_31;
r2[1] = m->_32;
r2[2] = m->_33;
r2[3] = m->_34;
r2[6] = 1.0f;
r2[4] = r2[5] = r2[7] = 0.0f;
r3[0] = m->_41;
r3[1] = m->_42;
r3[2] = m->_43;
r3[3] = m->_44;
r3[7] = 1.0f;
r3[4] = r3[5] = r3[6] = 0.0f;
/* Choose pivot - or die. */
if (fabsf(r3[0]) > fabsf(r2[0]))
swap_rows(&r3, &r2);
if (fabsf(r2[0]) > fabsf(r1[0]))
swap_rows(&r2, &r1);
if (fabsf(r1[0]) > fabsf(r0[0]))
swap_rows(&r1, &r0);
if (r0[0] == 0.0f)
return FALSE;
/* Eliminate first variable. */
m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
s = r0[4];
if (s != 0.0f)
{
r1[4] -= m1 * s;
r2[4] -= m2 * s;
r3[4] -= m3 * s;
}
s = r0[5];
if (s != 0.0f)
{
r1[5] -= m1 * s;
r2[5] -= m2 * s;
r3[5] -= m3 * s;
}
s = r0[6];
if (s != 0.0f)
{
r1[6] -= m1 * s;
r2[6] -= m2 * s;
r3[6] -= m3 * s;
}
s = r0[7];
if (s != 0.0f)
{
r1[7] -= m1 * s;
r2[7] -= m2 * s;
r3[7] -= m3 * s;
}
/* Choose pivot - or die. */
if (fabsf(r3[1]) > fabsf(r2[1]))
swap_rows(&r3, &r2);
if (fabsf(r2[1]) > fabsf(r1[1]))
swap_rows(&r2, &r1);
if (r1[1] == 0.0f)
return FALSE;
/* Eliminate second variable. */
m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
s = r1[4];
if (s != 0.0f)
{
r2[4] -= m2 * s;
r3[4] -= m3 * s;
}
s = r1[5];
if (s != 0.0f)
{
r2[5] -= m2 * s;
r3[5] -= m3 * s;
}
s = r1[6];
if (s != 0.0f)
{
r2[6] -= m2 * s;
r3[6] -= m3 * s;
}
s = r1[7];
if (s != 0.0f)
{
r2[7] -= m2 * s;
r3[7] -= m3 * s;
}
/* Choose pivot - or die. */
if (fabsf(r3[2]) > fabsf(r2[2]))
swap_rows(&r3, &r2);
if (r2[2] == 0.0f)
return FALSE;
/* Eliminate third variable. */
m3 = r3[2] / r2[2];
r3[3] -= m3 * r2[3];
r3[4] -= m3 * r2[4];
r3[5] -= m3 * r2[5];
r3[6] -= m3 * r2[6];
r3[7] -= m3 * r2[7];
/* Last check. */
if (r3[3] == 0.0f)
return FALSE;
/* Back substitute row 3. */
s = 1.0f / r3[3];
r3[4] *= s;
r3[5] *= s;
r3[6] *= s;
r3[7] *= s;
/* Back substitute row 2. */
m2 = r2[3];
s = 1.0f / r2[2];
r2[4] = s * (r2[4] - r3[4] * m2);
r2[5] = s * (r2[5] - r3[5] * m2);
r2[6] = s * (r2[6] - r3[6] * m2);
r2[7] = s * (r2[7] - r3[7] * m2);
m1 = r1[3];
r1[4] -= r3[4] * m1;
r1[5] -= r3[5] * m1;
r1[6] -= r3[6] * m1;
r1[7] -= r3[7] * m1;
m0 = r0[3];
r0[4] -= r3[4] * m0;
r0[5] -= r3[5] * m0;
r0[6] -= r3[6] * m0;
r0[7] -= r3[7] * m0;
/* Back substitute row 1. */
m1 = r1[2];
s = 1.0f / r1[1];
r1[4] = s * (r1[4] - r2[4] * m1);
r1[5] = s * (r1[5] - r2[5] * m1);
r1[6] = s * (r1[6] - r2[6] * m1);
r1[7] = s * (r1[7] - r2[7] * m1);
m0 = r0[2];
r0[4] -= r2[4] * m0;
r0[5] -= r2[5] * m0;
r0[6] -= r2[6] * m0;
r0[7] -= r2[7] * m0;
/* Back substitute row 0. */
m0 = r0[1];
s = 1.0f / r0[0];
r0[4] = s * (r0[4] - r1[4] * m0);
r0[5] = s * (r0[5] - r1[5] * m0);
r0[6] = s * (r0[6] - r1[6] * m0);
r0[7] = s * (r0[7] - r1[7] * m0);
out->_11 = r0[4];
out->_12 = r0[5];
out->_13 = r0[6];
out->_14 = r0[7];
out->_21 = r1[4];
out->_22 = r1[5];
out->_23 = r1[6];
out->_24 = r1[7];
out->_31 = r2[4];
out->_32 = r2[5];
out->_33 = r2[6];
out->_34 = r2[7];
out->_41 = r3[4];
out->_42 = r3[5];
out->_43 = r3[6];
out->_44 = r3[7];
return TRUE;
}
static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
{
struct wined3d_matrix temp;
@ -1587,24 +1312,14 @@ static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_con
const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
{
const struct wined3d_gl_info *gl_info = context->gl_info;
float mat[3 * 3];
struct wined3d_matrix mv;
unsigned int i, j;
float mat[3 * 3];
if (prog->vs.normal_matrix_location == -1)
return;
get_modelview_matrix(context, state, 0, &mv);
if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
invert_matrix_3d(&mv, &mv);
else
invert_matrix(&mv, &mv);
/* Tests show that singular modelview matrices are used unchanged as normal
* matrices on D3D3 and older. There seems to be no clearly consistent
* behavior on newer D3D versions so always follow older ddraw behavior. */
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j)
mat[i * 3 + j] = (&mv._11)[j * 4 + i];
compute_normal_matrix(mat, context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING, &mv);
GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
checkGLcall("glUniformMatrix3fv");

View File

@ -6713,3 +6713,297 @@ BOOL wined3d_array_reserve(void **elements, SIZE_T *capacity, SIZE_T count, SIZE
*capacity = new_capacity;
return TRUE;
}
static void swap_rows(float **a, float **b)
{
float *tmp = *a;
*a = *b;
*b = tmp;
}
BOOL invert_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
{
float wtmp[4][8];
float m0, m1, m2, m3, s;
float *r0, *r1, *r2, *r3;
r0 = wtmp[0];
r1 = wtmp[1];
r2 = wtmp[2];
r3 = wtmp[3];
r0[0] = m->_11;
r0[1] = m->_12;
r0[2] = m->_13;
r0[3] = m->_14;
r0[4] = 1.0f;
r0[5] = r0[6] = r0[7] = 0.0f;
r1[0] = m->_21;
r1[1] = m->_22;
r1[2] = m->_23;
r1[3] = m->_24;
r1[5] = 1.0f;
r1[4] = r1[6] = r1[7] = 0.0f;
r2[0] = m->_31;
r2[1] = m->_32;
r2[2] = m->_33;
r2[3] = m->_34;
r2[6] = 1.0f;
r2[4] = r2[5] = r2[7] = 0.0f;
r3[0] = m->_41;
r3[1] = m->_42;
r3[2] = m->_43;
r3[3] = m->_44;
r3[7] = 1.0f;
r3[4] = r3[5] = r3[6] = 0.0f;
/* Choose pivot - or die. */
if (fabsf(r3[0]) > fabsf(r2[0]))
swap_rows(&r3, &r2);
if (fabsf(r2[0]) > fabsf(r1[0]))
swap_rows(&r2, &r1);
if (fabsf(r1[0]) > fabsf(r0[0]))
swap_rows(&r1, &r0);
if (r0[0] == 0.0f)
return FALSE;
/* Eliminate first variable. */
m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
s = r0[4];
if (s != 0.0f)
{
r1[4] -= m1 * s;
r2[4] -= m2 * s;
r3[4] -= m3 * s;
}
s = r0[5];
if (s != 0.0f)
{
r1[5] -= m1 * s;
r2[5] -= m2 * s;
r3[5] -= m3 * s;
}
s = r0[6];
if (s != 0.0f)
{
r1[6] -= m1 * s;
r2[6] -= m2 * s;
r3[6] -= m3 * s;
}
s = r0[7];
if (s != 0.0f)
{
r1[7] -= m1 * s;
r2[7] -= m2 * s;
r3[7] -= m3 * s;
}
/* Choose pivot - or die. */
if (fabsf(r3[1]) > fabsf(r2[1]))
swap_rows(&r3, &r2);
if (fabsf(r2[1]) > fabsf(r1[1]))
swap_rows(&r2, &r1);
if (r1[1] == 0.0f)
return FALSE;
/* Eliminate second variable. */
m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
s = r1[4];
if (s != 0.0f)
{
r2[4] -= m2 * s;
r3[4] -= m3 * s;
}
s = r1[5];
if (s != 0.0f)
{
r2[5] -= m2 * s;
r3[5] -= m3 * s;
}
s = r1[6];
if (s != 0.0f)
{
r2[6] -= m2 * s;
r3[6] -= m3 * s;
}
s = r1[7];
if (s != 0.0f)
{
r2[7] -= m2 * s;
r3[7] -= m3 * s;
}
/* Choose pivot - or die. */
if (fabsf(r3[2]) > fabsf(r2[2]))
swap_rows(&r3, &r2);
if (r2[2] == 0.0f)
return FALSE;
/* Eliminate third variable. */
m3 = r3[2] / r2[2];
r3[3] -= m3 * r2[3];
r3[4] -= m3 * r2[4];
r3[5] -= m3 * r2[5];
r3[6] -= m3 * r2[6];
r3[7] -= m3 * r2[7];
/* Last check. */
if (r3[3] == 0.0f)
return FALSE;
/* Back substitute row 3. */
s = 1.0f / r3[3];
r3[4] *= s;
r3[5] *= s;
r3[6] *= s;
r3[7] *= s;
/* Back substitute row 2. */
m2 = r2[3];
s = 1.0f / r2[2];
r2[4] = s * (r2[4] - r3[4] * m2);
r2[5] = s * (r2[5] - r3[5] * m2);
r2[6] = s * (r2[6] - r3[6] * m2);
r2[7] = s * (r2[7] - r3[7] * m2);
m1 = r1[3];
r1[4] -= r3[4] * m1;
r1[5] -= r3[5] * m1;
r1[6] -= r3[6] * m1;
r1[7] -= r3[7] * m1;
m0 = r0[3];
r0[4] -= r3[4] * m0;
r0[5] -= r3[5] * m0;
r0[6] -= r3[6] * m0;
r0[7] -= r3[7] * m0;
/* Back substitute row 1. */
m1 = r1[2];
s = 1.0f / r1[1];
r1[4] = s * (r1[4] - r2[4] * m1);
r1[5] = s * (r1[5] - r2[5] * m1);
r1[6] = s * (r1[6] - r2[6] * m1);
r1[7] = s * (r1[7] - r2[7] * m1);
m0 = r0[2];
r0[4] -= r2[4] * m0;
r0[5] -= r2[5] * m0;
r0[6] -= r2[6] * m0;
r0[7] -= r2[7] * m0;
/* Back substitute row 0. */
m0 = r0[1];
s = 1.0f / r0[0];
r0[4] = s * (r0[4] - r1[4] * m0);
r0[5] = s * (r0[5] - r1[5] * m0);
r0[6] = s * (r0[6] - r1[6] * m0);
r0[7] = s * (r0[7] - r1[7] * m0);
out->_11 = r0[4];
out->_12 = r0[5];
out->_13 = r0[6];
out->_14 = r0[7];
out->_21 = r1[4];
out->_22 = r1[5];
out->_23 = r1[6];
out->_24 = r1[7];
out->_31 = r2[4];
out->_32 = r2[5];
out->_33 = r2[6];
out->_34 = r2[7];
out->_41 = r3[4];
out->_42 = r3[5];
out->_43 = r3[6];
out->_44 = r3[7];
return TRUE;
}
/* Taken and adapted from Mesa. */
static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
{
float pos, neg, t, det;
struct wined3d_matrix temp;
/* Calculate the determinant of upper left 3x3 submatrix and
* determine if the matrix is singular. */
pos = neg = 0.0f;
t = in->_11 * in->_22 * in->_33;
if (t >= 0.0f)
pos += t;
else
neg += t;
t = in->_21 * in->_32 * in->_13;
if (t >= 0.0f)
pos += t;
else
neg += t;
t = in->_31 * in->_12 * in->_23;
if (t >= 0.0f)
pos += t;
else
neg += t;
t = -in->_31 * in->_22 * in->_13;
if (t >= 0.0f)
pos += t;
else
neg += t;
t = -in->_21 * in->_12 * in->_33;
if (t >= 0.0f)
pos += t;
else
neg += t;
t = -in->_11 * in->_32 * in->_23;
if (t >= 0.0f)
pos += t;
else
neg += t;
det = pos + neg;
if (fabsf(det) < 1e-25f)
return FALSE;
det = 1.0f / det;
temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
*out = temp;
return TRUE;
}
void compute_normal_matrix(float *normal_matrix, BOOL legacy_lighting,
const struct wined3d_matrix *modelview)
{
struct wined3d_matrix mv;
unsigned int i, j;
mv = *modelview;
if (legacy_lighting)
invert_matrix_3d(&mv, &mv);
else
invert_matrix(&mv, &mv);
/* Tests show that singular modelview matrices are used unchanged as normal
* matrices on D3D3 and older. There seems to be no clearly consistent
* behavior on newer D3D versions so always follow older ddraw behavior. */
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j)
normal_matrix[i * 3 + j] = (&mv._11)[j * 4 + i];
}

View File

@ -4850,6 +4850,11 @@ static inline enum wined3d_material_color_source validate_material_colour_source
return WINED3D_MCS_MATERIAL;
}
BOOL invert_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m) DECLSPEC_HIDDEN;
void compute_normal_matrix(float *normal_matrix, BOOL legacy_lighting,
const struct wined3d_matrix *modelview) DECLSPEC_HIDDEN;
/* The WNDCLASS-Name for the fake window which we use to retrieve the GL capabilities */
#define WINED3D_OPENGL_WINDOW_CLASS_NAME "WineD3D_OpenGL"