From 16cba63d43f319ac33bc993eca2b1b51679727b3 Mon Sep 17 00:00:00 2001 From: Matthew Waters Date: Tue, 10 Jul 2018 20:03:12 +1000 Subject: [PATCH] gl/utils: fixup matrix math again for column major e4bf9ed8f060021151cd57e2b00493ed696cb47b was not quite right and changed the wrong thing. Intead we needed to change the multiplication order and should have kept the previous to/from matrices as is done in this patch. --- ext/gl/gstglutils.c | 24 +++++++-------- gst-libs/gst/gl/gstglutils.c | 24 +++++++-------- tests/check/libs/gstglmatrix.c | 53 ++++++++++++++++++++++++++-------- 3 files changed, 65 insertions(+), 36 deletions(-) diff --git a/ext/gl/gstglutils.c b/ext/gl/gstglutils.c index 5014277d0b..12209a6998 100644 --- a/ext/gl/gstglutils.c +++ b/ext/gl/gstglutils.c @@ -115,17 +115,17 @@ static const gfloat identity_matrix[] = { }; static const gfloat from_ndc_matrix[] = { - 0.5, 0.0, 0.0, 0.5, - 0.0, 0.5, 0.0, 0.5, - 0.0, 0.0, 0.5, 0.5, - 0.0, 0.0, 0.0, 1.0, + 0.5, 0.0, 0.0, 0.0, + 0.0, 0.5, 0.0, 0.0, + 0.0, 0.0, 0.5, 0.0, + 0.5, 0.5, 0.5, 1.0, }; static const gfloat to_ndc_matrix[] = { - 2.0, 0.0, 0.0, -1.0, - 0.0, 2.0, 0.0, -1.0, - 0.0, 0.0, 2.0, -1.0, - 0.0, 0.0, 0.0, 1.0, + 2.0, 0.0, 0.0, 0.0, + 0.0, 2.0, 0.0, 0.0, + 0.0, 0.0, 2.0, 0.0, + -1.0, -1.0, -1.0, 1.0, }; void @@ -161,8 +161,8 @@ void gst_gl_get_affine_transformation_meta_as_ndc_ext } else { float tmp[16]; - gst_gl_multiply_matrix4 (to_ndc_matrix, meta->matrix, tmp); - gst_gl_multiply_matrix4 (tmp, from_ndc_matrix, matrix); + gst_gl_multiply_matrix4 (from_ndc_matrix, meta->matrix, tmp); + gst_gl_multiply_matrix4 (tmp, to_ndc_matrix, matrix); } } @@ -173,6 +173,6 @@ void gst_gl_set_affine_transformation_meta_from_ndc_ext g_return_if_fail (meta != NULL); - gst_gl_multiply_matrix4 (from_ndc_matrix, matrix, tmp); - gst_gl_multiply_matrix4 (tmp, to_ndc_matrix, meta->matrix); + gst_gl_multiply_matrix4 (to_ndc_matrix, matrix, tmp); + gst_gl_multiply_matrix4 (tmp, from_ndc_matrix, meta->matrix); } diff --git a/gst-libs/gst/gl/gstglutils.c b/gst-libs/gst/gl/gstglutils.c index f57b38190e..574fadf199 100644 --- a/gst-libs/gst/gl/gstglutils.c +++ b/gst-libs/gst/gl/gstglutils.c @@ -793,17 +793,17 @@ static const gfloat identity_matrix[] = { }; static const gfloat from_ndc_matrix[] = { - 0.5, 0.0, 0.0, 0.5, - 0.0, 0.5, 0.0, 0.5, - 0.0, 0.0, 0.5, 0.5, - 0.0, 0.0, 0.0, 1.0, + 0.5, 0.0, 0.0, 0.0, + 0.0, 0.5, 0.0, 0.0, + 0.0, 0.0, 0.5, 0.0, + 0.5, 0.5, 0.5, 1.0, }; static const gfloat to_ndc_matrix[] = { - 2.0, 0.0, 0.0, -1.0, - 0.0, 2.0, 0.0, -1.0, - 0.0, 0.0, 2.0, -1.0, - 0.0, 0.0, 0.0, 1.0, + 2.0, 0.0, 0.0, 0.0, + 0.0, 2.0, 0.0, 0.0, + 0.0, 0.0, 2.0, 0.0, + -1.0, -1.0, -1.0, 1.0, }; /* multiplies two 4x4 matrices, @a X @b, and stores the result in @result @@ -856,8 +856,8 @@ gst_gl_get_affine_transformation_meta_as_ndc (GstVideoAffineTransformationMeta * float tmp[16]; /* change of basis multiplications */ - gst_gl_multiply_matrix4 (to_ndc_matrix, meta->matrix, tmp); - gst_gl_multiply_matrix4 (tmp, from_ndc_matrix, matrix); + gst_gl_multiply_matrix4 (from_ndc_matrix, meta->matrix, tmp); + gst_gl_multiply_matrix4 (tmp, to_ndc_matrix, matrix); } } @@ -869,6 +869,6 @@ void gst_gl_set_affine_transformation_meta_from_ndc g_return_if_fail (meta != NULL); /* change of basis multiplications */ - gst_gl_multiply_matrix4 (from_ndc_matrix, matrix, tmp); - gst_gl_multiply_matrix4 (tmp, to_ndc_matrix, meta->matrix); + gst_gl_multiply_matrix4 (to_ndc_matrix, matrix, tmp); + gst_gl_multiply_matrix4 (tmp, from_ndc_matrix, meta->matrix); } diff --git a/tests/check/libs/gstglmatrix.c b/tests/check/libs/gstglmatrix.c index d73171625f..2fcdb3ae5b 100644 --- a/tests/check/libs/gstglmatrix.c +++ b/tests/check/libs/gstglmatrix.c @@ -137,7 +137,7 @@ GST_START_TEST (test_matrix_ndc) } GST_END_TEST; -#if 0 + static void transpose_matrix4 (float *m, float *res) { @@ -148,6 +148,8 @@ transpose_matrix4 (float *m, float *res) int idx = i + (j * 4); int swapped_idx = j + (i * 4); + GST_ERROR ("swapping %i %f into %i", idx, m[idx], swapped_idx); + if (i == j) fail_unless (idx == swapped_idx); @@ -155,7 +157,7 @@ transpose_matrix4 (float *m, float *res) } } } -#endif + static float dot4 (float *v1, float *v2) { @@ -174,21 +176,22 @@ _matrix_mult_vertex4 (float *m, float *v, float *res) res[3] = dot4 (&m[12], v); } -#if 0 /* v * m */ -/* Not the prevailing multiplication convention and not really used in - * GStreamer. Kept around for extra testing */ +/* Used because the default is for OpenGL to read matrices transposed on + * uploading */ static void -_vertex_mult_matrix4 (float *v, float *m, float *res) +_vertex_mult_matrix4 (float *m, float *v, float *res) { float tmp[16] = { 0., }; + GST_TRACE ("original matrix"); + debug_matrix (m); transpose_matrix4 (m, tmp); GST_TRACE ("transposed matrix"); debug_matrix (tmp); _matrix_mult_vertex4 (tmp, v, res); } -#endif + GST_START_TEST (test_matrix_vertex_identity) { float identity[] = { @@ -202,7 +205,7 @@ GST_START_TEST (test_matrix_vertex_identity) float res[4] = { 0., }; int i; - _matrix_mult_vertex4 (identity, v, res); + _vertex_mult_matrix4 (identity, v, res); GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v)); GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res)); @@ -228,7 +231,7 @@ GST_START_TEST (test_matrix_vertex_scale) float res[4] = { 0., }; int i; - _matrix_mult_vertex4 (scale, v, res); + _vertex_mult_matrix4 (scale, v, res); GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v)); GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res)); @@ -241,6 +244,31 @@ GST_START_TEST (test_matrix_vertex_scale) GST_END_TEST; +GST_START_TEST (test_matrix_vertex_translate) +{ + float translate_1[] = { + 1., 0., 0., 0., + 0., 1., 0., 0., + 0., 0., 1., 0., + 1., 2., 3., 1., + }; + + float v[] = { 1., 1., 1., 1. }; + float expected[] = { 2., 3., 4., 1. }; + float res[4] = { 0., }; + int i; + + _vertex_mult_matrix4 (translate_1, v, res); + + for (i = 0; i < 4; i++) { + fail_unless (FEQ (res[i], expected[i]), + "value %f at index %u does not match " "expected value %f", res[i], i, + expected[i]); + } +} + +GST_END_TEST; + GST_START_TEST (test_matrix_vertex_y_invert) { GstBuffer *buffer = gst_buffer_new (); @@ -266,7 +294,7 @@ GST_START_TEST (test_matrix_vertex_y_invert) GST_DEBUG ("y-invert"); debug_matrix (y_invert); - _matrix_mult_vertex4 (y_invert, v, res); + _vertex_mult_matrix4 (y_invert, v, res); GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v)); GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res)); @@ -283,7 +311,7 @@ GST_START_TEST (test_matrix_vertex_y_invert) GST_DEBUG ("y-invert from ndc [-1,1]^3 to [0,1]^3"); debug_matrix (aff_meta->matrix); - _matrix_mult_vertex4 (aff_meta->matrix, v, res); + _vertex_mult_matrix4 (aff_meta->matrix, v, res); GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v)); GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res)); @@ -296,7 +324,7 @@ GST_START_TEST (test_matrix_vertex_y_invert) /* test vec4(1,0,1,1) -> vec4(1,1,1,1) */ v[1] = 0.; expected[1] = 1.; - _matrix_mult_vertex4 (aff_meta->matrix, v, res); + _vertex_mult_matrix4 (aff_meta->matrix, v, res); GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v)); GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res)); @@ -320,6 +348,7 @@ gst_gl_matrix_suite (void) tcase_add_test (tc_chain, test_matrix_ndc); tcase_add_test (tc_chain, test_matrix_vertex_identity); tcase_add_test (tc_chain, test_matrix_vertex_scale); + tcase_add_test (tc_chain, test_matrix_vertex_translate); tcase_add_test (tc_chain, test_matrix_vertex_y_invert); return s;