예제 #1
0
파일: sample.cpp 프로젝트: Kai-Fu/KRender
int main(int argc, char* argv[])
{
	KSC_Initialize();

	FILE* f = NULL;
	fopen_s(&f, "ray_tri_test.ls", "r");
	if (f == NULL)
		return -1;
	fseek(f, 0, SEEK_END);
	long len = ftell(f);
	fseek(f, 0, SEEK_SET);

	char* content = new char[len + 1];
	char* line = content;
	size_t totalLen = 0;

	while (fgets(line, len, f) != NULL) {
		size_t lineLen = strlen(line);
		line += lineLen;
		totalLen += lineLen;
	}

	if (totalLen == 0)
		return -1;
	else {
		content[totalLen] = '\0';

		ModuleHandle hModule = KSC_Compile(content);
		if (!hModule) {
			printf(KSC_GetLastErrorMsg());
			return -1;
		}


		typedef void (*TestFunction)(void* ref, float* v);
		FunctionHandle hFunc = KSC_GetFunctionHandleByName("VectorAssign_N", hModule);
		KSC_TypeInfo typeInfo = KSC_GetFunctionArgumentType(hFunc, 0);
		TestFunction pFunc = (TestFunction)KSC_GetFunctionPtr(hFunc);
		float* pData = (float*)KSC_AllocMemForType(typeInfo, 10);
		float v_array[3] = {1.2345f, 3.456f, 5.678f};
		pFunc(pData, v_array);
		pData[1] = 0;
		
	}
	

	return 0;
}
예제 #2
0
bool KSC_SurfaceShader::HandleModule(ModuleHandle kscModule)
{
	// Look for the function "ShadeTransmission(SurfaceContext% ctx, KColor& outClr)"
	FunctionHandle shadeFunc = KSC_GetFunctionHandleByName("ShadeTransmission", kscModule);
	if (shadeFunc != NULL && KSC_GetFunctionArgumentCount(shadeFunc) == 3) {
		KSC_TypeInfo argUniform = KSC_GetFunctionArgumentType(shadeFunc, 0);
		KSC_TypeInfo arg0TypeInfo = KSC_GetFunctionArgumentType(shadeFunc, 1);
		KSC_TypeInfo arg1TypeInfo = KSC_GetFunctionArgumentType(shadeFunc, 2);
		if (0 == strcmp(argUniform.typeString, mUnifomArgType.typeString) &&
			arg0TypeInfo.isRef && arg0TypeInfo.isKSCLayout && arg0TypeInfo.hStruct != NULL &&
			arg1TypeInfo.isRef && arg1TypeInfo.type == SC::kFloat3) {
			mpTransmissionFuncPtr = KSC_GetFunctionPtr(shadeFunc);
			if (mpTransmissionFuncPtr)
				mHasTransmission = true;
		}
	}
	return true;
}
예제 #3
0
int main(int argc, char* argv[])
{
	KSC_Initialize();

	FILE* f = NULL;
	fopen_s(&f, "for_expression.ls", "r");
	if (f == NULL)
		return -1;
	fseek(f, 0, SEEK_END);
	long len = ftell(f);
	fseek(f, 0, SEEK_SET);

	char* content = new char[len + 1];
	char* line = content;
	size_t totalLen = 0;

	while (fgets(line, len, f) != NULL) {
		size_t lineLen = strlen(line);
		line += lineLen;
		totalLen += lineLen;
	}

	if (totalLen == 0)
		return -1;
	else {
		content[totalLen] = '\0';

		ModuleHandle hModule = KSC_Compile(content);
		if (!hModule) {
			printf(KSC_GetLastErrorMsg());
			return -1;
		}

		typedef int (*PFN_factorial)(int arg);
		FunctionHandle hFunc = KSC_GetFunctionHandleByName("factorial", hModule);
		PFN_factorial factorial = (PFN_factorial)KSC_GetFunctionPtr(hFunc);
		int result = factorial(9);
		printf("result is %d\n", result);
	}
	

	return 0;
}
예제 #4
0
int main(int argc, char* argv[])
{
	KSC_Initialize();

	FILE* f = NULL;
	fopen_s(&f, "struct_mem_layout.ls", "r");
	if (f == NULL)
		return -1;
	fseek(f, 0, SEEK_END);
	long len = ftell(f);
	fseek(f, 0, SEEK_SET);

	char* content = new char[len + 1];
	char* line = content;
	size_t totalLen = 0;

	while (fgets(line, len, f) != NULL) {
		size_t lineLen = strlen(line);
		line += lineLen;
		totalLen += lineLen;
	}

	if (totalLen == 0)
		return -1;
	else {
		content[totalLen] = '\0';

		ModuleHandle hModule = KSC_Compile(content);
		if (!hModule) {
			printf(KSC_GetLastErrorMsg());
			return -1;
		}

		typedef int (*PFN_RW_Structure)(TestStructure* arg, void* arg1);
		
		TestStructure tempStruct;
		FunctionHandle hFunc = KSC_GetFunctionHandleByName("PFN_RW_Structure", hModule);
		assert(KSC_GetFunctionArgumentCount(hFunc) == 2);

		// The the type info of the second argument
		KSC_TypeInfo typeInfo = KSC_GetFunctionArgumentType(hFunc, 1);
		printf("Argument(1) type stirng is %s, size is %d.\n", typeInfo.typeString, KSC_GetStructSize(typeInfo.hStruct));
		assert(KSC_GetStructHandleByName(typeInfo.typeString, hModule));

		void* tempStruct1 = KSC_AllocMemForType(typeInfo, 1);
		float* pVar0 = (float*)KSC_GetStructMemberPtr(typeInfo.hStruct, tempStruct1, "var0");
		int* pVar1 = (int*)KSC_GetStructMemberPtr(typeInfo.hStruct, tempStruct1, "var1");
		pVar0[0] = 7.0f;
		pVar0[1] = 8.0f;
		pVar0[2] = 9.0f;
		pVar0[3] = 10.0f;
		pVar1[0] = 6;
		pVar1[1] = 7;

		PFN_RW_Structure RW_Structure = (PFN_RW_Structure)KSC_GetFunctionPtr(hFunc);
		int ret = RW_Structure(&tempStruct, tempStruct1);
		assert(ret == 123);

		{
			typedef int (*PFN_DotProductFloat8)(void* arg, void* arg1, void* outArg);
			hFunc = KSC_GetFunctionHandleByName("DotProductFloat8", hModule);
			typeInfo = KSC_GetFunctionArgumentType(hFunc, 0);
			float* arg0 = (float*)KSC_AllocMemForType(typeInfo, 1);

			typeInfo = KSC_GetFunctionArgumentType(hFunc, 1);
			float* arg1 = (float*)KSC_AllocMemForType(typeInfo, 1);

			typeInfo = KSC_GetFunctionArgumentType(hFunc, 2);
			float* arg2 = (float*)KSC_AllocMemForType(typeInfo, 1);
			for (int i = 0; i < 8; ++i) {
				arg0[i] = 1.23f;
				arg1[i] = 2.23f;
			}

			PFN_DotProductFloat8 DotProductFloat8 = (PFN_DotProductFloat8)KSC_GetFunctionPtr(hFunc);
			DotProductFloat8(arg0, arg1, arg2);
			printf("Test finished.\n");
		}
	}
	

	return 0;
}
예제 #5
0
bool KSC_Shader::LoadTemplate(const char* templateFile)
{
	printf("Loading shader template %s...\n", templateFile);

	mpUniformData = NULL;
	FILE* f = NULL;
	std::string templatePath = "./asset/";
	templatePath += templateFile;
	fopen_s(&f, templatePath.c_str(), "r");
	if (f == NULL)
		return false;
	char line[256];
	// The first line must be "Shader = xxx", where xxx should be the file that contains KSC code.
	fgets(line, 256, f);
	char kscFile[256];
	{
		char tempBuf0[256];
		if (1 != sscanf(line, "%*[^=]=%[^=]", tempBuf0)) {
			printf("\tIncorrect line for shader assignment: \"%s\" \n", line);
			return false;
		}
		sscanf(tempBuf0, "%s", kscFile);
	}

	FunctionHandle shadeFunc = NULL;
	ModuleHandle shaderModule = NULL;
	if (sLoadedShadeFunctions.find(kscFile) == sLoadedShadeFunctions.end()) {

		std::string shaderPath = "./asset/shader/";
		std::string shaderContent;
		shaderPath += kscFile;
		{
			FILE* fShadeFile = NULL;
			// Load the content of the shader file
			fopen_s(&fShadeFile, shaderPath.c_str(), "r");
			if (fShadeFile == NULL)
				return false;
			fseek(fShadeFile, 0, SEEK_END);
			long len = ftell(fShadeFile);
			fseek(fShadeFile, 0, SEEK_SET);

			shaderContent.resize(len + 1);
			char* line = &shaderContent[0];

			while (fgets(line, len, fShadeFile) != NULL) {
				size_t lineLen = strlen(line);
				line += lineLen;
			}
			fclose(fShadeFile);
		}

		shaderModule = KSC_Compile(shaderContent.c_str());
		if (shaderModule == NULL) {
			printf("Shader compilation failed with following error:\n");
			printf(KSC_GetLastErrorMsg());
			return false;
		}

		shadeFunc = KSC_GetFunctionHandleByName("Shade", shaderModule);
		if (shadeFunc == NULL) {
			printf("Shade function does not exist in KSC code.\n");
			return false;
		}
		sLoadedShadeFunctions[kscFile] = shadeFunc;
	}
	else {
		shadeFunc = sLoadedShadeFunctions[kscFile];
	}

	mpFuncPtr = KSC_GetFunctionPtr(shadeFunc);
	if (mpFuncPtr == NULL) {
		printf("Shade function JIT failed.\n");
		return false;
	}

	// The Shade function should have three arguments:
	// arg0 - the reference to structure that contains uniform data
	// arg1 - shader input data
	// arg2 - shader output data
	if (KSC_GetFunctionArgumentCount(shadeFunc) != 3) {
		printf("Incorrect argument count for Shade function.\n");
		return false;
	}

	KSC_TypeInfo arg0TypeInfo = KSC_GetFunctionArgumentType(shadeFunc, 0);
	if (!arg0TypeInfo.isRef || !arg0TypeInfo.isKSCLayout || arg0TypeInfo.hStruct == NULL) {
		printf("Incorrect type for argument 0.\n");
		return false;
	}
	for (int i = 1; i < 3; ++i) {
		KSC_TypeInfo typeInfo = KSC_GetFunctionArgumentType(shadeFunc, i);
		if (!typeInfo.isRef) {
			printf("Incorrect type for argument %d.\n", i);
			return false;
		}
	}
	mUnifomArgType = arg0TypeInfo;

	
	if (!HandleModule(shaderModule)) {
		printf("Failed to handle the compiled KSC module.\n");
		return false;
	}

	// Perform the additional check
	if (!Validate(shadeFunc)) {
		printf("Shade function validation failed.\n");
		return false;
	}

	mShadeFunction = shadeFunc;

	mpUniformData = KSC_AllocMemForType(arg0TypeInfo, 0);

	while (!feof(f)) {
		// Parse each line and set the initial uniform values
		fgets(line, 256, f);
		char varStr[256];
		char valueStr[256];
		{
			char tempBuf0[256];
			char tempBuf1[256];
			if (2 != sscanf(line, "%[^=]=%[^=]", tempBuf0, tempBuf1)) {
				printf("\tIncorrect line for shader parameter initializer: \"%s\" \n", line);
				continue;
			}
			sscanf(tempBuf0, "%s", varStr);
			sscanf(tempBuf1 + strspn(tempBuf1, " \t"), "%[^ ]", valueStr);
		}

		if (InitializeUniform(varStr))
			continue;  // This uniform is initialized by the derived class

		KSC_TypeInfo memberType = KSC_GetStructMemberType(arg0TypeInfo.hStruct, varStr);
		if (memberType.hStruct != NULL) {
			printf("\tShader parameter initializer must be applied to built-in types.\n");
			continue;
		}


		if (memberType.type == SC::kExternType) {
			void* extData = CreateExternalData(memberType.typeString, valueStr);
			if (extData == NULL) 
				printf("\tExternal data(type %s, value %s) creation failed.\n", memberType.typeString, valueStr);
			else {
				KSC_SetStructMemberData(arg0TypeInfo.hStruct, mpUniformData, varStr, &extData, (int)sizeof(void*));
			}
		}
		else {
			SC::Int iData[4] = {0,0,0,0};
			SC::Float fData[4] = {0,0,0,0};
			SC::Boolean bData = 0;
			switch (memberType.type) {
			case SC::kBoolean:
				sscanf(valueStr, "<%d>", &bData);
				break;
			case SC::kInt:			
				sscanf(valueStr, "<%d>", &iData[0]);
				break;
			case SC::kInt2:
				sscanf(valueStr, "<%d,%d>", &iData[0], &iData[1]);
				break;
			case SC::kInt3:
				sscanf(valueStr, "<%d,%d,%d>", &iData[0], &iData[1], &iData[2]);
				break;
			case SC::kInt4:
				sscanf(valueStr, "<%d,%d,%d,%d>", &iData[0], &iData[1], &iData[2], &iData[3]);
				break;
			case SC::kFloat:
				sscanf(valueStr, "<%f>", &fData[0]);
				break;
			case SC::kFloat2:
				sscanf(valueStr, "<%f,%f>", &fData[0], &fData[1]);
				break;
			case SC::kFloat3:
				sscanf(valueStr, "<%f,%f,%f>", &fData[0], &fData[1], &fData[2]);
				break;
			case SC::kFloat4:
				sscanf(valueStr, "<%f,%f,%f,%f>", &fData[0], &fData[1], &fData[2], &fData[3]);
				break;
			}

			switch (memberType.type) {
			case SC::kBoolean:
				KSC_SetStructMemberData(arg0TypeInfo.hStruct, mpUniformData, varStr, &bData, (int)sizeof(int));
				break;
			case SC::kInt:
			case SC::kInt2:
			case SC::kInt3:
			case SC::kInt4:
				KSC_SetStructMemberData(arg0TypeInfo.hStruct, mpUniformData, varStr, iData, (int)sizeof(iData));
				break;
			case SC::kFloat:
			case SC::kFloat2:
			case SC::kFloat3:
			case SC::kFloat4:
				KSC_SetStructMemberData(arg0TypeInfo.hStruct, mpUniformData, varStr, fData, (int)sizeof(fData));
				break;
			}
		}
	}

	fclose(f);
	return true;
}