Browse Source

33-pom: Built shaders and assets.

Branimir Karadžić 8 years ago
parent
commit
42aa94504e

+ 19 - 16
examples/33-pom/fs_pom.sc

@@ -6,20 +6,22 @@ SAMPLER2D(s_texColor,  0);
 SAMPLER2D(s_texNormal, 1);
 SAMPLER2D(s_texDepth,  2);
 
-uniform int u_shading_type;
-uniform int u_show_diffuse_texture;
-uniform int u_parallax_scale;
-uniform int u_num_steps;
+uniform vec4 u_pomParam;
+
+#define u_shading_type         u_pomParam.x
+#define u_show_diffuse_texture u_pomParam.y
+#define u_parallax_scale       u_pomParam.z
+#define u_num_steps            u_pomParam.w
 
 vec2 parallax_uv(vec2 uv, vec3 view_dir)
 {
 	float depth_scale = float(u_parallax_scale) / 1000.0;
-	if (u_shading_type == 2)
+	if (u_shading_type == 2.0)
 	{
 		// Parallax mapping
-		float depth = texture2D(s_texDepth, uv).r;    
+		float depth = texture2D(s_texDepth, uv).r;
 		vec2 p = view_dir.xy * (depth * depth_scale) / view_dir.z;
-		return uv - p;  
+		return uv - p;
 	}
 	else
 	{
@@ -41,7 +43,7 @@ vec2 parallax_uv(vec2 uv, vec3 view_dir)
 			}
 		}
 
-		if (u_shading_type == 3)
+		if (u_shading_type == 3.0)
 		{
 			// Steep parallax mapping
 			return cur_uv;
@@ -61,13 +63,13 @@ vec2 parallax_uv(vec2 uv, vec3 view_dir)
 void main()
 {
 	vec3 light_dir = normalize(v_ts_light_pos - v_ts_frag_pos);
-	vec3 view_dir = normalize(v_ts_view_pos - v_ts_frag_pos);
+	vec3 view_dir  = normalize(v_ts_view_pos  - v_ts_frag_pos);
 
 	// Only perturb the texture coordinates if a parallax technique is selected
-	vec2 uv = (u_shading_type < 2) ? v_texcoord0 : parallax_uv(v_texcoord0, view_dir);
+	vec2 uv = (u_shading_type < 2.0) ? v_texcoord0 : parallax_uv(v_texcoord0, view_dir);
 
 	vec3 albedo;
-	if (u_show_diffuse_texture == 0)
+	if (u_show_diffuse_texture == 0.0)
 	{
 		albedo = vec3(1.0, 1.0, 1.0);
 	}
@@ -78,17 +80,18 @@ void main()
 
 	vec3 ambient = 0.3 * albedo;
 
-	vec3 norm;
+	vec3 normal;
 
-	if (u_shading_type == 0)
+	if (u_shading_type == 0.0)
 	{
-		norm = vec3(0, 0, 1);
+		normal = vec3(0.0, 0.0, 1.0);
 	}
 	else
 	{
-		norm = normalize(texture2D(s_texNormal, uv).rgb * 2.0 - 1.0);
+		normal.xy = texture2D(s_texNormal, uv).xy * 2.0 - 1.0;
+		normal.z = sqrt(1.0 - dot(normal.xy, normal.xy) );
 	}
 
-	float diffuse = max(dot(light_dir, norm), 0.0);
+	float diffuse = max(dot(light_dir, normal), 0.0);
 	gl_FragColor = vec4(diffuse * albedo + ambient, 1.0);
 }

+ 12 - 22
examples/33-pom/pom.cpp

@@ -147,23 +147,20 @@ class ExamplePom : public entry::AppI
 
 
 		u_light_pos = bgfx::createUniform("u_light_pos", bgfx::UniformType::Vec4);
-		u_norm_mtx = bgfx::createUniform("u_norm_mtx", bgfx::UniformType::Mat4);
-		u_shading_type = bgfx::createUniform("u_shading_type", bgfx::UniformType::Int1);
-		u_show_diffuse_texture = bgfx::createUniform("u_show_diffuse_texture", bgfx::UniformType::Int1);
-		u_parallax_scale = bgfx::createUniform("u_parallax_scale", bgfx::UniformType::Int1);
-		u_num_steps = bgfx::createUniform("u_num_steps", bgfx::UniformType::Int1);
+		u_norm_mtx  = bgfx::createUniform("u_norm_mtx",  bgfx::UniformType::Mat4);
+		u_pomParam  = bgfx::createUniform("u_pomParam",  bgfx::UniformType::Vec4);
 
 		// Create program from shaders.
 		m_program = loadProgram("vs_pom", "fs_pom");
 
 		// Load diffuse texture.
-		m_textureColor = loadTexture("textures/parallax-d.png");
+		m_textureColor = loadTexture("textures/parallax-d.ktx");
 
 		// Load normal texture.
-		m_textureNormal = loadTexture("textures/parallax-n.png");
+		m_textureNormal = loadTexture("textures/parallax-n.ktx");
 
 		// Load depth texture.
-		m_textureDepth = loadTexture("textures/parallax-h.png");
+		m_textureDepth = loadTexture("textures/parallax-h.ktx");
 
 		imguiCreate();
 
@@ -188,10 +185,7 @@ class ExamplePom : public entry::AppI
 		bgfx::destroyUniform(s_texDepth);
 		bgfx::destroyUniform(u_light_pos);
 		bgfx::destroyUniform(u_norm_mtx);
-		bgfx::destroyUniform(u_shading_type);
-		bgfx::destroyUniform(u_show_diffuse_texture);
-		bgfx::destroyUniform(u_parallax_scale);
-		bgfx::destroyUniform(u_num_steps);
+		bgfx::destroyUniform(u_pomParam);
 
 		imguiDestroy();
 
@@ -275,12 +269,10 @@ class ExamplePom : public entry::AppI
 			ImGui::RadioButton("Parallax mapping", &m_shading_type, 2);
 			ImGui::RadioButton("Steep parallax mapping", &m_shading_type, 3);
 			ImGui::RadioButton("Parallax occlusion mapping", &m_shading_type, 4);
-			bgfx::setUniform(u_shading_type, &m_shading_type);
 
 			ImGui::Separator();
 
 			ImGui::Checkbox("Show diffuse texture", &m_show_diffuse_texture);
-			bgfx::setUniform(u_show_diffuse_texture, &m_show_diffuse_texture);
 
 			if (m_shading_type > 1)
 			{
@@ -290,7 +282,6 @@ class ExamplePom : public entry::AppI
 				float x = (float)m_parallax_scale / multiplier;
 				ImGui::SliderFloat("Parallax scale", &x, 0.0f, 0.1f);
 				m_parallax_scale = (int32_t)(x * multiplier);
-				bgfx::setUniform(u_parallax_scale, &m_parallax_scale);
 			}
 
 			if (m_shading_type > 2)
@@ -298,15 +289,14 @@ class ExamplePom : public entry::AppI
 				ImGui::Separator();
 
 				ImGui::SliderInt("Number of steps", &m_num_steps, 1, 32);
-				bgfx::setUniform(u_num_steps, &m_num_steps);
 			}
 
 			ImGui::End();
 
 			imguiEndFrame();
 
-			float light_pos[4] = { 1, 2, 0, 0 };
-			bgfx::setUniform(u_light_pos, light_pos);
+			float lightPos[4] = { 1.0f, 2.0f, 0.0f, 0.0f };
+			bgfx::setUniform(u_light_pos, lightPos);
 
 			float a[16];
 			float b[16];
@@ -322,6 +312,9 @@ class ExamplePom : public entry::AppI
 			// Set transform for draw call.
 			bgfx::setTransform(mtx);
 
+			float pomParam[4] = { float(m_shading_type), float(m_show_diffuse_texture), float(m_parallax_scale), float(m_num_steps) };
+			bgfx::setUniform(u_pomParam, pomParam);
+
 			// Set normal matrix uniform
 			float inv[16];
 			float transpose[16];
@@ -367,10 +360,7 @@ class ExamplePom : public entry::AppI
 	bgfx::UniformHandle s_texDepth;
 	bgfx::UniformHandle u_light_pos;
 	bgfx::UniformHandle u_norm_mtx;
-	bgfx::UniformHandle u_shading_type;
-	bgfx::UniformHandle u_show_diffuse_texture;
-	bgfx::UniformHandle u_parallax_scale;
-	bgfx::UniformHandle u_num_steps;
+	bgfx::UniformHandle u_pomParam;
 	bgfx::ProgramHandle m_program;
 	bgfx::TextureHandle m_textureColor;
 	bgfx::TextureHandle m_textureNormal;

+ 9 - 24
examples/33-pom/vs_pom.sc

@@ -6,39 +6,24 @@ uniform vec4 u_light_pos;
 
 #include "../common/common.sh"
 
-mat3 transpose(mat3 inMatrix)
-{
-	vec3 i0 = inMatrix[0];
-	vec3 i1 = inMatrix[1];
-	vec3 i2 = inMatrix[2];
-
-	mat3 outMatrix = mat3(
-		vec3(i0.x, i1.x, i2.x),
-		vec3(i0.y, i1.y, i2.y),
-		vec3(i0.z, i1.z, i2.z)
-	);
-
-	return outMatrix;
-}
-
 void main()
 {
 	vec3 wpos = mul(u_model[0], vec4(a_position, 1.0) ).xyz;
 	gl_Position = mul(u_viewProj, vec4(wpos, 1.0) );
 
-	vec3 tangent = a_tangent * 2.0 - 1.0;
+	vec3 tangent   = a_tangent   * 2.0 - 1.0;
 	vec3 bitangent = a_bitangent * 2.0 - 1.0;
-	vec3 normal = cross(tangent, bitangent);
+	vec3 normal    = cross(tangent, bitangent);
 
-	vec3 t = normalize(mat3(u_norm_mtx) * tangent);
-	vec3 b = normalize(mat3(u_norm_mtx) * bitangent);
-	vec3 n = normalize(mat3(u_norm_mtx) * normal);
-	mat3 tbn = transpose(mat3(t, b, n));
+	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);
 
-	v_ts_light_pos = tbn * u_light_pos.xyz;
+	v_ts_light_pos = mul(tbn, u_light_pos.xyz);
 	// Our camera is always at the origin
-	v_ts_view_pos = tbn * vec3(0, 0, 0);
-	v_ts_frag_pos = tbn * wpos;
+	v_ts_view_pos = mul(tbn, vec3_splat(0.0) );
+	v_ts_frag_pos = mul(tbn, wpos);
 
 	v_texcoord0 = a_texcoord0;
 }

BIN
examples/assets/textures/parallax-d.png


BIN
examples/assets/textures/parallax-h.png


BIN
examples/assets/textures/parallax-n.png


+ 4 - 0
examples/assets/textures/textures.ninja

@@ -5,3 +5,7 @@ build $textures/texture_compression_bc2.ktx:  texturec_bc2  $pwd/texture_compres
 build $textures/texture_compression_bc3.ktx:  texturec_bc3  $pwd/texture_compression.png
 build $textures/texture_compression_etc1.ktx: texturec_etc1 $pwd/texture_compression.png
 build $textures/texture_compression_etc2.ktx: texturec_etc2 $pwd/texture_compression.png
+
+build $textures/parallax-d.ktx: texturec_diffuse $pwd/parallax-d.png
+build $textures/parallax-n.ktx: texturec_normal  $pwd/parallax-n.png
+build $textures/parallax-h.ktx: texturec_height  $pwd/parallax-h.png

BIN
examples/runtime/meshes/bunny_decimated.bin


BIN
examples/runtime/meshes/orb.bin


BIN
examples/runtime/meshes/tree.bin


BIN
examples/runtime/shaders/dx11/fs_pom.bin


BIN
examples/runtime/shaders/dx11/vs_pom.bin


BIN
examples/runtime/shaders/dx9/fs_pom.bin


BIN
examples/runtime/shaders/dx9/vs_pom.bin


BIN
examples/runtime/shaders/essl/fs_pom.bin


BIN
examples/runtime/shaders/essl/vs_pom.bin


BIN
examples/runtime/shaders/glsl/fs_pom.bin


BIN
examples/runtime/shaders/glsl/vs_pom.bin


BIN
examples/runtime/shaders/metal/fs_pom.bin


BIN
examples/runtime/shaders/metal/vs_pom.bin


BIN
examples/runtime/textures/parallax-d.ktx


BIN
examples/runtime/textures/parallax-h.ktx


BIN
examples/runtime/textures/parallax-n.ktx


+ 9 - 0
scripts/build.ninja

@@ -29,6 +29,15 @@ rule texturec_etc1
 rule texturec_etc2
     command = texturec -f $in -o $out -t etc2 -m
 
+rule texturec_diffuse
+    command = texturec -f $in -o $out -t bc2 -m
+
+rule texturec_normal
+    command = texturec -f $in -o $out -t bc5 -m -n
+
+rule texturec_height
+    command = texturec -f $in -o $out -t r8
+
 pwd = ../examples/assets/meshes
 subninja ../examples/assets/meshes/meshes.ninja
 

+ 2 - 0
scripts/genie.lua

@@ -370,6 +370,8 @@ bgfxProject("", "StaticLib", {})
 
 dofile(path.join(BX_DIR,   "scripts/bx.lua"))
 dofile(path.join(BIMG_DIR, "scripts/bimg.lua"))
+dofile(path.join(BIMG_DIR, "scripts/bimg_decode.lua"))
+dofile(path.join(BIMG_DIR, "scripts/bimg_encode.lua"))
 
 if _OPTIONS["with-examples"] or _OPTIONS["with-tools"] then
 	group "examples"