<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>


<!--
This demo shows how to render a mesh (torus) with a textured phong shader.
-->


<glsl_hacker>
	
  <window name="win3d01" title="Vulkan - Normal Mapping" 
          width="800" height="400" resizable="1" menubar="1" 
          separate_render_thread="1" gpu_index="0"
          renderer_type="Vulkan" vsync="0" />
  
  
	<script name="init_scene" run_mode="INIT" >
		<raw_data><![CDATA[	
    
    
local demo_dir = gh_utils.get_demo_dir()
local framework_dir = demo_dir .. "../framework/"
dofile(framework_dir .. "framework_vk.lua")


VK_Framework_Init_Begin(framework_dir)

VK_Framework_Set_Main_Title("Vulkan / Normal Mapping")
    
    
    
    
    
    
    

camera = VK_Framework_Get_Camera()
gh_camera.set_position(camera, 0, 5, 10)
gh_camera.set_lookat(camera, 0, 0, 0, 1)
gh_camera.set_upvec(camera, 0, 1, 0, 0)











-----------------------------------------------------------------------
--
function UpdateCameraTransform(ub, cam)
  local buffer_offset_bytes = 0
  gh_gpu_buffer.set_matrix4x4(ub, buffer_offset_bytes, cam, "camera_projection")
  
  buffer_offset_bytes = 64
  gh_gpu_buffer.set_matrix4x4(ub, buffer_offset_bytes, cam, "camera_view")  
end


function UpdateObjectTransform(ub, obj)
  local buffer_offset_bytes = 64 * 2
  gh_gpu_buffer.set_matrix4x4(ub, buffer_offset_bytes, obj, "object_global_transform")
end


function UpdateUVTiling(ub, s, t)
  local buffer_offset_bytes = 64 * 3
  gh_gpu_buffer.set_value_4f(ub, buffer_offset_bytes, s, t, 0, 0)
end


local ub_size = 512
ub1 = gh_gpu_buffer.create("UNIFORM", "NONE", ub_size, "")
gh_gpu_buffer.bind(ub1)
gh_gpu_buffer.map(ub1)



UpdateCameraTransform(ub1, camera)
-- UpdateUVTiling(ub1, 4.0, 2.0)
UpdateUVTiling(ub1, 8.0, 4.0)





-----------------------------------------------------------------------
--
local vertex_shader = demo_dir .. "spirv/bumpmapping-vs.spv"
local pixel_shader = demo_dir .. "spirv/bumpmapping-ps.spv"
bump_mapping_shader = gh_gpu_program.vk_create_from_spirv_module_file("bump_mapping_shader",   vertex_shader, "main",     pixel_shader, "main",    "", "",    "", "",     "", "",    "", "") 





-----------------------------------------------------------------------
--
local PF_U8_RGBA = 3
tex0 = gh_texture.create_from_file_v5(framework_dir .. "data/textures/14244-diffuse.jpg", PF_U8_RGBA)
tex1 = gh_texture.create_from_file_v5(framework_dir .. "data/textures/14244-normal.jpg", PF_U8_RGBA)



-----------------------------------------------------------------------
--
sampler = gh_renderer.texture_sampler_create("LINEAR", "WRAP", 0.0, 0)







-----------------------------------------------------------------------
--
ds = gh_renderer.vk_descriptorset_create()
ub_binding_point = 0
gh_renderer.vk_descriptorset_add_resource_gpu_buffer(ds, ub1, ub_binding_point, VK_SHADER_STAGE_VERTEX)
tex_binding_point = 1
tex_res_index = gh_renderer.vk_descriptorset_add_resource_texture(ds, tex0, sampler, tex_binding_point, VK_SHADER_STAGE_FRAGMENT)
tex_binding_point = 2
tex_res_index = gh_renderer.vk_descriptorset_add_resource_texture(ds, tex1, sampler, tex_binding_point, VK_SHADER_STAGE_FRAGMENT)
tex_binding_point = 3
tex_res_index = gh_renderer.vk_descriptorset_add_resource_texture(ds, tex2, sampler, tex_binding_point, VK_SHADER_STAGE_FRAGMENT)

--local constant_size_bytes = 4
--local constant_offset_bytes = 0
--gh_renderer.vk_descriptorset_add_push_constant_range(ds, constant_size_bytes, constant_offset_bytes, VK_SHADER_STAGE_FRAGMENT)

gh_renderer.vk_descriptorset_build(ds)
gh_renderer.vk_descriptorset_update(ds)






-----------------------------------------------------------------------
--
pso01 = gh_renderer.pipeline_state_create("pso01", bump_mapping_shader, "")
gh_renderer.pipeline_state_set_attrib_4i(pso01, "DEPTH_TEST", 1, 0, 0, 0)
gh_renderer.pipeline_state_set_attrib_4i(pso01, "FILL_MODE", POLYGON_MODE_SOLID, 0, 0, 0)
gh_renderer.pipeline_state_set_attrib_4i(pso01, "PRIMITIVE_TYPE", PRIMITIVE_TRIANGLE, 0, 0, 0)
gh_renderer.pipeline_state_set_attrib_4i(pso01, "CULL_MODE", POLYGON_FACE_NONE, 0, 0, 0)
gh_renderer.pipeline_state_set_attrib_4i(pso01, "CCW", 0, 0, 0, 0)

pso_valid = gh_renderer.vk_pipeline_state_build(pso01, ds)
if (pso_valid == 0) then
	print("ERROR: pipeline state pso01 is not valid.")
end






-----------------------------------------------------------------------
--
--mesh = gh_mesh.create_torus(6.0, 2.0, 10) -- 200f / 121v
--mesh = gh_mesh.create_torus(6.0, 2.0, 20) -- 800f / 441v
mesh = gh_mesh.create_torus(6.0, 2.0, 40) -- 3200f / 1681v
--mesh = gh_mesh.create_torus(6.0, 2.0, 50) -- 5000f / 2601v
--mesh = gh_mesh.create_torus(6.0, 2.0, 100) -- 20'000f / 10'201v
--mesh = gh_mesh.create_torus(6.0, 2.0, 200) -- 80'000f / 40'401v
--mesh = gh_mesh.create_torus(6.0, 2.0, 400) -- 320'000f / 160'801v
--mesh = gh_mesh.create_torus(6.0, 2.0, 500) -- 500'000f / 251'001v
--mesh = gh_mesh.create_torus(6.0, 2.0, 1000) -- 2'000'000f / 1'002'001v

gh_mesh.set_vertices_color(mesh, 1.0, 1.0, 0.0, 1.0)







-----------------------------------------------------------------------
--
VK_Framework_Init_End()




		]]></raw_data>
	</script> 
	
	

	
	<script name="terminate_scene" run_mode="TERMINATE" >
		<raw_data><![CDATA[	

gh_gpu_buffer.unmap(ub1)
VK_Framework_Terminate()

		]]></raw_data>
	</script> 
	
	
	
  
	<script name="update_scene" run_mode="FRAME" >
		<raw_data><![CDATA[	

-----------------------------------------------------------------------
VK_Framework_Frame_Begin(0.2, 0.2, 0.2)
-----------------------------------------------------------------------


local elapsed_time = VK_Framework_GetTime()


if (pso_valid == 1) then

  gh_renderer.vk_descriptorset_bind(ds)
  gh_renderer.pipeline_state_bind(pso01)

  gh_object.set_euler_angles(mesh, elapsed_time * 3.0, elapsed_time * 7.0, elapsed_time * 11.0)
  UpdateObjectTransform(ub1, mesh)
  gh_object.render(mesh)

end




-----------------------------------------------------------------------
VK_Framework_Frame_End(1)
-----------------------------------------------------------------------


		]]></raw_data>
	</script> 
	

	<script name="resize_scene" run_mode="SIZE" >
		<raw_data><![CDATA[	

VK_Framework_Resize()

UpdateCameraTransform(ub1, camera)

		]]></raw_data>
	</script> 
  
  
</glsl_hacker>
