Sfoglia il codice sorgente

Create global resources in render_world_globals

Daniele Bartolini 11 anni fa
parent
commit
a8b433a1da
4 ha cambiato i file con 89 aggiunte e 156 eliminazioni
  1. 7 126
      engine/gui/Gui.cpp
  2. 1 1
      engine/gui/Gui.h
  3. 80 29
      engine/renderers/RenderWorld.cpp
  4. 1 0
      engine/renderers/RenderWorld.h

+ 7 - 126
engine/gui/Gui.cpp

@@ -49,89 +49,6 @@ namespace crown
 
 using namespace matrix4x4;
 
-ShaderId			gui_default_vs;
-ShaderId			gui_default_fs;
-ShaderId			gui_texture_fs;
-GPUProgramId		gui_default_program;
-GPUProgramId		gui_texture_program;
-UniformId			gui_albedo_0;
-
-ShaderId			font_vs;
-ShaderId			font_fs;
-GPUProgramId		font_program;
-UniformId			font_uniform;
-
-static const char* default_vertex =
-	"precision mediump float;"
-	"uniform mat4      	u_model_view_projection;"
-
-	"attribute vec4    	a_position;"
-	"attribute vec2    	a_tex_coord0;"
-	"attribute vec4    	a_color;"
-
-	"varying vec2		tex_coord0;"
-	"varying vec4		color;"
-
-	"void main(void)"
-	"{"
-	"	tex_coord0 = a_tex_coord0;"
-	"   color = a_color;"
-	"	gl_Position = u_model_view_projection * a_position;"
-	"}";
-
-static const char* default_fragment =
-	"precision mediump float;"
-	"varying vec4            color;"
-
-	"void main(void)"
-	"{"
-	"	gl_FragColor = color;"
-	"}";
-
-static const char* texture_fragment = 
-	"precision mediump float;"
-	"varying vec2       tex_coord0;"
-	"varying vec4       color;"
-
-	"uniform sampler2D  u_albedo_0;"
-
-	"void main(void)"
-	"{"
-	"	gl_FragColor = texture2D(u_albedo_0, tex_coord0);"
-	"}";
-
-static const char* sdf_vertex =
-	"precision mediump float;"
-	"uniform mat4      	u_model_view_projection;"
-
-	"attribute vec4		a_position;"
-	"attribute vec2		a_tex_coord0;"
-
-	"varying vec2		v_tex_coord;"
-	"varying vec4		v_color;"
-
-	"void main(void)"
-	"{"
-	"	gl_Position = u_model_view_projection * a_position;"
-	"	v_tex_coord = a_tex_coord0;"
-	"	v_color = vec4(1, 1, 1, 1);"
-	"}";
-
-static const char* sdf_fragment =
-	"precision mediump float;"
-	"uniform sampler2D u_texture;"
-
-	"varying vec4 v_color;"
-	"varying vec2 v_tex_coord;"
-
-	"const float smoothing = 1.0/16.0;"
-
-	"void main() {"
-		"float distance = texture2D(u_texture, v_tex_coord).a;"
-		"float alpha = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance);"
-		"gl_FragColor = vec4(v_color.rgb, alpha);"
-	"}";
-
 //-----------------------------------------------------------------------------
 Gui::Gui(RenderWorld& render_world, GuiResource* gr, Renderer& r)
 	: m_render_world(render_world)
@@ -152,8 +69,6 @@ Gui::Gui(RenderWorld& render_world, GuiResource* gr, Renderer& r)
 	set_identity(m_pose);
 	set_translation(m_pose, pos);
 
-	create_gfx();
-
 	// Gui's rects creation
 	for (uint32_t i = 0; i < m_resource->num_rects(); i++)
 	{
@@ -211,8 +126,6 @@ Gui::~Gui()
 	{
 		CE_DELETE(m_text_pool, m_texts[i]);
 	}
-
-	destroy_gfx();
 }
 
 //-----------------------------------------------------------------------------
@@ -357,7 +270,7 @@ void Gui::destroy_text(GuiTextId id)
 }
 
 //-----------------------------------------------------------------------------
-void Gui::render()
+void Gui::render(UniformId font, UniformId albedo)
 {
 	// resolution
 	uint32_t width = 0;
@@ -375,7 +288,7 @@ void Gui::render()
 	// Render all Rects
 	for (uint32_t i = 0; i < m_rects.size(); i++)
 	{
-		m_r.set_program(gui_default_program);
+		m_r.set_program(render_world_globals::default_program());
 		m_r.set_state(STATE_DEPTH_WRITE 
 		| STATE_COLOR_WRITE
 		| STATE_ALPHA_WRITE
@@ -391,7 +304,7 @@ void Gui::render()
 	// Render all Triangles
 	for (uint32_t i = 0; i < m_triangles.size(); i++)
 	{
-		m_r.set_program(gui_default_program);
+		m_r.set_program(render_world_globals::default_program());
 		m_r.set_state(STATE_DEPTH_WRITE 
 		| STATE_COLOR_WRITE
 		| STATE_ALPHA_WRITE
@@ -407,7 +320,7 @@ void Gui::render()
 	// Render all Images
 	for (uint32_t i = 0; i < m_images.size(); i++)
 	{
-		m_r.set_program(gui_texture_program);
+		m_r.set_program(render_world_globals::default_texture_program());
 		m_r.set_state(STATE_DEPTH_WRITE 
 		| STATE_COLOR_WRITE 
 		| STATE_ALPHA_WRITE 
@@ -417,13 +330,13 @@ void Gui::render()
 		| STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
 		m_r.set_pose(m_pose);
 
-		m_images[i]->render(gui_albedo_0);
+		m_images[i]->render(albedo);
 	}
 
 	// Render all Texts
 	for (uint32_t i = 0; i < m_texts.size(); i++)
 	{
-		m_r.set_program(font_program);
+		m_r.set_program(render_world_globals::default_font_program());
 		m_r.set_state(STATE_DEPTH_WRITE 
 		| STATE_COLOR_WRITE 
 		| STATE_ALPHA_WRITE 
@@ -433,40 +346,8 @@ void Gui::render()
 		| STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
 		m_r.set_pose(m_pose);
 
-		m_texts[i]->render(font_uniform);
+		m_texts[i]->render(font);
 	}
 }
 
-//-----------------------------------------------------------------------------
-void Gui::create_gfx()
-{
-	gui_default_vs = m_r.create_shader(ShaderType::VERTEX, default_vertex);
-	gui_default_fs = m_r.create_shader(ShaderType::FRAGMENT, default_fragment);
-	gui_texture_fs = m_r.create_shader(ShaderType::FRAGMENT, texture_fragment);
-	gui_default_program = m_r.create_gpu_program(gui_default_vs, gui_default_fs);
-	gui_texture_program = m_r.create_gpu_program(gui_default_vs, gui_texture_fs);
-	gui_albedo_0 = m_r.create_uniform("u_albedo_0", UniformType::INTEGER_1, 1);
-
-	font_vs = m_r.create_shader(ShaderType::VERTEX, sdf_vertex);
-	font_fs = m_r.create_shader(ShaderType::FRAGMENT, sdf_fragment);
-	font_program = m_r.create_gpu_program(font_vs, font_fs);
-	font_uniform = m_r.create_uniform("u_texture", UniformType::INTEGER_1, 1);
-}
-
-//-----------------------------------------------------------------------------
-void Gui::destroy_gfx()
-{
-	m_r.destroy_uniform(gui_albedo_0);
-	m_r.destroy_gpu_program(gui_texture_program);
-	m_r.destroy_gpu_program(gui_default_program);
-	m_r.destroy_shader(gui_texture_fs);
-	m_r.destroy_shader(gui_default_fs);
-	m_r.destroy_shader(gui_default_vs);
-
-	m_r.destroy_uniform(font_uniform);
-	m_r.destroy_gpu_program(font_program);
-	m_r.destroy_shader(font_vs);
-	m_r.destroy_shader(font_fs);
-}
-
 } // namespace crown

+ 1 - 1
engine/gui/Gui.h

@@ -87,7 +87,7 @@ struct Gui
 	void				update_text(GuiTextId id, const char* str, uint32_t font_size, const Vector3& pos);
 	void				destroy_text(GuiTextId id);
 
-	void				render();
+	void				render(UniformId font, UniformId albedo);
 
 private:
 

+ 80 - 29
engine/renderers/RenderWorld.cpp

@@ -45,50 +45,86 @@ namespace crown
 namespace render_world_globals
 {
 	static const char* default_vertex =
-		"precision mediump float;"
-		"uniform mat4      	u_model;"
-		"uniform mat4      	u_model_view_projection;"
+		"precision mediump float;\n"
+		"uniform mat4      	u_model_view_projection;\n"
 
-		"attribute vec4    	a_position;"
-		"attribute vec2    	a_tex_coord0;"
-		"attribute vec4    	a_color;"
+		"attribute vec4    	a_position;\n"
+		"attribute vec2    	a_tex_coord0;\n"
+		"attribute vec4    	a_color;\n"
 
-		"varying vec2		tex_coord0;"
-		"varying vec4		color;"
+		"varying vec2		tex_coord0;\n"
+		"varying vec4		color;\n"
 
-		"void main(void)"
-		"{"
-		"	tex_coord0 = a_tex_coord0;"
-		"   color = a_color;"
-		"	gl_Position = u_model_view_projection * a_position;"
-		"}";
+		"void main(void)\n"
+		"{\n"
+		"	tex_coord0 = a_tex_coord0;\n"
+		"   color = a_color;\n"
+		"	gl_Position = u_model_view_projection * a_position;\n"
+		"}\n";
 
 	static const char* default_fragment = 
-		"precision mediump float;"
-		"varying vec4 color;"
-		"void main(void)"
-		"{"
-		"	gl_FragColor = color;"
-		"}";
+		"precision mediump float;\n"
+		"varying vec4 color;\n"
+		"void main(void)\n"
+		"{\n"
+		"	gl_FragColor = color;\n"
+		"}\n";
 
 	static const char* texture_fragment = 
-		"precision mediump float;"
-		"varying vec2       tex_coord0;"
-		"varying vec4       color;"
+		"precision mediump float;\n"
+		"varying vec2       tex_coord0;\n"
+		"varying vec4       color;\n"
 
-		"uniform sampler2D  u_albedo_0;"
+		"uniform sampler2D  u_albedo_0;\n"
+
+		"void main(void)\n"
+		"{\n"
+		"	gl_FragColor = texture2D(u_albedo_0, tex_coord0);\n"
+		"}\n";
+
+	static const char* sdf_vertex =
+		"precision mediump float;\n"
+		"uniform mat4      	u_model_view_projection;\n"
+
+		"attribute vec4		a_position;\n"
+		"attribute vec2		a_tex_coord0;\n"
+
+		"varying vec2		v_tex_coord;\n"
+		"varying vec4		v_color;\n"
+
+		"void main(void)\n"
+		"{\n"
+		"	gl_Position = u_model_view_projection * a_position;\n"
+		"	v_tex_coord = a_tex_coord0;\n"
+		"	v_color = vec4(1, 1, 1, 1);\n"
+		"}\n";
+
+	static const char* sdf_fragment =
+		"precision mediump float;\n"
+		"uniform sampler2D u_texture;\n"
+
+		"varying vec4 v_color;\n"
+		"varying vec2 v_tex_coord;\n"
+
+		"const float smoothing = 1.0/16.0;\n"
+
+		"void main() {\n"
+			"float distance = texture2D(u_texture, v_tex_coord).a;\n"
+			"float alpha = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance);\n"
+			"gl_FragColor = vec4(v_color.rgb, alpha);\n"
+		"}\n";
 
-		"void main(void)"
-		"{"
-		"	gl_FragColor = texture2D(u_albedo_0, tex_coord0);"
-		"}";
 
 	ShaderId default_vs;
 	ShaderId default_fs;
 	ShaderId texture_fs;
+	ShaderId font_vs;
+	ShaderId font_fs;
 	GPUProgramId texture_program;
 	GPUProgramId def_program;
+	GPUProgramId font_program;
 	UniformId u_albedo_0;
+	UniformId u_font;
 	uint32_t num_refs = 0;
 
 	void init()
@@ -99,11 +135,17 @@ namespace render_world_globals
 		num_refs++;
 
 		Renderer* r = device()->renderer();
+
 		default_vs = r->create_shader(ShaderType::VERTEX, default_vertex);
 		default_fs = r->create_shader(ShaderType::FRAGMENT, default_fragment);
 		texture_fs = r->create_shader(ShaderType::FRAGMENT, texture_fragment);
+		font_vs = r->create_shader(ShaderType::VERTEX, sdf_vertex);
+		font_fs = r->create_shader(ShaderType::FRAGMENT, sdf_fragment);
+
 		def_program = r->create_gpu_program(default_vs, default_fs);
 		texture_program = r->create_gpu_program(default_vs, texture_fs);
+		font_program = r->create_gpu_program(font_vs, font_fs);
+		u_font = r->create_uniform("u_font", UniformType::INTEGER_1, 1);
 		u_albedo_0 = r->create_uniform("u_albedo_0", UniformType::INTEGER_1, 1);
 	}
 
@@ -117,10 +159,14 @@ namespace render_world_globals
 		Renderer* r = device()->renderer();
 		r->destroy_gpu_program(texture_program);
 		r->destroy_gpu_program(def_program);
+		r->destroy_gpu_program(font_program);
 		r->destroy_shader(default_vs);
 		r->destroy_shader(default_fs);
 		r->destroy_shader(texture_fs);
+		r->destroy_shader(font_vs);
+		r->destroy_shader(font_fs);
 		r->destroy_uniform(u_albedo_0);
+		r->destroy_uniform(u_font);
 	}
 
 	GPUProgramId default_program()
@@ -132,6 +178,11 @@ namespace render_world_globals
 	{
 		return texture_program;
 	}
+
+	GPUProgramId default_font_program()
+	{
+		return font_program;
+	}
 };
 
 //-----------------------------------------------------------------------------
@@ -290,7 +341,7 @@ void RenderWorld::update(const Matrix4x4& view, const Matrix4x4& projection, uin
 	// Draw all guis
 	for (uint32_t g = 0; g < m_guis.size(); g++)
 	{
-		m_guis[g]->render();
+		m_guis[g]->render(render_world_globals::u_font, render_world_globals::u_albedo_0);
 	}
 }
 

+ 1 - 0
engine/renderers/RenderWorld.h

@@ -61,6 +61,7 @@ namespace render_world_globals
 
 	GPUProgramId default_program();
 	GPUProgramId default_texture_program();
+	GPUProgramId default_font_program();
 };
 
 class RenderWorld