Browse Source

Fix compilation on other platforms

Panagiotis Christopoulos Charitos 3 years ago
parent
commit
fc4687e840

+ 11 - 7
AnKi/Core/NativeWindowAndroid.cpp

@@ -9,15 +9,15 @@ namespace anki {
 
 Error NativeWindow::newInstance(const NativeWindowInitInfo& initInfo, NativeWindow*& nativeWindow)
 {
-	HeapAllocator<U8> alloc(initInfo.m_allocCallback, initInfo.m_allocCallbackUserData, "NativeWindow");
-	NativeWindowAndroid* andwin = alloc.newInstance<NativeWindowAndroid>();
-
-	andwin->m_alloc = alloc;
+	NativeWindowAndroid* andwin = static_cast<NativeWindowAndroid*>(initInfo.m_allocCallback(
+		initInfo.m_allocCallbackUserData, nullptr, sizeof(NativeWindowAndroid), alignof(NativeWindowAndroid)));
+	callConstructor(*andwin);
 
 	const Error err = andwin->init(initInfo);
 	if(err)
 	{
-		alloc.deleteInstance(andwin);
+		callDestructor(*andwin);
+		initInfo.m_allocCallback(initInfo.m_allocCallbackUserData, andwin, 0, 0);
 		nativeWindow = nullptr;
 		return err;
 	}
@@ -33,8 +33,10 @@ void NativeWindow::deleteInstance(NativeWindow* window)
 	if(window)
 	{
 		NativeWindowAndroid* self = static_cast<NativeWindowAndroid*>(window);
-		HeapAllocator<U8> alloc = self->m_alloc;
-		alloc.deleteInstance(self);
+		AllocAlignedCallback callback = self->m_pool.getAllocationCallback();
+		void* userData = self->m_pool.getAllocationCallbackUserData();
+		callDestructor(*self);
+		callback(userData, self, 0, 0);
 	}
 }
 
@@ -71,6 +73,8 @@ Error NativeWindowAndroid::init([[maybe_unused]] const NativeWindowInitInfo& ini
 {
 	ANKI_CORE_LOGI("Initializing Android window");
 
+	m_pool.init(init.m_allocCallback, init.m_allocCallbackUserData);
+
 	// Loop until the window is ready
 	while(g_androidApp->window == nullptr)
 	{

+ 10 - 6
AnKi/Core/NativeWindowHeadless.cpp

@@ -9,9 +9,11 @@ namespace anki {
 
 Error NativeWindow::newInstance(const NativeWindowInitInfo& initInfo, NativeWindow*& nativeWindow)
 {
-	HeapAllocator<U8> alloc(initInfo.m_allocCallback, initInfo.m_allocCallbackUserData, "NativeWindow");
-	NativeWindowHeadless* hwin = alloc.newInstance<NativeWindowHeadless>();
-	hwin->m_alloc = alloc;
+	NativeWindowHeadless* hwin = static_cast<NativeWindowHeadless*>(initInfo.m_allocCallback(
+		initInfo.m_allocCallbackUserData, nullptr, sizeof(NativeWindowHeadless), alignof(NativeWindowHeadless)));
+	callConstructor(*hwin);
+
+	hwin->m_pool.init(initInfo.m_allocCallback, initInfo.m_allocCallbackUserData);
 	hwin->m_width = initInfo.m_width;
 	hwin->m_height = initInfo.m_height;
 
@@ -24,12 +26,14 @@ void NativeWindow::deleteInstance(NativeWindow* window)
 	if(window)
 	{
 		NativeWindowHeadless* self = static_cast<NativeWindowHeadless*>(window);
-		HeapAllocator<U8> alloc = self->m_alloc;
-		alloc.deleteInstance(self);
+		AllocAlignedCallback callback = self->m_pool.getAllocationCallback();
+		void* userData = self->m_pool.getAllocationCallbackUserData();
+		callDestructor(*self);
+		callback(userData, self, 0, 0);
 	}
 }
 
-void NativeWindow::setWindowTitle(CString title)
+void NativeWindow::setWindowTitle([[maybe_unused]] CString title)
 {
 	// Nothing
 }

+ 2 - 2
AnKi/Gr/Vulkan/GrManagerImpl.cpp

@@ -99,7 +99,7 @@ GrManagerImpl::~GrManagerImpl()
 	}
 
 #if ANKI_PLATFORM_MOBILE
-	m_alloc.deleteInstance(m_globalCreatePipelineMtx);
+	anki::deleteInstance(m_pool, m_globalCreatePipelineMtx);
 #endif
 }
 
@@ -538,7 +538,7 @@ Error GrManagerImpl::initInstance()
 	{
 		// Calling vkCreateGraphicsPipeline from multiple threads crashes qualcomm's compiler
 		ANKI_VK_LOGI("Enabling workaround for vkCreateGraphicsPipeline crashing when called from multiple threads");
-		m_globalCreatePipelineMtx = m_alloc.newInstance<Mutex>();
+		m_globalCreatePipelineMtx = anki::newInstance<Mutex>(m_pool);
 	}
 #endif
 

+ 2 - 2
AnKi/Importer/GltfImporterMaterial.cpp

@@ -10,7 +10,7 @@
 
 namespace anki {
 
-inline constexpr Char* kMaterialTemplate = R"(<!-- This file is auto generated by ImporterMaterial.cpp -->
+inline constexpr const Char* kMaterialTemplate = R"(<!-- This file is auto generated by ImporterMaterial.cpp -->
 <material shadows="1">
 	<shaderPrograms>
 		<shaderProgram name="GBufferGeneric">
@@ -42,7 +42,7 @@ inline constexpr Char* kMaterialTemplate = R"(<!-- This file is auto generated b
 </material>
 )";
 
-inline constexpr Char* kRtMaterialTemplate = R"(
+inline constexpr const Char* kRtMaterialTemplate = R"(
 		<shaderProgram name="RtShadowsHit">
 			<mutation>
 				<mutator name="ALPHA_TEXTURE" value="0"/>

+ 11 - 11
AnKi/Input/InputAndroid.cpp

@@ -14,19 +14,18 @@ Error Input::newInstance(AllocAlignedCallback allocCallback, void* allocCallback
 {
 	ANKI_ASSERT(allocCallback && nativeWindow);
 
-	HeapAllocator<U8> alloc(allocCallback, allocCallbackUserData, "Input");
-	InputAndroid* ainput =
-		static_cast<InputAndroid*>(alloc.getMemoryPool().allocate(sizeof(InputAndroid), alignof(InputAndroid)));
-	::new(ainput) InputAndroid();
+	InputAndroid* ainput = static_cast<InputAndroid*>(
+		allocCallback(allocCallbackUserData, nullptr, sizeof(InputAndroid), alignof(InputAndroid)));
+	callConstructor(*ainput);
 
-	ainput->m_alloc = alloc;
+	ainput->m_pool.init(allocCallback, allocCallbackUserData);
 	ainput->m_nativeWindow = nativeWindow;
 
 	const Error err = ainput->init();
 	if(err)
 	{
-		ainput->~InputAndroid();
-		alloc.getMemoryPool().free(ainput);
+		callDestructor(*ainput);
+		allocCallback(allocCallbackUserData, ainput, 0, 0);
 		input = nullptr;
 		return err;
 	}
@@ -42,9 +41,10 @@ void Input::deleteInstance(Input* input)
 	if(input)
 	{
 		InputAndroid* self = static_cast<InputAndroid*>(input);
-		HeapAllocator<U8> alloc = self->m_alloc;
-		self->~InputAndroid();
-		alloc.getMemoryPool().free(self);
+		AllocAlignedCallback callback = self->m_pool.getAllocationCallback();
+		void* userData = self->m_pool.getAllocationCallbackUserData();
+		callDestructor(*self);
+		callback(userData, self, 0, 0);
 	}
 }
 
@@ -114,7 +114,7 @@ void InputAndroid::handleAndroidEvents(android_app* app, int32_t cmd)
 	{
 	case APP_CMD_TERM_WINDOW:
 	case APP_CMD_LOST_FOCUS:
-		addEvent(InputEvent::WINDOW_CLOSED);
+		addEvent(InputEvent::kWindowClosed);
 		break;
 	}
 }

+ 8 - 6
AnKi/Input/InputDummy.cpp

@@ -13,11 +13,11 @@ Error Input::newInstance(AllocAlignedCallback allocCallback, void* allocCallback
 {
 	ANKI_ASSERT(allocCallback && nativeWindow);
 
-	HeapAllocator<U8> alloc(allocCallback, allocCallbackUserData, "Input");
-	Input* ainput = alloc.newInstance<Input>();
+	Input* ainput = static_cast<Input*>(allocCallback(allocCallbackUserData, nullptr, sizeof(Input), alignof(Input)));
+	callConstructor(*ainput);
 
+	ainput->m_pool.init(allocCallback, allocCallbackUserData);
 	ainput->m_nativeWindow = nativeWindow;
-	ainput->m_alloc = alloc;
 
 	input = ainput;
 	return Error::kNone;
@@ -27,8 +27,10 @@ void Input::deleteInstance(Input* input)
 {
 	if(input)
 	{
-		HeapAllocator<U8> alloc = input->m_alloc;
-		alloc.deleteInstance(input);
+		AllocAlignedCallback callback = input->m_pool.getAllocationCallback();
+		void* userData = input->m_pool.getAllocationCallbackUserData();
+		callDestructor(*input);
+		callback(userData, input, 0, 0);
 	}
 }
 
@@ -49,7 +51,7 @@ void Input::moveCursor(const Vec2& posNdc)
 	m_mousePosWin.y() = U32(F32(m_nativeWindow->getHeight()) * (-posNdc.y() * 0.5f + 0.5f));
 }
 
-void Input::hideCursor(Bool hide)
+void Input::hideCursor([[maybe_unused]] Bool hide)
 {
 	// Nothing
 }

+ 6 - 6
AnKi/Util/String.h

@@ -1003,16 +1003,16 @@ using StringRaii = BaseStringRaii<>;
 	ANKI_STRING_COMPARE_OPERATOR(TypeA, TypeB, >, __VA_ARGS__) \
 	ANKI_STRING_COMPARE_OPERATOR(TypeA, TypeB, >=, __VA_ARGS__)
 
-ANKI_STRING_COMPARE_OPS(const Char*, CString)
-ANKI_STRING_COMPARE_OPS(const Char*, const String&)
+ANKI_STRING_COMPARE_OPS(const Char*, CString, )
+ANKI_STRING_COMPARE_OPS(const Char*, const String&, )
 ANKI_STRING_COMPARE_OPS(const Char*, const BaseStringRaii<TMemPool>&, template<typename TMemPool>)
 
-ANKI_STRING_COMPARE_OPS(CString, const Char*)
-ANKI_STRING_COMPARE_OPS(CString, const String&)
+ANKI_STRING_COMPARE_OPS(CString, const Char*, )
+ANKI_STRING_COMPARE_OPS(CString, const String&, )
 ANKI_STRING_COMPARE_OPS(CString, const BaseStringRaii<TMemPool>&, template<typename TMemPool>)
 
-ANKI_STRING_COMPARE_OPS(const String&, const Char*)
-ANKI_STRING_COMPARE_OPS(const String&, CString)
+ANKI_STRING_COMPARE_OPS(const String&, const Char*, )
+ANKI_STRING_COMPARE_OPS(const String&, CString, )
 ANKI_STRING_COMPARE_OPS(const String&, const BaseStringRaii<TMemPool>&, template<typename TMemPool>)
 
 ANKI_STRING_COMPARE_OPS(const BaseStringRaii<TMemPool>&, const Char*, template<typename TMemPool>)

+ 4 - 4
AnKi/Util/System.cpp

@@ -105,7 +105,7 @@ static Error getAndroidApkName(StringRaii& name)
 {
 	const pid_t pid = getpid();
 
-	StringRaii path(name.getAllocator());
+	StringRaii path(&name.getMemoryPool());
 	path.sprintf("/proc/%d/cmdline", pid);
 
 	const int fd = open(path.cstr(), O_RDONLY);
@@ -153,8 +153,8 @@ void* getAndroidCommandLineArguments(int& argc, char**& argv)
 	jstring jsParam1 = static_cast<jstring>(env->CallObjectMethod(intent, gseid, env->NewStringUTF("cmd")));
 
 	// Parse the command line args
-	HeapAllocator<U8> alloc(allocAligned, nullptr, "getAndroidCommandLineArguments temp");
-	StringListRaii args(alloc);
+	HeapMemoryPool pool(allocAligned, nullptr, "getAndroidCommandLineArguments temp");
+	StringListRaii args(&pool);
 
 	if(jsParam1)
 	{
@@ -164,7 +164,7 @@ void* getAndroidCommandLineArguments(int& argc, char**& argv)
 	}
 
 	// Add the apk name
-	StringRaii apkName(alloc);
+	StringRaii apkName(&pool);
 	if(!getAndroidApkName(apkName))
 	{
 		args.pushFront(apkName);

+ 34 - 19
Docs/CodeStyle.md

@@ -16,7 +16,7 @@ Table of contents
 Comments in C++ & GLSL
 ======================
 
-All comments are C++ like:
+All comments are C++ like (double slash):
 
 	// Some comment
 
@@ -43,13 +43,13 @@ All **types** are PascalCase and that includes classes, structs, typedefs and en
 	class MyClass {...};
 	using MyType = int;
 
-All **constant expressions** are SCREAMING_SNAKE_CASE and that includes enumerants.
+All **constexpr expressions** start with a `k` and that includes enumerants.
 
-	constexpr int MY_CONSTANT = ...;
+	constexpr int kMyConstant = ...;
 
 	enum MyEnum
 	{
-		ENUMERANT_A
+		kEnumerantA
 	};
 
 All **macros and defines** should be SCREAMING_SNAKE_CASE and they should have an `ANKI_` prefix.
@@ -58,9 +58,9 @@ All **macros and defines** should be SCREAMING_SNAKE_CASE and they should have a
 
 	#define ANKI_MAX_SOMETHING ...
 
-All **template arguments** should start with `T`.
+All **template typedefs** should start with `T`. No need for constant template arguments.
 
-	template<typename TSomething, typename TOther, U32 T_SOME_CONST>
+	template<typename TSomething, typename TOther, U32 kSomeConst>
 
 All **function and method names** should form a sentence with at least one verb.
 
@@ -68,8 +68,9 @@ All **function and method names** should form a sentence with at least one verb.
 	getSomething();
 	calculateSomething(...);
 
-**No hungarian notations** with 2 exceptions.
+**Using some hungarian notations**.
 
+- Constants start with `k` as mentioned above.
 - All member variables have the `m_` prefix. That applies to classes and structs.
 - All global variables have the `g_` prefix.
 
@@ -128,7 +129,7 @@ C++ rules
 
 **Always use `constexpr`** when applicable. Never use defines for constants.
 
-	constexpr uint SOME_CONST = ...; // YES!!
+	constexpr uint kSomeConst = ...; // YES!!
 	#define SOME_CONST (...)         // NO
 
 **Never use `struct`** and always use `class` instead. Since it's difficult to come up with rules on when to use struct over class always use class and be done with it.
@@ -140,7 +141,10 @@ C++ rules
 		int m_y;
 	};
 
-**Avoid using `auto`**. It's only allowed in some templates and in iterators. auto makes reading code difficult.
+**Avoid using `auto`**. It's only allowed in some templates and in iterators. auto makes reviewing code difficult.
+
+	auto i = 10;               // NO
+	auto it = list.getBegin(); // ... FINE
 
 **Includes should always have the full file path**. This avoids issues with files of similar name. Non-AnKi includes follow AnKi includes.
 
@@ -149,7 +153,7 @@ C++ rules
 	#include <AnKi/Util/Allocator.h>
 	#include <cstdio>
 
-**Never use public nested classes**. Only private nested classes are allowed. Nested classes cannot be forward declared.
+**Never use public nested classes**. Only private nested classes are allowed. Nested classes cannot be forward declared. Nexted typedefs are fine.
 
 	class MyClass
 	{
@@ -157,11 +161,14 @@ C++ rules
 		// NO!!!!!!!!!!!
 		class Nested {...};
 
+		// OK
+		using SomeTypedef = U32;
+
 	private:
-		// It's fine
+		// OK
 		class Nested {...};
 
-		// Also fine
+		// Also OK
 		class
 		{
 			...
@@ -193,6 +200,7 @@ AnKi uses **const correctness** throughout and that's mandatory. Always use `con
 Always **use AnKi's fudmental types** (U32, I32, F32, Bool etc etc). For integers and if you don't care about the size of the integer you can use the type `U` for unsinged or `I` for signed. Both of them are at least 32bit.
 
 	for(U32 i = 0; i < 10; ++i) {...}
+	U32 i = ...;
 
 If you have to overload the operator Bool always use **`explicit operator Bool` operator** overloading. Omitting the `explicit` may lead to bugs.
 
@@ -211,13 +219,14 @@ The use of **references and pointers** is govern by some rules. Always use refer
 		ref += ...;
 	}
 
-Always **use `nullptr`**.
+Always **use `nullptr`** and never NULL.
 
 	void* ptr = nullptr;
 
 **Never use C style arrays**. Use the `Array<>` template instead.
 
-	Array<MyType, 10> myArray;
+	Array<MyType, 10> myArray; // YES
+	MyType myArray[10];        // NO
 
 **Don't use STL**. AnKi has a replacement for most STL templates. There are some exceptions though.
 
@@ -227,6 +236,15 @@ Always **use `nullptr`**.
 
 Always try to **comment parts of the source code**. Self documenting code is never enough.
 
+	void someFunction(...)
+	{
+		// Now do X
+		...
+
+		// Now do Y
+		...
+	}
+
 Always **use `override` or `final`** on virtual methods and try to use `final` on classes when applicable.
 
 	class Foo final
@@ -248,11 +266,8 @@ Always **use `override` or `final`** on virtual methods and try to use `final` o
 
 Always place the **condition of a conditional ternary operator** inside parenthesis.
 
-	// NO!!!!!!!!!!!
-	a = b ? 1 : 0;
-
-	// YES!!!!!!!!!!!!!!
-	a = (b) ? 1 : 0;
+	a = b ? 1 : 0;   // NO
+	a = (b) ? 1 : 0; // YES
 
 GLSL rules
 ==========

+ 1 - 1
Tools/Shader/ShaderProgramCompilerMain.cpp

@@ -7,7 +7,7 @@
 #include <AnKi/Util.h>
 using namespace anki;
 
-static constexpr char* kUsage = R"(Compile an AnKi shader program
+static constexpr const char* kUsage = R"(Compile an AnKi shader program
 Usage: %s [options] input_shader_program_file
 Options:
 -o <name of output>  : The name of the output binary