Ver código fonte

Port DebugLine to BGFX

mikymod 11 anos atrás
pai
commit
ae77379919
3 arquivos alterados com 105 adições e 43 exclusões
  1. 3 0
      engine/Device.cpp
  2. 91 39
      engine/renderers/DebugLine.cpp
  3. 11 4
      engine/renderers/DebugLine.h

+ 3 - 0
engine/Device.cpp

@@ -55,6 +55,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "WorldManager.h"
 #include "NetworkFilesystem.h"
 #include "LuaSystem.h"
+#include "DebugLine.h"
 
 #if defined(LINUX) || defined(WINDOWS)
 	#include "BundleCompiler.h"
@@ -161,6 +162,7 @@ void Device::init()
 	// Create renderer
 	CE_LOGD("Creating renderer...");
 	graphics_system::init();
+	debug_line::init();
 
 	CE_LOGD("Creating lua system...");
 	lua_system::init();
@@ -217,6 +219,7 @@ void Device::shutdown()
 	CE_DELETE(m_allocator, m_keyboard);
 
 	CE_LOGD("Releasing renderer...");
+	debug_line::shutdown();
 	graphics_system::shutdown();
 
 	CE_LOGD("Releasing world manager...");

+ 91 - 39
engine/renderers/DebugLine.cpp

@@ -28,11 +28,74 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "MathUtils.h"
 #include "Color4.h"
 #include "Vector3.h"
-#include "RenderWorld.h"
+#include <bgfx.h>
 
 namespace crown
 {
 
+namespace debug_line
+{
+	static const uint8_t vs_debug_line_bin_h[335] =
+	{
+		0x56, 0x53, 0x48, 0x02, 0xa4, 0x8b, 0xef, 0x49, 0x01, 0x00, 0x0f, 0x75, 0x5f, 0x6d, 0x6f, 0x64, // VSH....I...u_mod
+		0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x09, 0x01, 0x00, 0x00, 0x01, 0x00, // elViewProj......
+		0x2a, 0x01, 0x00, 0x00, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x6d, 0x65, // *...attribute me
+		0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x61, 0x5f, 0x63, 0x6f, 0x6c, // diump vec4 a_col
+		0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x6d, // or0;.attribute m
+		0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x61, 0x5f, 0x70, 0x6f, // ediump vec3 a_po
+		0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x76, 0x61, 0x72, 0x79, 0x69, 0x6e, 0x67, 0x20, // sition;.varying 
+		0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x76, 0x5f, 0x63, // mediump vec4 v_c
+		0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x6d, // olor0;.uniform m
+		0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x6d, 0x61, 0x74, 0x34, 0x20, 0x75, 0x5f, 0x6d, 0x6f, // ediump mat4 u_mo
+		0x64, 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x3b, 0x0a, 0x76, 0x6f, 0x69, // delViewProj;.voi
+		0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x20, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20, 0x6d, 0x65, // d main ().{.  me
+		0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, // diump vec4 tmpva
+		0x72, 0x5f, 0x31, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, 0x31, 0x2e, // r_1;.  tmpvar_1.
+		0x77, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, // w = 1.0;.  tmpva
+		0x72, 0x5f, 0x31, 0x2e, 0x78, 0x79, 0x7a, 0x20, 0x3d, 0x20, 0x61, 0x5f, 0x70, 0x6f, 0x73, 0x69, // r_1.xyz = a_posi
+		0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x50, 0x6f, 0x73, 0x69, 0x74, // tion;.  gl_Posit
+		0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x28, 0x75, 0x5f, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x56, 0x69, // ion = (u_modelVi
+		0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x20, 0x2a, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, // ewProj * tmpvar_
+		0x31, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x76, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x20, 0x3d, // 1);.  v_color0 =
+		0x20, 0x61, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x00,       //  a_color0;.}...
+	};
+
+	static const uint8_t fs_debug_line_bin_h[91] =
+	{
+		0x46, 0x53, 0x48, 0x02, 0xa4, 0x8b, 0xef, 0x49, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x76, 0x61, // FSH....I..L...va
+		0x72, 0x79, 0x69, 0x6e, 0x67, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, // rying mediump ve
+		0x63, 0x34, 0x20, 0x76, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x76, 0x6f, 0x69, // c4 v_color0;.voi
+		0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x20, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20, 0x67, 0x6c, // d main ().{.  gl
+		0x5f, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x63, // _FragColor = v_c
+		0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x00,                               // olor0;.}...
+	};
+
+	static bgfx::VertexDecl s_decl;
+	static bgfx::ProgramHandle s_prog;
+
+	void init()
+	{
+		s_decl
+			.begin()
+			.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
+			.add(bgfx::Attrib::Color0,   4, bgfx::AttribType::Uint8, true)
+			.end();
+
+		bgfx::ShaderHandle vs = bgfx::createShader(
+			bgfx::makeRef(vs_debug_line_bin_h, sizeof(vs_debug_line_bin_h)));
+		bgfx::ShaderHandle fs = bgfx::createShader(
+			bgfx::makeRef(fs_debug_line_bin_h, sizeof(fs_debug_line_bin_h)));
+
+		s_prog = bgfx::createProgram(vs, fs, true);
+	}
+
+	void shutdown()
+	{
+		bgfx::destroyProgram(s_prog);
+	}
+
+} // namespace debug_line
+
 //-----------------------------------------------------------------------------
 DebugLine::DebugLine(bool depth_test)
 	: m_depth_test(depth_test)
@@ -46,20 +109,15 @@ void DebugLine::add_line(const Color4& color, const Vector3& start, const Vector
 	if (m_num_lines >= CE_MAX_DEBUG_LINES)
 		 return;
 
-	m_lines[m_num_lines].position_0[0] = start.x;
-	m_lines[m_num_lines].position_0[1] = start.y;
-	m_lines[m_num_lines].position_0[2] = start.z;
-	m_lines[m_num_lines].color_0[0] = color.r;
-	m_lines[m_num_lines].color_0[1] = color.g;
-	m_lines[m_num_lines].color_0[2] = color.b;
-	m_lines[m_num_lines].color_0[3] = color.a;
-	m_lines[m_num_lines].position_1[0] = end.x;
-	m_lines[m_num_lines].position_1[1] = end.y;
-	m_lines[m_num_lines].position_1[2] = end.z;
-	m_lines[m_num_lines].color_1[0] = color.r;
-	m_lines[m_num_lines].color_1[1] = color.g;
-	m_lines[m_num_lines].color_1[2] = color.b;
-	m_lines[m_num_lines].color_1[3] = color.a;
+	m_lines[m_num_lines].p0[0] = start.x;
+	m_lines[m_num_lines].p0[1] = start.y;
+	m_lines[m_num_lines].p0[2] = start.z;
+	m_lines[m_num_lines].c0    = color4::to_abgr(color);
+
+	m_lines[m_num_lines].p1[0] = end.x;
+	m_lines[m_num_lines].p1[1] = end.y;
+	m_lines[m_num_lines].p1[2] = end.z;
+	m_lines[m_num_lines].c1    = color4::to_abgr(color);
 
 	m_num_lines++;
 }
@@ -100,30 +158,24 @@ void DebugLine::clear()
 //-----------------------------------------------------------------------------
 void DebugLine::commit()
 {
-	// if (!m_num_lines)
-	// 	return;
-
-	// Renderer* r = device()->renderer();
-
-	// TransientVertexBuffer tvb;
-	// TransientIndexBuffer tib;
-	// r->reserve_transient_vertex_buffer(&tvb, m_num_lines * 2, VertexFormat::P3_C4);
-	// r->reserve_transient_index_buffer(&tib, m_num_lines * 2);
-
-	// memcpy(tvb.data, m_lines, sizeof(Line) * m_num_lines);
-
-	// uint16_t* indices = (uint16_t*) tib.data;
-	// for (uint32_t i = 0; i < m_num_lines * 2; i++)
-	// {
-	// 	indices[i] = i;
-	// }
-
-	// r->set_state((m_depth_test ? STATE_DEPTH_TEST_LESS : 0) | STATE_COLOR_WRITE | STATE_CULL_CW | STATE_PRIMITIVE_LINES);
-	// r->set_vertex_buffer(tvb);
-	// r->set_index_buffer(tib);
-	// r->set_program(render_world_globals::default_program());
-	// r->set_pose(matrix4x4::IDENTITY);
-	// r->commit(0);
+	if (!m_num_lines)
+		return;
+
+	bgfx::TransientVertexBuffer tvb;
+	bgfx::allocTransientVertexBuffer(&tvb, CE_MAX_DEBUG_LINES * 2, debug_line::s_decl);
+
+	memcpy(tvb.data, m_lines, sizeof(Line) * m_num_lines);
+
+	bgfx::setState(BGFX_STATE_PT_LINES
+		| BGFX_STATE_RGB_WRITE
+		| BGFX_STATE_DEPTH_WRITE
+		| (m_depth_test ? BGFX_STATE_DEPTH_TEST_LESS
+			: BGFX_STATE_DEPTH_TEST_ALWAYS)
+		| BGFX_STATE_CULL_CW);
+
+	bgfx::setProgram(debug_line::s_prog);
+	bgfx::setVertexBuffer(&tvb, 0, m_num_lines * 2);
+	bgfx::submit(0);
 }
 
 } // namespace crown

+ 11 - 4
engine/renderers/DebugLine.h

@@ -34,6 +34,13 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
+namespace debug_line
+{
+	void init();
+
+	void shutdown();
+} // namespace debug_line
+
 struct DebugLine
 {
 	/// Whether to enable @a depth_test
@@ -55,10 +62,10 @@ private:
 
 	struct Line
 	{
-		float position_0[3];
-		float color_0[4];
-		float position_1[3];
-		float color_1[4];
+		float p0[3];
+		uint32_t c0;
+		float p1[3];
+		uint32_t c1;
 	};
 
 	bool m_depth_test;