Browse Source

Add u_invModelView uniform to shader and documentation (#3421)

Luigi Malomo 8 months ago
parent
commit
c8f237a39b

+ 1 - 0
bindings/bf/bgfx.bf

@@ -3398,6 +3398,7 @@ public static class bgfx
 	///      - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices.
 	///      - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices.
 	///      - `u_modelView mat4` - concatenated model view matrix, only first
 	///      - `u_modelView mat4` - concatenated model view matrix, only first
 	///        model matrix from array is used.
 	///        model matrix from array is used.
+	///      - `u_invModelView mat4` - inverted concatenated model view matrix.
 	///      - `u_modelViewProj mat4` - concatenated model view projection matrix.
 	///      - `u_modelViewProj mat4` - concatenated model view projection matrix.
 	///      - `u_alphaRef float` - alpha reference value for alpha test.
 	///      - `u_alphaRef float` - alpha reference value for alpha test.
 	/// </summary>
 	/// </summary>

+ 1 - 0
bindings/cs/bgfx.cs

@@ -3353,6 +3353,7 @@ public static partial class bgfx
 	///      - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices.
 	///      - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices.
 	///      - `u_modelView mat4` - concatenated model view matrix, only first
 	///      - `u_modelView mat4` - concatenated model view matrix, only first
 	///        model matrix from array is used.
 	///        model matrix from array is used.
+	///      - `u_invModelView mat4` - inverted concatenated model view matrix.
 	///      - `u_modelViewProj mat4` - concatenated model view projection matrix.
 	///      - `u_modelViewProj mat4` - concatenated model view projection matrix.
 	///      - `u_alphaRef float` - alpha reference value for alpha test.
 	///      - `u_alphaRef float` - alpha reference value for alpha test.
 	/// </summary>
 	/// </summary>

+ 1 - 0
bindings/d/package.d

@@ -2807,6 +2807,7 @@ mixin(joinFnBinds((){
 		*      - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices.
 		*      - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices.
 		*      - `u_modelView mat4` - concatenated model view matrix, only first
 		*      - `u_modelView mat4` - concatenated model view matrix, only first
 		*        model matrix from array is used.
 		*        model matrix from array is used.
+		*      - `u_invModelView mat4` - inverted concatenated model view matrix.
 		*      - `u_modelViewProj mat4` - concatenated model view projection matrix.
 		*      - `u_modelViewProj mat4` - concatenated model view projection matrix.
 		*      - `u_alphaRef float` - alpha reference value for alpha test.
 		*      - `u_alphaRef float` - alpha reference value for alpha test.
 		Params:
 		Params:

+ 1 - 0
bindings/zig/bgfx.zig

@@ -2731,6 +2731,7 @@ extern fn bgfx_destroy_frame_buffer(_handle: FrameBufferHandle) void;
 ///      - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices.
 ///      - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices.
 ///      - `u_modelView mat4` - concatenated model view matrix, only first
 ///      - `u_modelView mat4` - concatenated model view matrix, only first
 ///        model matrix from array is used.
 ///        model matrix from array is used.
+///      - `u_invModelView mat4` - inverted concatenated model view matrix.
 ///      - `u_modelViewProj mat4` - concatenated model view projection matrix.
 ///      - `u_modelViewProj mat4` - concatenated model view projection matrix.
 ///      - `u_alphaRef float` - alpha reference value for alpha test.
 ///      - `u_alphaRef float` - alpha reference value for alpha test.
 /// <param name="_name">Uniform name in shader.</param>
 /// <param name="_name">Uniform name in shader.</param>

+ 1 - 0
docs/tools.rst

@@ -128,6 +128,7 @@ Predefined Uniforms
   mat4    u_invViewProj       Transform clip-to-world  space.
   mat4    u_invViewProj       Transform clip-to-world  space.
   mat4[N] u_model             Transform local-to-world space array.
   mat4[N] u_model             Transform local-to-world space array.
   mat4    u_modelView         Transform local-to-view  space.
   mat4    u_modelView         Transform local-to-view  space.
+  mat4    u_invModelView      Transform view-to-local  space.
   mat4    u_modelViewProj     Transform local-to-clip  space.
   mat4    u_modelViewProj     Transform local-to-clip  space.
   float   u_alphaRef          | The reference value to which incoming alpha
   float   u_alphaRef          | The reference value to which incoming alpha
                               | values are compared.
                               | values are compared.

+ 1 - 0
include/bgfx/bgfx.h

@@ -3244,6 +3244,7 @@ namespace bgfx
 	///      - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices.
 	///      - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices.
 	///      - `u_modelView mat4` - concatenated model view matrix, only first
 	///      - `u_modelView mat4` - concatenated model view matrix, only first
 	///        model matrix from array is used.
 	///        model matrix from array is used.
+	//       - `u_invModelView mat4` - inverted model view matrix.
 	///      - `u_modelViewProj mat4` - concatenated model view projection matrix.
 	///      - `u_modelViewProj mat4` - concatenated model view projection matrix.
 	///      - `u_alphaRef float` - alpha reference value for alpha test.
 	///      - `u_alphaRef float` - alpha reference value for alpha test.
 	///
 	///

+ 1 - 0
include/bgfx/c99/bgfx.h

@@ -2167,6 +2167,7 @@ BGFX_C_API void bgfx_destroy_frame_buffer(bgfx_frame_buffer_handle_t _handle);
  *      - `u_modelView mat4` - concatenated model view matrix, only first
  *      - `u_modelView mat4` - concatenated model view matrix, only first
  *        model matrix from array is used.
  *        model matrix from array is used.
  *      - `u_modelViewProj mat4` - concatenated model view projection matrix.
  *      - `u_modelViewProj mat4` - concatenated model view projection matrix.
+ *      - `u_invModelView mat4` - inverted concatenated model view matrix.
  *      - `u_alphaRef float` - alpha reference value for alpha test.
  *      - `u_alphaRef float` - alpha reference value for alpha test.
  *
  *
  * @param[in] _name Uniform name in shader.
  * @param[in] _name Uniform name in shader.

+ 1 - 0
scripts/bgfx.idl

@@ -1954,6 +1954,7 @@ func.destroy { cname = "destroy_frame_buffer" }
 ---      - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices.
 ---      - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices.
 ---      - `u_modelView mat4` - concatenated model view matrix, only first
 ---      - `u_modelView mat4` - concatenated model view matrix, only first
 ---        model matrix from array is used.
 ---        model matrix from array is used.
+---      - `u_invModelView mat4` - inverted concatenated model view matrix.
 ---      - `u_modelViewProj mat4` - concatenated model view projection matrix.
 ---      - `u_modelViewProj mat4` - concatenated model view projection matrix.
 ---      - `u_alphaRef float` - alpha reference value for alpha test.
 ---      - `u_alphaRef float` - alpha reference value for alpha test.
 ---
 ---

+ 1 - 0
src/bgfx.cpp

@@ -982,6 +982,7 @@ namespace bgfx
 		"u_invViewProj",
 		"u_invViewProj",
 		"u_model",
 		"u_model",
 		"u_modelView",
 		"u_modelView",
+		"u_invModelView",
 		"u_modelViewProj",
 		"u_modelViewProj",
 		"u_alphaRef4",
 		"u_alphaRef4",
 	};
 	};

+ 1 - 0
src/bgfx_p.h

@@ -843,6 +843,7 @@ namespace bgfx
 			InvViewProj,
 			InvViewProj,
 			Model,
 			Model,
 			ModelView,
 			ModelView,
+			InvModelView,
 			ModelViewProj,
 			ModelViewProj,
 			AlphaRef,
 			AlphaRef,
 			Count
 			Count

+ 1 - 0
src/bgfx_shader.sh

@@ -721,6 +721,7 @@ uniform mat4  u_viewProj;
 uniform mat4  u_invViewProj;
 uniform mat4  u_invViewProj;
 uniform mat4  u_model[BGFX_CONFIG_MAX_BONES];
 uniform mat4  u_model[BGFX_CONFIG_MAX_BONES];
 uniform mat4  u_modelView;
 uniform mat4  u_modelView;
+uniform mat4  u_invModelView;
 uniform mat4  u_modelViewProj;
 uniform mat4  u_modelViewProj;
 uniform vec4  u_alphaRef4;
 uniform vec4  u_alphaRef4;
 #define u_alphaRef u_alphaRef4.x
 #define u_alphaRef u_alphaRef4.x

+ 20 - 0
src/renderer.h

@@ -244,6 +244,26 @@ namespace bgfx
 					}
 					}
 					break;
 					break;
 
 
+				case PredefinedUniform::InvModelView:
+					{
+						Matrix4 modelView;
+						Matrix4 invModelView;
+						const Matrix4& model = frameCache.m_matrixCache.m_cache[_draw.m_startMatrix];
+						bx::model4x4_mul(&modelView.un.f4x4
+							, &model.un.f4x4
+							, &m_view[_view].un.f4x4
+							);
+						bx::float4x4_inverse(&invModelView.un.f4x4
+							, &modelView.un.f4x4
+							);
+						_renderer->setShaderUniform4x4f(flags
+							, predefined.m_loc
+							, invModelView.un.val
+							, bx::uint32_min(mtxRegs, predefined.m_count)
+							);
+					}
+					break;
+
 				case PredefinedUniform::ModelViewProj:
 				case PredefinedUniform::ModelViewProj:
 					{
 					{
 						Matrix4 modelViewProj;
 						Matrix4 modelViewProj;