Przeglądaj źródła

shaderc: Removed instMul. (#3536)

Branimir Karadžić 2 tygodni temu
rodzic
commit
aa94189553

+ 6 - 10
examples/06-bump/vs_bump_instanced.sc

@@ -10,31 +10,27 @@ $output v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0
 
 void main()
 {
-	mat4 model;
-	model[0] = i_data0;
-	model[1] = i_data1;
-	model[2] = i_data2;
-	model[3] = i_data3;
+	mat4 model = mtxFromCols(i_data0, i_data1, i_data2, i_data3);
 
-	vec3 wpos = instMul(model, vec4(a_position, 1.0) ).xyz;
+	vec3 wpos = mul(model, vec4(a_position, 1.0) ).xyz;
 	gl_Position = mul(u_viewProj, vec4(wpos, 1.0) );
 
 	vec4 normal = a_normal * 2.0 - 1.0;
-	vec3 wnormal = instMul(model, vec4(normal.xyz, 0.0) ).xyz;
+	vec3 wnormal = mul(model, vec4(normal.xyz, 0.0) ).xyz;
 
 	vec4 tangent = a_tangent * 2.0 - 1.0;
-	vec3 wtangent = instMul(model, vec4(tangent.xyz, 0.0) ).xyz;
+	vec3 wtangent = mul(model, vec4(tangent.xyz, 0.0) ).xyz;
 
 	v_normal = wnormal;
 	v_tangent = wtangent;
 	v_bitangent = cross(v_normal, v_tangent) * tangent.w;
 
-	mat3 tbn = mat3(v_tangent, v_bitangent, v_normal);
+	mat3 tbn = mtxFromCols(v_tangent, v_bitangent, v_normal);
 
 	v_wpos = wpos;
 
 	vec3 weyepos = mul(vec4(0.0, 0.0, 0.0, 1.0), u_view).xyz;
-	v_view = instMul(weyepos - wpos, tbn);
+	v_view = mul(weyepos - wpos, tbn);
 
 	v_texcoord0 = a_texcoord0;
 }

+ 3 - 7
examples/18-ibl/fs_ibl_mesh.sc

@@ -81,14 +81,10 @@ void main()
 	// --edgeFixup warp   //!< This must be used on DirectX9. When fileted with 'warp', fixCubeLookup() should be used.
 	float mip = 1.0 + 5.0*(1.0 - inGloss); // Use mip levels [1..6] for radiance.
 
-	mat4 mtx;
-	mtx[0] = u_mtx0;
-	mtx[1] = u_mtx1;
-	mtx[2] = u_mtx2;
-	mtx[3] = u_mtx3;
+	mat4 mtx = mtxFromCols(u_mtx0, u_mtx1, u_mtx2, u_mtx3);
 	vec3 vr = 2.0*ndotv*nn - vv; // Same as: -reflect(vv, nn);
-	vec3 cubeR = normalize(instMul(mtx, vec4(vr, 0.0)).xyz);
-	vec3 cubeN = normalize(instMul(mtx, vec4(nn, 0.0)).xyz);
+	vec3 cubeR = normalize(mul(mtx, vec4(vr, 0.0)).xyz);
+	vec3 cubeN = normalize(mul(mtx, vec4(nn, 0.0)).xyz);
 	cubeR = fixCubeLookup(cubeR, mip, 256.0);
 
 	vec3 radiance    = toLinear(textureCubeLod(s_texCube, cubeR, mip).xyz);

+ 2 - 6
examples/18-ibl/vs_ibl_skybox.sc

@@ -20,10 +20,6 @@ void main()
 	float aspect = height*(u_viewRect.z / u_viewRect.w);
 	vec2 tex = (2.0*a_texcoord0-1.0) * vec2(aspect, height);
 
-	mat4 mtx;
-	mtx[0] = u_mtx0;
-	mtx[1] = u_mtx1;
-	mtx[2] = u_mtx2;
-	mtx[3] = u_mtx3;
-	v_dir = instMul(mtx, vec4(tex, 1.0, 0.0) ).xyz;
+	mat4 mtx = mtxFromCols(u_mtx0, u_mtx1, u_mtx2, u_mtx3);
+	v_dir = mul(mtx, vec4(tex, 1.0, 0.0) ).xyz;
 }

+ 4 - 4
examples/33-pom/vs_pom.sc

@@ -18,12 +18,12 @@ void main()
 	vec3 t = normalize(mul(u_norm_mtx, vec4(tangent,   0.0) ).xyz);
 	vec3 b = normalize(mul(u_norm_mtx, vec4(bitangent, 0.0) ).xyz);
 	vec3 n = normalize(mul(u_norm_mtx, vec4(normal,    0.0) ).xyz);
-	mat3 tbn = mat3(t, b, n);
+	mat3 tbn = mtxFromCols(t, b, n);
 
-	v_ts_light_pos = instMul(u_light_pos.xyz, tbn);
+	v_ts_light_pos = mul(u_light_pos.xyz, tbn);
 	// Our camera is always at the origin
-	v_ts_view_pos  = instMul(vec3_splat(0.0), tbn);
-	v_ts_frag_pos  = instMul(wpos,            tbn);
+	v_ts_view_pos  = mul(vec3_splat(0.0), tbn);
+	v_ts_frag_pos  = mul(wpos,            tbn);
 
 	v_texcoord0 = a_texcoord0;
 }

+ 2 - 2
examples/37-gpudrivenrendering/cs_gdr_downscale_hi_z.sc

@@ -22,7 +22,7 @@ void main()
 		float maxDepth = 1.0;
 
 		vec4 depths = vec4(
-				imageLoad(s_texOcclusionDepthIn, ivec2(u_inputRTSize.zw * coord.xy                   ) ).x
+			  imageLoad(s_texOcclusionDepthIn, ivec2(u_inputRTSize.zw * coord.xy                   ) ).x
 			, imageLoad(s_texOcclusionDepthIn, ivec2(u_inputRTSize.zw * coord.xy + ivec2(1.0, 0.0) ) ).x
 			, imageLoad(s_texOcclusionDepthIn, ivec2(u_inputRTSize.zw * coord.xy + ivec2(0.0, 1.0) ) ).x
 			, imageLoad(s_texOcclusionDepthIn, ivec2(u_inputRTSize.zw * coord.xy + ivec2(1.0, 1.0) ) ).x
@@ -30,7 +30,7 @@ void main()
 
 		// find and return max depth
 		maxDepth = max(
-				max(depths.x, depths.y)
+			  max(depths.x, depths.y)
 			, max(depths.z, depths.w)
 			);
 

+ 7 - 6
examples/37-gpudrivenrendering/vs_gdr_instanced_indirect_rendering.sc

@@ -10,14 +10,15 @@ $output v_materialID
 
 void main()
 {
-	mat4 model;
-	model[0] = vec4(i_data0.xyz, 0.0);
-	model[1] = i_data1;
-	model[2] = i_data2;
-	model[3] = i_data3;
+	mat4 model = mtxFromCols(
+		  vec4(i_data0.xyz, 0.0)
+		, i_data1
+		, i_data2
+		, i_data3
+		);
 
 	v_materialID = i_data0.w;
 
-	vec4 worldPos = instMul(model, vec4(a_position, 1.0) );
+	vec4 worldPos = mul(model, vec4(a_position, 1.0) );
 	gl_Position = mul(u_viewProj, worldPos);
 }

+ 2 - 6
examples/37-gpudrivenrendering/vs_gdr_render_occlusion.sc

@@ -9,12 +9,8 @@ $input a_position, i_data0, i_data1, i_data2, i_data3, i_data4
 
 void main()
 {
-	mat4 model;
-	model[0] = i_data0;
-	model[1] = i_data1;
-	model[2] = i_data2;
-	model[3] = i_data3;
+	mat4 model = mtxFromCols(i_data0, i_data1, i_data2, i_data3);
 
-	vec4 worldPos = instMul(model, vec4(a_position, 1.0) );
+	vec4 worldPos = mul(model, vec4(a_position, 1.0) );
 	gl_Position = mul(u_viewProj, worldPos);
 }

+ 0 - 68
examples/41-tess/matrices.sh

@@ -1,68 +0,0 @@
-
-
-//I decided to keep the non square matrices definition in the example, since I am still not sure how non square matrices should be treated in bgfx (Daniel Gavin)
-
-#ifndef MATRICES_H_HEADER_GUARD
-#define MATRICES_H_HEADER_GUARD
-
-#ifndef __cplusplus
-
-#if BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
-
-#   define mat3x4 float4x3
-#   define mat4x3 float3x4
-
-#else
-
-
-#endif // BGFX_SHADER_LANGUAGE_*
-
-mat4x3 mtxFromRows(vec4 _0, vec4 _1, vec4 _2)
-{
-#if BGFX_SHADER_LANGUAGE_GLSL
-    return transpose(mat3x4(_0, _1, _2) );
-#else
-	return mat4x3(_0, _1, _2);
-#endif // BGFX_SHADER_LANGUAGE_GLSL
-}
-
-vec4 mtxGetRow(mat4x3 _0, uint row)
-{
-#if BGFX_SHADER_LANGUAGE_GLSL
-    return vec4(_0[0][row], _0[1][row], _0[2][row], _0[3][row]);
-#else
-    return vec4(_0[row]);
-#endif // BGFX_SHADER_LANGUAGE_GLSL
-}
-
-vec4 mtxGetRow(mat4 _0, uint row)
-{
-#if BGFX_SHADER_LANGUAGE_GLSL
-    return vec4(_0[0][row], _0[1][row], _0[2][row], _0[3][row]);
-#else
-    return vec4(_0[row]);
-#endif // BGFX_SHADER_LANGUAGE_GLSL
-}
-
-vec4 mtxGetColumn(mat4 _0, uint column)
-{
-#if BGFX_SHADER_LANGUAGE_GLSL
-    return vec4(_0[column]);
-#else
-    return vec4(_0[0][column], _0[1][column], _0[2][column], _0[3][column]);
-#endif // BGFX_SHADER_LANGUAGE_GLSL
-}
-
-float mtxGetElement(mat4 _0, uint column, uint row)
-{
-#if BGFX_SHADER_LANGUAGE_GLSL
-    return _0[column][row];
-#else
-    return _0[row][column];
-#endif // BGFX_SHADER_LANGUAGE_GLSL
-}
-
-
-#endif // __cplusplus
-
-#endif // MATRICES_H_HEADER_GUARD

+ 0 - 1
examples/41-tess/terrain_common.sh

@@ -1,5 +1,4 @@
 #include "bgfx_compute.sh"
-#include "matrices.sh"
 #include "isubd.sh"
 #include "uniforms.sh"
 

+ 2 - 2
examples/43-denoise/fs_denoise_gbuffer.sc

@@ -25,7 +25,7 @@ mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv)
 
 	// construct a scale-invariant frame
 	float invMax = inversesqrt(max(dot(T,T), dot(B,B)));
-	return mat3(T*invMax, B*invMax, N);
+	return mtxFromCols(T*invMax, B*invMax, N);
 }
 
 void main()
@@ -47,7 +47,7 @@ void main()
 	// perturb geometry normal by normal map
 	vec3 pos = v_texcoord2.xyz; // contains world space pos
 	mat3 TBN = cotangentFrame(normal, pos, v_texcoord0);
-	vec3 bumpedNormal = normalize(instMul(TBN, normalMap));
+	vec3 bumpedNormal = normalize(mul(TBN, normalMap));
 
 	// need some proxy for roughness value w/o roughness texture
 	// assume horizontal (blue) normal map is smooth, and then

+ 14 - 14
examples/43-denoise/vs_denoise_gbuffer.sc

@@ -16,22 +16,22 @@ void main()
 	gl_Position = mul(u_modelViewProj, vec4(pos, 1.0));
 
 	// Calculate previous frame's position
-	mat4 worldToViewPrev = mat4(
-		u_worldToViewPrev0,
-		u_worldToViewPrev1,
-		u_worldToViewPrev2,
-		u_worldToViewPrev3
-	);
-	mat4 viewToProjPrev = mat4(
-		u_viewToProjPrev0,
-		u_viewToProjPrev1,
-		u_viewToProjPrev2,
-		u_viewToProjPrev3
-	);
+	mat4 worldToViewPrev = mtxFromCols(
+		  u_worldToViewPrev0
+		, u_worldToViewPrev1
+		, u_worldToViewPrev2
+		, u_worldToViewPrev3
+		);
+	mat4 viewToProjPrev = mtxFromCols(
+		  u_viewToProjPrev0
+		, u_viewToProjPrev1
+		, u_viewToProjPrev2
+		, u_viewToProjPrev3
+		);
 
 	vec3 wsPos  = mul(u_model[0], vec4(pos, 1.0)).xyz;
-	vec3 vspPos = instMul(worldToViewPrev, vec4(wsPos, 1.0)).xyz;
-	vec4 pspPos = instMul(viewToProjPrev, vec4(vspPos, 1.0));
+	vec3 vspPos = mul(worldToViewPrev, vec4(wsPos, 1.0)).xyz;
+	vec4 pspPos = mul(viewToProjPrev, vec4(vspPos, 1.0));
 
 	// Calculate normal, unpack
 	vec3 osNormal = a_normal.xyz * 2.0 - 1.0;

+ 7 - 7
examples/44-sss/fs_screen_space_shadows.sc

@@ -53,19 +53,19 @@ void main()
 	float initialOffset = (0.0 < u_useNoiseOffset) ? (0.5+random) : 1.0;
 	samplePosition += initialOffset * lightStep;
 
-	mat4 viewToProj = mat4(
-		u_viewToProj0,
-		u_viewToProj1,
-		u_viewToProj2,
-		u_viewToProj3
-	);
+	mat4 viewToProj = mtxFromCols(
+		  u_viewToProj0
+		, u_viewToProj1
+		, u_viewToProj2
+		, u_viewToProj3
+		);
 
 	float occluded = 0.0;
 	float softOccluded = 0.0;
 	float firstHit = u_shadowSteps;
 	for (int i = 0; i < int(u_shadowSteps); ++i, samplePosition += lightStep)
 	{
-		vec3 psSamplePosition = instMul(viewToProj, vec4(samplePosition, 1.0)).xyw;
+		vec3 psSamplePosition = mul(viewToProj, vec4(samplePosition, 1.0)).xyw;
 		psSamplePosition.xy *= (1.0/psSamplePosition.z);
 
 		vec2 sampleCoord = psSamplePosition.xy * 0.5 + 0.5;

+ 7 - 7
examples/44-sss/fs_sss_deferred_combine.sc

@@ -41,13 +41,13 @@ void main()
 		float roughness = normalRoughness.w;
 
 		// transform normal into view space
-		mat4 worldToView = mat4(
-			u_worldToView0,
-			u_worldToView1,
-			u_worldToView2,
-			u_worldToView3
-		);
-		vec3 vsNormal = instMul(worldToView, vec4(normal, 0.0)).xyz;
+		mat4 worldToView = mtxFromCols(
+			  u_worldToView0
+			, u_worldToView1
+			, u_worldToView2
+			, u_worldToView3
+			);
+		vec3 vsNormal = mul(worldToView, vec4(normal, 0.0)).xyz;
 
 		// read depth and recreate position
 		float linearDepth = texture2D(s_depth, texCoord).x;

+ 2 - 2
examples/44-sss/fs_sss_gbuffer.sc

@@ -25,7 +25,7 @@ mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv)
 
 	// construct a scale-invariant frame
 	float invMax = inversesqrt(max(dot(T,T), dot(B,B)));
-	return mat3(T*invMax, B*invMax, N);
+	return mtxFromCols(T*invMax, B*invMax, N);
 }
 
 void main()
@@ -47,7 +47,7 @@ void main()
 	// perturb geometry normal by normal map
 	vec3 pos = v_texcoord1.xyz; // contains world space pos
 	mat3 TBN = cotangentFrame(normal, pos, v_texcoord0);
-	vec3 bumpedNormal = normalize(instMul(TBN, normalMap));
+	vec3 bumpedNormal = normalize(mul(TBN, normalMap));
 
 	// need some proxy for roughness value w/o roughness texture
 	// assume horizontal (blue) normal map is smooth, and then

+ 4 - 5
examples/45-bokeh/fs_bokeh_forward.sc

@@ -13,9 +13,8 @@ SAMPLER2D(s_normal, 1);
 // struct ModelUniforms
 uniform vec4 u_modelParams[2];
 
-#define u_color				(u_modelParams[0].xyz)
-#define u_lightPosition		(u_modelParams[1].xyz)
-
+#define u_color         (u_modelParams[0].xyz)
+#define u_lightPosition (u_modelParams[1].xyz)
 
 // http://www.thetenthplanet.de/archives/1180
 // "followup: normal mapping without precomputed tangents"
@@ -35,7 +34,7 @@ mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv)
 
 	// construct a scale-invariant frame
 	float invMax = inversesqrt(max(dot(T,T), dot(B,B)));
-	return mat3(T*invMax, B*invMax, N);
+	return mtxFromCols(T*invMax, B*invMax, N);
 }
 
 void main()
@@ -57,7 +56,7 @@ void main()
 	// perturb geometry normal by normal map
 	vec3 pos = v_texcoord1.xyz; // contains world space pos
 	mat3 TBN = cotangentFrame(normal, pos, v_texcoord0);
-	vec3 bumpedNormal = normalize(instMul(TBN, normalMap));
+	vec3 bumpedNormal = normalize(mul(TBN, normalMap));
 
 	vec3 light = (u_lightPosition - pos);
 	light = normalize(light);

+ 4 - 4
examples/46-fsr/fs_fsr_forward.sc

@@ -13,8 +13,8 @@ SAMPLER2D(s_normal, 1);
 // struct ModelUniforms
 uniform vec4 u_modelParams[2];
 
-#define u_color				(u_modelParams[0].xyz)
-#define u_lightPosition		(u_modelParams[1].xyz)
+#define u_color         (u_modelParams[0].xyz)
+#define u_lightPosition (u_modelParams[1].xyz)
 
 // http://www.thetenthplanet.de/archives/1180
 // "followup: normal mapping without precomputed tangents"
@@ -34,7 +34,7 @@ mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv)
 
 	// construct a scale-invariant frame
 	float invMax = inversesqrt(max(dot(T,T), dot(B,B)));
-	return mat3(T*invMax, B*invMax, N);
+	return mtxFromCols(T*invMax, B*invMax, N);
 }
 
 void main()
@@ -56,7 +56,7 @@ void main()
 	// perturb geometry normal by normal map
 	vec3 pos = v_texcoord1.xyz; // contains world space pos
 	mat3 TBN = cotangentFrame(normal, pos, v_texcoord0);
-	vec3 bumpedNormal = normalize(instMul(TBN, normalMap));
+	vec3 bumpedNormal = normalize(mul(TBN, normalMap));
 
 	vec3 light = (u_lightPosition - pos);
 	light = normalize(light);

+ 31 - 30
src/bgfx_compute.sh

@@ -14,13 +14,15 @@
 #	error "Compute is not supported!"
 #endif // BGFX_SHADER_LANGUAGE_HLSL
 
-#if BGFX_SHADER_LANGUAGE_METAL || BGFX_SHADER_LANGUAGE_SPIRV
+#if BGFX_SHADER_LANGUAGE_METAL \
+ || BGFX_SHADER_LANGUAGE_SPIRV \
+ || BGFX_SHADER_LANGUAGE_WGSL
 #	define FORMAT(_format) [[spv::format_ ## _format]]
 #	define WRITEONLY [[spv::nonreadable]]
 #else
 #	define FORMAT(_format)
 #	define WRITEONLY
-#endif // BGFX_SHADER_LANGUAGE_METAL || BGFX_SHADER_LANGUAGE_SPIRV
+#endif // BGFX_SHADER_LANGUAGE_*
 
 #if BGFX_SHADER_LANGUAGE_GLSL
 
@@ -148,15 +150,17 @@
 
 #define UIMAGE3D_RW(_name, _format, _reg) IMAGE3D_RW(_name, _format, _reg)
 
-#if BGFX_SHADER_LANGUAGE_METAL || BGFX_SHADER_LANGUAGE_SPIRV
-#define BUFFER_RO(_name, _struct, _reg) StructuredBuffer<_struct>   _name : REGISTER(t, _reg)
+#if BGFX_SHADER_LANGUAGE_METAL \
+ || BGFX_SHADER_LANGUAGE_SPIRV \
+ || BGFX_SHADER_LANGUAGE_WGSL
+#define BUFFER_RO(_name, _struct, _reg) StructuredBuffer<_struct>    _name : REGISTER(t, _reg)
 #define BUFFER_RW(_name, _struct, _reg) RWStructuredBuffer <_struct> _name : REGISTER(u, _reg)
 #define BUFFER_WO(_name, _struct, _reg) BUFFER_RW(_name, _struct, _reg)
 #else
 #define BUFFER_RO(_name, _struct, _reg) Buffer<_struct>   _name : REGISTER(t, _reg)
 #define BUFFER_RW(_name, _struct, _reg) RWBuffer<_struct> _name : REGISTER(u, _reg)
 #define BUFFER_WO(_name, _struct, _reg) BUFFER_RW(_name, _struct, _reg)
-#endif
+#endif // BGFX_SHADER_LANGUAGE_*
 
 #define NUM_THREADS(_x, _y, _z) [numthreads(_x, _y, _z)]
 
@@ -165,82 +169,82 @@
 	{                                                                           \
 		return _image[_uv]._loadComponents;                                     \
 	}                                                                           \
-	\
+	                                                                            \
 	ivec2 imageSize(Texture2D<_format> _image)                                  \
 	{                                                                           \
 		uvec2 result;                                                           \
 		_image.GetDimensions(result.x, result.y);                               \
 		return ivec2(result);                                                   \
 	}                                                                           \
-	\
+	                                                                            \
 	_type imageLoad(RWTexture2D<_format> _image, ivec2 _uv)                     \
 	{                                                                           \
 		return _image[_uv]._loadComponents;                                     \
 	}                                                                           \
-	\
+	                                                                            \
 	void imageStore(RWTexture2D<_format> _image, ivec2 _uv,  _type _value)      \
 	{                                                                           \
 		_image[_uv] = _value._storeComponents;                                  \
 	}                                                                           \
-	\
+	                                                                            \
 	ivec2 imageSize(RWTexture2D<_format> _image)                                \
 	{                                                                           \
 		uvec2 result;                                                           \
 		_image.GetDimensions(result.x, result.y);                               \
 		return ivec2(result);                                                   \
 	}                                                                           \
-	\
+	                                                                            \
 	_type imageLoad(Texture2DArray<_format> _image, ivec3 _uvw)                 \
 	{                                                                           \
 		return _image[_uvw]._loadComponents;                                    \
 	}                                                                           \
-	\
+	                                                                            \
 	ivec3 imageSize(Texture2DArray<_format> _image)                             \
 	{                                                                           \
 		uvec3 result;                                                           \
 		_image.GetDimensions(result.x, result.y, result.z);                     \
 		return ivec3(result);                                                   \
 	}                                                                           \
-	\
+	                                                                            \
 	_type imageLoad(RWTexture2DArray<_format> _image, ivec3 _uvw)               \
 	{                                                                           \
 		return _image[_uvw]._loadComponents;                                    \
 	}                                                                           \
-	\
+	                                                                            \
 	void imageStore(RWTexture2DArray<_format> _image, ivec3 _uvw, _type _value) \
 	{                                                                           \
 		_image[_uvw] = _value._storeComponents;                                 \
 	}                                                                           \
-	\
+	                                                                            \
 	ivec3 imageSize(RWTexture2DArray<_format> _image)                           \
 	{                                                                           \
 		uvec3 result;                                                           \
 		_image.GetDimensions(result.x, result.y, result.z);                     \
 		return ivec3(result);                                                   \
 	}                                                                           \
-	\
-	_type imageLoad(Texture3D<_format> _image, ivec3 _uvw)                    \
+	                                                                            \
+	_type imageLoad(Texture3D<_format> _image, ivec3 _uvw)                      \
 	{                                                                           \
 		return _image[_uvw]._loadComponents;                                    \
 	}                                                                           \
-	\
-	ivec3 imageSize(Texture3D<_format> _image)                                \
+	                                                                            \
+	ivec3 imageSize(Texture3D<_format> _image)                                  \
 	{                                                                           \
 		uvec3 result;                                                           \
 		_image.GetDimensions(result.x, result.y, result.z);                     \
 		return ivec3(result);                                                   \
 	}                                                                           \
-	\
+	                                                                            \
 	_type imageLoad(RWTexture3D<_format> _image, ivec3 _uvw)                    \
 	{                                                                           \
 		return _image[_uvw]._loadComponents;                                    \
 	}                                                                           \
-	\
+	                                                                            \
 	void imageStore(RWTexture3D<_format> _image, ivec3 _uvw, _type _value)      \
 	{                                                                           \
 		_image[_uvw] = _value._storeComponents;                                 \
 	}                                                                           \
-	\
+	                                                                            \
 	ivec3 imageSize(RWTexture3D<_format> _image)                                \
 	{                                                                           \
 		uvec3 result;                                                           \
@@ -248,13 +252,11 @@
 		return ivec3(result);                                                   \
 	}
 
-#define __IMAGE_IMPL_ATOMIC(_format, _storeComponents, _type, _loadComponents)            \
-	\
-	void imageAtomicAdd(RWTexture2D<_format> _image, ivec2 _uv,  _type _value)       \
-	{				                                                                 \
-		InterlockedAdd(_image[_uv], _value._storeComponents);	                     \
-	}                                                                                \
-
+#define __IMAGE_IMPL_ATOMIC(_format, _storeComponents, _type, _loadComponents)  \
+	void imageAtomicAdd(RWTexture2D<_format> _image, ivec2 _uv,  _type _value)  \
+	{                                                                           \
+		InterlockedAdd(_image[_uv], _value._storeComponents);                   \
+	}                                                                           \
 
 __IMAGE_IMPL_A(float,       x,    vec4,  xxxx)
 __IMAGE_IMPL_A(float2,      xy,   vec4,  xyyy)
@@ -268,11 +270,10 @@ __IMAGE_IMPL_A(uint4,       xyzw, uvec4, xyzw)
 __IMAGE_IMPL_A(unorm float,       x,    vec4,  xxxx)
 __IMAGE_IMPL_A(unorm float2,      xy,   vec4,  xyyy)
 __IMAGE_IMPL_A(unorm float4,      xyzw, vec4,  xyzw)
-#endif
+#endif // BGFX_SHADER_LANGUAGE_HLSL
 
 __IMAGE_IMPL_ATOMIC(uint,       x,    uvec4, xxxx)
 
-
 #define atomicAdd(_mem, _data)                                       InterlockedAdd(_mem, _data)
 #define atomicAnd(_mem, _data)                                       InterlockedAnd(_mem, _data)
 #define atomicMax(_mem, _data)                                       InterlockedMax(_mem, _data)

+ 153 - 62
src/bgfx_shader.sh

@@ -12,21 +12,29 @@
 
 #ifndef __cplusplus
 
-#if BGFX_SHADER_LANGUAGE_HLSL > 300
-#	define BRANCH [branch]
-#	define LOOP   [loop]
-#	define UNROLL [unroll]
-#else
+#if BGFX_SHADER_LANGUAGE_GLSL
 #	define BRANCH
 #	define LOOP
 #	define UNROLL
+#else
+#	define BRANCH [branch]
+#	define LOOP   [loop]
+#	define UNROLL [unroll]
 #endif // BGFX_SHADER_LANGUAGE_HLSL > 300
 
-#if (BGFX_SHADER_LANGUAGE_HLSL > 300 || BGFX_SHADER_LANGUAGE_METAL || BGFX_SHADER_LANGUAGE_SPIRV) && BGFX_SHADER_TYPE_FRAGMENT
-#	define EARLY_DEPTH_STENCIL [earlydepthstencil]
-#else
-#	define EARLY_DEPTH_STENCIL
-#endif // BGFX_SHADER_LANGUAGE_HLSL > 300 && BGFX_SHADER_TYPE_FRAGMENT
+#define BGFX_SHADER_MATRIX_COLUMN_MAJOR (0 \
+	|| BGFX_SHADER_LANGUAGE_GLSL           \
+	)
+
+#if BGFX_SHADER_TYPE_FRAGMENT
+#	if BGFX_SHADER_LANGUAGE_HLSL  \
+	|| BGFX_SHADER_LANGUAGE_METAL \
+	|| BGFX_SHADER_LANGUAGE_SPIRV
+#		define EARLY_DEPTH_STENCIL [earlydepthstencil]
+	#else
+#		define EARLY_DEPTH_STENCIL
+#	endif // BGFX_SHADER_LANGUAGE_...
+#endif // BGFX_SHADER_TYPE_FRAGMENT
 
 #if BGFX_SHADER_LANGUAGE_GLSL
 #	define ARRAY_BEGIN(_type, _name, _count) _type _name[_count] = _type[](
@@ -36,8 +44,8 @@
 #	define ARRAY_END() }
 #endif // BGFX_SHADER_LANGUAGE_GLSL
 
-#if BGFX_SHADER_LANGUAGE_HLSL \
- || BGFX_SHADER_LANGUAGE_PSSL \
+#if BGFX_SHADER_LANGUAGE_HLSL  \
+ || BGFX_SHADER_LANGUAGE_PSSL  \
  || BGFX_SHADER_LANGUAGE_SPIRV \
  || BGFX_SHADER_LANGUAGE_METAL
 #	define CONST(_x) static const _x
@@ -52,26 +60,32 @@
 
 // To be able to patch the uav registers on the DXBC SPDB Chunk (D3D11 renderer) the whitespaces around
 // '_type[_reg]' are necessary. This only affects shaders with debug info (i.e., those that have the SPDB Chunk).
-#	if BGFX_SHADER_LANGUAGE_HLSL > 400 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
+#	if BGFX_SHADER_LANGUAGE_HLSL > 400 \
+	|| BGFX_SHADER_LANGUAGE_PSSL \
+	|| BGFX_SHADER_LANGUAGE_SPIRV \
+	|| BGFX_SHADER_LANGUAGE_METAL
 #		define REGISTER(_type, _reg) register( _type[_reg] )
 #	else
 #		define REGISTER(_type, _reg) register(_type ## _reg)
 #	endif // BGFX_SHADER_LANGUAGE_HLSL
 
-#	if BGFX_SHADER_LANGUAGE_HLSL > 300 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
-#		if BGFX_SHADER_LANGUAGE_HLSL > 400 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
-#			define dFdxCoarse(_x) ddx_coarse(_x)
-#			define dFdxFine(_x)   ddx_fine(_x)
-#			define dFdyCoarse(_y) ddy_coarse(-(_y))
-#			define dFdyFine(_y)   ddy_fine(-(_y))
-#		endif // BGFX_SHADER_LANGUAGE_HLSL > 400
-
-#		if BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
+#	if BGFX_SHADER_LANGUAGE_HLSL > 400 \
+	|| BGFX_SHADER_LANGUAGE_PSSL       \
+	|| BGFX_SHADER_LANGUAGE_SPIRV      \
+	|| BGFX_SHADER_LANGUAGE_METAL
+#		define dFdxCoarse(_x) ddx_coarse(_x)
+#		define dFdxFine(_x)   ddx_fine(_x)
+#		define dFdyCoarse(_y) ddy_coarse(-(_y))
+#		define dFdyFine(_y)   ddy_fine(-(_y))
+
+#		if BGFX_SHADER_LANGUAGE_HLSL  \
+		|| BGFX_SHADER_LANGUAGE_SPIRV \
+		|| BGFX_SHADER_LANGUAGE_METAL
 float intBitsToFloat(int   _x) { return asfloat(_x); }
 vec2  intBitsToFloat(uint2 _x) { return asfloat(_x); }
 vec3  intBitsToFloat(uint3 _x) { return asfloat(_x); }
 vec4  intBitsToFloat(uint4 _x) { return asfloat(_x); }
-#		endif // BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
+#		endif // BGFX_SHADER_LANGUAGE_*
 
 float uintBitsToFloat(uint  _x) { return asfloat(_x); }
 vec2  uintBitsToFloat(uint2 _x) { return asfloat(_x); }
@@ -571,11 +585,6 @@ float bgfxShadow2DProj(sampler2DShadow _sampler, vec4 _coord)
 
 #	endif // BGFX_SHADER_LANGUAGE_HLSL > 300
 
-vec3 instMul(vec3 _vec, mat3 _mtx) { return mul(_mtx, _vec); }
-vec3 instMul(mat3 _mtx, vec3 _vec) { return mul(_vec, _mtx); }
-vec4 instMul(vec4 _vec, mat4 _mtx) { return mul(_mtx, _vec); }
-vec4 instMul(mat4 _mtx, vec4 _vec) { return mul(_vec, _mtx); }
-
 bvec2 lessThan(vec2 _a, vec2 _b) { return _a < _b; }
 bvec3 lessThan(vec3 _a, vec3 _b) { return _a < _b; }
 bvec4 lessThan(vec4 _a, vec4 _b) { return _a < _b; }
@@ -613,7 +622,6 @@ vec4  mod(vec4  _a, vec4  _b) { return _a - _b * floor(_a / _b); }
 #else
 #	define CONST(_x) const _x
 #	define atan2(_x, _y) atan(_x, _y)
-#	define mul(_a, _b) ( (_a) * (_b) )
 #	define saturate(_x) clamp(_x, 0.0, 1.0)
 #	define SAMPLER2D(_name, _reg)       uniform sampler2D _name
 #	define SAMPLER2DMS(_name, _reg)     uniform sampler2DMS _name
@@ -645,11 +653,6 @@ vec4  mod(vec4  _a, vec4  _b) { return _a - _b * floor(_a / _b); }
 #		define textureCubeBias(_sampler, _coord, _bias)    textureCube(_sampler, _coord, _bias)
 #	endif // BGFX_SHADER_LANGUAGE_GLSL >= 130
 
-vec3 instMul(vec3 _vec, mat3 _mtx) { return mul(_vec, _mtx); }
-vec3 instMul(mat3 _mtx, vec3 _vec) { return mul(_mtx, _vec); }
-vec4 instMul(vec4 _vec, mat4 _mtx) { return mul(_vec, _mtx); }
-vec4 instMul(mat4 _mtx, vec4 _vec) { return mul(_mtx, _vec); }
-
 float rcp(float _a) { return 1.0/_a; }
 vec2  rcp(vec2  _a) { return vec2(1.0)/_a; }
 vec3  rcp(vec3  _a) { return vec3(1.0)/_a; }
@@ -660,71 +663,159 @@ vec2 vec2_splat(float _x) { return vec2(_x, _x); }
 vec3 vec3_splat(float _x) { return vec3(_x, _x, _x); }
 vec4 vec4_splat(float _x) { return vec4(_x, _x, _x, _x); }
 
-#if BGFX_SHADER_LANGUAGE_GLSL >= 130 || BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
+#if BGFX_SHADER_LANGUAGE_GLSL >= 130 \
+ || BGFX_SHADER_LANGUAGE_HLSL        \
+ || BGFX_SHADER_LANGUAGE_PSSL        \
+ || BGFX_SHADER_LANGUAGE_SPIRV       \
+ || BGFX_SHADER_LANGUAGE_METAL
 uvec2 uvec2_splat(uint _x) { return uvec2(_x, _x); }
 uvec3 uvec3_splat(uint _x) { return uvec3(_x, _x, _x); }
 uvec4 uvec4_splat(uint _x) { return uvec4(_x, _x, _x, _x); }
-#endif // BGFX_SHADER_LANGUAGE_GLSL >= 130 || BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
+#endif // BGFX_SHADER_LANGUAGE_*
+
+#if BGFX_SHADER_LANGUAGE_GLSL
+#	define mul(_a, _b) ( (_a) * (_b) )
+#else
+#	define mul(_a, _b) mul(_a, _b)
+#	define mat3x4 float4x3
+#	define mat4x3 float3x4
+#endif // BGFX_SHADER_LANGUAGE_*
 
 mat4 mtxFromRows(vec4 _0, vec4 _1, vec4 _2, vec4 _3)
 {
-#if BGFX_SHADER_LANGUAGE_GLSL
+#if BGFX_SHADER_MATRIX_COLUMN_MAJOR
 	return transpose(mat4(_0, _1, _2, _3) );
 #else
 	return mat4(_0, _1, _2, _3);
 #endif // BGFX_SHADER_LANGUAGE_GLSL
 }
+
 mat4 mtxFromCols(vec4 _0, vec4 _1, vec4 _2, vec4 _3)
 {
-#if BGFX_SHADER_LANGUAGE_GLSL
+#if BGFX_SHADER_MATRIX_COLUMN_MAJOR
 	return mat4(_0, _1, _2, _3);
 #else
 	return transpose(mat4(_0, _1, _2, _3) );
 #endif // BGFX_SHADER_LANGUAGE_GLSL
 }
+
 mat3 mtxFromRows(vec3 _0, vec3 _1, vec3 _2)
 {
-#if BGFX_SHADER_LANGUAGE_GLSL
+#if BGFX_SHADER_MATRIX_COLUMN_MAJOR
 	return transpose(mat3(_0, _1, _2) );
 #else
 	return mat3(_0, _1, _2);
 #endif // BGFX_SHADER_LANGUAGE_GLSL
 }
+
 mat3 mtxFromCols(vec3 _0, vec3 _1, vec3 _2)
 {
-#if BGFX_SHADER_LANGUAGE_GLSL
+#if BGFX_SHADER_MATRIX_COLUMN_MAJOR
 	return mat3(_0, _1, _2);
 #else
 	return transpose(mat3(_0, _1, _2) );
 #endif // BGFX_SHADER_LANGUAGE_GLSL
 }
 
-#if BGFX_SHADER_LANGUAGE_GLSL
-#define mtxFromRows3(_0, _1, _2)     transpose(mat3(_0, _1, _2) )
-#define mtxFromRows4(_0, _1, _2, _3) transpose(mat4(_0, _1, _2, _3) )
-#define mtxFromCols3(_0, _1, _2)               mat3(_0, _1, _2)
-#define mtxFromCols4(_0, _1, _2, _3)           mat4(_0, _1, _2, _3)
+#if !BGFX_SHADER_LANGUAGE_ESSL
+mat4x3 mtxFromRows(vec4 _0, vec4 _1, vec4 _2)
+{
+#if BGFX_SHADER_MATRIX_COLUMN_MAJOR
+    return transpose(mat3x4(_0, _1, _2) );
+#else
+	return mat4x3(_0, _1, _2);
+#endif // BGFX_SHADER_LANGUAGE_GLSL
+}
+#endif // !BGFX_SHADER_LANGUAGE_ESSL
+
+vec3 mtxGetRow(mat3 _mtx, int _row)
+{
+#if BGFX_SHADER_MATRIX_COLUMN_MAJOR
+    return vec3(_mtx[0][_row], _mtx[1][_row], _mtx[2][_row]);
 #else
-#define mtxFromRows3(_0, _1, _2)               mat3(_0, _1, _2)
-#define mtxFromRows4(_0, _1, _2, _3)           mat4(_0, _1, _2, _3)
-#define mtxFromCols3(_0, _1, _2)     transpose(mat3(_0, _1, _2) )
-#define mtxFromCols4(_0, _1, _2, _3) transpose(mat4(_0, _1, _2, _3) )
+    return vec3(_mtx[_row]);
 #endif // BGFX_SHADER_LANGUAGE_GLSL
+}
+
+vec3 mtxGetColumn(mat3 _mtx, int _column)
+{
+#if BGFX_SHADER_MATRIX_COLUMN_MAJOR
+    return vec3(_mtx[_column]);
+#else
+    return vec3(_mtx[0][_column], _mtx[1][_column], _mtx[2][_column]);
+#endif // BGFX_SHADER_LANGUAGE_GLSL
+}
+
+#if !BGFX_SHADER_LANGUAGE_ESSL
+vec4 mtxGetRow(mat4x3 _mtx, int _row)
+{
+#if BGFX_SHADER_MATRIX_COLUMN_MAJOR
+    return vec4(_mtx[0][_row], _mtx[1][_row], _mtx[2][_row], _mtx[3][_row]);
+#else
+    return vec4(_mtx[_row]);
+#endif // BGFX_SHADER_LANGUAGE_GLSL
+}
+
+vec3 mtxGetColumn(mat4x3 _mtx, int _column)
+{
+#if BGFX_SHADER_MATRIX_COLUMN_MAJOR
+    return vec3(_mtx[_column]);
+#else
+    return vec3(_mtx[0][_column], _mtx[1][_column], _mtx[2][_column]);
+#endif // BGFX_SHADER_LANGUAGE_GLSL
+}
+#endif // !BGFX_SHADER_LANGUAGE_ESSL
+
+vec4 mtxGetRow(mat4 _mtx, int _row)
+{
+#if BGFX_SHADER_MATRIX_COLUMN_MAJOR
+    return vec4(_mtx[0][_row], _mtx[1][_row], _mtx[2][_row], _mtx[3][_row]);
+#else
+    return vec4(_mtx[_row]);
+#endif // BGFX_SHADER_LANGUAGE_GLSL
+}
+
+vec4 mtxGetColumn(mat4 _mtx, int _column)
+{
+#if BGFX_SHADER_MATRIX_COLUMN_MAJOR
+    return vec4(_mtx[_column]);
+#else
+    return vec4(_mtx[0][_column], _mtx[1][_column], _mtx[2][_column], _mtx[3][_column]);
+#endif // BGFX_SHADER_LANGUAGE_GLSL
+}
+
+float mtxGetElement(mat3 _mtx, int _column, int _row)
+{
+#if BGFX_SHADER_MATRIX_COLUMN_MAJOR
+    return _mtx[_column][_row];
+#else
+    return _mtx[_row][_column];
+#endif // BGFX_SHADER_LANGUAGE_GLSL
+}
+
+float mtxGetElement(mat4 _mtx, int _column, int _row)
+{
+#if BGFX_SHADER_MATRIX_COLUMN_MAJOR
+    return _mtx[_column][_row];
+#else
+    return _mtx[_row][_column];
+#endif // BGFX_SHADER_LANGUAGE_GLSL
+}
 
-uniform vec4  u_viewRect;
-uniform vec4  u_viewTexel;
-uniform mat4  u_view;
-uniform mat4  u_invView;
-uniform mat4  u_proj;
-uniform mat4  u_invProj;
-uniform mat4  u_viewProj;
-uniform mat4  u_invViewProj;
-uniform mat4  u_model[BGFX_CONFIG_MAX_BONES];
-uniform mat4  u_modelView;
-uniform mat4  u_invModelView;
-uniform mat4  u_modelViewProj;
-uniform vec4  u_alphaRef4;
+uniform vec4 u_viewRect;
+uniform vec4 u_viewTexel;
+uniform mat4 u_view;
+uniform mat4 u_invView;
+uniform mat4 u_proj;
+uniform mat4 u_invProj;
+uniform mat4 u_viewProj;
+uniform mat4 u_invViewProj;
+uniform mat4 u_modelView;
+uniform mat4 u_invModelView;
+uniform mat4 u_modelViewProj;
+uniform vec4 u_alphaRef4;
 #define u_alphaRef u_alphaRef4.x
+uniform mat4 u_model[BGFX_CONFIG_MAX_BONES];
 
 #endif // __cplusplus
 

+ 26 - 5
tools/shaderc/shaderc.cpp

@@ -797,9 +797,12 @@ namespace bgfx
 
 		void setDefine(const char* _define)
 		{
-			m_tagptr->tag = FPPTAG_DEFINE;
-			m_tagptr->data = scratch(_define);
-			m_tagptr++;
+			if (0 != bx::strLen(_define) )
+			{
+				m_tagptr->tag = FPPTAG_DEFINE;
+				m_tagptr->data = scratch(_define);
+				m_tagptr++;
+			}
 		}
 
 		void setDefaultDefine(const char* _name)
@@ -1172,6 +1175,7 @@ namespace bgfx
 		preprocessor.setDefaultDefine("BX_PLATFORM_XBOXONE");
 
 		preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_GLSL");
+		preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_ESSL");	
 		preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_HLSL");
 		preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_METAL");
 		preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_PSSL");
@@ -1181,7 +1185,10 @@ namespace bgfx
 		preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_FRAGMENT");
 		preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_VERTEX");
 
-		char glslDefine[128];
+		char glslDefine[128] = { '\0' };
+		char esslDefine[128] = { '\0' };
+		char hlslDefine[128] = { '\0' };
+
 		if (profile->lang == ShadingLang::GLSL
 		||  profile->lang == ShadingLang::ESSL)
 		{
@@ -1189,9 +1196,16 @@ namespace bgfx
 				, "BGFX_SHADER_LANGUAGE_GLSL=%d"
 				, profile->id
 				);
+
+			if (profile->lang == ShadingLang::ESSL)
+			{
+				bx::snprintf(esslDefine, BX_COUNTOF(esslDefine)
+					, "BGFX_SHADER_LANGUAGE_ESSL=%d"
+					, profile->id
+					);
+			}
 		}
 
-		char hlslDefine[128];
 		if (profile->lang == ShadingLang::HLSL)
 		{
 			bx::snprintf(hlslDefine, BX_COUNTOF(hlslDefine)
@@ -1211,12 +1225,14 @@ namespace bgfx
 			else
 			{
 				preprocessor.setDefine(glslDefine);
+				preprocessor.setDefine(esslDefine);
 			}
 		}
 		else if (0 == bx::strCmpI(platform, "asm.js") )
 		{
 			preprocessor.setDefine("BX_PLATFORM_EMSCRIPTEN=1");
 			preprocessor.setDefine(glslDefine);
+			preprocessor.setDefine(esslDefine);
 		}
 		else if (0 == bx::strCmpI(platform, "linux") )
 		{
@@ -1229,6 +1245,7 @@ namespace bgfx
 			else
 			{
 				preprocessor.setDefine(glslDefine);
+				preprocessor.setDefine(esslDefine);
 			}
 		}
 		else if (0 == bx::strCmpI(platform, "ios")
@@ -1252,7 +1269,9 @@ namespace bgfx
 			if (profile->lang != ShadingLang::Metal)
 			{
 				preprocessor.setDefine(glslDefine);
+				preprocessor.setDefine(esslDefine);
 			}
+
 			char temp[32];
 			bx::snprintf(
 				temp
@@ -1273,6 +1292,7 @@ namespace bgfx
 			     ||  profile->lang == ShadingLang::ESSL)
 			{
 				preprocessor.setDefine(glslDefine);
+				preprocessor.setDefine(esslDefine);
 			}
 			else if (profile->lang == ShadingLang::SpirV)
 			{
@@ -1295,6 +1315,7 @@ namespace bgfx
 			     ||  profile->lang == ShadingLang::ESSL)
 			{
 				preprocessor.setDefine(glslDefine);
+				preprocessor.setDefine(esslDefine);
 			}
 			else if (profile->lang == ShadingLang::SpirV)
 			{