瀏覽代碼

Fixing bugs

Panagiotis Christopoulos Charitos 11 年之前
父節點
當前提交
e1140e6e87

+ 21 - 5
include/anki/core/App.h

@@ -33,7 +33,8 @@ class ResourceManager;
 class App
 {
 public:
-	using UserMainLoopCallback = void(*)(App& app, void* userData);
+	/// User callback of main loop
+	using UserMainLoopCallback = I32(*)(App& app, void* userData);
 
 	/// Create and initialize the application
 	App(const ConfigSet& config, 
@@ -81,10 +82,10 @@ public:
 		return m_heapAlloc;
 	}
 
-	/// TODO
-	void quit(int code);
-
-	/// Run the main loop
+	/// Run the main loop.
+	/// @param callback The user callback to run along with the other main loop
+	///                 code.
+	/// @param userData The data to pass to the user callback.
 	void mainLoop(UserMainLoopCallback callback, void* userData);
 
 	Input& getInput()
@@ -102,6 +103,21 @@ public:
 		return *m_renderer;
 	}
 
+	ResourceManager& getResourceManager()
+	{
+		return *m_resources;
+	}
+
+	ScriptManager& getScriptManager()
+	{
+		return *m_script;
+	}
+
+	HeapAllocator<U8> getAllocator() const
+	{
+		return m_heapAlloc;
+	}
+
 private:
 	// Allocation
 	AllocAlignedCallback m_allocCb;

+ 7 - 1
include/anki/renderer/Renderer.h

@@ -247,6 +247,11 @@ public:
 	{
 		return *m_threadpool;
 	}
+
+	const String& _getShadersPrependedSource() const
+	{
+		return m_shadersPrependedSource;
+	}
 	/// @}
 
 private:
@@ -279,7 +284,6 @@ private:
 	/// @name For drawing a quad into the active framebuffer
 	/// @{
 	GlBufferHandle m_quadPositionsBuff; ///< The VBO for quad positions
-
 	ProgramResourcePointer m_drawQuadVert;
 	/// @}
 
@@ -301,6 +305,8 @@ private:
 
 	GlFramebufferHandle m_defaultFb;
 
+	String m_shadersPrependedSource; ///< String to append in user shaders
+
 	void computeProjectionParams(const Mat4& projMat);
 };
 

+ 12 - 0
include/anki/resource/ResourceManager.h

@@ -190,6 +190,17 @@ public:
 		return m_cacheDir;
 	}
 
+	/// Set it with information from the renderer
+	void _setShadersPrependedSource(const CString& cstr)
+	{
+		m_shadersPrependedSource = cstr;
+	}
+
+	const ResourceString& _getShadersPrependedSource() const
+	{
+		return m_shadersPrependedSource;
+	}
+
 	template<typename T>
 	Bool _findLoadedResource(const CString& filename, 
 		ResourcePointer<T, ResourceManager>& ptr)
@@ -219,6 +230,7 @@ private:
 	ResourceString m_dataDir;
 	U32 m_maxTextureSize;
 	U32 m_textureAnisotropy;
+	ResourceString m_shadersPrependedSource;
 };
 
 #undef ANKI_RESOURCE

+ 4 - 2
include/anki/script/LuaBinder.h

@@ -9,6 +9,7 @@
 #include "anki/util/Assert.h"
 #include "anki/util/StdTypes.h"
 #include "anki/util/Allocator.h"
+#include "anki/util/String.h"
 #include <lua.hpp>
 #ifndef ANKI_LUA_HPP
 #	error "Wrong LUA header included"
@@ -70,9 +71,10 @@ public:
 	void exposeVariable(const char* name, T* y);
 
 	/// Evaluate a file
-	void evalFile(const char* filename);
+	void evalFile(const CString& filename);
+
 	/// Evaluate a string
-	void evalString(const char* str);
+	void evalString(const CString& str);
 
 	/// For debugging purposes
 	static void stackDump(lua_State* l);

+ 2 - 2
include/anki/util/String.h

@@ -228,10 +228,10 @@ public:
 
 private:
 	const Char* m_ptr = nullptr;
-	mutable U16 m_length = 0;
+	mutable U32 m_length = 0;
 
 	/// Constructor for friends
-	CString(const Char* ptr, U16 length) noexcept
+	CString(const Char* ptr, U32 length) noexcept
 	:	m_ptr(ptr),
 		m_length(length)
 	{

+ 8 - 4
shaders/BsCommonFrag.glsl

@@ -53,8 +53,10 @@ void particleAlpha(in sampler2D tex, in float alpha)
 void particleSoftTextureAlpha(in sampler2D depthMap, in sampler2D tex, 
 	in float alpha)
 {
-	const vec2 screenSize = 
-		vec2(1.0 / float(RENDERING_WIDTH), 1.0 / float(RENDERING_HEIGHT));
+	const vec2 screenSize = vec2(
+		1.0 / float(ANKI_RENDERER_WIDTH), 
+		1.0 / float(ANKI_RENDERER_HEIGHT));
+
 	float depth = texture(depthMap, gl_FragCoord.xy * screenSize).r;
 
 	float delta = depth - gl_FragCoord.z;
@@ -71,8 +73,10 @@ void particleSoftTextureAlpha(in sampler2D depthMap, in sampler2D tex,
 void particleSoftColorAlpha(in sampler2D depthMap, in vec3 icolor, 
 	in float alpha)
 {
-	const vec2 screenSize = 
-		vec2(1.0 / float(RENDERING_WIDTH), 1.0 / float(RENDERING_HEIGHT));
+	const vec2 screenSize = vec2(
+		1.0 / float(ANKI_RENDERER_WIDTH), 
+		1.0 / float(ANKI_RENDERER_HEIGHT));
+
 	float depth = texture(depthMap, gl_FragCoord.xy * screenSize).r;
 
 	float delta = depth - gl_FragCoord.z;

+ 1 - 1
shaders/BsCommonVert.glsl

@@ -40,7 +40,7 @@ void particle(in mat4 mvp)
 {
 	gl_Position = mvp * vec4(inPosition, 1);
 	outAlpha = inAlpha;
-	gl_PointSize = inScale * float(RENDERING_WIDTH) / gl_Position.w;
+	gl_PointSize = inScale * float(ANKI_RENDERER_WIDTH) / gl_Position.w;
 }
 
 //==============================================================================

+ 7 - 7
src/core/App.cpp

@@ -175,7 +175,7 @@ void App::init(const ConfigSet& config)
 	rinit.m_cacheDir = m_cacheDir.toCString();
 	rinit.m_allocCallback = m_allocCb;
 	rinit.m_allocCallbackData = m_allocCbData;
-	rinit.m_tempAllocatorMemorySize = 1024 * 1024 * 2;
+	rinit.m_tempAllocatorMemorySize = 1024 * 1024 * 4;
 	m_resources = m_heapAlloc.newInstance<ResourceManager>(rinit);
 
 	// Renderer
@@ -186,6 +186,9 @@ void App::init(const ConfigSet& config)
 		m_heapAlloc,
 		config);
 
+	m_resources->_setShadersPrependedSource(
+		m_renderer->_getShadersPrependedSource().toCString());
+
 	// Scene
 	m_scene = m_heapAlloc.newInstance<SceneGraph>(
 		m_allocCb, m_allocCbData, m_threadpool, m_resources);
@@ -253,20 +256,17 @@ void App::initDirs()
 #endif
 }
 
-//==============================================================================
-void App::quit(int code)
-{}
-
 //==============================================================================
 void App::mainLoop(UserMainLoopCallback callback, void* userData)
 {
 	ANKI_LOGI("Entering main loop");
+	I32 userRetCode = 0;
 
 	HighRezTimer::Scalar prevUpdateTime = HighRezTimer::getCurrentTime();
 	HighRezTimer::Scalar crntTime = prevUpdateTime;
 
 	ANKI_COUNTER_START_TIMER(FPS);
-	while(true)
+	while(userRetCode == 0)
 	{
 		HighRezTimer timer;
 		timer.start();
@@ -280,7 +280,7 @@ void App::mainLoop(UserMainLoopCallback callback, void* userData)
 		m_renderer->render(*m_scene);
 
 		// User update
-		callback(*this, userData);
+		userRetCode = callback(*this, userData);
 
 		m_gl->swapBuffers();
 		ANKI_COUNTERS_RESOLVE_FRAME();

+ 8 - 1
src/renderer/Renderer.cpp

@@ -29,7 +29,8 @@ Renderer::Renderer(
 	m_bs(this),
 	m_dbg(this), 
 	m_tiler(this),
-	m_sceneDrawer(this)
+	m_sceneDrawer(this),
+	m_shadersPrependedSource(alloc)
 {}
 
 //==============================================================================
@@ -91,6 +92,12 @@ void Renderer::init(const ConfigSet& config)
 	m_defaultFb = GlFramebufferHandle(cmdBuff, {});
 
 	cmdBuff.finish();
+
+	// Set the default preprocessor string
+	m_shadersPrependedSource.sprintf(
+		"#define ANKI_RENDERER_WIDTH %u\n"
+		"#define ANKI_RENDERER_HEIGHT %u\n",
+		m_width, m_height);
 }
 
 //==============================================================================

+ 5 - 2
src/resource/Material.cpp

@@ -390,10 +390,13 @@ void Material::parseMaterialTag(const XmlElement& materialEl,
 				{
 					TempResourceString src(rinit.m_tempAlloc);
 
-					src.sprintf("#define LOD %u\n"
+					src.sprintf(
+						"%s\n"
+						"#define LOD %u\n"
 						"#define PASS %u\n"
 						"#define TESSELLATION %u\n"
-						"%s\n", 
+						"%s\n",
+						&rinit.m_resources._getShadersPrependedSource()[0],
 						level, pid, tess, &loader.getProgramSource(shader)[0]);
 
 					TempResourceString filename =

+ 2 - 1
src/resource/ResourceManager.cpp

@@ -25,7 +25,8 @@ ResourceManager::ResourceManager(Initializer& init)
 		init.m_allocCallback, init.m_allocCallbackData, 
 		init.m_tempAllocatorMemorySize)),
 	m_cacheDir(init.m_cacheDir, m_alloc),
-	m_dataDir(m_alloc)
+	m_dataDir(m_alloc),
+	m_shadersPrependedSource(m_alloc)
 {
 	// Init the data path
 	//

+ 4 - 4
src/script/LuaBinder.cpp

@@ -127,14 +127,14 @@ void* LuaBinder::luaAllocCallback(
 }
 
 //==============================================================================
-void LuaBinder::evalString(const char* str)
+void LuaBinder::evalString(const CString& str)
 {
-	int e = luaL_dostring(m_l, str);
+	int e = luaL_dostring(m_l, &str[0]);
 	if(e)
 	{
-		std::string str(lua_tostring(m_l, -1));
+		String str(lua_tostring(m_l, -1), m_alloc);
 		lua_pop(m_l, 1);
-		throw ANKI_EXCEPTION("%s", str.c_str());
+		throw ANKI_EXCEPTION("%s", &str[0]);
 	}
 }
 

+ 19 - 0
src/script/Math.cpp

@@ -209,5 +209,24 @@ ANKI_SCRIPT_WRAP(Mat3)
 	ANKI_LUA_CLASS_END()
 }
 
+//==============================================================================
+static void mat3x4SetAt(Mat3x4* self, U j, U i, F32 f)
+{
+	(*self)(j, i) = f;
+}
+
+ANKI_SCRIPT_WRAP(Mat3x4)
+{
+	ANKI_LUA_CLASS_BEGIN(lb, Mat3x4)
+		ANKI_LUA_EMPTY_CONSTRUCTOR()
+		ANKI_LUA_METHOD("copy", &Mat3x4::operator=)
+		// Accessors
+		ANKI_LUA_METHOD_DETAIL("getAt", 
+			F32 (Mat3x4::Base::*)(const U, const U) const, 
+			&Mat3x4::operator(), BinderFlag::NONE)
+		ANKI_LUA_FUNCTION_AS_METHOD("setAt", &mat3x4SetAt)
+	ANKI_LUA_CLASS_END()
+}
+
 } // end namespace anki
 

+ 1 - 1
src/script/Scene.cpp

@@ -74,7 +74,7 @@ ANKI_SCRIPT_WRAP(SceneGraph)
 {
 	ANKI_LUA_CLASS_BEGIN_NO_DESTRUCTOR(lb, SceneGraph)
 		ANKI_LUA_METHOD("newModelNode", 
-			(&SceneGraph::newSceneNode<ModelNode, const char*>))
+			(&SceneGraph::newSceneNode<ModelNode, const CString&>))
 		ANKI_LUA_METHOD("newInstanceNode", 
 			(&SceneGraph::newSceneNode<InstanceNode>))
 		ANKI_LUA_METHOD("tryFindSceneNode", &SceneGraph::tryFindSceneNode)

+ 1 - 0
src/script/ScriptManager.cpp

@@ -82,6 +82,7 @@ ScriptManager::ScriptManager(HeapAllocator<U8>& alloc, SceneGraph* scene)
 	ANKI_SCRIPT_CALL_WRAP(Vec3);
 	ANKI_SCRIPT_CALL_WRAP(Vec4);
 	ANKI_SCRIPT_CALL_WRAP(Mat3);
+	ANKI_SCRIPT_CALL_WRAP(Mat3x4);
 
 	// Renderer
 	ANKI_SCRIPT_CALL_WRAP(Dbg);

+ 15 - 5
testapp/Main.cpp

@@ -43,6 +43,7 @@ void init()
 
 	SceneGraph& scene = app->getSceneGraph();
 	MainRenderer& renderer = app->getMainRenderer();
+	ResourceManager& resources = app->getResourceManager();
 
 	scene.setAmbientColor(Vec4(0.1, 0.05, 0.05, 0.0) * 3);
 
@@ -208,12 +209,14 @@ void init()
 		0.7));*/
 #endif
 
-#if 0
+#if 1
 	{
-		String str;
-		File file(ANKI_R("maps/sponza/scene.lua"), File::OpenFlag::READ);
+		String str(app->getAllocator());
+		File file(
+			resources.fixResourceFilename("maps/sponza/scene.lua").toCString(), 
+			File::OpenFlag::READ);
 		file.readAllText(str);
-		ScriptManagerSingleton::get().evalString(str.c_str());
+		app->getScriptManager().evalString(str.toCString());
 	}
 #endif
 
@@ -296,7 +299,7 @@ void execStdinScpripts()
 #endif
 
 //==============================================================================
-void mainLoopExtra(App& app, void*)
+I32 mainLoopExtra(App& app, void*)
 {
 	F32 dist = 0.1;
 	F32 ang = toRad(1.5);
@@ -307,6 +310,11 @@ void mainLoopExtra(App& app, void*)
 	Input& in = app.getInput();
 	MainRenderer& renderer = app.getMainRenderer();
 
+	if(in.getKey(KeyCode::ESCAPE))
+	{
+		return 1;
+	}
+
 	// move the camera
 	static MoveComponent* mover = 
 		&scene.getActiveCamera().getComponent<MoveComponent>();
@@ -443,6 +451,8 @@ end)");
 	}
 
 	//execStdinScpripts();
+
+	return 0;
 }
 
 //==============================================================================

+ 14 - 7
tools/scene/Main.cpp

@@ -189,14 +189,15 @@ static void writeNodeTransform(const Exporter& exporter, std::ofstream& file,
 	pos[1] = m[1][3];
 	pos[2] = m[2][3];
 
-	file << "pos = Vec3.new()\n";
+	file << "pos = Vec4.new()\n";
 	file << "pos:setX(" << pos[0] << ")\n";
 	file << "pos:setY(" << pos[1] << ")\n";
 	file << "pos:setZ(" << pos[2] << ")\n";
+	file << "pos:setW(0.0)\n";
 	file << node 
 		<< ":getSceneNodeBase():getMoveComponent():setLocalOrigin(pos)\n";
 
-	file << "rot = Mat3.new()\n";
+	file << "rot = Mat3x4.new()\n";
 	for(unsigned j = 0; j < 3; j++)
 	{
 		for(unsigned i = 0; i < 3; i++)
@@ -204,6 +205,11 @@ static void writeNodeTransform(const Exporter& exporter, std::ofstream& file,
 			file << "rot:setAt(" << j << ", " << i << ", " << m[j][i] << ")\n";
 		}
 	}
+
+	file << "rot:setAt(0, 3, 0.0)\n"
+		<< "rot:setAt(1, 3, 0.0)\n"
+		<< "rot:setAt(2, 3, 0.0)\n";
+
 	file << node 
 		<< ":getSceneNodeBase():getMoveComponent():setLocalRotation(rot)\n";
 }
@@ -219,7 +225,7 @@ static void exportScene(Exporter& exporter)
 	std::ofstream file;
 	file.open(exporter.outDir + "scene.lua");
 
-	file << "scene = SceneGraphSingleton.get()\n";
+	file << "scene = Anki.getSceneGraph()\n";
 
 	//
 	// Get all the data
@@ -248,15 +254,16 @@ static void exportScene(Exporter& exporter)
 		std::string name = getModelName(exporter, model);
 
 		// Write the main node
-		file << "\nnode = scene:newModelNode(\"" << name << "\", \"" 
-			<< exporter.rpath << name << ".ankimdl" << "\")\n"; 
+		file << "\nnode = scene:newModelNode(CString.new(\"" 
+			<< name << "\"), CString.new(\"" 
+			<< exporter.rpath << name << ".ankimdl" << "\"))\n"; 
 		writeNodeTransform(exporter, file, "node", node.transforms[0]);
 
 		// Write instance nodes
 		for(unsigned j = 1; j < node.transforms.size(); j++)
 		{
-			file << "inst = scene:newInstanceNode(\"" 
-				<< name << "_inst" << (j - 1) << "\")\n"
+			file << "inst = scene:newInstanceNode(CString.new(\"" 
+				<< name << "_inst" << (j - 1) << "\"))\n"
 				<< "node:getSceneNodeBase():addChild("
 				<< "inst:getSceneNodeBase())\n";