From 62a1427154131f2387675957c1d2027061f354f0 Mon Sep 17 00:00:00 2001 From: Andrew Reidmeyer Date: Tue, 13 Jun 2017 20:38:29 -0600 Subject: NvFlow 1.0.1 --- demo/DemoAppCodeGen/DemoAppCodeGen.vcxproj | 280 +++++++++++++++++ demo/DemoAppCodeGen/DemoAppCodeGen.vcxproj.filters | 33 ++ demo/DemoAppCodeGen/demoAppCodeGen.cpp | 292 ++++++++++++++++++ demo/DemoAppCodeGen/functionDefinitionExtract.h | 230 ++++++++++++++ demo/DemoAppCodeGen/loaderCodeGen.h | 335 +++++++++++++++++++++ 5 files changed, 1170 insertions(+) create mode 100644 demo/DemoAppCodeGen/DemoAppCodeGen.vcxproj create mode 100644 demo/DemoAppCodeGen/DemoAppCodeGen.vcxproj.filters create mode 100644 demo/DemoAppCodeGen/demoAppCodeGen.cpp create mode 100644 demo/DemoAppCodeGen/functionDefinitionExtract.h create mode 100644 demo/DemoAppCodeGen/loaderCodeGen.h (limited to 'demo/DemoAppCodeGen') diff --git a/demo/DemoAppCodeGen/DemoAppCodeGen.vcxproj b/demo/DemoAppCodeGen/DemoAppCodeGen.vcxproj new file mode 100644 index 0000000..42bb3ae --- /dev/null +++ b/demo/DemoAppCodeGen/DemoAppCodeGen.vcxproj @@ -0,0 +1,280 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + win32 + + + win64 + + + {15a2730a-1be6-4a99-be96-feb4b8944ac2} + Win32Proj + DemoAppCodeGen + 10.0.14393.0 + + + + Application + true + v140 + Unicode + Static + + + Application + false + v140 + true + Unicode + Static + + + Application + true + v140 + Unicode + Static + + + Application + false + v140 + true + Unicode + Static + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\include;$(SolutionDir)\NvFlow;$(SolutionDir)\NvFlowContext;$(SolutionDir)external\SDL2\include;$(VC_IncludePath);$(WindowsSDK_IncludePath) + $(SolutionDir)lib\win32;$(SolutionDir)external\SDL2\lib\x86;$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(NETFXKitsDir)Lib\um\x86 + PostBuildEvent + $(SolutionDir)bin\$(PlatformName)\ + interm\$(Configuration)_$(PlatformName)\ + + + true + $(SolutionDir)bin\$(PlatformName)\ + interm\$(Configuration)_$(PlatformName)\ + $(SolutionDir)\include;$(SolutionDir)\NvFlow;$(SolutionDir)\NvFlowContext;$(SolutionDir)external\SDL2\include;$(VC_IncludePath);$(WindowsSDK_IncludePath) + $(SolutionDir)lib\win64;$(SolutionDir)external\SDL2\lib\x64;$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(NETFXKitsDir)Lib\um\x64 + PostBuildEvent + + + false + $(SolutionDir)\include;$(SolutionDir)\NvFlow;$(SolutionDir)\NvFlowContext;$(SolutionDir)external\SDL2\include;$(VC_IncludePath);$(WindowsSDK_IncludePath) + $(SolutionDir)lib\win32;$(SolutionDir)external\SDL2\lib\x86;$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(NETFXKitsDir)Lib\um\x86 + PostBuildEvent + $(SolutionDir)bin\$(PlatformName)\ + interm\$(Configuration)_$(PlatformName)\ + + + false + $(SolutionDir)bin\$(PlatformName)\ + interm\$(Configuration)_$(PlatformName)\ + $(SolutionDir)\include;$(SolutionDir)\NvFlow;$(SolutionDir)\NvFlowContext;$(SolutionDir)external\SDL2\include;$(VC_IncludePath);$(WindowsSDK_IncludePath) + $(SolutionDir)lib\win64;$(SolutionDir)external\SDL2\lib\x64;$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(NETFXKitsDir)Lib\um\x64 + PostBuildEvent + + + + + + Level3 + Disabled + DLL_SUFFIX=$(Configuration)_$(PlatformName);WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + + + Windows + true + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + mainCRTStartup + + + %(filename) + 5.0 + $(ProjectDir)%(Filename).hlsl.h + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + DLL_SUFFIX=$(Configuration)_$(PlatformName);_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + + + Windows + true + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + mainCRTStartup + + + %(filename) + 5.0 + $(ProjectDir)%(Filename).hlsl.h + + + + + + + + + + + + + + + + + + Level3 + + + MaxSpeed + true + true + DLL_SUFFIX=$(Configuration)_$(PlatformName);WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + + + Windows + true + true + true + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + mainCRTStartup + + + %(filename) + 5.0 + $(ProjectDir)%(Filename).hlsl.h + + + + + + + + + + + + + + + + + + Level3 + + + MaxSpeed + true + true + DLL_SUFFIX=$(Configuration)_$(PlatformName);NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + + + Windows + true + true + true + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + mainCRTStartup + + + %(filename) + 5.0 + $(ProjectDir)%(Filename).hlsl.h + + + + + + + + + + + + + + + + + $(OutDir) + $(ProjectName)$(Configuration)_$(PlatformName) + + + $(OutDir) + $(ProjectName)$(Configuration)_$(PlatformName) + + + $(OutDir) + $(ProjectName)$(Configuration)_$(PlatformName) + + + $(OutDir) + $(ProjectName)$(Configuration)_$(PlatformName) + + + + + \ No newline at end of file diff --git a/demo/DemoAppCodeGen/DemoAppCodeGen.vcxproj.filters b/demo/DemoAppCodeGen/DemoAppCodeGen.vcxproj.filters new file mode 100644 index 0000000..c648f4f --- /dev/null +++ b/demo/DemoAppCodeGen/DemoAppCodeGen.vcxproj.filters @@ -0,0 +1,33 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + {12de0363-d75b-4f29-bd10-e8b9832dd449} + + + + + Source Files + + + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/demo/DemoAppCodeGen/demoAppCodeGen.cpp b/demo/DemoAppCodeGen/demoAppCodeGen.cpp new file mode 100644 index 0000000..103e19f --- /dev/null +++ b/demo/DemoAppCodeGen/demoAppCodeGen.cpp @@ -0,0 +1,292 @@ +/* +* Copyright (c) 2014-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include +#include +#include + +#include "loaderCodeGen.h" +#include "functionDefinitionExtract.h" + +LoaderType gLoaderType = eLoaderTypeDynamicLink; + +void genLoaderAppGraphCtx() +{ + const unsigned int numFunctions = 12u; + + const char* functionDefinitions[numFunctions] = { + "AppGraphCtx* AppGraphCtxCreate(int deviceID);", + "bool AppGraphCtxUpdateSize(AppGraphCtx* context, SDL_Window* window, bool fullscreen);", + "void AppGraphCtxReleaseRenderTarget(AppGraphCtx* context);", + "void AppGraphCtxRelease(AppGraphCtx* context);", + "void AppGraphCtxFrameStart(AppGraphCtx* context, AppGraphColor clearColor);", + "void AppGraphCtxFramePresent(AppGraphCtx* context, bool fullsync);", + "void AppGraphCtxWaitForFrames(AppGraphCtx* context, unsigned int maxFramesInFlight);", + "void AppGraphCtxProfileEnable(AppGraphCtx* context, bool enabled);", + "void AppGraphCtxProfileBegin(AppGraphCtx* context, const char* label);", + "void AppGraphCtxProfileEnd(AppGraphCtx* context, const char* label);", + "bool AppGraphCtxProfileGet(AppGraphCtx* context, const char** plabel, float* cpuTime, float* gpuTime, int index);", + "size_t AppGraphCtxDedicatedVideoMemory(AppGraphCtx* context);" + }; + + StrHeap strHeap = allocateStrHeap(functionDefinitions, numFunctions); + + Function functions[numFunctions]; + for (unsigned int functionIdx = 0u; functionIdx < numFunctions; functionIdx++) + { + functions[functionIdx] = genFunction(&strHeap, functionDefinitions[functionIdx]); + } + + GenerateCodeParams genCodeParams = {}; + genCodeParams.loaderType = gLoaderType; + genCodeParams.file = nullptr; + genCodeParams.functions = functions; + genCodeParams.numFunctions = numFunctions; + genCodeParams.filenameTmp = "appGraphCtxLoaderGenerated.tmp.h"; + genCodeParams.filenameDst = "appGraphCtxLoaderGenerated.h"; + genCodeParams.moduleNameUpperCase = "AppGraphCtx"; + genCodeParams.moduleNameLowerCase = "appGraphCtx"; + genCodeParams.instName = "gAppGraphCtxLoader"; + genCodeParams.apiMarker = "APP_GRAPH_CTX_API"; + + fopen_s(&genCodeParams.file, genCodeParams.filenameTmp, "w"); + + generateCode(&genCodeParams); + + fclose(genCodeParams.file); + + freeStrHeap(&strHeap); + + fileDiffAndWriteIfModified(&genCodeParams); +} + +void genLoaderComputeContext() +{ + const unsigned int numFunctions = 23u; + + const char* functionDefinitions[numFunctions] = { + "ComputeContext* ComputeContextCreate(ComputeContextDesc* desc);", + "void ComputeContextUpdate(ComputeContext* context, ComputeContextDesc* desc);", + "void ComputeContextRelease(ComputeContext* context);", + "ComputeShader* ComputeShaderCreate(ComputeContext* context, const ComputeShaderDesc* desc);", + "void ComputeShaderRelease(ComputeShader* shader);", + "ComputeConstantBuffer* ComputeConstantBufferCreate(ComputeContext* context, const ComputeConstantBufferDesc* desc);", + "void ComputeConstantBufferRelease(ComputeConstantBuffer* constantBuffer);", + "void* ComputeConstantBufferMap(ComputeContext* context, ComputeConstantBuffer* constantBuffer);", + "void ComputeConstantBufferUnmap(ComputeContext* context, ComputeConstantBuffer* constantBuffer);", + "ComputeResource* ComputeResourceCreate(ComputeContext* context, const ComputeResourceDesc* desc);", + "void ComputeResourceUpdate(ComputeContext* context, ComputeResource* resource, const ComputeResourceDesc* desc);", + "void ComputeResourceRelease(ComputeResource* resource);", + "ComputeResourceRW* ComputeResourceRWCreate(ComputeContext* context, const ComputeResourceRWDesc* desc);", + "void ComputeResourceRWUpdate(ComputeContext* context, ComputeResourceRW* resourceRW, const ComputeResourceRWDesc* desc);", + "void ComputeResourceRWRelease(ComputeResourceRW* resourceRW);", + "ComputeResource* ComputeResourceRWGetResource(ComputeResourceRW* resourceRW);", + "void ComputeContextDispatch(ComputeContext* context, const ComputeDispatchParams* params);", + "ComputeContext* ComputeContextNvFlowContextCreate(NvFlowContext* flowContext);", + "void ComputeContextNvFlowContextUpdate(ComputeContext* computeContext, NvFlowContext* flowContext);", + "ComputeResource* ComputeResourceNvFlowCreate(ComputeContext* context, NvFlowContext* flowContext, NvFlowResource* flowResource);", + "void ComputeResourceNvFlowUpdate(ComputeContext* context, ComputeResource* resource, NvFlowContext* flowContext, NvFlowResource* flowResource);", + "ComputeResourceRW* ComputeResourceRWNvFlowCreate(ComputeContext* context, NvFlowContext* flowContext, NvFlowResourceRW* flowResourceRW);", + "void ComputeResourceRWNvFlowUpdate(ComputeContext* context, ComputeResourceRW* resourceRW, NvFlowContext* flowContext, NvFlowResourceRW* flowResourceRW);" + }; + + StrHeap strHeap = allocateStrHeap(functionDefinitions, numFunctions); + + Function functions[numFunctions]; + for (unsigned int functionIdx = 0u; functionIdx < numFunctions; functionIdx++) + { + functions[functionIdx] = genFunction(&strHeap, functionDefinitions[functionIdx]); + } + + GenerateCodeParams genCodeParams = {}; + genCodeParams.loaderType = gLoaderType; + genCodeParams.file = nullptr; + genCodeParams.functions = functions; + genCodeParams.numFunctions = numFunctions; + genCodeParams.filenameTmp = "computeContextLoaderGenerated.tmp.h"; + genCodeParams.filenameDst = "computeContextLoaderGenerated.h"; + genCodeParams.moduleNameUpperCase = "ComputeContext"; + genCodeParams.moduleNameLowerCase = "computeContext"; + genCodeParams.instName = "gComputeContextLoader"; + genCodeParams.apiMarker = "COMPUTE_API"; + + fopen_s(&genCodeParams.file, genCodeParams.filenameTmp, "w"); + + generateCode(&genCodeParams); + + fclose(genCodeParams.file); + + freeStrHeap(&strHeap); + + fileDiffAndWriteIfModified(&genCodeParams); +} + +void genLoaderImgui() +{ + const unsigned int numFunctions = 18u; + + const char* functionDefinitions[numFunctions] = { + "void imguiGraphContextInit(const ImguiGraphDesc* desc);", + "void imguiGraphContextUpdate(const ImguiGraphDesc* desc);", + "void imguiGraphContextDestroy();", + "void imguiGraphRecordBegin();", + "void imguiGraphRecordEnd();", + "void imguiGraphVertex2f(float x, float y);", + "void imguiGraphVertex2fv(const float* v);", + "void imguiGraphTexCoord2f(float u, float v);", + "void imguiGraphColor4ub(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);", + "void imguiGraphColor4ubv(const uint8_t* v);", + "void imguiGraphFontTextureEnable();", + "void imguiGraphFontTextureDisable();", + "void imguiGraphEnableScissor(int x, int y, int width, int height);", + "void imguiGraphDisableScissor();", + "void imguiGraphFontTextureInit(unsigned char* data);", + "void imguiGraphFontTextureRelease();", + "bool imguiInteropGraphInit(imguiGraphInit_t func, const char* fontpath, AppGraphCtx* appctx);", + "void imguiInteropGraphUpdate(imguiGraphUpdate_t func, AppGraphCtx* appctx);" + }; + + StrHeap strHeap = allocateStrHeap(functionDefinitions, numFunctions); + + Function functions[numFunctions]; + for (unsigned int functionIdx = 0u; functionIdx < numFunctions; functionIdx++) + { + functions[functionIdx] = genFunction(&strHeap, functionDefinitions[functionIdx]); + } + + GenerateCodeParams genCodeParams = {}; + genCodeParams.loaderType = gLoaderType; + genCodeParams.file = nullptr; + genCodeParams.functions = functions; + genCodeParams.numFunctions = numFunctions; + genCodeParams.filenameTmp = "imguiGraphLoaderGenerated.tmp.h"; + genCodeParams.filenameDst = "imguiGraphLoaderGenerated.h"; + genCodeParams.moduleNameUpperCase = "Imgui"; + genCodeParams.moduleNameLowerCase = "imgui"; + genCodeParams.instName = "gImguiLoader"; + genCodeParams.apiMarker = "IMGUI_GRAPH_API"; + + fopen_s(&genCodeParams.file, genCodeParams.filenameTmp, "w"); + + generateCode(&genCodeParams); + + fclose(genCodeParams.file); + + freeStrHeap(&strHeap); + + fileDiffAndWriteIfModified(&genCodeParams); +} + +void genLoaderMesh() +{ + const unsigned int numFunctions = 10u; + + const char* functionDefinitions[numFunctions] = { + "MeshContext* MeshContextCreate(const MeshContextDesc* desc);", + "void MeshContextUpdate(MeshContext* context, const MeshContextDesc* desc);", + "void MeshContextRelease(MeshContext* context);", + "MeshIndexBuffer* MeshIndexBufferCreate(MeshContext* context, MeshUint* indices, MeshUint numIndices);", + "void MeshIndexBufferRelease(MeshIndexBuffer* buffer);", + "MeshVertexBuffer* MeshVertexBufferCreate(MeshContext* context, MeshVertex* vertices, MeshUint numVertices);", + "void MeshVertexBufferRelease(MeshVertexBuffer* buffer);", + "void MeshContextDraw(MeshContext* context, const MeshContextDrawParams* params);", + "MeshContext* MeshInteropContextCreate(AppGraphCtx* appctx);", + "void MeshInteropContextUpdate(MeshContext* context, AppGraphCtx* appctx);" + }; + + StrHeap strHeap = allocateStrHeap(functionDefinitions, numFunctions); + + Function functions[numFunctions]; + for (unsigned int functionIdx = 0u; functionIdx < numFunctions; functionIdx++) + { + functions[functionIdx] = genFunction(&strHeap, functionDefinitions[functionIdx]); + } + + GenerateCodeParams genCodeParams = {}; + genCodeParams.loaderType = gLoaderType; + genCodeParams.file = nullptr; + genCodeParams.functions = functions; + genCodeParams.numFunctions = numFunctions; + genCodeParams.filenameTmp = "meshLoaderGenerated.tmp.h"; + genCodeParams.filenameDst = "meshLoaderGenerated.h"; + genCodeParams.moduleNameUpperCase = "Mesh"; + genCodeParams.moduleNameLowerCase = "mesh"; + genCodeParams.instName = "gMeshLoader"; + genCodeParams.apiMarker = "MESH_API"; + + fopen_s(&genCodeParams.file, genCodeParams.filenameTmp, "w"); + + generateCode(&genCodeParams); + + fclose(genCodeParams.file); + + freeStrHeap(&strHeap); + + fileDiffAndWriteIfModified(&genCodeParams); +} + +void genNvFlowInteropLoader() +{ + const unsigned int numFunctions = 6u; + + const char* functionDefinitions[numFunctions] = { + "NvFlowContext* NvFlowInteropCreateContext(AppGraphCtx* appctx);", + "NvFlowDepthStencilView* NvFlowInteropCreateDepthStencilView(AppGraphCtx* appctx, NvFlowContext* flowctx);", + "NvFlowRenderTargetView* NvFlowInteropCreateRenderTargetView(AppGraphCtx* appctx, NvFlowContext* flowctx);", + "void NvFlowInteropUpdateContext(NvFlowContext* context, AppGraphCtx* appctx);", + "void NvFlowInteropUpdateDepthStencilView(NvFlowDepthStencilView* view, AppGraphCtx* appctx, NvFlowContext* flowctx);", + "void NvFlowInteropUpdateRenderTargetView(NvFlowRenderTargetView* view, AppGraphCtx* appctx, NvFlowContext* flowctx);" + }; + + StrHeap strHeap = allocateStrHeap(functionDefinitions, numFunctions); + + Function functions[numFunctions]; + for (unsigned int functionIdx = 0u; functionIdx < numFunctions; functionIdx++) + { + functions[functionIdx] = genFunction(&strHeap, functionDefinitions[functionIdx]); + } + + GenerateCodeParams genCodeParams = {}; + genCodeParams.loaderType = gLoaderType; + genCodeParams.file = nullptr; + genCodeParams.functions = functions; + genCodeParams.numFunctions = numFunctions; + genCodeParams.filenameTmp = "NvFlowInteropLoaderGenerated.tmp.h"; + genCodeParams.filenameDst = "NvFlowInteropLoaderGenerated.h"; + genCodeParams.moduleNameUpperCase = "NvFlowInterop"; + genCodeParams.moduleNameLowerCase = "nvFlowInterop"; + genCodeParams.instName = "gNvFlowInteropLoader"; + genCodeParams.apiMarker = "NV_FLOW_API"; + + fopen_s(&genCodeParams.file, genCodeParams.filenameTmp, "w"); + + generateCode(&genCodeParams); + + fclose(genCodeParams.file); + + freeStrHeap(&strHeap); + + fileDiffAndWriteIfModified(&genCodeParams); +} + +int main(int argc, char** argv) +{ + genLoaderAppGraphCtx(); + + genLoaderComputeContext(); + + genLoaderImgui(); + + genLoaderMesh(); + + genNvFlowInteropLoader(); + + return 0; +} \ No newline at end of file diff --git a/demo/DemoAppCodeGen/functionDefinitionExtract.h b/demo/DemoAppCodeGen/functionDefinitionExtract.h new file mode 100644 index 0000000..ca08253 --- /dev/null +++ b/demo/DemoAppCodeGen/functionDefinitionExtract.h @@ -0,0 +1,230 @@ +/* +* Copyright (c) 2014-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once + +struct StrHeap +{ + char* data; + unsigned int size; + unsigned int allocIdx; +}; + +StrHeap allocateStrHeap(const char** definitions, unsigned int numFunctions) +{ + unsigned int count = 0u; + for (unsigned int functionIdx = 0u; functionIdx < numFunctions; functionIdx++) + { + count += (unsigned int)strlen(definitions[functionIdx]); + } + + // offset count to cover null terminators + count += numFunctions * (2 * maxFunctionParams + 1 + 1); + + StrHeap heap = {}; + heap.data = (char*)malloc(count); + heap.size = count; + heap.allocIdx = 0u; + return heap; +} + +void freeStrHeap(StrHeap* heap) +{ + free(heap->data); + heap->data = nullptr; + heap->size = 0u; + heap->allocIdx = 0u; +} + +inline bool isSpace(char c) +{ + return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f' || c == '\v'; +} + +inline bool isAlphaNum(char c) +{ + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '_'); +} + +void skipSpace(unsigned int* readIdx, const char* definition) +{ + while (definition[*readIdx] != '\0') + { + if (isSpace(definition[*readIdx])) + { + *readIdx = *readIdx + 1; + } + else + { + break; + } + } +} + +void findChar(unsigned int* readIdx, const char* definition, char c) +{ + while (definition[*readIdx] != '\0') + { + if (definition[*readIdx] == c) + { + break; + } + else + { + *readIdx = *readIdx + 1; + } + } +} + +unsigned int findParamDelimiter(unsigned int readIdx, const char* definition) +{ + while (definition[readIdx] != '\0') + { + if (definition[readIdx] == ',' || definition[readIdx] == ')') + { + return readIdx; + } + else + { + readIdx = readIdx + 1; + } + } + return readIdx; +} + +unsigned int reverseFindAlphaNum(unsigned int maxReadIdx, const char* definition) +{ + int readIdx = (int)maxReadIdx; + readIdx--; + bool hitAlphaNum = false; + while (readIdx >= 0) + { + if (isAlphaNum(definition[readIdx])) + { + hitAlphaNum = true; + readIdx--; + } + else if (definition[readIdx] == '(' || definition[readIdx] == ',') + { + if (hitAlphaNum) + { + return (unsigned int)(readIdx + 1); + } + else + { + return maxReadIdx; + }; + } + else if (isSpace(definition[readIdx])) + { + if (hitAlphaNum) + { + return (unsigned int)(readIdx + 1); + } + else + { + readIdx--; + } + } + else + { + break; + } + } + return maxReadIdx; +} + +void extractAlphaNum(StrHeap* heap, const char** dstPtr, unsigned int* readIdx, const char* definition) +{ + skipSpace(readIdx, definition); + + *dstPtr = heap->data + heap->allocIdx; + + while (heap->allocIdx < heap->size) + { + if (isAlphaNum(definition[(*readIdx)])) + { + heap->data[heap->allocIdx++] = definition[(*readIdx)++]; + } + else + { + break; + } + } + heap->data[heap->allocIdx++] = '\0'; +} + +void extractRegion(StrHeap* heap, const char** dstPtr, unsigned char readIdx, unsigned char maxReadIdx, const char* definition) +{ + *dstPtr = heap->data + heap->allocIdx; + + while (heap->allocIdx < heap->size) + { + if (readIdx < maxReadIdx) + { + heap->data[heap->allocIdx++] = definition[readIdx++]; + } + else + { + break; + } + } + heap->data[heap->allocIdx++] = '\0'; +} + +Function genFunction(StrHeap* heap, const char* definition) +{ + Function function = {}; + + unsigned int readIdx = 0u; + unsigned int tempReadIdx = 0u; + + unsigned int paramStartReadIdx = readIdx; + findChar(¶mStartReadIdx, definition, '('); + + unsigned int methodReadIdx = reverseFindAlphaNum(paramStartReadIdx, definition); + + extractRegion(heap, &function.retType, readIdx, methodReadIdx, definition); + + tempReadIdx = methodReadIdx; + extractAlphaNum(heap, &function.method, &tempReadIdx, definition); + + readIdx = paramStartReadIdx; + + function.numParams = 0; + while (function.numParams < maxFunctionParams) + { + unsigned int maxReadIdx = findParamDelimiter(readIdx, definition); + + unsigned int instNameReadIdx = reverseFindAlphaNum(maxReadIdx, definition); + + // break if no name found + if (instNameReadIdx == maxReadIdx) + { + break; + } + + FunctionParams funcParams = {}; + + // extract type name, found on interval readIdx to instNameReadIdx + extractRegion(heap, &funcParams.typeName, readIdx + 1, instNameReadIdx, definition); + + // extract inst name, found on interval instNameReadIdx to maxReadIdx + tempReadIdx = instNameReadIdx; + extractAlphaNum(heap, &funcParams.instName, &tempReadIdx, definition); + + // advance read index + readIdx = maxReadIdx + 1; + + function.params[function.numParams++] = funcParams; + } + + return function; +} \ No newline at end of file diff --git a/demo/DemoAppCodeGen/loaderCodeGen.h b/demo/DemoAppCodeGen/loaderCodeGen.h new file mode 100644 index 0000000..b92beb1 --- /dev/null +++ b/demo/DemoAppCodeGen/loaderCodeGen.h @@ -0,0 +1,335 @@ +/* +* Copyright (c) 2014-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once + +enum LoaderType +{ + eLoaderTypeDynamicLink = 0, + eLoaderTypeStaticLink = 1 +}; + +const unsigned int maxFunctionParams = 8u; + +struct FunctionParams +{ + const char* typeName; + const char* instName; +}; + +struct Function +{ + const char* retType; + const char* method; + unsigned int numParams; + FunctionParams params[maxFunctionParams]; +}; + +struct GenerateCodeParams +{ + LoaderType loaderType; + FILE* file; + Function* functions; + unsigned int numFunctions; + + const char* filenameTmp; + const char* filenameDst; + const char* moduleNameUpperCase; + const char* moduleNameLowerCase; + const char* instName; + const char* apiMarker; +}; + +void typedef_function_ptrs(const GenerateCodeParams* params) +{ + for (unsigned int functionIdx = 0u; functionIdx < params->numFunctions; functionIdx++) + { + auto& function = params->functions[functionIdx]; + + fprintf(params->file, "typedef %s (*%s_ptr_t)(", function.retType, function.method); + + for (unsigned int paramIdx = 0u; paramIdx < function.numParams; paramIdx++) + { + fprintf(params->file, "%s %s", function.params[paramIdx].typeName, function.params[paramIdx].instName); + + if (paramIdx < function.numParams - 1) fprintf(params->file, ", "); + } + + fprintf(params->file, ");\n"); + } + + fprintf(params->file, "\n"); +} + +void declare_function_ptrs(const GenerateCodeParams* params) +{ + for (unsigned int functionIdx = 0u; functionIdx < params->numFunctions; functionIdx++) + { + auto& function = params->functions[functionIdx]; + + fprintf(params->file, "\t%s_ptr_t %s_ptr;\n", function.method, function.method); + } +} + +void implement_pointer_wrappers(const GenerateCodeParams* params) +{ + for (unsigned int functionIdx = 0u; functionIdx < params->numFunctions; functionIdx++) + { + auto& function = params->functions[functionIdx]; + + fprintf(params->file, "%s %s(", function.retType, function.method); + + for (unsigned int paramIdx = 0u; paramIdx < function.numParams; paramIdx++) + { + fprintf(params->file, "%s %s", function.params[paramIdx].typeName, function.params[paramIdx].instName); + + if (paramIdx < function.numParams - 1) fprintf(params->file, ", "); + } + + fprintf(params->file, ")\n{\n"); + + fprintf(params->file, "\treturn %s.%s_ptr(", params->instName, function.method); + + for (unsigned int paramIdx = 0u; paramIdx < function.numParams; paramIdx++) + { + fprintf(params->file, "%s", function.params[paramIdx].instName); + + if (paramIdx < function.numParams - 1) fprintf(params->file, ", "); + } + + fprintf(params->file, ");\n"); + + fprintf(params->file, "}\n\n"); + } +} + +void load_function_ptrs(const GenerateCodeParams* params) +{ + for (unsigned int functionIdx = 0u; functionIdx < params->numFunctions; functionIdx++) + { + auto& function = params->functions[functionIdx]; + + fprintf(params->file, "\t%s.%s_ptr = ", params->instName, function.method); + + fprintf(params->file, "(%s_ptr_t", function.method); + + fprintf(params->file, ")(%sLoaderLoadFunction(&%s, \"%s\")", params->moduleNameLowerCase, params->instName, function.method); + + fprintf(params->file, ");\n"); + } +} + +void load_function_ptrs_static(const GenerateCodeParams* params, const char* suffix) +{ + fprintf(params->file, "\tif (type == APP_CONTEXT_%s)\n\t{\n", suffix); + + for (unsigned int functionIdx = 0u; functionIdx < params->numFunctions; functionIdx++) + { + auto& function = params->functions[functionIdx]; + + fprintf(params->file, "\t\t%s.%s_ptr = ", params->instName, function.method); + + fprintf(params->file, "%s%s;\n", function.method, suffix); + } + + fprintf(params->file, "\t}\n\n"); +} + +void unload_function_ptrs(const GenerateCodeParams* params) +{ + for (unsigned int functionIdx = 0u; functionIdx < params->numFunctions; functionIdx++) + { + auto& function = params->functions[functionIdx]; + + fprintf(params->file, "\t%s.%s_ptr = nullptr;\n", params->instName, function.method); + } +} + +void declare_backend_functions(const GenerateCodeParams* params, const char* suffix) +{ + for (unsigned int functionIdx = 0u; functionIdx < params->numFunctions; functionIdx++) + { + auto& function = params->functions[functionIdx]; + + fprintf(params->file, "%s %s %s%s(", params->apiMarker, function.retType, function.method, suffix); + + for (unsigned int paramIdx = 0u; paramIdx < function.numParams; paramIdx++) + { + fprintf(params->file, "%s %s", function.params[paramIdx].typeName, function.params[paramIdx].instName); + + if (paramIdx < function.numParams - 1) fprintf(params->file, ", "); + } + + fprintf(params->file, ");\n"); + } + + fprintf(params->file, "\n"); +} + +const char* fileHeader = +"/*\n" +"* Copyright (c) 2014-2017, NVIDIA CORPORATION. All rights reserved.\n" +"*\n" +"* NVIDIA CORPORATION and its licensors retain all intellectual property\n" +"* and proprietary rights in and to this software, related documentation\n" +"* and any modifications thereto. Any use, reproduction, disclosure or\n" +"* distribution of this software and related documentation without an express\n" +"* license agreement from NVIDIA CORPORATION is strictly prohibited.\n" +"*/\n\n"; + +void generateCode(const GenerateCodeParams* params) +{ + fprintf(params->file, fileHeader); + + typedef_function_ptrs(params); + + const char* loaderBegin = + "struct %sLoader \n" + "{ \n" + " void* module = nullptr; \n" + " const char* suffix = \"\"; \n" + " char buf[1024u]; \n\n"; + fprintf(params->file, loaderBegin, params->moduleNameUpperCase); + + declare_function_ptrs(params); + + const char* loaderEnd = + "\n" + "}%s; \n\n"; + fprintf(params->file, loaderEnd, params->instName); + + implement_pointer_wrappers(params); + + if (params->loaderType == eLoaderTypeDynamicLink) + { + const char* loaderFunction = + "void* %sLoaderLoadFunction(%sLoader* inst, const char* name)\n" + "{\n" + " snprintf(inst->buf, 1024u, \"%s%s\", name, inst->suffix);\n" + "\n" + " return SDL_LoadFunction(inst->module, inst->buf);\n" + "}\n\n"; + fprintf(params->file, loaderFunction, params->moduleNameLowerCase, params->moduleNameUpperCase, "%s", "%s"); + + const char* loadModule = + "void load%s(AppGraphCtxType type)\n" + "{\n" + " const char* moduleName = demoAppDLLName(type);\n" + "\n" + " %s.suffix = demoAppBackendSuffix(type);\n" + "\n" + " %s.module = SDL_LoadObject(moduleName);\n\n"; + fprintf(params->file, loadModule, params->moduleNameUpperCase, params->instName, params->instName); + + load_function_ptrs(params); + + const char* unloadModule = + "}\n" + "\n" + "void unload%s()\n" + "{\n"; + fprintf(params->file, unloadModule, params->moduleNameUpperCase); + + unload_function_ptrs(params); + + const char* unloadModuleEnd = + "\n" + " SDL_UnloadObject(%s.module);\n" + "}\n"; + fprintf(params->file, unloadModuleEnd, params->instName); + } + else if (params->loaderType == eLoaderTypeStaticLink) + { + const unsigned int suffixCount = 2u; + const char* suffix[suffixCount] = { "D3D11", "D3D12" }; + + for (unsigned int suffixIdx = 0u; suffixIdx < 2u; suffixIdx++) + { + declare_backend_functions(params, suffix[suffixIdx]); + } + + const char* loadModule = + "void load%s(AppGraphCtxType type)\n" + "{\n" + " %s.suffix = demoAppBackendSuffix(type);\n" + "\n"; + fprintf(params->file, loadModule, params->moduleNameUpperCase, params->instName); + + for (unsigned int suffixIdx = 0u; suffixIdx < 2u; suffixIdx++) + { + load_function_ptrs_static(params, suffix[suffixIdx]); + } + + const char* unloadModule = + "}\n" + "\n" + "void unload%s()\n" + "{\n"; + fprintf(params->file, unloadModule, params->moduleNameUpperCase); + + unload_function_ptrs(params); + + const char* unloadModuleEnd = + "\n" + "}\n"; + fprintf(params->file, unloadModuleEnd, params->instName); + } +} + +void fileDiffAndWriteIfModified(const GenerateCodeParams* params) +{ + FILE* fileTmp = nullptr; + FILE* fileDst = nullptr; + bool match = true; + + fopen_s(&fileDst, params->filenameDst, "r"); + if (fileDst) + { + fopen_s(&fileTmp, params->filenameTmp, "r"); + if (fileTmp) + { + while (1) + { + int a = fgetc(fileTmp); + int b = fgetc(fileDst); + + if (a == EOF && b == EOF) + { + break; + } + else if (a != b) + { + match = false; + break; + } + } + + fclose(fileTmp); + } + else + { + match = false; + } + fclose(fileDst); + } + else + { + match = false; + } + + if (!match) + { + remove(params->filenameDst); + rename(params->filenameTmp, params->filenameDst); + } + + // always cleanup temp file + remove(params->filenameTmp); +} \ No newline at end of file -- cgit v1.2.3