*
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
/.vs
|
||||
/Test/VulkanWrapperSandbox/build
|
||||
/Test/VulkanWrapperSandbox/x64
|
||||
/build
|
||||
/out
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
[submodule "Dependencies/LMath"]
|
||||
path = Dependencies/LMath
|
||||
url = https://github.com/L-proger/LMath.git
|
||||
branch = master
|
||||
[submodule "Dependencies/LFramework"]
|
||||
path = Dependencies/LFramework
|
||||
url = https://github.com/L-proger/LFramework.git
|
||||
branch = main
|
||||
[submodule "Dependencies/glslang"]
|
||||
path = Dependencies/glslang
|
||||
url = https://github.com/KhronosGroup/glslang.git
|
||||
branch = main
|
||||
[submodule "Dependencies/SPIRV-Tools"]
|
||||
path = Dependencies/SPIRV-Tools
|
||||
url = https://github.com/KhronosGroup/SPIRV-Tools.git
|
||||
branch = main
|
||||
[submodule "Dependencies/SPIRV-Headers"]
|
||||
path = Dependencies/SPIRV-Headers
|
||||
url = https://github.com/KhronosGroup/SPIRV-Headers.git
|
||||
branch = main
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "(msvc) Launch",
|
||||
"type": "cppvsdbg",
|
||||
"request": "launch",
|
||||
// Resolved by CMake Tools:
|
||||
"program": "${command:cmake.launchTargetPath}",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
|
||||
"console": "externalTerminal"
|
||||
}
|
||||
]
|
||||
}
|
||||
Vendored
+91
@@ -0,0 +1,91 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"chrono": "cpp",
|
||||
"system_error": "cpp",
|
||||
"xlocale": "cpp",
|
||||
"xtr1common": "cpp",
|
||||
"iostream": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"bit": "cpp",
|
||||
"cctype": "cpp",
|
||||
"charconv": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"codecvt": "cpp",
|
||||
"compare": "cpp",
|
||||
"concepts": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"exception": "cpp",
|
||||
"expected": "cpp",
|
||||
"filesystem": "cpp",
|
||||
"format": "cpp",
|
||||
"forward_list": "cpp",
|
||||
"fstream": "cpp",
|
||||
"functional": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"ios": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"istream": "cpp",
|
||||
"iterator": "cpp",
|
||||
"limits": "cpp",
|
||||
"list": "cpp",
|
||||
"locale": "cpp",
|
||||
"map": "cpp",
|
||||
"memory": "cpp",
|
||||
"new": "cpp",
|
||||
"optional": "cpp",
|
||||
"ostream": "cpp",
|
||||
"ratio": "cpp",
|
||||
"set": "cpp",
|
||||
"span": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"stop_token": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"string": "cpp",
|
||||
"thread": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"utility": "cpp",
|
||||
"vector": "cpp",
|
||||
"xfacet": "cpp",
|
||||
"xhash": "cpp",
|
||||
"xiosbase": "cpp",
|
||||
"xlocbuf": "cpp",
|
||||
"xlocinfo": "cpp",
|
||||
"xlocmes": "cpp",
|
||||
"xlocmon": "cpp",
|
||||
"xlocnum": "cpp",
|
||||
"xloctime": "cpp",
|
||||
"xmemory": "cpp",
|
||||
"xstring": "cpp",
|
||||
"xtree": "cpp",
|
||||
"xutility": "cpp",
|
||||
"condition_variable": "cpp",
|
||||
"deque": "cpp",
|
||||
"future": "cpp",
|
||||
"mutex": "cpp",
|
||||
"ranges": "cpp",
|
||||
"stack": "cpp",
|
||||
"unordered_set": "cpp",
|
||||
"bitset": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"numeric": "cpp",
|
||||
"queue": "cpp",
|
||||
"random": "cpp",
|
||||
"regex": "cpp",
|
||||
"variant": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
macro(Vulkan_isDeviceFunction outVar funcName funcParams)
|
||||
string(FIND "${funcParams}" "VkDevice" out1)
|
||||
if(("${out1}" EQUAL 0) AND NOT("${funcName}" STREQUAL "PFN_vkGetDeviceProcAddr"))
|
||||
set(${outVar} 1)
|
||||
else()
|
||||
set(${outVar} 0)
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
macro(Vulkan_isDeviceCmdFunction outVar funcName funcParams)
|
||||
string(FIND "${funcParams}" "VkCommandBuffer" out1)
|
||||
if("${out1}" EQUAL 0)
|
||||
set(${outVar} 1)
|
||||
else()
|
||||
set(${outVar} 0)
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
macro(Vulkan_isDeviceQueueFunction outVar funcName funcParams)
|
||||
string(FIND "${funcParams}" "VkQueue" out1)
|
||||
if("${out1}" EQUAL 0)
|
||||
set(${outVar} 1)
|
||||
else()
|
||||
set(${outVar} 0)
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
macro(Vulkan_isInstanceFunction outVar funcName funcParams)
|
||||
string(FIND "${funcParams}" "VkInstance" out1)
|
||||
string(FIND "${funcParams}" "VkPhysicalDevice" out2)
|
||||
if(("${out1}" EQUAL 0) OR ("${out2}" EQUAL 0) OR ("${funcName}" STREQUAL "PFN_vkGetDeviceProcAddr"))
|
||||
set(${outVar} 1)
|
||||
else()
|
||||
set(${outVar} 0)
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
macro(Vulkan_isGlobalFunction outVar funcName funcParams)
|
||||
if(NOT(${funcName} MATCHES ".*MyFolderName$") AND NOT(${funcName} MATCHES ".*Function$") AND NOT(${funcName} MATCHES ".*Notification$"))
|
||||
set(${outVar} 1)
|
||||
else()
|
||||
set(${outVar} 0)
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
|
||||
|
||||
|
||||
set(Vulkan_paramNameRegex "([^* ]+)[ ]*$")
|
||||
set(Vulkan_paramSplitFirstRestRegex "([ ]*[^,]+)[ ]*\,*[ ]*(.*)")
|
||||
|
||||
macro(Vulkan_functionsFile_function className funcResultType funcName funcParams contextVarName)
|
||||
string(REPLACE "PFN_" "" cleanFuncName ${funcName})
|
||||
|
||||
string(APPEND ${className}Loaders " _${cleanFuncName} = reinterpret_cast<${funcName}>(loader(\"${cleanFuncName}\"))\;\n" )
|
||||
|
||||
set(callArgs "")
|
||||
if (NOT("${contextVarName}" STREQUAL ""))
|
||||
string(REGEX MATCH "${Vulkan_paramSplitFirstRestRegex}" matchResultVar "${funcParams}")
|
||||
set(InputCallParams "${CMAKE_MATCH_2}")
|
||||
list(APPEND callArgs "_${contextVarName}")
|
||||
else()
|
||||
set(InputCallParams "${funcParams}")
|
||||
endif()
|
||||
|
||||
string(APPEND ${className}Pointers " ${funcName} _${cleanFuncName}\;\n")
|
||||
|
||||
string(REPLACE "," ";" SplittedParams "${InputCallParams}")
|
||||
foreach(param ${SplittedParams})
|
||||
string(REGEX MATCH "${Vulkan_paramNameRegex}" matchResultVar "${param}")
|
||||
set(ParamName ${CMAKE_MATCH_1})
|
||||
|
||||
#Clean param name
|
||||
string(REGEX MATCH "[ ]*([^ \[]+)" matchResultVar "${ParamName}")
|
||||
#[ ]*([^ ]+)[ ]*\[.\]*[ ]*$
|
||||
list(APPEND callArgs ${CMAKE_MATCH_1})
|
||||
endforeach()
|
||||
|
||||
list(JOIN callArgs ", " callArgsStr)
|
||||
|
||||
if("${funcResultType}" STREQUAL "void")
|
||||
set(returnString "")
|
||||
else()
|
||||
set(returnString "return ")
|
||||
endif()
|
||||
string(APPEND ${className}Functions " ${funcResultType} ${cleanFuncName}(${InputCallParams}) const {\n")
|
||||
string(APPEND ${className}Functions " ${returnString}_${cleanFuncName}(${callArgsStr})\;\n")
|
||||
string(APPEND ${className}Functions " }\n")
|
||||
endmacro()
|
||||
|
||||
macro(Vulkan_functionsFile_make className contextVarName contextVarType outFilePath )
|
||||
#File begin
|
||||
set(${className}Result "")
|
||||
string(APPEND ${className}Result "#pragma once\n")
|
||||
string(APPEND ${className}Result "#include <vulkan/vulkan.h>\n")
|
||||
string(APPEND ${className}Result "#include <functional>\n")
|
||||
#string(APPEND ${className}Result "#include \"VulkanInstance.h\"\n")
|
||||
string(APPEND ${className}Result "class ${className} { \n")
|
||||
|
||||
#File constructor
|
||||
string(APPEND ${className}Result "public:\n")
|
||||
if (NOT("${contextVarType}" STREQUAL ""))
|
||||
set(ctroArgSuffix ", ${contextVarType} ${contextVarName}")
|
||||
set(CtorInit " : _${contextVarName}(${contextVarName})")
|
||||
else()
|
||||
set(ctroArgSuffix "")
|
||||
set(CtorInit "")
|
||||
endif()
|
||||
string(APPEND ${className}Result " ${className}(std::function<void*(const char*)> loader${ctroArgSuffix})${CtorInit}{\n")
|
||||
string(APPEND ${className}Result "${${className}Loaders}\n")
|
||||
string(APPEND ${className}Result " }\n")
|
||||
string(APPEND ${className}Result " ${className}(){}\n")
|
||||
|
||||
|
||||
#Public functions
|
||||
string(APPEND ${className}Result "${${className}Functions}\n")
|
||||
#Private fields
|
||||
string(APPEND ${className}Result "private:\n")
|
||||
string(APPEND ${className}Result "${${className}Pointers}\n")
|
||||
|
||||
#Context object
|
||||
if (NOT("${contextVarType}" STREQUAL ""))
|
||||
string(APPEND ${className}Result " ${contextVarType} _${contextVarName} = {}\;\n")
|
||||
endif()
|
||||
|
||||
#File end
|
||||
string(APPEND ${className}Result "}\;\n") #End class
|
||||
|
||||
#File save
|
||||
file(WRITE "${outFilePath}" ${${className}Result})
|
||||
endmacro()
|
||||
|
||||
function(Vulkan_generateFunctionsFile outFileDir)
|
||||
set(VULKAN_CORE_H "${Vulkan_INCLUDE_DIR}/vulkan/vulkan_core.h")
|
||||
file(STRINGS "${VULKAN_CORE_H}" VULKAN_CORE_H_STRINGS)
|
||||
|
||||
|
||||
foreach(STR ${VULKAN_CORE_H_STRINGS})
|
||||
string(REGEX MATCH ".*typedef[ ]+([^ ]+)[ ]+\\([ ]*VKAPI_PTR[ ]+\\*([^\\)]+)\\)[ ]*\\(([^\\)]+)\\)" matchResultVar "${STR}")
|
||||
if(NOT("${CMAKE_MATCH_2}" STREQUAL ""))
|
||||
set(FN_RESULT "${CMAKE_MATCH_1}")
|
||||
set(FN_NAME "${CMAKE_MATCH_2}")
|
||||
set(FN_PARAMS "${CMAKE_MATCH_3}")
|
||||
|
||||
Vulkan_isDeviceFunction("found" "${FN_NAME}" "${FN_PARAMS}")
|
||||
if("${found}" EQUAL 1)
|
||||
Vulkan_functionsFile_function(VulkanDeviceFunctions "${FN_RESULT}" "${FN_NAME}" "${FN_PARAMS}" "device")
|
||||
endif()
|
||||
|
||||
Vulkan_isDeviceCmdFunction("found" "${FN_NAME}" "${FN_PARAMS}")
|
||||
if("${found}" EQUAL 1)
|
||||
Vulkan_functionsFile_function(VulkanDeviceFunctions "${FN_RESULT}" "${FN_NAME}" "${FN_PARAMS}" "")
|
||||
endif()
|
||||
|
||||
Vulkan_isDeviceQueueFunction("found" "${FN_NAME}" "${FN_PARAMS}")
|
||||
if("${found}" EQUAL 1)
|
||||
Vulkan_functionsFile_function(VulkanDeviceFunctions "${FN_RESULT}" "${FN_NAME}" "${FN_PARAMS}" "")
|
||||
endif()
|
||||
|
||||
|
||||
|
||||
Vulkan_isInstanceFunction("found" "${FN_NAME}" "${FN_PARAMS}")
|
||||
if("${found}" EQUAL 1)
|
||||
Vulkan_functionsFile_function(VulkanInstanceFunctions "${FN_RESULT}" "${FN_NAME}" "${FN_PARAMS}" "")
|
||||
endif()
|
||||
|
||||
Vulkan_isGlobalFunction("found" "${FN_NAME}" "${FN_PARAMS}")
|
||||
if("${found}" EQUAL 1)
|
||||
Vulkan_functionsFile_function(VulkanGlobalFunctions "${FN_RESULT}" "${FN_NAME}" "${FN_PARAMS}" "")
|
||||
endif()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
|
||||
|
||||
Vulkan_functionsFile_make(VulkanDeviceFunctions "device" "VkDevice" "${outFileDir}/VulkanDeviceFunctions.h")
|
||||
Vulkan_functionsFile_make(VulkanInstanceFunctions "" "" "${outFileDir}/VulkanInstanceFunctions.h")
|
||||
Vulkan_functionsFile_make(VulkanGlobalFunctions "" "" "${outFileDir}/VulkanGlobalFunctions.h")
|
||||
|
||||
endfunction()
|
||||
@@ -0,0 +1,39 @@
|
||||
cmake_minimum_required(VERSION 3.24)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake")
|
||||
|
||||
project(RenderFramework C CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
|
||||
if(MSVC_VERSION GREATER 1500)
|
||||
include(ProcessorCount)
|
||||
ProcessorCount(N)
|
||||
if(NOT N EQUAL 0)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP${N}" CACHE STRING "" FORCE)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj /MP${N}" CACHE STRING "" FORCE)
|
||||
set(CMAKE_CSharp_FLAGS "${CMAKE_CSharp_FLAGS} /m:${N}" CACHE STRING "" FORCE)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_subdirectory(Dependencies/LFramework)
|
||||
add_subdirectory(Dependencies/LMath)
|
||||
|
||||
|
||||
set(ENABLE_GLSLANG_BINARIES OFF)
|
||||
set(ALLOW_EXTERNAL_SPIRV_TOOLS ON)
|
||||
set(ENABLE_EXCEPTIONS ON)
|
||||
find_package(Vulkan REQUIRED)
|
||||
|
||||
set(SPIRV_TOOLS_BUILD_STATIC ON)
|
||||
set(SPIRV_SKIP_EXECUTABLES 1)
|
||||
|
||||
add_subdirectory(Dependencies/SPIRV-Headers)
|
||||
add_subdirectory(Dependencies/SPIRV-Tools)
|
||||
#
|
||||
add_subdirectory(Dependencies/glslang)
|
||||
|
||||
add_subdirectory(Src)
|
||||
add_subdirectory(Examples/GameEditor)
|
||||
|
||||
+1
Submodule Dependencies/LFramework added at 3f509a23c4
+1
Submodule Dependencies/LMath added at 428185341b
+1
Submodule Dependencies/SPIRV-Headers added at 6d0784e9f1
+1
Submodule Dependencies/SPIRV-Tools added at feba06f44f
+1
Submodule Dependencies/glslang added at 963588074b
@@ -0,0 +1,26 @@
|
||||
# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
|
||||
# File Created: 22.09.2015 22:34:25
|
||||
|
||||
newmtl RedCube
|
||||
Ns 9.999999
|
||||
Ni 1.500000
|
||||
d 1.000000
|
||||
Tr 0.000000
|
||||
Tf 1.000000 1.000000 1.000000
|
||||
illum 2
|
||||
Ka 1.000000 0.000000 0.000000
|
||||
Kd 1.000000 0.000000 0.000000
|
||||
Ks 0.000000 0.000000 0.000000
|
||||
Ke 0.000000 0.000000 0.000000
|
||||
|
||||
newmtl BlueCube
|
||||
Ns 9.999999
|
||||
Ni 1.500000
|
||||
d 1.000000
|
||||
Tr 0.000000
|
||||
Tf 1.000000 1.000000 1.000000
|
||||
illum 2
|
||||
Ka 0.047059 0.000000 1.000000
|
||||
Kd 0.047059 0.000000 1.000000
|
||||
Ks 0.000000 0.000000 0.000000
|
||||
Ke 0.000000 0.000000 0.000000
|
||||
@@ -0,0 +1,105 @@
|
||||
# 3dA Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
|
||||
# File Created: 22.09.2015 22:34:25
|
||||
|
||||
mtllib Boxes.mtl
|
||||
|
||||
#
|
||||
# object RedBox
|
||||
#
|
||||
|
||||
v -0.500000 0.000000 0.500000
|
||||
v -0.500000 0.000000 -0.500000
|
||||
v 0.500000 0.000000 -0.500000
|
||||
v 0.500000 0.000000 0.500000
|
||||
v -0.500000 1.000000 0.500000
|
||||
v 0.500000 1.000000 0.500000
|
||||
v 0.500000 1.000000 -0.500000
|
||||
v -0.500000 1.000000 -0.500000
|
||||
# 8 vertices
|
||||
|
||||
vn 0.000000 -1.000000 -0.000000
|
||||
vn 0.000000 1.000000 -0.000000
|
||||
vn 0.000000 0.000000 1.000000
|
||||
vn 1.000000 0.000000 -0.000000
|
||||
vn 0.000000 0.000000 -1.000000
|
||||
vn -1.000000 0.000000 -0.000000
|
||||
# 6 vertex normals
|
||||
|
||||
vt 1.000000 0.000000 0.000000
|
||||
vt 1.000000 1.000000 0.000000
|
||||
vt 0.000000 1.000000 0.000000
|
||||
vt 0.000000 0.000000 0.000000
|
||||
# 4 texture coords
|
||||
|
||||
g RedBox
|
||||
usemtl RedCube
|
||||
s 2
|
||||
f 1/1/1 2/2/1 3/3/1
|
||||
f 3/3/1 4/4/1 1/1/1
|
||||
s 4
|
||||
f 5/4/2 6/1/2 7/2/2
|
||||
f 7/2/2 8/3/2 5/4/2
|
||||
s 8
|
||||
f 1/4/3 4/1/3 6/2/3
|
||||
f 6/2/3 5/3/3 1/4/3
|
||||
s 16
|
||||
f 4/4/4 3/1/4 7/2/4
|
||||
f 7/2/4 6/3/4 4/4/4
|
||||
s 32
|
||||
f 3/4/5 2/1/5 8/2/5
|
||||
f 8/2/5 7/3/5 3/4/5
|
||||
s 64
|
||||
f 2/4/6 1/1/6 5/2/6
|
||||
f 5/2/6 8/3/6 2/4/6
|
||||
# 12 faces
|
||||
|
||||
#
|
||||
# object BlueBox
|
||||
#
|
||||
|
||||
v -0.500000 0.000000 1.978305
|
||||
v -0.500000 0.000000 0.978305
|
||||
v 0.500000 0.000000 0.978305
|
||||
v 0.500000 0.000000 1.978305
|
||||
v -0.500000 1.000000 1.978305
|
||||
v 0.500000 1.000000 1.978305
|
||||
v 0.500000 1.000000 0.978305
|
||||
v -0.500000 1.000000 0.978305
|
||||
# 8 vertices
|
||||
|
||||
vn 0.000000 -1.000000 -0.000000
|
||||
vn 0.000000 1.000000 -0.000000
|
||||
vn 0.000000 0.000000 1.000000
|
||||
vn 1.000000 0.000000 -0.000000
|
||||
vn 0.000000 0.000000 -1.000000
|
||||
vn -1.000000 0.000000 -0.000000
|
||||
# 6 vertex normals
|
||||
|
||||
vt 1.000000 0.000000 0.000000
|
||||
vt 1.000000 1.000000 0.000000
|
||||
vt 0.000000 1.000000 0.000000
|
||||
vt 0.000000 0.000000 0.000000
|
||||
# 4 texture coords
|
||||
|
||||
g BlueBox
|
||||
usemtl BlueCube
|
||||
s 2
|
||||
f 9/5/7 10/6/7 11/7/7
|
||||
f 11/7/7 12/8/7 9/5/7
|
||||
s 4
|
||||
f 13/8/8 14/5/8 15/6/8
|
||||
f 15/6/8 16/7/8 13/8/8
|
||||
s 8
|
||||
f 9/8/9 12/5/9 14/6/9
|
||||
f 14/6/9 13/7/9 9/8/9
|
||||
s 16
|
||||
f 12/8/10 11/5/10 15/6/10
|
||||
f 15/6/10 14/7/10 12/8/10
|
||||
s 32
|
||||
f 11/8/11 10/5/11 16/6/11
|
||||
f 16/6/11 15/7/11 11/8/11
|
||||
s 64
|
||||
f 10/8/12 9/5/12 13/6/12
|
||||
f 13/6/12 16/7/12 10/8/12
|
||||
# 12 faces
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
1.0
|
||||
{6799A53E-CBB1-451A-82C7-E5F10C33EBCA}
|
||||
14435573237290376
|
||||
@@ -0,0 +1,57 @@
|
||||
struct VS_INPUT {
|
||||
float4 vPosition : POSITION;
|
||||
float3 normal : NORMAL;
|
||||
float2 uv0 : UV0;
|
||||
float2 uv1 : UV1;
|
||||
};
|
||||
|
||||
struct VS_OUTPUT {
|
||||
float4 vPosition : SV_POSITION;
|
||||
float3 normal : TEXCOORD0;
|
||||
float2 uv0 : TEXCOORD1;
|
||||
float2 uv1 : TEXCOORD2;
|
||||
float3 worldPos : TEXCOORD3;
|
||||
};
|
||||
|
||||
cbuffer TestBuffer : register(b3) {
|
||||
float4x4 mWorld;
|
||||
float4x4 mView;
|
||||
float4x4 mProjection;
|
||||
float4 cameraWorldPos;
|
||||
}
|
||||
|
||||
Texture2D diffuse : register(t0);
|
||||
Texture2D tex2 : register(t1);
|
||||
SamplerState MeshTextureSampler;
|
||||
|
||||
|
||||
|
||||
VS_OUTPUT main_vs(VS_INPUT v) {
|
||||
VS_OUTPUT o;
|
||||
|
||||
float4x4 wvp = mul(mul(mProjection, mView), mWorld);
|
||||
|
||||
o.vPosition = mul(wvp, float4(v.vPosition.xyz, 1));
|
||||
|
||||
o.normal = mul((float3x3)mWorld, v.normal);
|
||||
o.uv0 = v.uv0;
|
||||
o.uv1 = v.uv1;
|
||||
o.worldPos = mul((float3x3)mWorld, v.vPosition.xyz);
|
||||
return o;
|
||||
}
|
||||
|
||||
#include "lighting/cook_torrance_term.inc"
|
||||
|
||||
float4 main_ps(VS_OUTPUT input) : SV_Target
|
||||
{
|
||||
float3 ambient = float3(0.3f, 0.3f, 0.4f);
|
||||
|
||||
float4 diffuseColor = 1.0f;// diffuse.Sample(MeshTextureSampler, input.uv0);
|
||||
float3 worldPos = input.worldPos;
|
||||
float3 camPos = cameraWorldPos.xyz;
|
||||
float3 viewDir = normalize(camPos - worldPos);
|
||||
float3 sun = normalize(float3(-1, 1, -1));
|
||||
|
||||
float3 surfaceColor = cook_torrance(normalize(input.normal), viewDir, sun, 0.1f, 1.0f, float3(1,1,1), diffuseColor.rgb);
|
||||
return float4(surfaceColor + ambient * diffuseColor, 1);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
struct VS_INPUT
|
||||
{
|
||||
float4 vPosition : POSITION;
|
||||
float3 normal : NORMAL;
|
||||
float2 uv0 : UV0;
|
||||
float2 uv1 : UV1;
|
||||
};
|
||||
|
||||
struct VS_OUTPUT {
|
||||
float4 vPosition : SV_POSITION;
|
||||
float3 normal : TEXCOORD0;
|
||||
float2 uv0 : TEXCOORD1;
|
||||
float2 uv1 : TEXCOORD2;
|
||||
float3 worldPos : TEXCOORD3;
|
||||
float3 viewPos : TEXCOORD4;
|
||||
};
|
||||
|
||||
cbuffer TestBuffer : register(b3) {
|
||||
float4x4 mWorld;
|
||||
float4x4 mView;
|
||||
float4x4 mProjection;
|
||||
float4 cameraWorldPos;
|
||||
float4 _DiffuseColor;
|
||||
}
|
||||
|
||||
Texture2D diffuse : register(t0);
|
||||
Texture2D tex2 : register(t1);
|
||||
SamplerState MeshTextureSampler;
|
||||
|
||||
|
||||
|
||||
VS_OUTPUT main_vs(VS_INPUT v){
|
||||
VS_OUTPUT o;
|
||||
|
||||
|
||||
float4x4 wv = mul(mView, mWorld);
|
||||
float4x4 wvp = mul(mul(mProjection, mView), mWorld);
|
||||
|
||||
o.vPosition = mul(wvp, float4(v.vPosition.xyz,1));
|
||||
|
||||
o.normal = mul((float3x3)mWorld, v.normal);
|
||||
o.uv0 = v.uv0;
|
||||
o.uv1 = v.uv1;
|
||||
o.worldPos = mul((float3x3)mWorld, v.vPosition.xyz);
|
||||
o.viewPos = mul(wv, float4(v.vPosition.xyz, 1));
|
||||
return o;
|
||||
}
|
||||
|
||||
#include "lighting/cook_torrance_term.inc"
|
||||
|
||||
float4 main_ps(VS_OUTPUT input) : SV_Target
|
||||
{
|
||||
float3 ambient = float3(0.3f, 0.3f, 0.4f);
|
||||
|
||||
float4 diffuseColor = _DiffuseColor * diffuse.Sample(MeshTextureSampler, input.uv0);
|
||||
float3 worldPos = input.worldPos;
|
||||
float3 camPos = cameraWorldPos.xyz;
|
||||
float3 viewDir = normalize(camPos - worldPos);
|
||||
float3 sun = normalize(float3(-1, 1, -1));
|
||||
|
||||
float3 surfaceColor = cook_torrance(normalize(input.normal), viewDir, sun, 0.7f, 0.1f, diffuseColor.rgb, diffuseColor.rgb);
|
||||
|
||||
return float4(surfaceColor + ambient * diffuseColor, 1);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
struct VS_INPUT
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float4 color : COLOR;
|
||||
};
|
||||
|
||||
struct VS_OUTPUT {
|
||||
float4 position : SV_POSITION;
|
||||
float4 color : TEXCOORD0;
|
||||
};
|
||||
|
||||
cbuffer TestBuffer : register(b3) {
|
||||
float4x4 mWorld;
|
||||
float4x4 mView;
|
||||
float4x4 mProjection;
|
||||
float alphaMul;
|
||||
}
|
||||
|
||||
VS_OUTPUT main_vs(VS_INPUT v) {
|
||||
VS_OUTPUT o;
|
||||
|
||||
float4x4 wvp = mul(mul(mProjection, mView), mWorld);
|
||||
o.position = mul(wvp, float4(v.position.xyz, 1));
|
||||
o.color = v.color;
|
||||
return o;
|
||||
}
|
||||
|
||||
float4 main_ps(VS_OUTPUT input) : SV_Target{
|
||||
float4 color = input.color;
|
||||
color *= alphaMul;
|
||||
return color;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#ifndef COOK_TORRANCE_TERM
|
||||
#define COOK_TORRANCE_TERM
|
||||
|
||||
float4 cook_torrance(
|
||||
in float3 normal,
|
||||
in float3 viewer,
|
||||
in float3 light, float roughness_value, float ref_at_norm_incidence, float3 cSpecular, float3 cDiffuse)
|
||||
{
|
||||
// Compute any aliases and intermediary values
|
||||
// -------------------------------------------
|
||||
float3 half_vector = normalize( light + viewer );
|
||||
float NdotL = saturate( dot( normal, light ) );
|
||||
float NdotH = max( dot( normal, half_vector ) , 1.0e-7);
|
||||
float NdotV = saturate( dot( normal, viewer ) );
|
||||
float VdotH = saturate( dot( viewer, half_vector ) );
|
||||
float r_sq = roughness_value * roughness_value;
|
||||
|
||||
|
||||
|
||||
// Evaluate the geometric term
|
||||
// --------------------------------
|
||||
float geo_numerator = 2.0f * NdotH;
|
||||
float geo_denominator = VdotH;
|
||||
|
||||
float geo_b = (geo_numerator * NdotV ) / geo_denominator;
|
||||
float geo_c = (geo_numerator * NdotL ) / geo_denominator;
|
||||
float geo = min( 1.0f, min( geo_b, geo_c ) );
|
||||
|
||||
|
||||
|
||||
// Now evaluate the roughness term
|
||||
// -------------------------------
|
||||
float roughness;
|
||||
float roughness_a = 1.0f / ( 4.0f * r_sq * pow( NdotH, 4 ) );
|
||||
float roughness_b = NdotH * NdotH - 1.0f;
|
||||
float roughness_c = r_sq * NdotH * NdotH;
|
||||
roughness = roughness_a * exp( roughness_b / roughness_c );
|
||||
|
||||
|
||||
// Next evaluate the Fresnel value
|
||||
// -------------------------------
|
||||
float fresnel = pow( 1.0f - VdotH, 5.0f );
|
||||
fresnel *= ( 1.0f - ref_at_norm_incidence );
|
||||
fresnel += ref_at_norm_incidence;
|
||||
|
||||
|
||||
|
||||
// Put all the terms together to compute
|
||||
// the specular term in the equation
|
||||
// -------------------------------------
|
||||
float3 Rs_numerator = ( fresnel * geo * roughness );
|
||||
float Rs_denominator = max(1.0e-7, NdotV * NdotL);
|
||||
float3 Rs = Rs_numerator/ Rs_denominator;
|
||||
|
||||
|
||||
|
||||
// Put all the parts together to generate
|
||||
// the final colour
|
||||
// --------------------------------------
|
||||
|
||||
|
||||
Rs = max(0, Rs);
|
||||
float3 final = max(0.0f, NdotL) *cDiffuse + (1 * Rs );
|
||||
|
||||
// Return the result
|
||||
// -----------------
|
||||
return float4( final, 1.0f );
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,47 @@
|
||||
struct VS_INPUT
|
||||
{
|
||||
float4 vPosition : POSITION;
|
||||
float3 normal : NORMAL;
|
||||
float2 uv0 : UV0;
|
||||
float2 uv1 : UV1;
|
||||
};
|
||||
|
||||
struct VS_OUTPUT {
|
||||
float4 vPosition : SV_POSITION;
|
||||
float3 normal : TEXCOORD0;
|
||||
float2 uv0 : TEXCOORD1;
|
||||
float2 uv1 : TEXCOORD2;
|
||||
};
|
||||
|
||||
cbuffer TestBuffer : register(b3) {
|
||||
float4x4 mWorld;
|
||||
float4x4 mView;
|
||||
float4x4 mProjection;
|
||||
}
|
||||
|
||||
Texture2D tex : register(t0);
|
||||
Texture2D tex2 : register(t1);
|
||||
SamplerState MeshTextureSampler;
|
||||
|
||||
VS_OUTPUT main_vs(VS_INPUT v) {
|
||||
VS_OUTPUT o;
|
||||
|
||||
float4x4 wvp = mul(mul(mProjection, mView), mWorld);
|
||||
|
||||
o.vPosition = mul(wvp, float4(v.vPosition.xyz, 1));
|
||||
|
||||
o.normal = mul((float3x3)mWorld, v.normal);
|
||||
o.uv0 = v.uv0;
|
||||
o.uv1 = v.uv1;
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
float4 main_ps(VS_OUTPUT input) : SV_Target
|
||||
{
|
||||
float3 sun = normalize(float3(-1,1,-1));
|
||||
float lit = dot(sun, input.normal);
|
||||
float3 diffuse = float3(1, 0, 0);
|
||||
|
||||
return float4(diffuse * lit, 1);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
struct VS_INPUT
|
||||
{
|
||||
float4 vPosition : POSITION;
|
||||
float3 normal : NORMAL;
|
||||
float2 uv0 : UV0;
|
||||
float2 uv1 : UV1;
|
||||
};
|
||||
|
||||
struct VS_OUTPUT {
|
||||
float4 vPosition : SV_POSITION;
|
||||
float3 normal : TEXCOORD0;
|
||||
float2 uv0 : TEXCOORD1;
|
||||
float2 uv1 : TEXCOORD2;
|
||||
float3 worldPos : TEXCOORD3;
|
||||
float3 viewPos : TEXCOORD4;
|
||||
};
|
||||
|
||||
cbuffer TestBuffer : register(b3) {
|
||||
float4x4 mWorld;
|
||||
float4x4 mView;
|
||||
float4x4 mProjection;
|
||||
float4 cameraWorldPos;
|
||||
}
|
||||
|
||||
Texture2D diffuse : register(t0);
|
||||
SamplerState MeshTextureSampler : register(s0);
|
||||
|
||||
|
||||
|
||||
VS_OUTPUT main_vs(VS_INPUT v){
|
||||
VS_OUTPUT o;
|
||||
|
||||
|
||||
float4x4 wv = mul(mView, mWorld);
|
||||
float4x4 wvp = mul(mul(mProjection, mView), mWorld);
|
||||
|
||||
o.vPosition = mul(wvp, float4(v.vPosition.xyz,1));
|
||||
|
||||
o.normal = mul((float3x3)mWorld, v.normal);
|
||||
o.uv0 = v.uv0;
|
||||
o.uv1 = v.uv1;
|
||||
o.worldPos = mul((float3x3)mWorld, v.vPosition.xyz);
|
||||
o.viewPos = mul(wv, float4(v.vPosition.xyz, 1));
|
||||
return o;
|
||||
}
|
||||
|
||||
#include "lighting/cook_torrance_term.inc"
|
||||
|
||||
float4 main_ps(VS_OUTPUT input) : SV_Target
|
||||
{
|
||||
float3 ambient = float3(0.3f, 0.3f, 0.4f);
|
||||
|
||||
float4 diffuseColor = diffuse.Sample(MeshTextureSampler, input.uv0);
|
||||
return diffuseColor;
|
||||
float3 worldPos = input.worldPos;
|
||||
float3 camPos = cameraWorldPos.xyz;
|
||||
float3 viewDir = normalize(camPos - worldPos);
|
||||
float3 sun = normalize(float3(-1, 1, -1));
|
||||
|
||||
float3 surfaceColor = cook_torrance(normalize(input.normal), viewDir, sun, 0.7f, 0.1f, diffuseColor.rgb, diffuseColor.rgb);
|
||||
|
||||
return float4(surfaceColor + ambient * diffuseColor, 1);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
|
||||
#version 450
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
layout(location = 0) in vec3 fragColor;
|
||||
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
void main() {
|
||||
outColor = vec4(fragColor, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#version 450
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
|
||||
layout(binding = 0) uniform UniformBufferObject {
|
||||
mat4 model;
|
||||
mat4 view;
|
||||
mat4 proj;
|
||||
} ubo;
|
||||
|
||||
layout(location = 0) in vec3 inPosition;
|
||||
layout(location = 2) in vec3 inColor;
|
||||
|
||||
layout(location = 0) out vec3 fragColor;
|
||||
|
||||
void main() {
|
||||
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition.xyz, 1.0);
|
||||
fragColor = vec3(inColor.xyz);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,13 @@
|
||||
#ifndef BoundsView_h__
|
||||
#define BoundsView_h__
|
||||
|
||||
#include <RenderFramework/ObjectsFactory.h>
|
||||
|
||||
LOBJECT(BoundsView, Component)
|
||||
public:
|
||||
void tick(float deltaTime) override {
|
||||
LEngine::Instance()->GetActiveScene()->GetGizmos().SetMatrix(transform()->GetGlobalTransform());
|
||||
LEngine::Instance()->GetActiveScene()->GetGizmos().DrawBounds(gameObject->GetComponent<MeshRenderer>()->GetMesh()->GetBounds());
|
||||
}
|
||||
};
|
||||
#endif // BoundsView_h__
|
||||
@@ -0,0 +1,29 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
add_executable(GameEditor)
|
||||
|
||||
target_sources(GameEditor
|
||||
PRIVATE
|
||||
BoundsView.h
|
||||
FreeCameraController.h
|
||||
GameEditor.cpp
|
||||
GameEditor.h
|
||||
LFrameworkConfig.h
|
||||
)
|
||||
|
||||
#target_include_directories(GameEditor
|
||||
#PRIVATE
|
||||
#
|
||||
#)
|
||||
|
||||
target_link_libraries(GameEditor
|
||||
PRIVATE
|
||||
RenderFramework
|
||||
LFramework
|
||||
LFramework::Input
|
||||
)
|
||||
|
||||
target_compile_definitions(GameEditor
|
||||
PRIVATE
|
||||
ASSETS_DIR=L"${CMAKE_CURRENT_LIST_DIR}/../Assets"
|
||||
)
|
||||
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include <RenderFramework/Component.h>
|
||||
#include "lframework/Input/Input.h"
|
||||
#include <iostream>
|
||||
#include <RenderFramework/Transform.h>
|
||||
#include <RenderFramework/ObjectsFactory.h>
|
||||
|
||||
LOBJECT(FreeCameraController, Component)
|
||||
public:
|
||||
float mouse_speed;
|
||||
float move_speed = 3.0f;
|
||||
static constexpr float pi = 3.14159265358979f;
|
||||
|
||||
FreeCameraController() : mouse_speed(0.01f){
|
||||
_angle_x = _angle_y = 0.0f;
|
||||
}
|
||||
|
||||
virtual void tick(float deltaTime) override {
|
||||
auto mice = Input::Instance()->mice();
|
||||
auto mouse = mice[0];
|
||||
_angle_x += mouse_speed * mouse->GetDx();
|
||||
_angle_y += mouse_speed * mouse->GetDy();
|
||||
|
||||
_angle_y = clamp(_angle_y, -pi / 2.0f, pi / 2.0f);
|
||||
|
||||
auto rv = Quaternion_f::angleAxis(_angle_y, float3(1.0f, 0.0f, 0.0f));
|
||||
auto rh = Quaternion_f::angleAxis(_angle_x, float3(0.0f, 1.0f, 0.0f));
|
||||
|
||||
/* if (mouse->GetDx() != 0) {
|
||||
std::cout << "Mouse DX: " << mouse->GetDx() << std::endl;
|
||||
}
|
||||
if (mouse->GetDy() != 0) {
|
||||
std::cout << "Mouse DY: " << mouse->GetDy() << std::endl;
|
||||
}*/
|
||||
|
||||
gameObject->transform->SetLocalRotation(lm::mul(rh, rv));
|
||||
|
||||
|
||||
|
||||
auto keyboards = Input::Instance()->keyboards();
|
||||
auto keyboard = keyboards[0];
|
||||
|
||||
if(keyboard->GetKeyState(0x57)){
|
||||
gameObject->transform->SetLocalPosition(gameObject->transform->GetLocalPosition() + gameObject->transform->forward() * (deltaTime * move_speed));
|
||||
}
|
||||
|
||||
if (keyboard->GetKeyState(0x53)) {
|
||||
gameObject->transform->SetLocalPosition(gameObject->transform->GetLocalPosition() - gameObject->transform->forward() * (deltaTime * move_speed));
|
||||
}
|
||||
|
||||
if (keyboard->GetKeyState(0x41)) {
|
||||
gameObject->transform->SetLocalPosition(gameObject->transform->GetLocalPosition() - gameObject->transform->right() * (deltaTime * move_speed));
|
||||
}
|
||||
|
||||
if (keyboard->GetKeyState(0x44)) {
|
||||
gameObject->transform->SetLocalPosition(gameObject->transform->GetLocalPosition() + gameObject->transform->right() * (deltaTime * move_speed));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
private:
|
||||
float _angle_x;
|
||||
float _angle_y;
|
||||
};
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
#pragma once
|
||||
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
#define LF_INPUT
|
||||
@@ -0,0 +1,86 @@
|
||||
|
||||
add_subdirectory(RenderFramework/Vulkan)
|
||||
|
||||
add_library(RenderFramework STATIC)
|
||||
|
||||
target_sources(RenderFramework
|
||||
PRIVATE
|
||||
RenderFramework/Archive.cpp
|
||||
RenderFramework/Archive.h
|
||||
RenderFramework/Asset.h
|
||||
RenderFramework/AssetDatabase.cpp
|
||||
RenderFramework/AssetDatabase.h
|
||||
RenderFramework/AssetFile.h
|
||||
RenderFramework/AssetImporter.h
|
||||
RenderFramework/AssetManager.cpp
|
||||
RenderFramework/AssetManager.h
|
||||
RenderFramework/AssetMetadata.cpp
|
||||
RenderFramework/AssetMetadata.h
|
||||
RenderFramework/Bounds.h
|
||||
RenderFramework/Camera.cpp
|
||||
RenderFramework/Camera.h
|
||||
RenderFramework/Component.cpp
|
||||
RenderFramework/Component.h
|
||||
RenderFramework/FatalError.h
|
||||
RenderFramework/File.h
|
||||
RenderFramework/Filesystem.h
|
||||
RenderFramework/ForwardDeclarations.h
|
||||
RenderFramework/ForwardListItem.h
|
||||
RenderFramework/GameObject.cpp
|
||||
RenderFramework/GameObject.h
|
||||
RenderFramework/Gizmos.cpp
|
||||
RenderFramework/Gizmos.h
|
||||
RenderFramework/GizmoVertex.h
|
||||
RenderFramework/Guid.h
|
||||
RenderFramework/InputArchive.h
|
||||
RenderFramework/LEngine.cpp
|
||||
RenderFramework/LEngine.h
|
||||
RenderFramework/Material.cpp
|
||||
RenderFramework/Material.h
|
||||
RenderFramework/Mathf.h
|
||||
RenderFramework/Mesh.cpp
|
||||
RenderFramework/Mesh.h
|
||||
RenderFramework/MeshGenerator.cpp
|
||||
RenderFramework/MeshGenerator.h
|
||||
RenderFramework/MeshRenderer.cpp
|
||||
RenderFramework/MeshRenderer.h
|
||||
RenderFramework/ModelImporter.h
|
||||
RenderFramework/Object.cpp
|
||||
RenderFramework/Object.h
|
||||
RenderFramework/ObjectsFactory.cpp
|
||||
RenderFramework/ObjectsFactory.h
|
||||
RenderFramework/ObjMeshLoader.h
|
||||
RenderFramework/OutputArchive.h
|
||||
RenderFramework/Renderer.cpp
|
||||
RenderFramework/Renderer.h
|
||||
RenderFramework/RenderOutput.h
|
||||
RenderFramework/RenderTargetOutput.cpp
|
||||
RenderFramework/RenderTargetOutput.h
|
||||
RenderFramework/Scene.cpp
|
||||
RenderFramework/Scene.h
|
||||
RenderFramework/SceneRenderer.cpp
|
||||
RenderFramework/SceneRenderer.h
|
||||
RenderFramework/Shader.cpp
|
||||
RenderFramework/Shader.h
|
||||
RenderFramework/ShaderInclude.h
|
||||
RenderFramework/ShaderParametersBlock.h
|
||||
RenderFramework/Submesh.h
|
||||
RenderFramework/SwapChainOutput.h
|
||||
RenderFramework/Texture.h
|
||||
RenderFramework/TextureImporter.h
|
||||
RenderFramework/Transform.h
|
||||
RenderFramework/Vertex.h
|
||||
RenderFramework/WindowRenderOutput.cpp
|
||||
RenderFramework/WindowRenderOutput.h
|
||||
)
|
||||
|
||||
target_link_libraries(RenderFramework
|
||||
PUBLIC
|
||||
VulkanWrapper
|
||||
LMath
|
||||
)
|
||||
|
||||
target_include_directories(RenderFramework
|
||||
PUBLIC
|
||||
./
|
||||
)
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "Archive.h"
|
||||
|
||||
|
||||
|
||||
Archive& Archive::operator << (AssetFileLink& value) {
|
||||
serialize(&value, sizeof(value)); return *this;
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
#ifndef Archive_h__
|
||||
#define Archive_h__
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include "Asset.h"
|
||||
#include <lmath/lmath.h>
|
||||
#include <type_traits>
|
||||
#include "Object.h"
|
||||
#include <cstdint>
|
||||
#define POD_SERIALIZE(_type) Archive& operator << (_type& value) { serialize(&value, sizeof(value)); return *this; }
|
||||
|
||||
class Object;
|
||||
class Archive {
|
||||
public:
|
||||
typedef uint64_t size_type;
|
||||
std::function<void(std::shared_ptr<Object>)> SaveReferenceCallback;
|
||||
std::function<std::shared_ptr<Object>(const AssetFileLink&)> LoadReferenceCallback;
|
||||
|
||||
Archive():SaveReferenceCallback(nullptr){
|
||||
|
||||
}
|
||||
virtual ~Archive() {}
|
||||
virtual bool is_loading() const = 0;
|
||||
virtual void serialize(void* data, size_t size) = 0;
|
||||
|
||||
virtual size_type get_offset() const = 0;
|
||||
virtual void set_offset(size_type offset) = 0;
|
||||
|
||||
virtual void flush() {}
|
||||
|
||||
POD_SERIALIZE(uint8_t);
|
||||
POD_SERIALIZE(uint16_t);
|
||||
POD_SERIALIZE(uint32_t);
|
||||
POD_SERIALIZE(uint64_t);
|
||||
POD_SERIALIZE(int8_t);
|
||||
POD_SERIALIZE(int16_t);
|
||||
POD_SERIALIZE(int32_t);
|
||||
POD_SERIALIZE(int64_t);
|
||||
POD_SERIALIZE(float);
|
||||
POD_SERIALIZE(double);
|
||||
POD_SERIALIZE(lm::float2);
|
||||
POD_SERIALIZE(lm::float3);
|
||||
POD_SERIALIZE(lm::float4);
|
||||
POD_SERIALIZE(lm::double2);
|
||||
POD_SERIALIZE(lm::double3);
|
||||
POD_SERIALIZE(lm::double4);
|
||||
|
||||
POD_SERIALIZE(lm::Quaternion_f);
|
||||
|
||||
Archive& operator << (std::string& value) {
|
||||
uint32_t string_len = 0;
|
||||
if (is_loading()) {
|
||||
serialize(&string_len, sizeof(string_len));
|
||||
value.resize(string_len);
|
||||
serialize(&value[0], string_len);
|
||||
} else {
|
||||
string_len = value.size();
|
||||
serialize(&string_len, sizeof(string_len));
|
||||
serialize(&value[0], string_len);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Archive& operator << (std::wstring& value) {
|
||||
uint32_t string_len = 0;
|
||||
if (is_loading()) {
|
||||
serialize(&string_len, sizeof(string_len));
|
||||
value.resize(string_len);
|
||||
serialize(&value[0], string_len * sizeof(std::wstring::value_type));
|
||||
} else {
|
||||
string_len = value.size();
|
||||
serialize(&string_len, sizeof(string_len));
|
||||
serialize(&value[0], string_len * sizeof(std::wstring::value_type));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T, typename = typename std::enable_if<std::is_base_of<Object, T>::value>::type>
|
||||
Archive& operator << (std::shared_ptr<T>& value) {
|
||||
if (is_loading()) {
|
||||
AssetFileLink link;
|
||||
(*this) << link;
|
||||
if(link.localId == -1)
|
||||
{
|
||||
value = nullptr;
|
||||
}else
|
||||
{
|
||||
auto pos = get_offset();
|
||||
value = LoadReferenceCallback == nullptr ? nullptr : std::dynamic_pointer_cast<T>(LoadReferenceCallback(link));
|
||||
set_offset(pos);
|
||||
}
|
||||
|
||||
} else {
|
||||
if (SaveReferenceCallback != nullptr) {
|
||||
SaveReferenceCallback(value);
|
||||
}
|
||||
AssetFileLink link = value == nullptr ? AssetFileLink() : value->GetFileLink();
|
||||
(*this) << link;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
private:
|
||||
Archive& operator << (AssetFileLink& value);
|
||||
};
|
||||
#endif // Archive_h__
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef Asset_h__
|
||||
#define Asset_h__
|
||||
|
||||
#include <Windows.h>
|
||||
#include <cstdint>
|
||||
#include <array>
|
||||
#include <algorithm>
|
||||
#include "Guid.h"
|
||||
|
||||
class AssetFileLink {
|
||||
public:
|
||||
using LocalFileID = std::uint32_t;
|
||||
static constexpr LocalFileID InvalidID = -1;
|
||||
|
||||
Guid fileGuid;
|
||||
LocalFileID localId;
|
||||
|
||||
AssetFileLink():localId(InvalidID){}
|
||||
|
||||
AssetFileLink(const AssetFileLink& link) :fileGuid(link.fileGuid), localId(link.localId) {}
|
||||
|
||||
AssetFileLink(Guid _fileGuid, LocalFileID _localId) :fileGuid(_fileGuid), localId(_localId) {}
|
||||
|
||||
AssetFileLink& operator=(const AssetFileLink& link) {
|
||||
fileGuid = link.fileGuid;
|
||||
localId = link.localId;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const AssetFileLink& link) const {
|
||||
return (fileGuid == link.fileGuid) && (localId == link.localId);
|
||||
}
|
||||
|
||||
bool operator!=(const AssetFileLink& link) const {
|
||||
return (fileGuid != link.fileGuid) || (localId != link.localId);
|
||||
}
|
||||
|
||||
bool IsValid() const{
|
||||
return (localId != InvalidID) && !fileGuid.IsZero();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // Asset_h__
|
||||
@@ -0,0 +1,110 @@
|
||||
#include "AssetDatabase.h"
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include "ModelImporter.h"
|
||||
#include "TextureImporter.h"
|
||||
#include <algorithm>
|
||||
|
||||
AssetDatabase::AssetDatabase()
|
||||
{
|
||||
_importers.push_back(std::make_shared<ModelImporter>());
|
||||
//_importers.push_back(std::make_shared<TextureImporter>());
|
||||
}
|
||||
|
||||
void AssetDatabase::setRootPath(const std::wstring& path)
|
||||
{
|
||||
_rootAssetsPath = path;
|
||||
}
|
||||
|
||||
std::wstring AssetDatabase::getFullAssetPath(const std::string& localPathStr) {
|
||||
std::filesystem::path rootPath(_rootAssetsPath);
|
||||
std::filesystem::path localPath(localPathStr);
|
||||
auto fullPath = rootPath / localPath;
|
||||
auto result = fullPath.wstring();
|
||||
return result;
|
||||
}
|
||||
|
||||
std::wstring AssetDatabase::asset_to_meta_path(const std::wstring& asset_path)
|
||||
{
|
||||
std::filesystem::path src(asset_path);
|
||||
return src.parent_path().append(src.filename().wstring() + L".meta").wstring();
|
||||
}
|
||||
|
||||
void AssetDatabase::register_importer(std::shared_ptr<AssetImporter> importer)
|
||||
{
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<Object>> AssetDatabase::load_all_objects(std::shared_ptr<AssetFile> asset)
|
||||
{
|
||||
std::vector<std::shared_ptr<Object>> result;
|
||||
auto table = asset->objectsTable;
|
||||
for (auto& obj : table.objects)
|
||||
{
|
||||
result.push_back(LoadObject(AssetFileLink(asset->guid(), obj.localId)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
std::shared_ptr<AssetImporter> AssetDatabase::find_importer(const std::wstring& asset_path) const
|
||||
{
|
||||
auto importer = std::find_if(_importers.begin(), _importers.end(), [asset_path](std::shared_ptr<AssetImporter> im) { return im->can_import(asset_path); });
|
||||
if (importer != _importers.end())
|
||||
{
|
||||
return *importer;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Guid AssetDatabase::file_to_guid(const std::wstring& path) const
|
||||
{
|
||||
return AssetMetadata(path).get_guid();
|
||||
}
|
||||
|
||||
std::shared_ptr<AssetFile> AssetDatabase::open_asset_file(const std::wstring& path)
|
||||
{
|
||||
return std::make_shared<AssetFile>(file_to_guid(path), path);
|
||||
}
|
||||
|
||||
std::shared_ptr<Object> AssetDatabase::LoadReference(const AssetFileLink& link)
|
||||
{
|
||||
return LoadObject(link);
|
||||
}
|
||||
|
||||
|
||||
std::wstring guid_to_path(Guid guid)
|
||||
{//TODO: fix hardcode
|
||||
return LR"(C:\Users\Sergey\Desktop\Assets\test.asset)";
|
||||
}
|
||||
|
||||
|
||||
std::shared_ptr<AssetFile> AssetDatabase::OpenArchive(Guid guid)
|
||||
{ //TODO: fix hardcode
|
||||
if (open_archives.empty())
|
||||
{
|
||||
auto a = std::make_shared<AssetFile>(guid, guid_to_path(guid));
|
||||
a->input->LoadReferenceCallback = std::bind(&AssetDatabase::LoadReference, this, std::placeholders::_1);
|
||||
open_archives.push_back(a);
|
||||
}
|
||||
return open_archives[0];
|
||||
}
|
||||
|
||||
std::shared_ptr<Object> AssetDatabase::LoadObject(AssetFileLink link)
|
||||
{
|
||||
auto f = Object::Find(link);
|
||||
if (f != nullptr) { return f; }
|
||||
|
||||
auto archive = OpenArchive(link.fileGuid);
|
||||
archive->SeekToFile(link.localId);
|
||||
|
||||
std::wstring type_name;
|
||||
(*archive->input) << type_name;
|
||||
|
||||
auto obj = ObjectsFactory::instance().create_object(type_name);
|
||||
obj->SetLink(link);
|
||||
|
||||
obj->serialize(*archive->input);
|
||||
|
||||
|
||||
return obj;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef AssetDatabase_h__
|
||||
#define AssetDatabase_h__
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "AssetMetadata.h"
|
||||
#include "AssetImporter.h"
|
||||
#include "AssetFile.h"
|
||||
|
||||
|
||||
class AssetDatabase{
|
||||
public:
|
||||
static AssetDatabase& get() {
|
||||
static AssetDatabase instance;
|
||||
return instance;
|
||||
}
|
||||
static std::wstring asset_to_meta_path(const std::wstring& asset_path);
|
||||
Guid file_to_guid(const std::wstring& path) const;
|
||||
std::shared_ptr<AssetFile> open_asset_file(const std::wstring& path);
|
||||
void setRootPath(const std::wstring& path);
|
||||
static void register_importer(std::shared_ptr<AssetImporter> importer);
|
||||
std::shared_ptr<AssetImporter> find_importer(const std::wstring& asset_path) const;
|
||||
|
||||
std::shared_ptr<Object> LoadReference(const AssetFileLink& link);
|
||||
std::shared_ptr<AssetFile> OpenArchive(Guid guid);
|
||||
std::shared_ptr<Object> LoadObject(AssetFileLink link);
|
||||
|
||||
std::vector<std::shared_ptr<Object>> load_all_objects(std::shared_ptr<AssetFile> asset);
|
||||
|
||||
std::wstring getFullAssetPath(const std::string& localPath);
|
||||
private:
|
||||
std::vector<std::shared_ptr<AssetFile>> open_archives;
|
||||
AssetDatabase();
|
||||
std::vector<std::shared_ptr<AssetImporter>> _importers;
|
||||
std::wstring _rootAssetsPath;
|
||||
};
|
||||
#endif // AssetDatabase_h__
|
||||
@@ -0,0 +1,226 @@
|
||||
#ifndef AssetFile_h__
|
||||
#define AssetFile_h__
|
||||
|
||||
#include "InputArchive.h"
|
||||
#include "OutputArchive.h"
|
||||
#include "Asset.h"
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = std::experimental::filesystem::v1;
|
||||
|
||||
class AssetObjectDescriptor : public Object {
|
||||
public:
|
||||
uint64_t localId;
|
||||
int64_t offset;
|
||||
uint64_t length;
|
||||
AssetObjectDescriptor():localId(0), offset(0), length(0)
|
||||
{
|
||||
|
||||
}
|
||||
AssetObjectDescriptor(uint64_t _localId, int64_t _offset, uint64_t _length):localId(_localId), offset(_offset), length(_length){
|
||||
|
||||
}
|
||||
virtual void serialize(Archive& archive) override {
|
||||
archive << localId << offset << length;
|
||||
}
|
||||
virtual std::wstring get_type_name() const override {
|
||||
return L"AssetObjectDescriptor";
|
||||
}
|
||||
};
|
||||
|
||||
class AssetObjectsTable : public Object{
|
||||
public:
|
||||
uint64_t lastId;
|
||||
std::vector<AssetObjectDescriptor> objects;
|
||||
|
||||
AssetObjectsTable():lastId(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool GetFileDescriptor(uint64_t localId, AssetObjectDescriptor& desc){
|
||||
for (auto& o : objects) {
|
||||
if (o.localId == localId) {
|
||||
desc = o;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Contains(uint64_t localId) const {
|
||||
for (auto& o : objects) {
|
||||
if (o.localId == localId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual void serialize(Archive& archive) override {
|
||||
archive << lastId;
|
||||
if(archive.is_loading()){
|
||||
uint64_t objectsCount = 0;
|
||||
archive << objectsCount;
|
||||
objects.clear();
|
||||
for(uint64_t i = 0; i < objectsCount; ++i){
|
||||
AssetObjectDescriptor d;
|
||||
d.serialize(archive);
|
||||
objects.push_back(d);
|
||||
}
|
||||
}else{
|
||||
uint64_t objectsCount = objects.size();
|
||||
archive << objectsCount;
|
||||
for (auto& o : objects){
|
||||
o.serialize(archive);
|
||||
}
|
||||
}
|
||||
}
|
||||
virtual std::wstring get_type_name() const override {
|
||||
return L"AssetObjectsTable";
|
||||
}
|
||||
};
|
||||
|
||||
inline std::shared_ptr<OutputArchive> CreateMemoryOutArchive(){
|
||||
return std::make_shared<OutputArchive>(std::make_shared<std::stringstream>(std::ios::in | std::ios::out | std::ios::binary));
|
||||
}
|
||||
|
||||
class AssetFile {
|
||||
public:
|
||||
typedef uint64_t table_offset_t;
|
||||
AssetObjectsTable objectsTable;
|
||||
std::shared_ptr<InputArchive> input;
|
||||
std::shared_ptr<OutputArchive> output;
|
||||
std::shared_ptr<std::fstream> stream;
|
||||
|
||||
AssetFile(Guid fileGuid, const std::wstring& path):_fileGuid(fileGuid), _path(path){
|
||||
OpenFile(false);
|
||||
|
||||
if(stream->is_open()){
|
||||
stream->seekg(0, std::ios::end);
|
||||
if((int)stream->tellg() == 0){
|
||||
std::cout << "Empty asset created!" << std::endl;
|
||||
SaveFooter();
|
||||
}
|
||||
ReadFooter();
|
||||
}
|
||||
//input = std::make_shared<InputArchive>(path);
|
||||
}
|
||||
|
||||
void OpenFile(bool trunc){
|
||||
input = nullptr;
|
||||
output = nullptr;
|
||||
stream = std::make_shared<std::fstream>(_path, (trunc ? std::ios::trunc : std::ios::app) | std::ios::in | std::ios::out | std::ios::binary);
|
||||
if (stream->is_open()) {
|
||||
input = std::make_shared<InputArchive>(stream);
|
||||
output = std::make_shared<OutputArchive>(stream);
|
||||
}
|
||||
}
|
||||
|
||||
void CloseFile()
|
||||
{
|
||||
input = nullptr;
|
||||
output = nullptr;
|
||||
if(stream != nullptr && stream->is_open()){
|
||||
stream->close();
|
||||
}
|
||||
stream = nullptr;
|
||||
}
|
||||
|
||||
void Save(){
|
||||
auto tmpOut = CreateMemoryOutArchive();
|
||||
//save objects
|
||||
int64_t objectOffset = 0;
|
||||
for(auto& v : objectsTable.objects){
|
||||
auto object = Object::Find(AssetFileLink(_fileGuid, v.localId));
|
||||
|
||||
v.offset = objectOffset;
|
||||
|
||||
auto startOffset = objectOffset = tmpOut->GetStream()->tellp();
|
||||
//save all loaded objects, copy not loaded
|
||||
if(object != nullptr){
|
||||
auto typeName = object->get_type_name();
|
||||
(*tmpOut) << typeName;
|
||||
object->serialize(*tmpOut);
|
||||
}else{
|
||||
stream->seekg(v.offset, std::ios::beg);
|
||||
std::vector<uint8_t> objectBytes(v.length);
|
||||
stream->read((char*)&objectBytes[0], objectBytes.size());
|
||||
tmpOut->GetStream()->write((char*)&objectBytes[0], objectBytes.size());
|
||||
}
|
||||
objectOffset = tmpOut->GetStream()->tellp();
|
||||
v.length = objectOffset - startOffset;
|
||||
}
|
||||
|
||||
CloseFile();
|
||||
OpenFile(true);
|
||||
|
||||
auto content = std::static_pointer_cast<std::stringstream>(tmpOut->GetStream())->str();
|
||||
stream->write(&content[0], content.size());
|
||||
stream->seekg(0, std::ios::end);
|
||||
//save footer
|
||||
SaveFooter();
|
||||
}
|
||||
|
||||
bool SeekToFile(uint64_t fileId) {
|
||||
AssetObjectDescriptor desc;
|
||||
if (objectsTable.GetFileDescriptor(fileId, desc)) {
|
||||
if (desc.offset >= 0) {
|
||||
stream->seekg(desc.offset, std::ios::beg);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ReadFooter()
|
||||
{
|
||||
std::cout << "Read footer" << std::endl;
|
||||
//read table offset
|
||||
auto off = -((std::streamoff)sizeof(table_offset_t));
|
||||
stream->seekg(off, std::ios::end);
|
||||
table_offset_t offset = 0;
|
||||
(*input) << offset;
|
||||
|
||||
auto table_file_offset = off - (std::streamoff)offset;
|
||||
stream->seekg(table_file_offset, std::ios::end);
|
||||
objectsTable.serialize(*input);
|
||||
}
|
||||
|
||||
void SaveFooter(){
|
||||
std::cout << "Save footer" << std::endl;
|
||||
stream->seekg(0, std::ios::end);
|
||||
auto tmpArchive = CreateMemoryOutArchive();
|
||||
objectsTable.serialize(*tmpArchive);
|
||||
auto data = std::static_pointer_cast<std::stringstream>(tmpArchive->GetStream())->str();
|
||||
table_offset_t size = data.size();
|
||||
stream->write(&data[0], size);
|
||||
(*output) << size;
|
||||
|
||||
output->flush();
|
||||
}
|
||||
|
||||
void AddObject(std::shared_ptr<Object> obj){
|
||||
if (obj->GetFileLink().IsValid()) {
|
||||
if (obj->GetFileLink().fileGuid == _fileGuid) {
|
||||
std::wcout << "Object " << obj->get_name() << " is already in current asset. Can't add twice" << std::endl;
|
||||
} else {
|
||||
std::wcout << "Object " << obj->get_name() << " is already in another asset. Can't add." << std::endl;
|
||||
}
|
||||
} else {
|
||||
obj->SetLink(AssetFileLink(_fileGuid, objectsTable.lastId++));
|
||||
objectsTable.objects.push_back(AssetObjectDescriptor(obj->GetFileLink().localId, 0, 0));
|
||||
}
|
||||
}
|
||||
const Guid& guid() const
|
||||
{
|
||||
return _fileGuid;
|
||||
}
|
||||
private:
|
||||
Guid _fileGuid;
|
||||
std::wstring _path;
|
||||
};
|
||||
|
||||
#endif // AssetFile_h__
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
class AssetImporter {
|
||||
public:
|
||||
virtual void import(std::wstring asset_path) = 0;
|
||||
virtual bool can_import(const std::wstring& path) const = 0;
|
||||
virtual bool set_parameters(std::wstring params) = 0;
|
||||
virtual std::wstring get_parameters() const = 0;
|
||||
virtual std::wstring name() = 0;
|
||||
};
|
||||
@@ -0,0 +1,73 @@
|
||||
#include "AssetManager.h"
|
||||
#include "LEngine.h"
|
||||
#include "Texture.h"
|
||||
|
||||
std::shared_ptr<AssetManager> AssetManager::_instance = nullptr;
|
||||
|
||||
TexturePtr AssetManager::LoadTexture(const std::wstring& path) const
|
||||
{
|
||||
return {};
|
||||
/*
|
||||
auto renderer = LEngine::Instance()->GetRenderer();
|
||||
auto device = renderer->GetDevice();
|
||||
|
||||
ID3D12ResourcePtr resource;
|
||||
|
||||
std::unique_ptr<uint8_t[]> data;
|
||||
std::vector<D3D12_SUBRESOURCE_DATA> srData;
|
||||
DirectX::LoadDDSTextureFromFile(device.Get(), GetFullAssetPath(path).c_str(), &resource, data, srData);
|
||||
|
||||
//auto cmdList = renderer->BeginCommandList();
|
||||
|
||||
|
||||
//create upload heap
|
||||
D3D12_RESOURCE_DESC desc;
|
||||
desc = resource->GetDesc();
|
||||
|
||||
UINT64 textureUploadBufferSize;
|
||||
device->GetCopyableFootprints(&desc, 0, srData.size(), 0, nullptr, nullptr, nullptr, &textureUploadBufferSize);
|
||||
|
||||
// now we create an upload heap to upload our texture to the GPU
|
||||
ID3D12ResourcePtr textureBufferUploadHeap;
|
||||
auto hr = device->CreateCommittedResource(
|
||||
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD), // upload heap
|
||||
D3D12_HEAP_FLAG_NONE, // no flags
|
||||
&CD3DX12_RESOURCE_DESC::Buffer(textureUploadBufferSize), // resource description for a buffer (storing the image data in this heap just to copy to the default heap)
|
||||
D3D12_RESOURCE_STATE_GENERIC_READ, // We will copy the contents from this heap to the default heap above
|
||||
nullptr,
|
||||
IID_PPV_ARGS(&textureBufferUploadHeap));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
FatalError(L"Failed to create upload heap");
|
||||
}
|
||||
textureBufferUploadHeap->SetName(L"Texture Buffer Upload Resource Heap");
|
||||
|
||||
auto commandList = renderer->BeginCommandList();
|
||||
//copy subresource
|
||||
UpdateSubresources(commandList.Get(), resource.Get(), textureBufferUploadHeap.Get(), 0, 0, srData.size(), &srData[0]);
|
||||
commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(resource.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));
|
||||
|
||||
|
||||
renderer->EndCommandList();
|
||||
renderer->WaitCommandsQueue();
|
||||
|
||||
//create shader resource view
|
||||
auto srvMemory = renderer->getSrvDescriptorsAllocator()->alloc();
|
||||
|
||||
D3D12_RESOURCE_DESC texDesc;
|
||||
texDesc = resource->GetDesc();
|
||||
|
||||
|
||||
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
|
||||
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
|
||||
srvDesc.Format = texDesc.Format;
|
||||
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
|
||||
srvDesc.Texture2D.MipLevels = texDesc.MipLevels;
|
||||
device->CreateShaderResourceView(resource.Get(), &srvDesc, srvMemory);
|
||||
|
||||
|
||||
auto result = std::make_shared<Texture>(resource, srvMemory);
|
||||
|
||||
//DirectX::CreateTexture(renderer->GetDevice(), image.GetImages(), image.GetImageCount(), metadata, &resource);
|
||||
return result;*/
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "Texture.h"
|
||||
#include "ForwardDeclarations.h"
|
||||
|
||||
class AssetManager {
|
||||
public:
|
||||
static std::shared_ptr<AssetManager> Instance() {
|
||||
if (_instance == nullptr) {
|
||||
_instance = std::make_shared<AssetManager>();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
|
||||
void SetAssetsFolder(const std::wstring& folder) {
|
||||
_assetsFolder = folder;
|
||||
}
|
||||
|
||||
std::wstring GetFullAssetPath(const std::wstring& relativePath) const
|
||||
{
|
||||
return _assetsFolder + relativePath;
|
||||
}
|
||||
TexturePtr LoadTexture(const std::wstring& path) const;
|
||||
private:
|
||||
static std::shared_ptr<AssetManager> _instance;
|
||||
std::wstring _assetsFolder;
|
||||
};
|
||||
@@ -0,0 +1,75 @@
|
||||
#include "AssetMetadata.h"
|
||||
#include <fstream>
|
||||
#include <combaseapi.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include "AssetDatabase.h"
|
||||
#include <Windows.h>
|
||||
#include <wrl.h>
|
||||
using namespace Microsoft::WRL;
|
||||
|
||||
AssetMetadata::AssetMetadata(const std::wstring& asset_path) : _asset_path(asset_path) {
|
||||
_meta_path = AssetDatabase::asset_to_meta_path(_asset_path);
|
||||
|
||||
if (std::filesystem::exists(std::filesystem::path(_meta_path))) {
|
||||
Load();
|
||||
} else {
|
||||
create();
|
||||
Save();
|
||||
}
|
||||
}
|
||||
|
||||
void AssetMetadata::create() {
|
||||
CoCreateGuid((GUID*)&_guid);
|
||||
_modification_time = std::filesystem::last_write_time(std::filesystem::path(_asset_path)).time_since_epoch().count();
|
||||
_importer = AssetDatabase::get().find_importer(_asset_path);
|
||||
}
|
||||
|
||||
void AssetMetadata::Load() {
|
||||
std::wifstream file(_meta_path);
|
||||
|
||||
std::wstring v;
|
||||
std::getline(file, v);
|
||||
|
||||
std::wstring guid_str;
|
||||
std::getline(file, guid_str);
|
||||
_guid = str_to_guid(guid_str);
|
||||
|
||||
std::wstring time_str;
|
||||
std::getline(file, time_str);
|
||||
_modification_time = std::stoull(time_str);
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
void AssetMetadata::Save() {
|
||||
std::wofstream file;
|
||||
file.open(_meta_path, std::ios::trunc);
|
||||
file << version << std::endl;
|
||||
file << guid_to_str(_guid) << std::endl;
|
||||
file << _modification_time << std::endl;
|
||||
//file << _importer->name();
|
||||
file.close();
|
||||
}
|
||||
|
||||
Guid AssetMetadata::str_to_guid(const std::wstring& guid) {
|
||||
Guid iid;
|
||||
IIDFromString(guid.c_str(), (GUID*)&iid);
|
||||
return iid;
|
||||
}
|
||||
|
||||
std::wstring AssetMetadata::guid_to_str(const Guid& guid) {
|
||||
LPOLESTR guid_str = nullptr;
|
||||
StringFromIID(*((const GUID*)&guid), &guid_str);
|
||||
|
||||
//Microsoft::WRL::Wrappers::HStringReference
|
||||
|
||||
std::wstring result(guid_str);
|
||||
CoTaskMemFree(guid_str);
|
||||
return result;
|
||||
}
|
||||
|
||||
const Guid& AssetMetadata::get_guid() const {
|
||||
return _guid;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef AssetMetadata_h__
|
||||
#define AssetMetadata_h__
|
||||
|
||||
#include <filesystem>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include "Guid.h"
|
||||
#include "AssetImporter.h"
|
||||
|
||||
class AssetMetadata {
|
||||
public:
|
||||
typedef uint64_t file_time_t;
|
||||
const std::wstring version = L"1.0";
|
||||
|
||||
AssetMetadata(const std::wstring& asset_path);
|
||||
void Save();
|
||||
void Load();
|
||||
static std::wstring guid_to_str(const Guid& guid);
|
||||
static Guid str_to_guid(const std::wstring& guid);
|
||||
const Guid& get_guid() const;
|
||||
private:
|
||||
std::shared_ptr<AssetImporter> _importer;
|
||||
void create();
|
||||
std::wstring _asset_path;
|
||||
std::wstring _meta_path;
|
||||
file_time_t _modification_time;
|
||||
Guid _guid;
|
||||
};
|
||||
#endif // AssetMetadata_h__
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef Bounds_h__
|
||||
#define Bounds_h__
|
||||
|
||||
#include <lmath/lmath.h>
|
||||
using namespace lm;
|
||||
|
||||
class Bounds {
|
||||
public:
|
||||
Bounds()
|
||||
:_center(0.0f,0.0f,0.0f), _size(0.0f,0.0f,0.0f) {
|
||||
|
||||
}
|
||||
Bounds(float3 center, float3 size)
|
||||
:_center(center), _size(size)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
float3 GetSize() {
|
||||
return _size;
|
||||
}
|
||||
float3 GetCenter() {
|
||||
return _center;
|
||||
}
|
||||
void SetSize(const float3& size) {
|
||||
_size = size;
|
||||
}
|
||||
void SetCenter(const float3& center) {
|
||||
_center = center;
|
||||
}
|
||||
void Scale(float value) {
|
||||
_size *= value;
|
||||
}
|
||||
float3 Minimum() const {
|
||||
return _size * -0.5f + _center;
|
||||
}
|
||||
float3 Maximum() const {
|
||||
return _size * 0.5f + _center;
|
||||
}
|
||||
private:
|
||||
float3 _center;
|
||||
float3 _size;
|
||||
};
|
||||
#endif // Bounds_h__
|
||||
@@ -0,0 +1,31 @@
|
||||
#include "Camera.h"
|
||||
#include "Transform.h"
|
||||
#include "Mathf.h"
|
||||
#include "Archive.h"
|
||||
|
||||
float4x4 Camera::worldToCameraMatrix() {
|
||||
auto world = transform()->GetGlobalTransform();
|
||||
|
||||
//TODO: why not affine ?
|
||||
world = inverse(world, false);
|
||||
|
||||
// auto test = lookatlh(transform()->GetGlobalPosition(), float3(0, 1, 1), float3(0, 1, 0));
|
||||
|
||||
return world;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Camera::serialize(Archive& archive) {
|
||||
Component::serialize(archive);
|
||||
archive << nearClipPlane << farClipPlane << fov << aspect;
|
||||
}
|
||||
|
||||
void Camera::UpdateProjectionMatrix() {
|
||||
_projection = matrix4x4Perspective(fov * Mathf::DegToRad(), aspect, nearClipPlane, farClipPlane);
|
||||
}
|
||||
|
||||
float4x4 Camera::projectionMatrix() {
|
||||
UpdateProjectionMatrix();
|
||||
return _projection;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef Camera_h__
|
||||
#define Camera_h__
|
||||
|
||||
#include "ObjectsFactory.h"
|
||||
#include <lmath/lmath.h>
|
||||
#include "Component.h"
|
||||
using namespace lm;
|
||||
|
||||
LOBJECT(Camera, Component)
|
||||
public:
|
||||
float nearClipPlane = 0.03f;
|
||||
float farClipPlane = 100.0f;
|
||||
float fov = 45.0f;
|
||||
float aspect = 1.333333333f;
|
||||
|
||||
virtual void serialize(Archive& archive) override;
|
||||
|
||||
float4x4 worldToCameraMatrix();
|
||||
float4x4 projectionMatrix();
|
||||
void test() {
|
||||
construct();
|
||||
}
|
||||
private:
|
||||
float4x4 _projection;
|
||||
void UpdateProjectionMatrix();
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<Camera> CameraPtr;
|
||||
#endif // Camera_h__
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "Component.h"
|
||||
#include "GameObject.h"
|
||||
|
||||
TransformPtr Component::transform() {
|
||||
return gameObject->transform;
|
||||
}
|
||||
|
||||
void Component::serialize(Archive& archive) {
|
||||
auto ptr = std::static_pointer_cast<Object>(gameObject);
|
||||
archive << ptr;
|
||||
|
||||
if (archive.is_loading()) {
|
||||
gameObject = std::static_pointer_cast<GameObject>(ptr);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef Component_h__
|
||||
#define Component_h__
|
||||
|
||||
#include <memory>
|
||||
#include "ObjectsFactory.h"
|
||||
|
||||
LOBJECT(Component, Object)
|
||||
public:
|
||||
GameObjectPtr gameObject;
|
||||
TransformPtr transform();
|
||||
virtual void tick(float deltaTime) {}
|
||||
Component(){}
|
||||
|
||||
void serialize(Archive& archive) override;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // Component_h__
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef FatalError_h__
|
||||
#define FatalError_h__
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
inline void FatalError(const TCHAR* message) {
|
||||
int b1 = 12750;
|
||||
int b2 = 4750;
|
||||
int len = 80;
|
||||
|
||||
for(int i = 0; i < 2; ++i) {
|
||||
Beep(b1, len);
|
||||
Beep(b2, len);
|
||||
}
|
||||
|
||||
MessageBox(NULL, message, TEXT("Error"), MB_OK);
|
||||
exit(0);
|
||||
}
|
||||
#endif // FatalError_h__
|
||||
@@ -0,0 +1,71 @@
|
||||
#ifndef File_h__
|
||||
#define File_h__
|
||||
|
||||
#include <fstream>
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
|
||||
uint32_t FileSize(const std::wstring& path) {
|
||||
std::ifstream file(path, std::ios_base::binary);
|
||||
if (!file.good()) {
|
||||
return 0;
|
||||
}
|
||||
std::streampos current_position = file.tellg();
|
||||
file.seekg(0, std::ios::end);
|
||||
std::streampos ending_position = file.tellg();
|
||||
file.seekg(current_position);
|
||||
file.close();
|
||||
return (uint32_t)(ending_position - current_position);
|
||||
}
|
||||
|
||||
uint32_t FileSize(const std::string& path) {
|
||||
std::ifstream file(path, std::ios_base::binary);
|
||||
if (!file.good()) {
|
||||
return 0;
|
||||
}
|
||||
std::streampos current_position = file.tellg();
|
||||
file.seekg(0, std::ios::end);
|
||||
std::streampos ending_position = file.tellg();
|
||||
file.seekg(current_position);
|
||||
file.close();
|
||||
return (uint32_t)(ending_position - current_position);
|
||||
}
|
||||
|
||||
void FileReadAllText(const std::wstring& path, std::string& result) {
|
||||
auto size = FileSize(path);
|
||||
if (size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::ifstream file(path, std::ios::in);
|
||||
result.reserve(size);
|
||||
result.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
|
||||
file.close();
|
||||
}
|
||||
|
||||
void FileReadAllBytes(const std::wstring& path, std::vector<char>& result) {
|
||||
auto size = FileSize(path);
|
||||
if (size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::ifstream file(path, std::ios::in | std::ios::binary);
|
||||
result.reserve(size);
|
||||
result.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
|
||||
file.close();
|
||||
}
|
||||
|
||||
void FileReadAllBytes(const std::string& path, std::vector<char>& result) {
|
||||
auto size = FileSize(path);
|
||||
if (size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::ifstream file(path, std::ios::in | std::ios::binary);
|
||||
result.reserve(size);
|
||||
result.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
|
||||
file.close();
|
||||
}
|
||||
|
||||
|
||||
#endif // File_h__
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef Filesystem_h__
|
||||
#define Filesystem_h__
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class Filesystem {
|
||||
public:
|
||||
//static std::shared_ptr<Filesystem> Create(const std::string& assetsFolder);
|
||||
|
||||
private:
|
||||
//Filesystem();
|
||||
};
|
||||
#endif // Filesystem_h__
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <lmath/lmath.h>
|
||||
|
||||
class GameObject;
|
||||
typedef std::shared_ptr<GameObject> GameObjectPtr;
|
||||
|
||||
class Component;
|
||||
typedef std::shared_ptr<Component> ComponentPtr;
|
||||
|
||||
class Scene;
|
||||
typedef std::shared_ptr<Scene> ScenePtr;
|
||||
|
||||
class Transform;
|
||||
typedef std::shared_ptr<Transform> TransformPtr;
|
||||
|
||||
class Shader;
|
||||
typedef std::shared_ptr<Shader> ShaderPtr;
|
||||
|
||||
class Mesh;
|
||||
typedef std::shared_ptr<Mesh> MeshPtr;
|
||||
|
||||
class Material;
|
||||
typedef std::shared_ptr<Material> MaterialPtr;
|
||||
|
||||
class Mouse;
|
||||
typedef std::shared_ptr<Mouse> MousePtr;
|
||||
|
||||
class Keyboard;
|
||||
typedef std::shared_ptr<Keyboard> KeyboardPtr;
|
||||
|
||||
class Texture;
|
||||
typedef std::shared_ptr<Texture> TexturePtr;
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef ForwardListItem_h__
|
||||
#define ForwardListItem_h__
|
||||
|
||||
class ForwardListItem{
|
||||
public:
|
||||
ForwardListItem* next;
|
||||
};
|
||||
#endif // ForwardListItem_h__
|
||||
@@ -0,0 +1,18 @@
|
||||
|
||||
#include "Transform.h"
|
||||
#include "LEngine.h"
|
||||
#include "Component.h"
|
||||
#include "GameObject.h"
|
||||
|
||||
void GameObject::tick(float deltaTime) {
|
||||
for (auto c : components) {
|
||||
c->tick(deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
void GameObject::construct2() {
|
||||
Object::construct();
|
||||
auto this_ptr = std::static_pointer_cast<GameObject>(shared_from_this());
|
||||
LEngine::Instance()->GetActiveScene()->objects.push_back(this_ptr);
|
||||
this_ptr->transform = this_ptr->AddComponent<Transform>();
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
#ifndef GameObject___h__
|
||||
#define GameObject___h__
|
||||
|
||||
#include "ForwardDeclarations.h"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <typeinfo>
|
||||
#include "ObjectsFactory.h"
|
||||
#include "OutputArchive.h"
|
||||
#include "Transform.h"
|
||||
|
||||
LOBJECT(GameObject, Object)
|
||||
public:
|
||||
TransformPtr transform;
|
||||
|
||||
void tick(float deltaTime);
|
||||
|
||||
virtual void serialize(Archive& archive) override {
|
||||
Object::serialize(archive);
|
||||
|
||||
if (archive.is_loading()) {
|
||||
components.clear();
|
||||
transform = nullptr;
|
||||
}
|
||||
|
||||
Archive::size_type cnt = components.size();
|
||||
archive << cnt;
|
||||
|
||||
if (archive.is_loading()) {
|
||||
for (Archive::size_type i = 0; i < cnt; ++i) {
|
||||
std::shared_ptr<Object> obj;
|
||||
archive << obj;
|
||||
components.push_back(std::static_pointer_cast<Component>(obj));
|
||||
|
||||
auto tp = std::dynamic_pointer_cast<Transform>(obj);
|
||||
if (tp != nullptr) {
|
||||
transform = tp;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (auto component : components) {
|
||||
auto ptr = std::static_pointer_cast<Object>(component);
|
||||
archive << ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<GameObject> as_game_object() {
|
||||
return std::static_pointer_cast<GameObject>(shared_from_this());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::shared_ptr<T> AddComponent() {
|
||||
auto component = T::create();
|
||||
component->gameObject = as_game_object();
|
||||
components.push_back(component);
|
||||
return component;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::shared_ptr<T> GetComponent() {
|
||||
for (auto c : components) {
|
||||
auto tmp = std::dynamic_pointer_cast<T>(c);
|
||||
if (tmp != nullptr) {
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::vector<std::shared_ptr<T>> GetComponents() {
|
||||
std::vector<std::shared_ptr<T>> result;
|
||||
for (auto c : components) {
|
||||
auto tmp = std::dynamic_pointer_cast<T>(c);
|
||||
if (tmp != nullptr) {
|
||||
result.push_back(tmp);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void construct() override {
|
||||
construct2();
|
||||
}
|
||||
void construct2();
|
||||
|
||||
private:
|
||||
GameObject() {}
|
||||
std::vector<ComponentPtr> components;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // GameObject___h__
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef GizmoVertex_h__
|
||||
#define GizmoVertex_h__
|
||||
|
||||
#include <lmath/lmath.h>
|
||||
|
||||
using namespace lm;
|
||||
|
||||
struct GizmoVertex {
|
||||
float4 position;
|
||||
float4 color;
|
||||
|
||||
GizmoVertex(float4 _position, float4 _color)
|
||||
:position(_position), color(_color)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
GizmoVertex(float3 _position, float4 _color)
|
||||
:position(_position.x(), _position.y(), _position.z(), 1.0f), color(_color)
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
#endif // GizmoVertex_h__
|
||||
@@ -0,0 +1,130 @@
|
||||
#include "Gizmos.h"
|
||||
#include "GameObject.h"
|
||||
#include "LEngine.h"
|
||||
#include "LEngine.h"
|
||||
|
||||
Gizmos::Gizmos()
|
||||
:_color(0.0f, 1.0f, 0.0f, 1.0f)
|
||||
{
|
||||
_matrix = float4x4::identity();
|
||||
}
|
||||
void Gizmos::SetMatrix(const float4x4& matrix) {
|
||||
_matrix = matrix;
|
||||
}
|
||||
float4x4 Gizmos::GetMatrix() {
|
||||
return _matrix;
|
||||
}
|
||||
void Gizmos::DrawBounds(const Bounds& bounds) {
|
||||
auto b = bounds;
|
||||
b.Scale(1.01f);
|
||||
auto bMin = b.Minimum();
|
||||
auto bMax = b.Maximum();
|
||||
|
||||
DrawLine(float3(bMin.x(), bMax.y(), bMax.z()), float3(bMax.x(), bMax.y(), bMax.z()));
|
||||
DrawLine(float3(bMin.x(), bMax.y(), bMin.z()), float3(bMax.x(), bMax.y(), bMin.z()));
|
||||
DrawLine(float3(bMin.x(), bMax.y(), bMax.z()), float3(bMin.x(), bMax.y(), bMin.z()));
|
||||
DrawLine(float3(bMax.x(), bMax.y(), bMax.z()), float3(bMax.x(), bMax.y(), bMin.z()));
|
||||
|
||||
DrawLine(float3(bMin.x(), bMin.y(), bMax.z()), float3(bMax.x(), bMin.y(), bMax.z()));
|
||||
DrawLine(float3(bMin.x(), bMin.y(), bMin.z()), float3(bMax.x(), bMin.y(), bMin.z()));
|
||||
DrawLine(float3(bMin.x(), bMin.y(), bMax.z()), float3(bMin.x(), bMin.y(), bMin.z()));
|
||||
DrawLine(float3(bMax.x(), bMin.y(), bMax.z()), float3(bMax.x(), bMin.y(), bMin.z()));
|
||||
|
||||
DrawLine(float3(bMin.x(), bMin.y(), bMin.z()), float3(bMin.x(), bMax.y(), bMin.z()));
|
||||
DrawLine(float3(bMax.x(), bMin.y(), bMin.z()), float3(bMax.x(), bMax.y(), bMin.z()));
|
||||
|
||||
DrawLine(float3(bMin.x(), bMin.y(), bMax.z()), float3(bMin.x(), bMax.y(), bMax.z()));
|
||||
DrawLine(float3(bMax.x(), bMin.y(), bMax.z()), float3(bMax.x(), bMax.y(), bMax.z()));
|
||||
|
||||
}
|
||||
void Gizmos::Init() {
|
||||
/*_backMaterial = Material::Create(Shader::Create(L"shaders/gizmos.fx"), "gizmo_back");
|
||||
|
||||
_frontMaterial = Material::Create(_backMaterial->GetShader(), "gizmo_front");
|
||||
|
||||
_backMaterial->GetParametersBlock().SetValue("alphaMul", 0.2f);
|
||||
_frontMaterial->GetParametersBlock().SetValue("alphaMul", 1.0f);
|
||||
|
||||
D3D11_INPUT_ELEMENT_DESC ied[] = {
|
||||
{ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
||||
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
||||
};
|
||||
|
||||
auto vs = _backMaterial->GetShader()->GetShader(ShaderType::Vs);
|
||||
auto device = LEngine::Instance()->GetRenderer()->GetDevice();
|
||||
auto hr = device->CreateInputLayout(ied, 2, vs->blob->GetBufferPointer(), vs->blob->GetBufferSize(), &_inputLayout);
|
||||
|
||||
D3D11_DEPTH_STENCIL_DESC desc;
|
||||
d3d11_helper::DefaultDepthStencilDesc(desc);
|
||||
hr = device->CreateDepthStencilState(&desc, &_frontLineDepthState);
|
||||
desc.DepthFunc = D3D11_COMPARISON_GREATER;
|
||||
hr = device->CreateDepthStencilState(&desc, &_backLineDepthState);*/
|
||||
}
|
||||
void Gizmos::DrawLine(const float3& from, const float3& to) {
|
||||
/*_vertices.push_back(GizmoVertex(lm::mul(_matrix, from), _color));
|
||||
_vertices.push_back(GizmoVertex(lm::mul(_matrix, to), _color));*/
|
||||
}
|
||||
void Gizmos::DrawAll(CameraPtr camera) {
|
||||
/*auto renderer = LEngine::Instance()->GetRenderer();
|
||||
|
||||
renderer->GetImmediateContext()->OMSetDepthStencilState(_frontLineDepthState, 0);
|
||||
DrawImpl(camera, _frontMaterial);
|
||||
renderer->GetImmediateContext()->OMSetDepthStencilState(_backLineDepthState, 0);
|
||||
DrawImpl(camera, _backMaterial);
|
||||
renderer->GetImmediateContext()->OMSetDepthStencilState(_frontLineDepthState, 0);*/
|
||||
}
|
||||
void Gizmos::DrawImpl(CameraPtr camera, MaterialPtr material) {
|
||||
/*if (_vertices.size() < 2) {
|
||||
return;
|
||||
}
|
||||
//set system variables
|
||||
auto& params = material->GetParametersBlock();
|
||||
float4x4 matrix = float4x4::identity();
|
||||
params.SetValue("mWorld", matrix);
|
||||
params.SetValue("mView", camera->worldToCameraMatrix());
|
||||
params.SetValue("mProjection", camera->projectionMatrix());
|
||||
|
||||
auto renderer = LEngine::Instance()->GetRenderer();
|
||||
|
||||
material->Apply(renderer->GetImmediateContext());
|
||||
|
||||
ID3D11Buffer* vb[1] = { _vertexBuffer };
|
||||
UINT stride = sizeof(GizmoVertex);
|
||||
UINT offset = 0;
|
||||
|
||||
renderer->GetImmediateContext()->IASetInputLayout(_inputLayout);
|
||||
renderer->GetImmediateContext()->IASetVertexBuffers(0, 1, vb, &stride, &offset);
|
||||
renderer->GetImmediateContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
|
||||
renderer->GetImmediateContext()->Draw(_vertices.size(), 0);*/
|
||||
}
|
||||
void Gizmos::Clear() {
|
||||
/*_vertices.clear();
|
||||
_matrix = float4x4::identity();*/
|
||||
}
|
||||
void Gizmos::PrepareRender() {
|
||||
/*_vertexBuffer = nullptr;
|
||||
|
||||
if (_vertices.size() < 2) {
|
||||
return;
|
||||
}
|
||||
//vertex buffer
|
||||
D3D11_BUFFER_DESC desc = { 0 };
|
||||
desc.Usage = D3D11_USAGE_DEFAULT;
|
||||
desc.ByteWidth = sizeof(_vertices[0])* _vertices.size();
|
||||
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
||||
desc.CPUAccessFlags = 0;
|
||||
desc.MiscFlags = 0;
|
||||
|
||||
D3D11_SUBRESOURCE_DATA data;
|
||||
data.pSysMem = &_vertices[0];
|
||||
data.SysMemPitch = 0;
|
||||
|
||||
_vertexBuffer = nullptr;
|
||||
LEngine::Instance()->GetRenderer()->GetDevice()->CreateBuffer(&desc, &data, &_vertexBuffer);*/
|
||||
}
|
||||
float4 Gizmos::GetColor() {
|
||||
return _color;
|
||||
}
|
||||
void Gizmos::SetColor(float4 color) {
|
||||
_color = color;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "GizmoVertex.h"
|
||||
#include <vector>
|
||||
#include "Material.h"
|
||||
#include "Shader.h"
|
||||
#include <lmath/lmath.h>
|
||||
#include "Camera.h"
|
||||
#include "Bounds.h"
|
||||
|
||||
using namespace lm;
|
||||
|
||||
class Gizmos {
|
||||
public:
|
||||
Gizmos();
|
||||
void Init();
|
||||
void DrawLine(const float3& from, const float3& to);
|
||||
void DrawBounds(const Bounds& bounds);
|
||||
void DrawAll(CameraPtr camera);
|
||||
void Clear();
|
||||
void PrepareRender();
|
||||
float4 GetColor();
|
||||
void SetColor(float4 color);
|
||||
void SetMatrix(const float4x4& matrix);
|
||||
float4x4 GetMatrix();
|
||||
private:
|
||||
void DrawImpl(CameraPtr camera, MaterialPtr material);
|
||||
//ID3D11InputLayoutPtr _inputLayout;
|
||||
//ID3D11BufferPtr _vertexBuffer;
|
||||
//ID3D11DepthStencilStatePtr _backLineDepthState;
|
||||
//ID3D11DepthStencilStatePtr _frontLineDepthState;
|
||||
MaterialPtr _backMaterial;
|
||||
MaterialPtr _frontMaterial;
|
||||
float4 _color;
|
||||
float4x4 _matrix;
|
||||
std::vector<GizmoVertex> _vertices;
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef Guid_h__
|
||||
#define Guid_h__
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
|
||||
struct Guid {
|
||||
union {
|
||||
std::array<uint8_t, 16> data;
|
||||
std::array<uint32_t, 4> datai;
|
||||
};
|
||||
|
||||
Guid() :datai{ 0,0,0,0 } {
|
||||
|
||||
}
|
||||
Guid(uint32_t i0, uint32_t i1, uint32_t i2, uint32_t i3) : datai{ i0,i1,i2,i3 } {
|
||||
|
||||
}
|
||||
Guid(const std::array<uint32_t, 4>& data_i) : datai(data_i) {
|
||||
|
||||
}
|
||||
Guid(const std::array<uint8_t, 16>& data) : data(data) {
|
||||
|
||||
}
|
||||
Guid(const Guid& guid) : data(guid.data) {
|
||||
|
||||
}
|
||||
Guid& operator =(const Guid& right) {
|
||||
data = right.data;
|
||||
return *this;
|
||||
}
|
||||
bool operator ==(const Guid& right) const {
|
||||
return std::equal(data.begin(), data.end(), right.data.begin());
|
||||
}
|
||||
bool operator !=(const Guid& right) const {
|
||||
return !std::equal(data.begin(), data.end(), right.data.begin());
|
||||
}
|
||||
bool IsZero() const {
|
||||
return (datai[0] == 0) && (datai[1] == 0) && (datai[2] == 0) && (datai[3] == 0);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // Guid_h__
|
||||
@@ -0,0 +1,49 @@
|
||||
#ifndef InputArchive_h__
|
||||
#define InputArchive_h__
|
||||
|
||||
|
||||
#include "Archive.h"
|
||||
#include "Object.h"
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
|
||||
class InputArchive : public Archive {
|
||||
public:
|
||||
InputArchive(std::shared_ptr<std::istream> stream)
|
||||
:_stream(stream) {
|
||||
}
|
||||
|
||||
virtual bool is_loading() const override {
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual void serialize(void* data, size_t size) override {
|
||||
|
||||
_stream->read(reinterpret_cast<char*>(data), size);
|
||||
if(!_stream)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
size_type get_offset() const override
|
||||
{
|
||||
return _stream->tellg();
|
||||
}
|
||||
|
||||
void set_offset(size_type offset) override {
|
||||
_stream->seekg(offset, std::ios::beg);
|
||||
}
|
||||
|
||||
~InputArchive() {}
|
||||
|
||||
std::shared_ptr<std::istream> GetStream(){
|
||||
return _stream;
|
||||
}
|
||||
private:
|
||||
std::shared_ptr<std::istream> _stream;
|
||||
};
|
||||
|
||||
#endif // InputArchive_h__
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "LEngine.h"
|
||||
#include "AssetManager.h"
|
||||
|
||||
std::shared_ptr<LEngine> LEngine::_instance = nullptr;
|
||||
|
||||
std::shared_ptr<LEngine> LEngine::Instance() {
|
||||
if (_instance == nullptr) {
|
||||
_instance = std::shared_ptr<LEngine>(new LEngine());
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
|
||||
ScenePtr LEngine::GetActiveScene() {
|
||||
return _activeScene;
|
||||
}
|
||||
|
||||
void LEngine::Init(const std::wstring& assetsPath) {
|
||||
AssetManager::Instance()->SetAssetsFolder(assetsPath);
|
||||
_renderer->Init();
|
||||
}
|
||||
|
||||
void LEngine::LoadScene(const std::string& path) {
|
||||
_activeScene = nullptr;
|
||||
_activeScene = std::make_shared<Scene>();
|
||||
_activeScene->SetName("New scene");
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef Application_h__
|
||||
#define Application_h__
|
||||
|
||||
#include "Scene.h"
|
||||
#include "ForwardDeclarations.h"
|
||||
#include <string>
|
||||
#include "Renderer.h"
|
||||
#include "RenderOutput.h"
|
||||
|
||||
class LEngine {
|
||||
public:
|
||||
static std::shared_ptr<LEngine> Instance();
|
||||
friend class Scene;
|
||||
ScenePtr GetActiveScene();
|
||||
void LoadScene(const std::string& path);
|
||||
void Init(const std::wstring& assetsPath);
|
||||
RendererPtr& GetRenderer() {
|
||||
return _renderer;
|
||||
}
|
||||
void AddOutput(RenderOutputPtr output) {
|
||||
_outputs.push_back(output);
|
||||
}
|
||||
std::vector<RenderOutputPtr> GetOutputs() {
|
||||
return _outputs;
|
||||
}
|
||||
private:
|
||||
LEngine()
|
||||
{
|
||||
_renderer = std::make_shared<Renderer>();
|
||||
}
|
||||
RendererPtr _renderer;
|
||||
ScenePtr _activeScene;
|
||||
static std::shared_ptr<LEngine> _instance;
|
||||
std::vector<RenderOutputPtr> _outputs;
|
||||
};
|
||||
|
||||
#endif // Application_h__
|
||||
@@ -0,0 +1,197 @@
|
||||
#include "Material.h"
|
||||
#include <d3d11.h>
|
||||
#include <stdint.h>
|
||||
//#include <NativeLINQ/NativeLINQ.h>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include "FatalError.h"
|
||||
|
||||
void Material::SetShader(ShaderPtr _shader) {
|
||||
shader = _shader;
|
||||
RebuildBuffers();
|
||||
}
|
||||
|
||||
std::string Material::DiffuseMapName() {
|
||||
return "_DiffuseMap";
|
||||
}
|
||||
std::string Material::NormalMapName() {
|
||||
return "_NormalMap";
|
||||
}
|
||||
std::string Material::DiffuseColorName() {
|
||||
return "_DiffuseColor";
|
||||
}
|
||||
MaterialPtr Material::Create(std::string name) {
|
||||
return std::shared_ptr<Material>(new Material(name));
|
||||
}
|
||||
|
||||
MaterialPtr Material::Create(ShaderPtr shader, std::string name) {
|
||||
auto material = Create(name);
|
||||
material->SetShader(shader);
|
||||
return material;
|
||||
}
|
||||
|
||||
void Material::Apply() {
|
||||
for(int i = 0; i < (int)ShaderType::Count; ++i) {
|
||||
auto s = shader->GetShader((ShaderType)i);
|
||||
if(s != nullptr) {
|
||||
|
||||
//apply shaders
|
||||
//s->Apply(context);
|
||||
|
||||
auto& p = shaderParameters[i];
|
||||
//update and apply buffers
|
||||
for (auto& bufferCtx : p.buffers) {
|
||||
|
||||
D3D12_RANGE range;
|
||||
range.Begin = range.End = 0;
|
||||
|
||||
uint8_t* data = nullptr;
|
||||
|
||||
// auto hr = bufferCtx.buffer->Map(0, &range, (void**)&data);
|
||||
// assert(hr == S_OK);
|
||||
|
||||
//write all variables
|
||||
for (auto& variable : bufferCtx.variables) {
|
||||
auto param = parameters.GetParameterByName(variable.name);
|
||||
if (param != nullptr) {
|
||||
memcpy(data + variable.offset, param->GetPointer(), variable.size);
|
||||
}
|
||||
}
|
||||
|
||||
range.End = bufferCtx.size;
|
||||
// bufferCtx.buffer->Unmap(0, &range);
|
||||
// assert(hr == S_OK);
|
||||
/*context->Unmap(bufferCtx.buffer, 0);
|
||||
ID3D11Buffer* buffers[1] = { bufferCtx.buffer };
|
||||
|
||||
switch ((ShaderType)i)
|
||||
{
|
||||
case ShaderType::Vs:
|
||||
context->VSSetConstantBuffers(bufferCtx.bindPoint, 1, buffers);
|
||||
break;
|
||||
case ShaderType::Ps:
|
||||
context->PSSetConstantBuffers(bufferCtx.bindPoint, 1, buffers);
|
||||
break;
|
||||
default:
|
||||
assert(false && "not implemented");
|
||||
}*/
|
||||
|
||||
//sr.pData
|
||||
|
||||
}
|
||||
|
||||
//apply shader resources
|
||||
/*for (auto& resourceCtx : p.resources) {
|
||||
auto param = parameters.GetParameterByName(resourceCtx.name);
|
||||
if (param != nullptr) {
|
||||
auto resourceParam = std::dynamic_pointer_cast<ShaderResourceParameter>(param);
|
||||
|
||||
ID3D11ShaderResourceView* resources[1] = { resourceParam->value };
|
||||
|
||||
switch ((ShaderType)i)
|
||||
{
|
||||
case ShaderType::Vs:
|
||||
context->VSSetShaderResources(resourceCtx.bindPoint, 1, resources);
|
||||
break;
|
||||
case ShaderType::Ps:
|
||||
context->PSSetShaderResources(resourceCtx.bindPoint, 1, resources);
|
||||
break;
|
||||
default:
|
||||
assert(false && "not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
}*/
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Material::RebuildShaderParameters(ShaderParametersContext& params, ComPtr<ID3D12ShaderReflection> reflection) {
|
||||
|
||||
|
||||
D3D12_SHADER_DESC d;
|
||||
reflection->GetDesc(&d);
|
||||
|
||||
params.Clear();
|
||||
|
||||
//get resource variables
|
||||
for(uint32_t i = 0; i < d.BoundResources; ++i) {
|
||||
D3D12_SHADER_INPUT_BIND_DESC bindDesc;
|
||||
reflection->GetResourceBindingDesc(i, &bindDesc);
|
||||
|
||||
ShaderResourceVariable var;
|
||||
var.bindPoint = bindDesc.BindPoint;
|
||||
var.name = bindDesc.Name;
|
||||
var.type = bindDesc.Type;
|
||||
params.resources.push_back(var);
|
||||
}
|
||||
|
||||
|
||||
//get constant buffers
|
||||
for(uint32_t i = 0; i < d.ConstantBuffers; ++i) {
|
||||
ShaderBufferContext ctx;
|
||||
ID3D12ShaderReflectionConstantBuffer* cb = reflection->GetConstantBufferByIndex(i);
|
||||
|
||||
auto device = shader->GetDevice();
|
||||
D3D12_SHADER_BUFFER_DESC bufferDesc;
|
||||
cb->GetDesc(&bufferDesc);
|
||||
|
||||
ctx.name = bufferDesc.Name;
|
||||
|
||||
D3D11_BUFFER_DESC cbDesc;
|
||||
cbDesc.ByteWidth = bufferDesc.Size;
|
||||
cbDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
cbDesc.MiscFlags = 0;
|
||||
cbDesc.StructureByteStride = 0;
|
||||
|
||||
|
||||
// auto hr = device->CreateCommittedResource(
|
||||
// &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD), // this heap will be used to upload the constant buffer data
|
||||
// D3D12_HEAP_FLAG_NONE, // no flags
|
||||
// &CD3DX12_RESOURCE_DESC::Buffer(1024 * 64), // Must be a multiple of 64KB for single-textures and constant buffers. TODO: handle size overflow o_O Maybe.
|
||||
// D3D12_RESOURCE_STATE_GENERIC_READ,
|
||||
// nullptr,
|
||||
// IID_PPV_ARGS(&ctx.buffer));
|
||||
|
||||
// ctx.buffer->SetName(L"Constant Buffer Upload Resource Heap");
|
||||
|
||||
|
||||
//device->CreateBuffer(&cbDesc, nullptr, &ctx.buffer);
|
||||
|
||||
for(uint32_t j = 0; j < bufferDesc.Variables; ++j) {
|
||||
auto variable = cb->GetVariableByIndex(j);
|
||||
D3D12_SHADER_VARIABLE_DESC varDesc;
|
||||
variable->GetDesc(&varDesc);
|
||||
ctx.variables.push_back(ShaderBufferVariable(varDesc.Name, varDesc.StartOffset, varDesc.Size));
|
||||
}
|
||||
|
||||
//find buffer's bind point
|
||||
|
||||
// auto res = Where(params.resources, [ctx](ShaderResourceVariable srv) {return srv.name == ctx.name && srv.type == D3D_SIT_CBUFFER; })->ToVector();
|
||||
// ctx.bindPoint = res[0].bindPoint;
|
||||
// ctx.type = res[0].type;
|
||||
// ctx.size = cbDesc.ByteWidth;
|
||||
// params.buffers.push_back(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
void Material::RebuildBuffers() {
|
||||
ComPtr<ID3D12ShaderReflection> reflection;
|
||||
for(int i = 0; i < (int)ShaderType::Count; ++i) {
|
||||
auto vs = shader->GetShader((ShaderType)i);
|
||||
if(vs != nullptr) {
|
||||
reflection = nullptr;
|
||||
D3DReflect(vs->blob->GetBufferPointer(), vs->blob->GetBufferSize(), __uuidof(reflection), (void**)&reflection);
|
||||
RebuildShaderParameters(shaderParameters[i], reflection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Material::SetParametersBlock(ShaderParametersBlock& block) {
|
||||
parameters = block;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#ifndef Material_h__
|
||||
#define Material_h__
|
||||
|
||||
#include "Shader.h"
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include "ForwardDeclarations.h"
|
||||
#include "ShaderParametersBlock.h"
|
||||
#include "Object.h"
|
||||
|
||||
#include <wrl.h>
|
||||
|
||||
using namespace Microsoft::WRL;
|
||||
|
||||
class Material : public Object {
|
||||
public:
|
||||
std::wstring class_name() const {
|
||||
return L"Material";
|
||||
}
|
||||
void SetShader(ShaderPtr _shader);
|
||||
ShaderPtr GetShader() {
|
||||
return shader;
|
||||
}
|
||||
void SetParametersBlock(ShaderParametersBlock& block);
|
||||
ShaderParametersBlock& GetParametersBlock() {
|
||||
return parameters;
|
||||
}
|
||||
void Apply();
|
||||
static MaterialPtr Create(std::string name);
|
||||
static MaterialPtr Create(ShaderPtr shader, std::string name);
|
||||
static std::string DiffuseMapName();
|
||||
static std::string NormalMapName();
|
||||
static std::string DiffuseColorName();
|
||||
ShaderParametersContext& GetCompiledParameters(ShaderType shader_type)
|
||||
{
|
||||
return shaderParameters[(uint32_t)shader_type];
|
||||
}
|
||||
private:
|
||||
|
||||
Material(std::string name)
|
||||
:_name(name)
|
||||
{
|
||||
|
||||
}
|
||||
std::string _name;
|
||||
ShaderPtr shader;
|
||||
ShaderParametersContext shaderParameters[(uint32_t)ShaderType::Count];
|
||||
ShaderParametersBlock parameters;
|
||||
|
||||
|
||||
void RebuildBuffers();
|
||||
void RebuildShaderParameters(ShaderParametersContext& params, ComPtr<ID3D12ShaderReflection> reflection);
|
||||
};
|
||||
|
||||
|
||||
#endif // Material_h__
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
class Mathf {
|
||||
public:
|
||||
static float Pi() {
|
||||
return 3.1415926535897932384626f;
|
||||
}
|
||||
static float RadToDeg() {
|
||||
return 180.0f / Pi();
|
||||
}
|
||||
static float DegToRad() {
|
||||
return Pi() / 180.0f;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,148 @@
|
||||
#include "Mesh.h"
|
||||
#include "LEngine.h"
|
||||
#include <string.h>
|
||||
|
||||
// Mesh::Mesh(ID3D12DevicePtr dev) {
|
||||
// _device = dev;
|
||||
// SetSubmeshCount(1);
|
||||
// }
|
||||
|
||||
MeshPtr Mesh::Create() {
|
||||
return nullptr;//std::shared_ptr<Mesh>(new Mesh(LEngine::Instance()->GetRenderer()->GetDevice()));
|
||||
}
|
||||
|
||||
// ID3D12ResourcePtr Mesh::GetVertexBuffer() {
|
||||
// return _vertexBuffer;
|
||||
// }
|
||||
|
||||
// ID3D12DevicePtr Mesh::GetDevice() {
|
||||
// return _device;
|
||||
// }
|
||||
|
||||
MeshPtr Mesh::Create(MeshProcessFunction initFunction) {
|
||||
auto mesh = Create();
|
||||
if (initFunction != nullptr) {
|
||||
initFunction(mesh);
|
||||
mesh->RecalculateBounds();
|
||||
}
|
||||
return mesh;
|
||||
}
|
||||
|
||||
Bounds Mesh::GetBounds() const {
|
||||
return _bounds;
|
||||
}
|
||||
|
||||
std::vector<Vertex> Mesh::GetVertices() {
|
||||
std::vector<Vertex> result;
|
||||
/*if (_vertexBuffer == nullptr) {
|
||||
return result;
|
||||
}
|
||||
|
||||
D3D11_BUFFER_DESC desc;
|
||||
_vertexBuffer->GetDesc(&desc);
|
||||
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
desc.Usage = D3D11_USAGE_STAGING;
|
||||
desc.BindFlags = 0;
|
||||
ID3D11BufferPtr stagingBuffer;
|
||||
_device->CreateBuffer(&desc, nullptr, &stagingBuffer);
|
||||
ID3D11DeviceContextPtr ctx;
|
||||
_device->GetImmediateContext(&ctx);
|
||||
|
||||
ctx->CopyResource(stagingBuffer, _vertexBuffer);
|
||||
D3D11_MAPPED_SUBRESOURCE sr;
|
||||
ctx->Map(stagingBuffer, 0, D3D11_MAP_READ, 0, &sr);
|
||||
result.resize(desc.ByteWidth / sizeof(Vertex));
|
||||
memcpy_s(&result[0], desc.ByteWidth, sr.pData, desc.ByteWidth);
|
||||
ctx->Unmap(stagingBuffer, 0);*/
|
||||
return result;
|
||||
}
|
||||
|
||||
D3D12_VERTEX_BUFFER_VIEW Mesh::GetVertexBufferView(){
|
||||
D3D12_VERTEX_BUFFER_VIEW result;
|
||||
// result.BufferLocation = _vertexBuffer->GetGPUVirtualAddress();
|
||||
result.SizeInBytes = _vertexCount * sizeof(Vertex);
|
||||
result.StrideInBytes = sizeof(Vertex);
|
||||
return result;
|
||||
}
|
||||
|
||||
void Mesh::SetVertices(const std::vector<Vertex>& vertices) {
|
||||
auto bytesCount = sizeof(vertices[0])* vertices.size();
|
||||
_vertexCount = vertices.size();
|
||||
|
||||
|
||||
//create vertex buffer
|
||||
// _device->CreateCommittedResource(
|
||||
// &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
|
||||
// D3D12_HEAP_FLAG_NONE,
|
||||
// &CD3DX12_RESOURCE_DESC::Buffer(bytesCount),
|
||||
// D3D12_RESOURCE_STATE_COPY_DEST,
|
||||
// nullptr,
|
||||
// IID_PPV_ARGS(&_vertexBuffer));
|
||||
|
||||
|
||||
//create upload buffer
|
||||
ComPtr<ID3D12Resource> vBufferUploadHeap;
|
||||
// _device->CreateCommittedResource(
|
||||
// &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
|
||||
// D3D12_HEAP_FLAG_NONE,
|
||||
// &CD3DX12_RESOURCE_DESC::Buffer(bytesCount),
|
||||
// D3D12_RESOURCE_STATE_GENERIC_READ,
|
||||
// nullptr,
|
||||
// IID_PPV_ARGS(&vBufferUploadHeap));
|
||||
// vBufferUploadHeap->SetName(L"Vertex Buffer Upload Resource Heap");
|
||||
|
||||
|
||||
D3D12_SUBRESOURCE_DATA vertexData = {};
|
||||
vertexData.pData = reinterpret_cast<const BYTE*>(&vertices[0]); // pointer to our vertex array
|
||||
vertexData.RowPitch = bytesCount; // size of all our triangle vertex data
|
||||
vertexData.SlicePitch = bytesCount; // also the size of our triangle vertex data
|
||||
|
||||
|
||||
auto renderer = LEngine::Instance()->GetRenderer();
|
||||
// auto cl = renderer->BeginCommandList();
|
||||
// UpdateSubresources(cl.Get(), _vertexBuffer.Get(), vBufferUploadHeap.Get(), 0, 0, 1, &vertexData);
|
||||
// cl->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(_vertexBuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER));
|
||||
renderer->EndCommandList();
|
||||
renderer->WaitCommandsQueue();
|
||||
}
|
||||
|
||||
void Mesh::SetIndices(const Submesh::index_array_t& indices, MeshTopology topology, uint32_t submeshId) {
|
||||
if (submeshId >= GetSubmeshCount()) {
|
||||
return;
|
||||
}
|
||||
_submeshes[submeshId]->Set(indices, topology);
|
||||
}
|
||||
|
||||
uint32_t Mesh::GetSubmeshCount() const {
|
||||
return _submeshes.size();
|
||||
}
|
||||
void Mesh::SetSubmeshCount(uint32_t count) {
|
||||
auto oldSize = _submeshes.size();
|
||||
_submeshes.resize(count);
|
||||
for (auto i = oldSize; i < count; ++i) {
|
||||
_submeshes[i] = std::make_shared<Submesh>();
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<SubmeshPtr>& Mesh::GetSubmeshes() {
|
||||
return _submeshes;
|
||||
}
|
||||
|
||||
void Mesh::RecalculateBounds() {
|
||||
/*auto vertices = GetVertices();
|
||||
if (vertices.size() == 0) {
|
||||
_bounds.SetSize(float3(0, 0, 0));
|
||||
_bounds.SetCenter(float3(0, 0, 0));
|
||||
}
|
||||
|
||||
float3 minPoint = vertices[0].position.xyz;
|
||||
float3 maxPoint = vertices[0].position.xyz;
|
||||
|
||||
for (size_t i = 1; i < vertices.size(); ++i) {
|
||||
minPoint = lm::min(minPoint, vertices[i].position.xyz);
|
||||
maxPoint = lm::max(maxPoint, vertices[i].position.xyz);
|
||||
}
|
||||
|
||||
_bounds.SetCenter((minPoint + maxPoint) * 0.5f);
|
||||
_bounds.SetSize(maxPoint - minPoint);*/
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#ifndef Mesh_h__
|
||||
#define Mesh_h__
|
||||
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include "ForwardDeclarations.h"
|
||||
#include "Vertex.h"
|
||||
#include "Submesh.h"
|
||||
#include "Bounds.h"
|
||||
#include "Object.h"
|
||||
|
||||
#include <wrl.h>
|
||||
|
||||
using namespace Microsoft::WRL;
|
||||
|
||||
typedef std::function<void(MeshPtr)> MeshProcessFunction;
|
||||
|
||||
class Mesh : public Object {
|
||||
public:
|
||||
Mesh():_vertexCount(0)
|
||||
{
|
||||
|
||||
}
|
||||
std::vector<Vertex> GetVertices();
|
||||
void SetVertices(const std::vector<Vertex>& vertices);
|
||||
void SetIndices(const Submesh::index_array_t& indices, MeshTopology topology, uint32_t submeshId);
|
||||
void RecalculateBounds();
|
||||
// ID3D12ResourcePtr GetVertexBuffer();
|
||||
// ID3D12DevicePtr GetDevice();
|
||||
static MeshPtr Create();
|
||||
static MeshPtr Create(MeshProcessFunction initFunction);
|
||||
uint32_t GetSubmeshCount() const;
|
||||
void SetSubmeshCount(uint32_t count);
|
||||
const std::vector<SubmeshPtr>& GetSubmeshes();
|
||||
Bounds GetBounds() const;
|
||||
D3D12_VERTEX_BUFFER_VIEW GetVertexBufferView();
|
||||
private:
|
||||
size_t _vertexCount;
|
||||
Bounds _bounds;
|
||||
// Mesh(ID3D12DevicePtr dev);
|
||||
// ID3D12ResourcePtr _vertexBuffer;
|
||||
// ID3D12DevicePtr _device;
|
||||
std::vector<SubmeshPtr> _submeshes;
|
||||
};
|
||||
|
||||
|
||||
#endif // Mesh_h__
|
||||
@@ -0,0 +1,80 @@
|
||||
#include "MeshGenerator.h"
|
||||
#include <vector>
|
||||
|
||||
void MeshGenerator::Cube(MeshPtr mesh, bool splitFaces){
|
||||
if (mesh == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<uint32_t> indices;
|
||||
|
||||
float3 corners[8];
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
int id = i * 4 + j;
|
||||
corners[id].x() = ((j == 0) || (j == 3)) ? -0.5f : 0.5f;
|
||||
corners[id].y() = -((float)(j >> 1) - 0.5f);
|
||||
corners[id].z() =(float)i - 0.5f;
|
||||
}
|
||||
}
|
||||
|
||||
int vid[] = { 0,1,2,3, 5,4,7,6, 1,5,6,2, 4,0,3,7, 4,5,1,0, 3,2,6,7 };
|
||||
for (int i = 0; i < 24; ++i) {
|
||||
Vertex v;
|
||||
v.position.x() = corners[vid[i]].x();
|
||||
v.position.y() = corners[vid[i]].y();
|
||||
v.position.z() = corners[vid[i]].z();
|
||||
vertices.push_back(v);
|
||||
}
|
||||
|
||||
float3 normals[6] = { float3(0.0f,0.0f,-1.0f), float3(0.0f,0.0f,1.0f), float3(1.0f,0.0f,0.0f), float3(-1.0f,0.0f,0.0f), float3(0.0f,1.0f,0.0f), float3(0.0f,-1.0f,0.0f) };
|
||||
int indicesBase[] = { 0,1,3,1,2,3 };
|
||||
float2 uvBase[] = { float2(0.0f,0.0f), float2(1.0f,0.0f), float2(1.0f,1.0f), float2(0.0f,1.0f) };
|
||||
|
||||
mesh->SetSubmeshCount(splitFaces ? 6 : 1);
|
||||
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
vertices[i * 4 + j].normal = normals[i];
|
||||
vertices[i * 4 + j].uv0 = uvBase[j];
|
||||
}
|
||||
|
||||
for (int j = 0; j < 6; ++j) {
|
||||
indices.push_back(indicesBase[j] + i * 4);
|
||||
}
|
||||
|
||||
if (splitFaces) {
|
||||
mesh->SetIndices(indices, MeshTopology::TriangleList, i);
|
||||
indices.resize(0);
|
||||
}
|
||||
}
|
||||
|
||||
mesh->SetVertices(vertices);
|
||||
|
||||
if (!splitFaces) {
|
||||
mesh->SetIndices(indices, MeshTopology::TriangleList, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void MeshGenerator::Plane(MeshPtr mesh) {
|
||||
if (mesh == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
float3 normal(0.0f, 1.0f, 0.0f);
|
||||
float extend = 0.5f;
|
||||
|
||||
std::vector<Vertex> vertices = {
|
||||
Vertex(float4(-extend, 0.0f, extend, 1.0f), normal, float2(0.0f,0.0f)),
|
||||
Vertex(float4(extend, 0.0f, extend, 1.0f), normal, float2(1.0f,0.0f)),
|
||||
Vertex(float4(extend, 0.0f, -extend, 1.0f), normal, float2(1.0f,1.0f)),
|
||||
Vertex(float4(-extend, 0.0f, -extend, 1.0f), normal, float2(0.0f,1.0f)),
|
||||
};
|
||||
|
||||
std::vector<uint32_t> indices = { 0,1,3,1,2,3 };
|
||||
|
||||
mesh->SetSubmeshCount(1);
|
||||
mesh->SetVertices(vertices);
|
||||
mesh->SetIndices(indices, MeshTopology::TriangleList, 0);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef MeshGenerator_h__
|
||||
#define MeshGenerator_h__
|
||||
|
||||
#include "Mesh.h"
|
||||
#include "ForwardDeclarations.h"
|
||||
|
||||
class MeshGenerator
|
||||
{
|
||||
public:
|
||||
static void Cube(MeshPtr mesh, bool splitFaces);
|
||||
|
||||
static void Plane(MeshPtr mesh);
|
||||
};
|
||||
#endif // MeshGenerator_h__
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
#include "MeshRenderer.h"
|
||||
#include <cassert>
|
||||
|
||||
void MeshRenderer::SyncMaterialSlots() {
|
||||
if (mesh == nullptr) {
|
||||
_materials.resize(0);
|
||||
}
|
||||
else {
|
||||
_materials.resize(mesh->GetSubmeshCount());
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<MeshRenderer> MeshRenderer::SetMesh(MeshPtr m) {
|
||||
mesh = m;
|
||||
SyncMaterialSlots();
|
||||
UpdateInputLayouts();
|
||||
return std::static_pointer_cast<MeshRenderer>(shared_from_this());
|
||||
}
|
||||
|
||||
MeshPtr MeshRenderer::GetMesh() {
|
||||
return mesh;
|
||||
}
|
||||
|
||||
void MeshRenderer::SetMaterial(MaterialPtr material, int id){
|
||||
SyncMaterialSlots();
|
||||
_materials[id] = material;
|
||||
UpdateInputLayouts();
|
||||
}
|
||||
|
||||
void MeshRenderer::UpdateInputLayouts(){
|
||||
|
||||
if (mesh == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_materials.size() == 0 || _materials[0] == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
//D3D12_APPEND_ALIGNED_ELEMENT
|
||||
_inputElements.clear();
|
||||
_inputElements.push_back({ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 });
|
||||
_inputElements.push_back({ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 16, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 });
|
||||
_inputElements.push_back({ "UV", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 });
|
||||
_inputElements.push_back({ "UV", 1, DXGI_FORMAT_R32G32_FLOAT, 0, 36, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 });
|
||||
|
||||
|
||||
_inputLayoutDesc.NumElements = _inputElements.size();
|
||||
_inputLayoutDesc.pInputElementDescs = &_inputElements[0];
|
||||
|
||||
//auto vs = _materials[0]->GetShader()->GetShader(ShaderType::Vs);
|
||||
//auto hr = mesh->GetDevice()->CreateInputLayout(ied, 4, vs->blob->GetBufferPointer(), vs->blob->GetBufferSize(), &_inputLayout);
|
||||
//assert(hr == S_OK);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef MeshRenderer_h__
|
||||
#define MeshRenderer_h__
|
||||
|
||||
#include "ObjectsFactory.h"
|
||||
#include "Mesh.h"
|
||||
#include "Material.h"
|
||||
#include <vector>
|
||||
#include "ForwardDeclarations.h"
|
||||
#include <memory>
|
||||
|
||||
|
||||
LOBJECT(MeshRenderer, Component)
|
||||
public:
|
||||
std::shared_ptr<MeshRenderer> SetMesh(MeshPtr m);
|
||||
MeshPtr GetMesh();
|
||||
void SetMaterial(MaterialPtr material, int id);
|
||||
std::vector<MaterialPtr> GetMaterials() {
|
||||
return _materials;
|
||||
}
|
||||
D3D12_INPUT_LAYOUT_DESC GetInputLayout() {
|
||||
return _inputLayoutDesc;
|
||||
}
|
||||
private:
|
||||
void UpdateInputLayouts();
|
||||
void SyncMaterialSlots();
|
||||
MeshPtr mesh;
|
||||
std::vector<MaterialPtr> _materials;
|
||||
std::vector<D3D12_INPUT_ELEMENT_DESC> _inputElements;
|
||||
D3D12_INPUT_LAYOUT_DESC _inputLayoutDesc;
|
||||
};
|
||||
#endif // MeshRenderer_h__
|
||||
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include "AssetImporter.h"
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "ObjMeshLoader.h"
|
||||
#include "AssetDatabase.h"
|
||||
|
||||
class ModelImporter : public AssetImporter {
|
||||
public:
|
||||
virtual bool can_import(const std::wstring& path) const override {
|
||||
std::filesystem::path src_path(path);
|
||||
return src_path.extension() == ".obj";
|
||||
}
|
||||
|
||||
virtual std::wstring name() override {
|
||||
return L"ModelImporter";
|
||||
}
|
||||
|
||||
virtual bool set_parameters(std::wstring params) override {
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual std::wstring get_parameters() const override {
|
||||
return L"";
|
||||
}
|
||||
|
||||
virtual void import(std::wstring asset_path) override {
|
||||
ObjMeshLoader loader;
|
||||
loader.Load(asset_path);
|
||||
}
|
||||
|
||||
};
|
||||
@@ -0,0 +1,416 @@
|
||||
#pragma once
|
||||
|
||||
#include "Mesh.h"
|
||||
#include "GameObject.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <lmath/lmath.h>
|
||||
#include <stack>
|
||||
#include <sstream>
|
||||
#include <array>
|
||||
#include <tuple>
|
||||
#include <map>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
|
||||
#include "MeshRenderer.h"
|
||||
using namespace lm;
|
||||
|
||||
|
||||
struct ObjVertex {
|
||||
int32_t vid;
|
||||
int32_t tid;
|
||||
int32_t nid;
|
||||
};
|
||||
|
||||
struct ObjFace {
|
||||
int32_t sid;
|
||||
int32_t mid;
|
||||
std::array<ObjVertex, 3> vertices;
|
||||
};
|
||||
|
||||
class ObjGroup {
|
||||
public:
|
||||
ObjGroup(std::string objName)
|
||||
:name(objName), smoothGroupsEnabled(true), activeSmoothGroup(0)
|
||||
{
|
||||
|
||||
}
|
||||
void AddFace(int32_t matId, const ObjVertex& v0, const ObjVertex& v1, const ObjVertex& v2) {
|
||||
ObjFace f;
|
||||
f.vertices[0] = v0;
|
||||
f.vertices[1] = v1;
|
||||
f.vertices[2] = v2;
|
||||
f.mid = matId;
|
||||
f.sid = activeSmoothGroup;
|
||||
faces.push_back(f);
|
||||
|
||||
if (std::find(materials.begin(), materials.end(), matId) == materials.end()) {
|
||||
materials.push_back(matId);
|
||||
}
|
||||
}
|
||||
std::vector<uint32_t> materials;
|
||||
std::string name;
|
||||
std::vector<ObjFace> faces;
|
||||
int32_t activeSmoothGroup;
|
||||
bool smoothGroupsEnabled;
|
||||
};
|
||||
|
||||
class ObjMeshLoader {
|
||||
public:
|
||||
friend class ObjGroup;
|
||||
ObjMeshLoader() : _activeMatId(-1){
|
||||
|
||||
}
|
||||
|
||||
std::string read_file(std::wstring path)
|
||||
{
|
||||
FILE* file = nullptr;
|
||||
auto err = _wfopen_s(&file, path.c_str(), L"rb");
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
auto file_size = ftell(file);
|
||||
|
||||
std::string str;
|
||||
str.resize(file_size);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
|
||||
auto done = fread_s(&str[0], file_size, 1, file_size, file);
|
||||
fclose(file);
|
||||
return str;
|
||||
}
|
||||
std::vector<GameObjectPtr> Load(const std::wstring& path) {
|
||||
std::vector<GameObjectPtr> result;
|
||||
|
||||
/*if (!file1.good()) {
|
||||
return result;
|
||||
}*/
|
||||
|
||||
auto str = read_file(path);
|
||||
|
||||
|
||||
std::stringstream buffer(str);
|
||||
std::vector<char> line_buffer;
|
||||
line_buffer.resize(8192);
|
||||
|
||||
|
||||
std::string previous_line;
|
||||
std::string line;
|
||||
//while (buffer.getline(&line_buffer[0], line_buffer.size())) {
|
||||
while (std::getline(buffer, line)) {
|
||||
|
||||
//std::string line(&line_buffer[0]);
|
||||
if (line.size() < 3) {
|
||||
continue;
|
||||
}
|
||||
switch(line[0]){
|
||||
case 'v':
|
||||
switch (line[1]) {
|
||||
case ' ': {
|
||||
float3 v;
|
||||
sscanf_s(&line[2], "%f%f%f", &v.x(), &v.y(), &v.z());
|
||||
_vertices.push_back(v);
|
||||
break;
|
||||
}
|
||||
case 'n': {
|
||||
float3 n;
|
||||
sscanf_s(&line[2], "%f%f%f", &n.x(), &n.y(), &n.z());
|
||||
_normals.push_back(n);
|
||||
break;
|
||||
}
|
||||
case 't':
|
||||
float3 t;
|
||||
sscanf_s(&line[2], "%f%f%f", &t.x(), &t.y(), &t.z());
|
||||
_texcoords.push_back(t);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'f': {
|
||||
bool is_open = false;
|
||||
size_t start = 2;
|
||||
int slash_pos_0 = 0;
|
||||
int slash_pos_1 = 0;
|
||||
|
||||
if(line[line.size() - 1] == '\r')
|
||||
{
|
||||
line[line.size() - 1] = ' ';
|
||||
}else
|
||||
{
|
||||
line += " ";
|
||||
}
|
||||
|
||||
std::vector<ObjVertex> fVertices;
|
||||
for (size_t i = 2; i < line.size(); ++i) {
|
||||
if (line[i] == ' ') {
|
||||
if (is_open) { //finalize chunk
|
||||
is_open = false;
|
||||
ObjVertex v;
|
||||
if (slash_pos_0 == -1) {
|
||||
auto pos2 = (int)buffer.tellg();
|
||||
sscanf_s(&line[start], "%d", &v.vid);
|
||||
v.tid = v.nid = -1;
|
||||
} else if (slash_pos_1 == -1) {
|
||||
sscanf_s(&line[start], "%d/%d", &v.vid, &v.tid);
|
||||
v.nid = -1;
|
||||
} else {
|
||||
if (slash_pos_1 - slash_pos_0 == 1) {
|
||||
sscanf_s(&line[start], "%d//%d", &v.vid, &v.nid);
|
||||
v.tid = -1;
|
||||
} else {
|
||||
sscanf_s(&line[start], "%d/%d/%d", &v.vid, &v.tid, &v.nid);
|
||||
}
|
||||
}
|
||||
fVertices.push_back(v);
|
||||
}
|
||||
} else {
|
||||
if (!is_open) {
|
||||
start = i;
|
||||
slash_pos_0 = -1;
|
||||
slash_pos_1 = -1;
|
||||
is_open = true;
|
||||
} else {
|
||||
if (line[i] == '/') {
|
||||
if (slash_pos_0 == -1) {
|
||||
slash_pos_0 = i;
|
||||
} else {
|
||||
slash_pos_1 = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//emit triangles (split if quads)
|
||||
if (fVertices.size() == 3) {
|
||||
_groups[_groups.size() - 1].AddFace(_activeMatId, fVertices[0], fVertices[1], fVertices[2]);
|
||||
} else if (fVertices.size() == 4) {
|
||||
_groups[_groups.size() - 1].AddFace(_activeMatId, fVertices[0], fVertices[1], fVertices[2]);
|
||||
_groups[_groups.size() - 1].AddFace(_activeMatId, fVertices[0], fVertices[2], fVertices[3]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'g': {
|
||||
auto name = line.substr(2);
|
||||
_groups.push_back(ObjGroup(name));
|
||||
break;
|
||||
}
|
||||
case 's':
|
||||
if (line.find("off") != std::string::npos) {
|
||||
_groups[_groups.size() - 1].smoothGroupsEnabled = false;
|
||||
} else {
|
||||
sscanf_s(&line[2], "%d", &_groups[_groups.size() - 1].activeSmoothGroup);
|
||||
}
|
||||
break;
|
||||
case 'm': {
|
||||
std::stringstream ss(line);
|
||||
std::string key, value;
|
||||
ss >> key;
|
||||
ss >> value;
|
||||
_mtlLibName = value;
|
||||
std::cout << "Found material library: " << value << std::endl;
|
||||
break;
|
||||
}
|
||||
case 'u': {
|
||||
std::stringstream ss(line);
|
||||
std::string key, value;
|
||||
ss >> key;
|
||||
ss >> value;
|
||||
SetActiveMaterial(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
previous_line = line;
|
||||
}
|
||||
|
||||
|
||||
auto mtllib_path = obj_to_mtl_path(path);
|
||||
|
||||
_mats = load_mtllib(mtllib_path);
|
||||
|
||||
for (auto& group : _groups) {
|
||||
result.push_back(UnpackGroup(group));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
private:
|
||||
std::vector<MaterialPtr> _mats;
|
||||
std::wstring obj_to_mtl_path(const std::wstring& obj_path){
|
||||
std::filesystem::path src(obj_path);
|
||||
auto dst = src.parent_path();
|
||||
return dst.append(_mtlLibName).wstring();
|
||||
}
|
||||
|
||||
std::vector<MaterialPtr> load_mtllib(const std::wstring& path){
|
||||
std::vector<MaterialPtr> result;
|
||||
|
||||
auto text = std::ifstream(path);
|
||||
|
||||
std::string line;
|
||||
auto default_shader = Shader::Create(L"Shaders/default.fx");
|
||||
|
||||
MaterialPtr active_material;
|
||||
while(std::getline(text, line))
|
||||
{
|
||||
if(line[0] == '#' || line.empty()){
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string word;
|
||||
std::stringstream ls(line);
|
||||
ls >> word;
|
||||
|
||||
if (word == "newmtl") {
|
||||
std::string mat_name;
|
||||
ls >> mat_name;
|
||||
if(active_material != nullptr)
|
||||
{
|
||||
result.push_back(active_material);
|
||||
}
|
||||
active_material = Material::Create(default_shader, mat_name);
|
||||
|
||||
} else {
|
||||
if (active_material == nullptr) {
|
||||
continue;
|
||||
} else {
|
||||
if(word == "Kd")
|
||||
{
|
||||
std::vector<float> color;
|
||||
float tmp = 0.0f;
|
||||
while(ls >> tmp)
|
||||
{
|
||||
color.push_back(tmp);
|
||||
}
|
||||
|
||||
if(color.size() == 3)
|
||||
{
|
||||
active_material->GetParametersBlock().SetValue(Material::DiffuseColorName(), float4(color[0], color[1], color[2], 1.0f));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (active_material != nullptr) {
|
||||
result.push_back(active_material);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
typedef std::tuple<ObjVertex, Vertex> VertexPair;
|
||||
GameObjectPtr UnpackGroup(const ObjGroup& group) {
|
||||
std::vector<std::vector<ObjFace*>> facesTable;
|
||||
facesTable.resize(_vertices.size());
|
||||
std::vector<Vertex> vertices;
|
||||
std::map<size_t, std::vector<uint32_t>> submeshes;
|
||||
|
||||
//EmitGeometry(group, vertices, submeshes);
|
||||
EmitGeometrySimple(group, vertices, submeshes);
|
||||
|
||||
auto mesh = Mesh::Create();
|
||||
mesh->SetSubmeshCount(group.materials.size());
|
||||
mesh->SetVertices(vertices);
|
||||
|
||||
|
||||
|
||||
int id = 0;
|
||||
for(auto& p : submeshes)
|
||||
{
|
||||
mesh->SetIndices(p.second, MeshTopology::TriangleList, id);
|
||||
id++;
|
||||
}
|
||||
|
||||
auto obj = GameObject::create();
|
||||
obj->AddComponent<MeshRenderer>()->SetMesh(mesh);
|
||||
|
||||
|
||||
|
||||
for (size_t i = 0; i < group.materials.size(); ++i){
|
||||
auto mat_id = group.materials[i];
|
||||
|
||||
obj->GetComponent<MeshRenderer>()->SetMaterial(_mats[mat_id], i);
|
||||
}
|
||||
|
||||
mesh->RecalculateBounds();
|
||||
return obj;
|
||||
}
|
||||
|
||||
/*MeshPtr BuildMesh(ObjGroup& group) {
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<int32_t> indices;
|
||||
EmitGeometry(group, vertices, indices);
|
||||
|
||||
auto mesh = Mesh::Create();
|
||||
mesh->SetSubmeshCount(group.materials.size());
|
||||
return mesh;
|
||||
}*/
|
||||
|
||||
void EmitGeometry(const ObjGroup& group, std::vector<Vertex>& vertices, std::map<size_t, std::vector<uint32_t>>& submeshes) {
|
||||
auto vComp = [](ObjVertex v1, ObjVertex v2) {
|
||||
return (v1.vid == v2.vid) && (v1.tid == v2.tid) && (v1.nid && v2.nid);
|
||||
};
|
||||
|
||||
std::vector<ObjVertex> tmpVertices;
|
||||
|
||||
for (auto& f : group.faces) {
|
||||
for (auto& v : f.vertices) {
|
||||
int id = -1;
|
||||
for (size_t i = 0; i < tmpVertices.size(); ++i) {
|
||||
if (vComp(tmpVertices[i], v)) {
|
||||
id = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (id == -1) {
|
||||
id = tmpVertices.size();
|
||||
tmpVertices.push_back(v);
|
||||
Vertex vertex;
|
||||
vertex.position = float4(_vertices[v.vid - 1], 1.0f);
|
||||
vertex.normal = _normals[v.nid - 1];
|
||||
vertex.uv0 = _texcoords[v.tid - 1].xy();
|
||||
vertices.push_back(vertex);
|
||||
}
|
||||
|
||||
submeshes[f.mid].push_back(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EmitGeometrySimple(const ObjGroup& group, std::vector<Vertex>& vertices, std::map<size_t, std::vector<uint32_t>>& submeshes) {
|
||||
int id = 0;
|
||||
for (auto& f : group.faces) {
|
||||
for (auto& v : f.vertices) {
|
||||
Vertex vertex;
|
||||
vertex.position = float4(_vertices[v.vid - 1], 1.0f);
|
||||
vertex.normal = _normals[v.nid - 1];
|
||||
vertex.uv0 = _texcoords[v.tid - 1].xy();
|
||||
vertices.push_back(vertex);
|
||||
submeshes[f.mid].push_back(id);
|
||||
++id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SetActiveMaterial(const std::string& name) {
|
||||
for (size_t i = 0; i < _materials.size(); ++i) {
|
||||
if (_materials[i] == name) {
|
||||
_activeMatId = i;
|
||||
return;
|
||||
}
|
||||
}
|
||||
_activeMatId = _materials.size();
|
||||
_materials.push_back(name);
|
||||
}
|
||||
int _activeMatId;
|
||||
std::string _mtlLibName;
|
||||
std::vector<float3> _vertices;
|
||||
std::vector<float3> _normals;
|
||||
std::vector<float3> _texcoords;
|
||||
std::vector<std::string> _materials;
|
||||
std::vector<ObjGroup> _groups;
|
||||
};
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "Object.h"
|
||||
#include "Archive.h"
|
||||
|
||||
std::map<ObjectRegistry::instance_id_t, std::weak_ptr<Object>> ObjectRegistry::_registry;
|
||||
|
||||
std::atomic<ObjectRegistry::instance_id_t> ObjectRegistry::_last_index = 0;
|
||||
|
||||
Object::Object():_instance_id(-1){
|
||||
|
||||
}
|
||||
|
||||
std::shared_ptr<Object> Object::create(){
|
||||
auto result = std::shared_ptr<Object>(new Object());
|
||||
result->Register();
|
||||
return result;
|
||||
}
|
||||
|
||||
Object::~Object(){
|
||||
ObjectRegistry::remove_object(_instance_id);
|
||||
}
|
||||
|
||||
void Object::Register(){
|
||||
_instance_id = ObjectRegistry::add_object(shared_from_this());
|
||||
}
|
||||
|
||||
void Object::serialize(Archive& archive) {
|
||||
archive << _name;
|
||||
}
|
||||
|
||||
ObjectRegistry::instance_id_t ObjectRegistry::add_object(std::shared_ptr<Object> object) {
|
||||
auto result = _last_index++;
|
||||
_registry[result] = object;
|
||||
return result;
|
||||
}
|
||||
|
||||
void ObjectRegistry::remove_object(instance_id_t object) {
|
||||
_registry.erase(object);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
#ifndef Object_h__
|
||||
#define Object_h__
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <windows.h>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
#include "Asset.h"
|
||||
|
||||
class Object;
|
||||
class OutputArchive;
|
||||
|
||||
class ObjectRegistry {
|
||||
public:
|
||||
using instance_id_t = uint64_t;
|
||||
friend class Object;
|
||||
private:
|
||||
static instance_id_t add_object(std::shared_ptr<Object> object);
|
||||
static void remove_object(instance_id_t object);
|
||||
static std::map<instance_id_t, std::weak_ptr<Object>> _registry;
|
||||
static std::atomic<instance_id_t> _last_index;
|
||||
};
|
||||
|
||||
class Archive;
|
||||
class Object : public std::enable_shared_from_this<Object>{
|
||||
public:
|
||||
using instance_id_t = ObjectRegistry::instance_id_t;
|
||||
virtual void serialize(Archive& archive);
|
||||
|
||||
virtual void construct() {
|
||||
|
||||
}
|
||||
|
||||
virtual std::wstring get_type_name() const{
|
||||
return L"Object";
|
||||
}
|
||||
|
||||
std::shared_ptr<Object> as_object(){
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static std::vector<std::shared_ptr<T>> FindObjectsOfType() {
|
||||
std::vector<std::shared_ptr<T>> result;
|
||||
auto& registry = ObjectRegistry::_registry;
|
||||
for(auto& pair : registry){
|
||||
auto obj = pair.second.lock();
|
||||
auto targetObj = std::dynamic_pointer_cast<T>(obj);
|
||||
if(targetObj != nullptr){
|
||||
result.push_back(targetObj);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static std::shared_ptr<Object> Find(const AssetFileLink& link)
|
||||
{
|
||||
auto& registry = ObjectRegistry::_registry;
|
||||
for (auto& pair : registry) {
|
||||
auto obj = pair.second.lock();
|
||||
if(obj != nullptr && obj->GetFileLink() == link)
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static std::shared_ptr<Object> create();
|
||||
virtual ~Object();
|
||||
|
||||
const std::wstring& get_name() const{
|
||||
return _name;
|
||||
}
|
||||
|
||||
void SetName(const std::wstring& name){
|
||||
_name = name;
|
||||
}
|
||||
|
||||
instance_id_t GetInstanceId() const{
|
||||
return _instance_id;
|
||||
}
|
||||
|
||||
const AssetFileLink& GetFileLink() const{
|
||||
return _fileLink;
|
||||
}
|
||||
void SetLink(const AssetFileLink& link){
|
||||
_fileLink = link;
|
||||
}
|
||||
protected:
|
||||
void Register();
|
||||
Object();
|
||||
AssetFileLink _fileLink;
|
||||
instance_id_t _instance_id;
|
||||
private:
|
||||
std::wstring _name;
|
||||
};
|
||||
|
||||
#endif // Object_h__
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "ObjectsFactory.h"
|
||||
|
||||
|
||||
void ObjectsFactory::register_factory(factory_function factory, const std::wstring& class_name) {
|
||||
|
||||
_factory_functions[class_name] = factory;
|
||||
}
|
||||
|
||||
ObjectsFactory& ObjectsFactory::instance()
|
||||
{
|
||||
static ObjectsFactory instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
std::shared_ptr<Object> ObjectsFactory::create_object(const std::wstring& class_name) const {
|
||||
try {
|
||||
return _factory_functions.at(class_name)();
|
||||
} catch (...) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#ifndef ComponentFactory_h__
|
||||
#define ComponentFactory_h__
|
||||
|
||||
#include "ForwardDeclarations.h"
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include "Object.h"
|
||||
|
||||
class ObjectsFactory {
|
||||
public:
|
||||
typedef std::function<std::shared_ptr<Object>()> factory_function;
|
||||
typedef std::map<std::wstring, factory_function> factory_container;
|
||||
|
||||
static ObjectsFactory& instance();
|
||||
void register_factory(factory_function factory, const std::wstring& class_name);
|
||||
std::shared_ptr<Object> create_object(const std::wstring& class_name) const;
|
||||
|
||||
template<typename T>
|
||||
std::shared_ptr<T> create_object(){
|
||||
return std::dynamic_pointer_cast<T>(create_object(T::type_name(), game_object));
|
||||
}
|
||||
private:
|
||||
factory_container _factory_functions;
|
||||
};
|
||||
|
||||
|
||||
template<typename T>
|
||||
struct ObjectFactory {
|
||||
typedef std::shared_ptr<T> object_pointer_t;
|
||||
ObjectFactory() {
|
||||
ObjectsFactory::instance().register_factory(&factory_func, T::type_name());
|
||||
}
|
||||
static std::shared_ptr<Object> factory_func() {
|
||||
return std::static_pointer_cast<Object>(T::create());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#define LOBJECT(__type_name, __base_type) \
|
||||
class __type_name;\
|
||||
static ObjectFactory<__type_name> __type_name##_factory;\
|
||||
class __type_name : public __base_type {\
|
||||
public:\
|
||||
static std::wstring type_name() {\
|
||||
return L#__type_name;\
|
||||
}\
|
||||
std::wstring get_type_name() const override{\
|
||||
return L#__type_name;\
|
||||
}\
|
||||
static std::shared_ptr<__type_name> create() {\
|
||||
auto result = std::shared_ptr<__type_name>(new __type_name());\
|
||||
result->Register();\
|
||||
result->construct();\
|
||||
return result;\
|
||||
}
|
||||
|
||||
|
||||
#endif // ComponentFactory_h__
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef OutputArchive_h__
|
||||
#define OutputArchive_h__
|
||||
|
||||
#include "Archive.h"
|
||||
#include "Object.h"
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
|
||||
class OutputArchive : public Archive {
|
||||
public:
|
||||
OutputArchive(std::shared_ptr<std::ostream> stream)
|
||||
:_stream(stream) {
|
||||
// _stream = std::ofstream(path, std::ios::binary);
|
||||
}
|
||||
|
||||
virtual bool is_loading() const override {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_type get_offset() const override {
|
||||
return _stream->tellp();
|
||||
}
|
||||
|
||||
void set_offset(size_type offset) override {
|
||||
_stream->seekp(offset, std::ios::beg);
|
||||
}
|
||||
|
||||
virtual void serialize(void* data, size_t size) override {
|
||||
_stream->write(reinterpret_cast<char*>(data), size);
|
||||
}
|
||||
|
||||
virtual void flush() override {
|
||||
if(_stream->good()){
|
||||
_stream->flush();
|
||||
}
|
||||
}
|
||||
|
||||
~OutputArchive() {
|
||||
flush();
|
||||
}
|
||||
std::shared_ptr<std::ostream> GetStream()
|
||||
{
|
||||
return _stream;
|
||||
}
|
||||
private:
|
||||
std::shared_ptr<std::ostream> _stream;
|
||||
std::vector<std::shared_ptr<Object>> _processed_objects;
|
||||
};
|
||||
|
||||
|
||||
#endif // OutputArchive_h__
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <stdint.h>
|
||||
#include "Renderer.h"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class RenderOutput {
|
||||
public:
|
||||
|
||||
virtual ~RenderOutput() {
|
||||
}
|
||||
virtual uint32_t GetWidth() = 0;
|
||||
virtual uint32_t GetHeight() = 0;
|
||||
|
||||
//virtual CD3DX12_CPU_DESCRIPTOR_HANDLE GetRenderTargetDescriptor(uint32_t id) = 0;
|
||||
//virtual CD3DX12_CPU_DESCRIPTOR_HANDLE GetDepthStencilDescriptor(uint32_t id) = 0;
|
||||
|
||||
//virtual ID3D12ResourcePtr GetRenderTarget(uint32_t id) = 0;
|
||||
//virtual ID3D12ResourcePtr GetDepthBuffer(uint32_t id) = 0;
|
||||
|
||||
virtual void Present() = 0;
|
||||
virtual void OnPreRender(RendererPtr renderer) = 0;
|
||||
|
||||
virtual uint32_t GetCurrentBufferIndex() = 0;
|
||||
virtual uint32_t GetBuffersCount() = 0;
|
||||
protected:
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<RenderOutput> RenderOutputPtr;
|
||||
@@ -0,0 +1,81 @@
|
||||
#include "RenderTargetOutput.h"
|
||||
/*
|
||||
std::shared_ptr<RenderTargetOutput> RenderTargetOutput::Create(RendererPtr renderer) {
|
||||
return std::shared_ptr<RenderTargetOutput>(new RenderTargetOutput(renderer));
|
||||
}
|
||||
RenderTargetOutput::RenderTargetOutput(RendererPtr renderer)
|
||||
:_renderer(renderer) {
|
||||
|
||||
}
|
||||
uint32_t RenderTargetOutput::GetWidth() {
|
||||
if (_rt == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
D3D11_TEXTURE2D_DESC desc;
|
||||
_rt->GetDesc(&desc);
|
||||
return desc.Width;
|
||||
}
|
||||
uint32_t RenderTargetOutput::GetHeight() {
|
||||
if (_rt == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
D3D11_TEXTURE2D_DESC desc;
|
||||
_rt->GetDesc(&desc);
|
||||
return desc.Height;
|
||||
}
|
||||
ID3D11RenderTargetViewPtr RenderTargetOutput::GetRenderTargetView() {
|
||||
return _rtView;
|
||||
}
|
||||
ID3D11DepthStencilViewPtr RenderTargetOutput::GetDepthStencilView() {
|
||||
return _dsView;
|
||||
}
|
||||
void RenderTargetOutput::Present() {
|
||||
|
||||
}
|
||||
void RenderTargetOutput::OnPreRender(RendererPtr renderer) {
|
||||
|
||||
}
|
||||
void RenderTargetOutput::Resize(uint32_t width, uint32_t height) {
|
||||
if (_rt != nullptr) {
|
||||
D3D11_TEXTURE2D_DESC desc;
|
||||
_rt->GetDesc(&desc);
|
||||
if (desc.Width == width && desc.Height == height) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_rt = nullptr;
|
||||
_ds = nullptr;
|
||||
_rtView = nullptr;
|
||||
_dsView = nullptr;
|
||||
|
||||
D3D11_TEXTURE2D_DESC desc = { 0 };
|
||||
desc.Width = width;
|
||||
desc.Height = height;
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.Usage = D3D11_USAGE_DEFAULT;
|
||||
desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
|
||||
desc.CPUAccessFlags = 0;
|
||||
desc.MiscFlags = 0;
|
||||
|
||||
auto hr = _renderer->GetDevice()->CreateTexture2D(&desc, nullptr, &_rt);
|
||||
hr = _renderer->GetDevice()->CreateRenderTargetView(_rt, nullptr, &_rtView);
|
||||
|
||||
D3D11_TEXTURE2D_DESC descDepth;
|
||||
descDepth.Width = width;
|
||||
descDepth.Height = height;
|
||||
descDepth.MipLevels = 1;
|
||||
descDepth.ArraySize = 1;
|
||||
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
|
||||
descDepth.SampleDesc.Count = 1;
|
||||
descDepth.SampleDesc.Quality = 0;
|
||||
descDepth.Usage = D3D11_USAGE_DEFAULT;
|
||||
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
|
||||
descDepth.CPUAccessFlags = 0;
|
||||
descDepth.MiscFlags = 0;
|
||||
hr = _renderer->GetDevice()->CreateTexture2D(&descDepth, NULL, &_ds);
|
||||
hr = _renderer->GetDevice()->CreateDepthStencilView(_ds, nullptr, &_dsView);
|
||||
}*/
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "RenderOutput.h"
|
||||
#include "Renderer.h"
|
||||
|
||||
|
||||
/*
|
||||
class RenderTargetOutput : public RenderOutput {
|
||||
public:
|
||||
static std::shared_ptr<RenderTargetOutput> Create(RendererPtr renderer);
|
||||
virtual uint32_t GetWidth() override;
|
||||
virtual uint32_t GetHeight() override;
|
||||
virtual ID3D11RenderTargetViewPtr GetRenderTargetView() override;
|
||||
virtual ID3D11DepthStencilViewPtr GetDepthStencilView() override;
|
||||
virtual void Present() override;
|
||||
virtual void OnPreRender(RendererPtr renderer) override;
|
||||
void Resize(uint32_t width, uint32_t height);
|
||||
private:
|
||||
RenderTargetOutput(RendererPtr renderer);
|
||||
ID3D11DepthStencilViewPtr _dsView;
|
||||
ID3D11RenderTargetViewPtr _rtView;
|
||||
ID3D11Texture2DPtr _ds;
|
||||
ID3D11Texture2DPtr _rt;
|
||||
RendererPtr _renderer;
|
||||
};*/
|
||||
@@ -0,0 +1,219 @@
|
||||
#include "Renderer.h"
|
||||
#include <iostream>
|
||||
#ifdef WIN32
|
||||
#include <vulkan/vulkan_win32.h>
|
||||
#endif
|
||||
|
||||
bool Renderer::Init() {
|
||||
_vulkanLibrary = std::make_shared<VulkanDynamicLibrary>();
|
||||
|
||||
auto layers = _vulkanLibrary->getSupportedLayers();
|
||||
|
||||
if (std::find_if(layers.begin(), layers.end(), [](VkLayerProperties& layer) { return std::string(layer.layerName) == "VK_LAYER_KHRONOS_validation"; }) == layers.end()) {
|
||||
throw std::runtime_error("Validation layer not found");
|
||||
}
|
||||
std::vector<std::string> instanceLayers;
|
||||
instanceLayers.push_back("VK_LAYER_KHRONOS_validation");
|
||||
|
||||
VulkanApplicationInfo appInfo("Test project", 1, "Test");
|
||||
|
||||
std::vector<std::string> requiredExtensions;
|
||||
requiredExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
|
||||
#ifdef WIN32
|
||||
requiredExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
|
||||
#endif
|
||||
|
||||
_vulkanInstance = _vulkanLibrary->createInstance(appInfo, 0, instanceLayers, requiredExtensions);
|
||||
|
||||
auto physicalDevices = _vulkanInstance->enumeratePhysicalDevices();
|
||||
auto physicalDevice = physicalDevices[0];
|
||||
|
||||
auto families = physicalDevice->getQueueFamilyProperties();
|
||||
|
||||
//Find queues
|
||||
for (std::uint32_t i = 0; i < static_cast<std::uint32_t>(families.size()); ++i) {
|
||||
if (!_presentQueueFamilyIndex.has_value() && families[i].queueCount > 0 && physicalDevice->getPresentationSupport(i)) {
|
||||
_presentQueueFamilyIndex = i;
|
||||
std::cout << "Found present queue: " << i << std::endl;
|
||||
}
|
||||
|
||||
if (!_graphicsQueueFamilyIndex.has_value() && families[i].isGraphics() && families[i].queueCount > 0) {
|
||||
std::cout << "Found render queue: " << i << std::endl;
|
||||
_graphicsQueueFamilyIndex = i;
|
||||
}
|
||||
|
||||
if (_graphicsQueueFamilyIndex.has_value() && _presentQueueFamilyIndex.has_value()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!_graphicsQueueFamilyIndex.has_value()) {
|
||||
throw std::runtime_error("Failed to find graphics queue family");
|
||||
}
|
||||
if (!_presentQueueFamilyIndex.has_value()) {
|
||||
throw std::runtime_error("Failed to find present queue family");
|
||||
}
|
||||
|
||||
|
||||
std::vector<VulkanQueueCreateInfo> queueDescriptions;
|
||||
if (_graphicsQueueFamilyIndex.has_value()) {
|
||||
queueDescriptions.push_back(VulkanQueueCreateInfo(_graphicsQueueFamilyIndex.value(), { 1.0f }));
|
||||
}
|
||||
if (_presentQueueFamilyIndex.has_value()) {
|
||||
if (_graphicsQueueFamilyIndex.value() != _presentQueueFamilyIndex.value()) {
|
||||
queueDescriptions.push_back(VulkanQueueCreateInfo(_presentQueueFamilyIndex.value(), { 1.0f }));
|
||||
}
|
||||
}
|
||||
|
||||
auto deviceExtensions = physicalDevice->getExtensions();
|
||||
std::vector<const char*> requiredDeviceExtensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
|
||||
auto swapChainExt = std::find_if(deviceExtensions.begin(), deviceExtensions.end(), [&requiredDeviceExtensions](VkExtensionProperties ext) { return std::string(requiredDeviceExtensions[0]) == ext.extensionName; });
|
||||
if (swapChainExt == deviceExtensions.end()) {
|
||||
throw std::runtime_error("Failed to find device with swap chain extension");
|
||||
}
|
||||
|
||||
_logicalDevice = PVulkanLogicalDevice(new VulkanLogicalDevice(physicalDevice, requiredDeviceExtensions, {}, queueDescriptions));
|
||||
|
||||
|
||||
//PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR p;
|
||||
//_vulkanInstance->functions().
|
||||
|
||||
/*
|
||||
|
||||
// Describe and create the command queue.
|
||||
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
|
||||
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
|
||||
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
|
||||
device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&_directCommandQueue));
|
||||
device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&_directCommandAllocator));
|
||||
device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, _directCommandAllocator.Get(), nullptr, IID_PPV_ARGS(&_directCommandList));
|
||||
|
||||
|
||||
//create descriptor heaps
|
||||
|
||||
constexpr size_t descriptorHeapsSize = 512;
|
||||
|
||||
D3D12_DESCRIPTOR_HEAP_DESC srvHeapDesc = {};
|
||||
srvHeapDesc.NumDescriptors = descriptorHeapsSize;
|
||||
srvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
|
||||
srvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
|
||||
hr = device->CreateDescriptorHeap(&srvHeapDesc, IID_PPV_ARGS(&_srvDescriptorsHeap));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
FatalError(L"Failed to create SRV descriptors heap");
|
||||
}
|
||||
_srvDescriptorsHeap->SetName(L"SRV Heap");
|
||||
|
||||
_srvDescriptorAllocator = std::make_shared<D3d12DescriptorHeapAllocator>(_srvDescriptorsHeap);
|
||||
*/
|
||||
return true;
|
||||
}
|
||||
|
||||
//std::shared_ptr<D3d12DescriptorHeapAllocator> Renderer::getSrvDescriptorsAllocator(){
|
||||
// return _srvDescriptorAllocator;
|
||||
//}
|
||||
|
||||
//ComPtr<ID3D12GraphicsCommandList> Renderer::BeginCommandList(){
|
||||
// auto HR = _directCommandAllocator->Reset();
|
||||
// HR = _directCommandList->Reset(_directCommandAllocator.Get(), nullptr);
|
||||
// return _directCommandList;
|
||||
//}
|
||||
|
||||
void Renderer::EndCommandList(){
|
||||
//_directCommandList->Close();
|
||||
//ID3D12CommandList* ppCommandLists[] = { _directCommandList.Get()};
|
||||
//_directCommandQueue->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists);
|
||||
}
|
||||
|
||||
void Renderer::WaitCommandsQueue(){
|
||||
//ID3D12Fence* fence = nullptr;
|
||||
//HRESULT hr = device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence));
|
||||
//if (hr)
|
||||
// return;
|
||||
//
|
||||
//auto eventHandle = CreateEvent(NULL, FALSE, FALSE, NULL);
|
||||
//
|
||||
//_directCommandQueue->Signal(fence, 1);
|
||||
//fence->SetEventOnCompletion(1, eventHandle);
|
||||
//WaitForSingleObject(eventHandle, INFINITE);
|
||||
//
|
||||
//CloseHandle(eventHandle);
|
||||
//fence->Release();
|
||||
}
|
||||
|
||||
PVulkanLogicalDevice Renderer::GetLogicalDevice() {
|
||||
return _logicalDevice;
|
||||
}
|
||||
|
||||
//std::vector<IDXGIOutput1Ptr> Renderer::EnumOutputs() {
|
||||
// IDXGIOutput1Ptr pOutput;
|
||||
// std::vector<IDXGIOutput1Ptr> vOutputs;
|
||||
// int outputId = 0;
|
||||
//
|
||||
// IDXGIOutputPtr o;
|
||||
// while (GetAdapter()->EnumOutputs(outputId, &o) != DXGI_ERROR_NOT_FOUND) {
|
||||
// o->QueryInterface(IID_PPV_ARGS(&pOutput));
|
||||
// vOutputs.push_back(pOutput);
|
||||
// pOutput = nullptr;
|
||||
// o = nullptr;
|
||||
// ++outputId;
|
||||
// }
|
||||
// return vOutputs;
|
||||
//}
|
||||
|
||||
// IDXGISwapChain1Ptr Renderer::CreateSwapChain(HWND window, bool windowed, uint32_t width, uint32_t height, uint32_t buffers_count, bool stereo) {
|
||||
// DXGI_SWAP_CHAIN_DESC1 swapChainDesc = { 0 };
|
||||
// swapChainDesc.Width = width;
|
||||
// swapChainDesc.Height = height;
|
||||
// swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
// swapChainDesc.Stereo = stereo;
|
||||
// swapChainDesc.SampleDesc.Count = 1;
|
||||
// swapChainDesc.SampleDesc.Quality = 0;
|
||||
// swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
// swapChainDesc.BufferCount = buffers_count;
|
||||
// swapChainDesc.Scaling = DXGI_SCALING_STRETCH;
|
||||
// swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
|
||||
// swapChainDesc.Flags = 0;
|
||||
// swapChainDesc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
|
||||
|
||||
// /*DXGI_SWAP_CHAIN_FULLSCREEN_DESC fullscreenDesc = { 0 };
|
||||
// fullscreenDesc.RefreshRate = {60 ,0};
|
||||
// fullscreenDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
|
||||
// fullscreenDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
|
||||
// fullscreenDesc.Windowed = windowed;*/
|
||||
|
||||
// IDXGISwapChain1Ptr result;
|
||||
// auto hr = GetFactory()->CreateSwapChainForHwnd(_directCommandQueue.Get(), window, &swapChainDesc, nullptr, nullptr, &result);
|
||||
// if (S_OK != hr){
|
||||
// FatalError(TEXT("Failed to create swap chain"));
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
|
||||
//auto backBufferFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
|
||||
//std::vector<DXGI_MODE_DESC1> Renderer::GetDisplayModes(DXGI_FORMAT format, IDXGIOutput1Ptr output) {
|
||||
// std::vector<DXGI_MODE_DESC1> result;
|
||||
// UINT numStereoModes = 0;
|
||||
// DXGI_MODE_DESC1 modes[1024];
|
||||
// auto enumFlags = DXGI_ENUM_MODES_STEREO;
|
||||
//
|
||||
// output->GetDisplayModeList1(format, enumFlags, &numStereoModes, 0);
|
||||
// output->GetDisplayModeList1(format, enumFlags, &numStereoModes, modes);
|
||||
//
|
||||
// for (UINT i = 0; i < numStereoModes; ++i) {
|
||||
// result.push_back(modes[i]);
|
||||
// }
|
||||
// return result;
|
||||
//}
|
||||
|
||||
//std::vector<DXGI_MODE_DESC1> Renderer::GetStereoModes(std::vector<DXGI_MODE_DESC1>& modes) {
|
||||
// std::vector<DXGI_MODE_DESC1> result;
|
||||
// for (auto& m : modes) {
|
||||
// if (m.Stereo) {
|
||||
// result.push_back(m);
|
||||
// }
|
||||
// }
|
||||
// return result;
|
||||
//}
|
||||
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include <dxgi1_3.h>
|
||||
#include <dxgi1_4.h>
|
||||
#include <dxgi1_5.h>
|
||||
#include <d3d11.h>
|
||||
#include <d3d12.h>
|
||||
#include <vector>
|
||||
#include "FatalError.h"
|
||||
#include <memory>
|
||||
#include <dxgidebug.h>
|
||||
#include <sstream>
|
||||
#include <RenderFramework/Vulkan/VulkanDynamicLibrary.h>
|
||||
#include <RenderFramework/Vulkan/VulkanLogicalDevice.h>
|
||||
#include <wrl.h>
|
||||
#include <optional>
|
||||
|
||||
using namespace Microsoft::WRL;
|
||||
|
||||
|
||||
class Renderer {
|
||||
public:
|
||||
bool Init();
|
||||
|
||||
PVulkanLogicalDevice GetLogicalDevice();
|
||||
|
||||
//ComPtr<IDXGIAdapter1> GetAdapter() {
|
||||
// return dxgiAdapter;
|
||||
//}
|
||||
//ComPtr<IDXGIFactory4> GetFactory() {
|
||||
// return dxgiFactory;
|
||||
//}
|
||||
|
||||
//ComPtr<ID3D12GraphicsCommandList> BeginCommandList();
|
||||
void EndCommandList();
|
||||
|
||||
//std::vector<IDXGIOutput1Ptr> EnumOutputs();
|
||||
//IDXGISwapChain1Ptr CreateSwapChain(HWND window, bool windowed, uint32_t width, uint32_t height, uint32_t buffers_count, bool stereo);
|
||||
//std::vector<DXGI_MODE_DESC1> GetDisplayModes(DXGI_FORMAT format, IDXGIOutput1Ptr output);
|
||||
//std::vector<DXGI_MODE_DESC1> GetStereoModes(std::vector<DXGI_MODE_DESC1>& modes);
|
||||
void WaitCommandsQueue();
|
||||
|
||||
//std::shared_ptr<D3d12DescriptorHeapAllocator> getSrvDescriptorsAllocator();
|
||||
private:
|
||||
std::shared_ptr<VulkanDynamicLibrary> _vulkanLibrary;
|
||||
IntrusivePtr<VulkanInstance> _vulkanInstance;
|
||||
PVulkanLogicalDevice _logicalDevice;
|
||||
|
||||
std::optional<std::uint32_t> _graphicsQueueFamilyIndex;
|
||||
std::optional<std::uint32_t> _presentQueueFamilyIndex;
|
||||
|
||||
//ComPtr<IDXGIInfoQueue> dxgiInfoQueue;
|
||||
//ID3D12DevicePtr device;
|
||||
//IDXGIAdapter1Ptr dxgiAdapter;
|
||||
//IDXGIFactory4Ptr dxgiFactory;
|
||||
//
|
||||
//ComPtr<ID3D12CommandQueue> _directCommandQueue;
|
||||
//ComPtr<ID3D12GraphicsCommandList> _directCommandList;
|
||||
//ComPtr<ID3D12CommandAllocator> _directCommandAllocator;
|
||||
//
|
||||
//ComPtr<ID3D12DescriptorHeap> _srvDescriptorsHeap;
|
||||
//std::shared_ptr<D3d12DescriptorHeapAllocator> _srvDescriptorAllocator;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<Renderer> RendererPtr;
|
||||
@@ -0,0 +1,14 @@
|
||||
#include "Scene.h"
|
||||
#include "GameObject.h"
|
||||
|
||||
void Scene::tick(float deltaTime) {
|
||||
for (auto go : objects) {
|
||||
go->tick(deltaTime);
|
||||
}
|
||||
}
|
||||
void Scene::SetName(const std::string& name) {
|
||||
_name = name;
|
||||
}
|
||||
std::string Scene::GetName() {
|
||||
return _name;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef Scene_h__
|
||||
#define Scene_h__
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "ForwardDeclarations.h"
|
||||
#include <string>
|
||||
#include "Gizmos.h"
|
||||
|
||||
class Scene {
|
||||
public:
|
||||
Scene() {
|
||||
_gizmos.Init();
|
||||
}
|
||||
std::vector<GameObjectPtr> objects;
|
||||
void tick(float deltaTime);
|
||||
void SetName(const std::string& name);
|
||||
std::string GetName();
|
||||
Gizmos& GetGizmos() {
|
||||
return _gizmos;
|
||||
}
|
||||
private:
|
||||
Gizmos _gizmos;
|
||||
std::string _name;
|
||||
};
|
||||
#endif // Scene_h__
|
||||
@@ -0,0 +1,308 @@
|
||||
#include "SceneRenderer.h"
|
||||
#include "GameObject.h"
|
||||
#include "Transform.h"
|
||||
#include "Texture.h"
|
||||
|
||||
SceneRenderer::SceneRenderer()
|
||||
{
|
||||
auto renderer = LEngine::Instance()->GetRenderer();
|
||||
auto device = renderer->GetLogicalDevice();
|
||||
|
||||
//create samplers heap
|
||||
//D3D12_DESCRIPTOR_HEAP_DESC samplers_heap_desc;
|
||||
//samplers_heap_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
|
||||
//samplers_heap_desc.NodeMask = 0;
|
||||
//samplers_heap_desc.NumDescriptors = 1;
|
||||
//samplers_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER;
|
||||
// HR_ASSERT(device->CreateDescriptorHeap(&samplers_heap_desc, IID_PPV_ARGS(&samplers_heap)));
|
||||
//samplers_heap->SetName(L"Samplers heap");
|
||||
|
||||
//create texture sampler
|
||||
/*D3D12_SAMPLER_DESC sampler_desc = {};
|
||||
sampler_desc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
sampler_desc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
|
||||
sampler_desc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
|
||||
sampler_desc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
|
||||
sampler_desc.MinLOD = 0;
|
||||
sampler_desc.MaxLOD = D3D12_FLOAT32_MAX;
|
||||
sampler_desc.MipLODBias = 0.0f;
|
||||
sampler_desc.MaxAnisotropy = 1;
|
||||
sampler_desc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;*/
|
||||
|
||||
// renderer->GetDevice()->CreateSampler(&sampler_desc, samplers_heap->GetCPUDescriptorHandleForHeapStart());
|
||||
}
|
||||
|
||||
void SceneRenderer::RenderScene(RendererPtr renderer, ScenePtr scene, CameraPtr camera, std::shared_ptr<RenderOutput> renderScreen) {
|
||||
camera->aspect = (float)renderScreen->GetWidth() / (float)renderScreen->GetHeight();
|
||||
renderScreen->OnPreRender(renderer);
|
||||
|
||||
D3D12_VIEWPORT viewport = {};
|
||||
viewport.TopLeftX = 0;
|
||||
viewport.TopLeftY = 0;
|
||||
viewport.Width = (float)renderScreen->GetWidth();
|
||||
viewport.Height = (float)renderScreen->GetHeight();
|
||||
viewport.MinDepth = 0;
|
||||
viewport.MaxDepth = 1;
|
||||
|
||||
|
||||
D3D12_RECT scissorRect = {};
|
||||
scissorRect.left = 0;
|
||||
scissorRect.top = 0;
|
||||
scissorRect.right = renderScreen->GetWidth();
|
||||
scissorRect.bottom = renderScreen->GetHeight();
|
||||
|
||||
// auto commandList = renderer->BeginCommandList();
|
||||
// auto rt = renderScreen->GetRenderTarget(renderScreen->GetCurrentBufferIndex());
|
||||
// commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(rt.Get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));
|
||||
// auto rtvHandle = renderScreen->GetRenderTargetDescriptor(renderScreen->GetCurrentBufferIndex());
|
||||
// auto dsvHandle = renderScreen->GetDepthStencilDescriptor(renderScreen->GetCurrentBufferIndex());
|
||||
// commandList->OMSetRenderTargets(1, &rtvHandle, FALSE, &dsvHandle);
|
||||
//
|
||||
// const float clearColor[] = { 0.1f, 0.1f, 0.14f, 1.0f };
|
||||
// commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
|
||||
// commandList->ClearDepthStencilView(dsvHandle, D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, 1.0f, 0, 0, nullptr);
|
||||
|
||||
//renderer->EndCommandList();
|
||||
// renderer->WaitCommandsQueue();
|
||||
|
||||
// commandList->RSSetViewports(1, &viewport);
|
||||
// commandList->RSSetScissorRects(1, &scissorRect);
|
||||
|
||||
std::vector<ComPtr<ID3D12RootSignature>> signatures;
|
||||
std::vector<ComPtr<ID3D12PipelineState>> pso;
|
||||
|
||||
|
||||
//draw all objects
|
||||
for (auto obj : scene->objects) {
|
||||
auto mr = obj->GetComponent<MeshRenderer>();
|
||||
if (mr != nullptr) {
|
||||
auto mesh = mr->GetMesh();
|
||||
if (mesh != nullptr) {
|
||||
for (uint32_t i = 0; i < mesh->GetSubmeshCount(); ++i) {
|
||||
auto mat = mr->GetMaterials()[i];
|
||||
auto submesh = mesh->GetSubmeshes()[i];
|
||||
if (mat == nullptr || submesh == nullptr) {
|
||||
continue;
|
||||
}
|
||||
auto& params = mat->GetParametersBlock();
|
||||
|
||||
//params.SetShaderResource()
|
||||
|
||||
//set system variables
|
||||
auto tf = obj->transform->GetGlobalTransform();
|
||||
auto view = camera->worldToCameraMatrix();
|
||||
auto proj = camera->projectionMatrix();
|
||||
params.SetValue("mWorld", transpose(tf));
|
||||
params.SetValue("mView", transpose(view));
|
||||
params.SetValue("mProjection", transpose(proj));
|
||||
|
||||
auto camPos = camera->transform()->GetGlobalPosition();
|
||||
params.SetValue("cameraWorldPos", float4(camPos.x(), camPos.y(), camPos.z(), 0.0f));
|
||||
|
||||
mat->Apply();
|
||||
|
||||
|
||||
//create single element ranges for constant buffers
|
||||
std::vector<D3D12_ROOT_PARAMETER> root_parameters;
|
||||
|
||||
auto compiled_params = mat->GetCompiledParameters(ShaderType::Vs);
|
||||
|
||||
for(auto p : compiled_params.buffers)
|
||||
{
|
||||
D3D12_ROOT_DESCRIPTOR rootCBVDescriptor;
|
||||
rootCBVDescriptor.RegisterSpace = 0;
|
||||
rootCBVDescriptor.ShaderRegister = p.bindPoint;
|
||||
|
||||
D3D12_ROOT_PARAMETER root_param = {};
|
||||
root_param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
|
||||
root_param.Descriptor = rootCBVDescriptor;
|
||||
|
||||
root_parameters.push_back(root_param);
|
||||
}
|
||||
|
||||
|
||||
size_t srvParamOffset = root_parameters.size();
|
||||
size_t srvParamCount = 0;
|
||||
|
||||
// auto srvHeapAllocator = renderer->getSrvDescriptorsAllocator();
|
||||
// auto srvHeap = srvHeapAllocator->getHeap();
|
||||
|
||||
std::vector<D3D12_DESCRIPTOR_RANGE> srvRanges[2];
|
||||
//size_t srvParamCount =
|
||||
//VS + PS
|
||||
for(int id = 0; id < 2; ++id){
|
||||
auto shader_params = mat->GetCompiledParameters((ShaderType)id);
|
||||
|
||||
for(auto r : shader_params.resources){
|
||||
if(r.type == D3D_SIT_TEXTURE)
|
||||
{
|
||||
auto param = mat->GetParametersBlock().GetParameterByName(r.name);
|
||||
auto texParam = std::static_pointer_cast<ShaderTextureParameter>(param);
|
||||
auto tex = texParam == nullptr ? nullptr : texParam->value;
|
||||
|
||||
D3D12_DESCRIPTOR_RANGE range{};
|
||||
range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
|
||||
range.RegisterSpace = 0;
|
||||
range.NumDescriptors = 1;
|
||||
range.BaseShaderRegister = r.bindPoint;
|
||||
// range.OffsetInDescriptorsFromTableStart = srvHeapAllocator->getDescriptorId(tex->srvDescriptor);
|
||||
srvRanges[id].push_back(range);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//for each shader => create root table parameter
|
||||
for(size_t s = 0; s < 2; ++s)
|
||||
{
|
||||
auto& ranges = srvRanges[s];
|
||||
if(!ranges.empty())
|
||||
{
|
||||
D3D12_ROOT_DESCRIPTOR_TABLE srvTable;
|
||||
srvTable.NumDescriptorRanges = ranges.size();
|
||||
srvTable.pDescriptorRanges = &ranges[0];
|
||||
|
||||
D3D12_ROOT_PARAMETER srvParameter;
|
||||
srvParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
|
||||
srvParameter.ShaderVisibility = ((ShaderType)s == ShaderType::Vs) ? D3D12_SHADER_VISIBILITY_VERTEX : D3D12_SHADER_VISIBILITY_PIXEL;
|
||||
srvParameter.DescriptorTable = srvTable;
|
||||
root_parameters.push_back(srvParameter);
|
||||
|
||||
srvParamCount++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// create a static sampler
|
||||
D3D12_STATIC_SAMPLER_DESC sampler = {};
|
||||
sampler.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT;
|
||||
sampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
|
||||
sampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
|
||||
sampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
|
||||
sampler.MipLODBias = 0;
|
||||
sampler.MaxAnisotropy = 0;
|
||||
sampler.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
|
||||
sampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
|
||||
sampler.MinLOD = 0.0f;
|
||||
sampler.MaxLOD = D3D12_FLOAT32_MAX;
|
||||
sampler.ShaderRegister = 0;
|
||||
sampler.RegisterSpace = 0;
|
||||
sampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
|
||||
|
||||
|
||||
//create root signature
|
||||
// CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc;
|
||||
// rootSignatureDesc.Init(root_parameters.size(), // we have 1 root parameter
|
||||
// &root_parameters[0], // a pointer to the beginning of our root parameters array
|
||||
// 1,
|
||||
// &sampler,
|
||||
// D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT | // we can deny shader stages here for better performance
|
||||
// D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS |
|
||||
// D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS |
|
||||
// D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS);
|
||||
|
||||
|
||||
ComPtr<ID3DBlob> signature;
|
||||
ComPtr<ID3DBlob> signatureErrors;
|
||||
// auto hr = D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &signatureErrors);
|
||||
// if (FAILED(hr)) {
|
||||
// FatalError(L"Failed to serialize root signature");
|
||||
// }
|
||||
|
||||
ComPtr<ID3D12RootSignature> rootSignature;
|
||||
// hr = renderer->GetDevice()->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&rootSignature));
|
||||
// if (FAILED(hr)) {
|
||||
// FatalError(L"Failed to create root signature");
|
||||
// }
|
||||
|
||||
signatures.push_back(rootSignature);
|
||||
|
||||
|
||||
//create PSO
|
||||
DXGI_SAMPLE_DESC sampleDesc = {};
|
||||
sampleDesc.Count = 1;
|
||||
|
||||
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
|
||||
psoDesc.pRootSignature = rootSignature.Get();
|
||||
psoDesc.InputLayout = mr->GetInputLayout();
|
||||
psoDesc.VS = mat->GetShader()->GetShader(ShaderType::Vs)->get_bytecode();
|
||||
psoDesc.PS = mat->GetShader()->GetShader(ShaderType::Ps)->get_bytecode();
|
||||
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
|
||||
psoDesc.RTVFormats[0] = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
psoDesc.SampleDesc = sampleDesc;
|
||||
psoDesc.SampleMask = 0xffffffff;
|
||||
// psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
|
||||
// psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
|
||||
// psoDesc.NumRenderTargets = 1;
|
||||
// psoDesc.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
|
||||
// psoDesc.DSVFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
|
||||
|
||||
auto device = renderer->GetLogicalDevice();
|
||||
|
||||
|
||||
// if (FAILED(hr)) {
|
||||
// FatalError(L"Failed to create PSO");
|
||||
// }
|
||||
|
||||
ComPtr<ID3D12PipelineState> pipelineStateObject;
|
||||
// hr = device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&pipelineStateObject));
|
||||
pipelineStateObject->SetName(L"PSO");
|
||||
|
||||
|
||||
pso.push_back(pipelineStateObject);
|
||||
|
||||
|
||||
//commandList = renderer->BeginCommandList();
|
||||
|
||||
// commandList->SetPipelineState(pipelineStateObject.Get());
|
||||
|
||||
// commandList->SetGraphicsRootSignature(rootSignature.Get());
|
||||
|
||||
// commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
// auto vbView = mesh->GetVertexBufferView();
|
||||
|
||||
// commandList->IASetVertexBuffers(0, 1, &vbView);
|
||||
|
||||
// auto ibView = submesh->GetIndexBufferView();
|
||||
// commandList->IASetIndexBuffer(&ibView);
|
||||
|
||||
//commandList->SetGraphicsRootShaderResourceView()
|
||||
|
||||
|
||||
// ID3D12DescriptorHeap* descriptorHeaps[] = { srvHeap.Get() };
|
||||
// commandList->SetDescriptorHeaps(_countof(descriptorHeaps), descriptorHeaps);
|
||||
|
||||
|
||||
// for (size_t p = srvParamOffset; p < srvParamOffset + srvParamCount; ++p) {
|
||||
// commandList->SetGraphicsRootDescriptorTable(p, srvHeap->GetGPUDescriptorHandleForHeapStart());
|
||||
// }
|
||||
|
||||
// //apply constant buffers
|
||||
// for (size_t k = 0; k < compiled_params.buffers.size(); ++k) {
|
||||
// commandList->SetGraphicsRootConstantBufferView(k, compiled_params.buffers[k].buffer->GetGPUVirtualAddress());
|
||||
// }
|
||||
|
||||
|
||||
// commandList->DrawIndexedInstanced(submesh->GetIndicesCount(), 1, 0, 0, 0);
|
||||
|
||||
//renderer->EndCommandList();
|
||||
//renderer->WaitCommandsQueue();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//commandList = renderer->BeginCommandList();
|
||||
// commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(rt.Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT));
|
||||
renderer->EndCommandList();
|
||||
renderer->WaitCommandsQueue();
|
||||
|
||||
|
||||
|
||||
renderScreen->Present();
|
||||
//signatures.clear();
|
||||
//renderer->reportLiveObjects();
|
||||
|
||||
scene->GetGizmos().PrepareRender();
|
||||
scene->GetGizmos().DrawAll(camera);
|
||||
scene->GetGizmos().Clear();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include "LEngine.h"
|
||||
#include "Mesh.h"
|
||||
#include "MeshGenerator.h"
|
||||
#include "Scene.h"
|
||||
#include "Renderer.h"
|
||||
#include "Camera.h"
|
||||
#include "FatalError.h"
|
||||
#include "MeshRenderer.h"
|
||||
|
||||
#pragma comment(lib, "d3d11.lib")
|
||||
|
||||
class SceneRenderer {
|
||||
public:
|
||||
SceneRenderer();
|
||||
void RenderScene(RendererPtr renderer, ScenePtr scene, CameraPtr camera, std::shared_ptr<RenderOutput> renderScreen);
|
||||
private:
|
||||
ComPtr<ID3D12DescriptorHeap> samplers_heap;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,240 @@
|
||||
#include "Shader.h"
|
||||
#include <d3dcompiler.h>
|
||||
#include <fstream>
|
||||
#include "File.h"
|
||||
#include "LEngine.h"
|
||||
#include "AssetManager.h"
|
||||
#include "ShaderInclude.h"
|
||||
#include <filesystem>
|
||||
#include <SPIRV/GlslangToSpv.h>
|
||||
#include <glslang/Public/ShaderLang.h>
|
||||
#include <glslang/Public/ResourceLimits.h>
|
||||
|
||||
#pragma comment(lib, "D3dcompiler.lib")
|
||||
|
||||
static TBuiltInResource getShaderResources() {
|
||||
TBuiltInResource resources = {};
|
||||
resources.maxLights = 32;
|
||||
resources.maxClipPlanes = 6;
|
||||
resources.maxTextureUnits = 32;
|
||||
resources.maxTextureCoords = 32;
|
||||
resources.maxVertexAttribs = 64;
|
||||
resources.maxVertexUniformComponents = 4096;
|
||||
resources.maxVaryingFloats = 64;
|
||||
resources.maxVertexTextureImageUnits = 32;
|
||||
resources.maxCombinedTextureImageUnits = 80;
|
||||
resources.maxTextureImageUnits = 32;
|
||||
resources.maxFragmentUniformComponents = 4096;
|
||||
resources.maxDrawBuffers = 32;
|
||||
resources.maxVertexUniformVectors = 128;
|
||||
resources.maxVaryingVectors = 8;
|
||||
resources.maxFragmentUniformVectors = 16;
|
||||
resources.maxVertexOutputVectors = 16;
|
||||
resources.maxFragmentInputVectors = 15;
|
||||
resources.minProgramTexelOffset = -8;
|
||||
resources.maxProgramTexelOffset = 7;
|
||||
resources.maxClipDistances = 8;
|
||||
resources.maxComputeWorkGroupCountX = 65535;
|
||||
resources.maxComputeWorkGroupCountY = 65535;
|
||||
resources.maxComputeWorkGroupCountZ = 65535;
|
||||
resources.maxComputeWorkGroupSizeX = 1024;
|
||||
resources.maxComputeWorkGroupSizeY = 1024;
|
||||
resources.maxComputeWorkGroupSizeZ = 64;
|
||||
resources.maxComputeUniformComponents = 1024;
|
||||
resources.maxComputeTextureImageUnits = 16;
|
||||
resources.maxComputeImageUniforms = 8;
|
||||
resources.maxComputeAtomicCounters = 8;
|
||||
resources.maxComputeAtomicCounterBuffers = 1;
|
||||
resources.maxVaryingComponents = 60;
|
||||
resources.maxVertexOutputComponents = 64;
|
||||
resources.maxGeometryInputComponents = 64;
|
||||
resources.maxGeometryOutputComponents = 128;
|
||||
resources.maxFragmentInputComponents = 128;
|
||||
resources.maxImageUnits = 8;
|
||||
resources.maxCombinedImageUnitsAndFragmentOutputs = 8;
|
||||
resources.maxCombinedShaderOutputResources = 8;
|
||||
resources.maxImageSamples = 0;
|
||||
resources.maxVertexImageUniforms = 0;
|
||||
resources.maxTessControlImageUniforms = 0;
|
||||
resources.maxTessEvaluationImageUniforms = 0;
|
||||
resources.maxGeometryImageUniforms = 0;
|
||||
resources.maxFragmentImageUniforms = 8;
|
||||
resources.maxCombinedImageUniforms = 8;
|
||||
resources.maxGeometryTextureImageUnits = 16;
|
||||
resources.maxGeometryOutputVertices = 256;
|
||||
resources.maxGeometryTotalOutputComponents = 1024;
|
||||
resources.maxGeometryUniformComponents = 1024;
|
||||
resources.maxGeometryVaryingComponents = 64;
|
||||
resources.maxTessControlInputComponents = 128;
|
||||
resources.maxTessControlOutputComponents = 128;
|
||||
resources.maxTessControlTextureImageUnits = 16;
|
||||
resources.maxTessControlUniformComponents = 1024;
|
||||
resources.maxTessControlTotalOutputComponents = 4096;
|
||||
resources.maxTessEvaluationInputComponents = 128;
|
||||
resources.maxTessEvaluationOutputComponents = 128;
|
||||
resources.maxTessEvaluationTextureImageUnits = 16;
|
||||
resources.maxTessEvaluationUniformComponents = 1024;
|
||||
resources.maxTessPatchComponents = 120;
|
||||
resources.maxPatchVertices = 32;
|
||||
resources.maxTessGenLevel = 64;
|
||||
resources.maxViewports = 16;
|
||||
resources.maxVertexAtomicCounters = 0;
|
||||
resources.maxTessControlAtomicCounters = 0;
|
||||
resources.maxTessEvaluationAtomicCounters = 0;
|
||||
resources.maxGeometryAtomicCounters = 0;
|
||||
resources.maxFragmentAtomicCounters = 8;
|
||||
resources.maxCombinedAtomicCounters = 8;
|
||||
resources.maxAtomicCounterBindings = 1;
|
||||
resources.maxVertexAtomicCounterBuffers = 0;
|
||||
resources.maxTessControlAtomicCounterBuffers = 0;
|
||||
resources.maxTessEvaluationAtomicCounterBuffers = 0;
|
||||
resources.maxGeometryAtomicCounterBuffers = 0;
|
||||
resources.maxFragmentAtomicCounterBuffers = 1;
|
||||
resources.maxCombinedAtomicCounterBuffers = 1;
|
||||
resources.maxAtomicCounterBufferSize = 16384;
|
||||
resources.maxTransformFeedbackBuffers = 4;
|
||||
resources.maxTransformFeedbackInterleavedComponents = 64;
|
||||
resources.maxCullDistances = 8;
|
||||
resources.maxCombinedClipAndCullDistances = 8;
|
||||
resources.maxSamples = 4;
|
||||
resources.limits.nonInductiveForLoops = true;
|
||||
resources.limits.whileLoops = true;
|
||||
resources.limits.doWhileLoops = true;
|
||||
resources.limits.generalUniformIndexing = true;
|
||||
resources.limits.generalAttributeMatrixVectorIndexing = true;
|
||||
resources.limits.generalVaryingIndexing = true;
|
||||
resources.limits.generalSamplerIndexing = true;
|
||||
resources.limits.generalVariableIndexing = true;
|
||||
resources.limits.generalConstantMatrixVectorIndexing = true;
|
||||
return resources;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static EShLanguage getShaderLanguage(ShaderType stage) {
|
||||
switch(stage){
|
||||
case ShaderType::Vs:
|
||||
return EShLanguage::EShLangVertex;
|
||||
case ShaderType::Ps:
|
||||
return EShLanguage::EShLangFragment;
|
||||
default:
|
||||
throw std::runtime_error("Not yet implemented");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void* Shader::CompileSHader(const std::filesystem::path& filePath, ShaderType stage) {
|
||||
auto filePathString = filePath.string();
|
||||
|
||||
|
||||
std::string shaderCode;
|
||||
FileReadAllText(filePath.wstring(), shaderCode);
|
||||
|
||||
auto language = getShaderLanguage(stage);
|
||||
glslang::TProgram program;
|
||||
glslang::TShader shader(language);
|
||||
|
||||
const char* moduleSource = shaderCode.c_str();
|
||||
|
||||
const char* moduleName = filePathString.c_str();
|
||||
shader.setStringsWithLengthsAndNames(&moduleSource, nullptr, &moduleName, 1);
|
||||
|
||||
shader.setEnvInput(glslang::EShSourceGlsl, language, glslang::EShClientVulkan, 110);
|
||||
auto clientVersion = glslang::EShTargetVulkan_1_1;
|
||||
shader.setEnvClient(glslang::EShClientVulkan, clientVersion);
|
||||
shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_0);
|
||||
|
||||
ShaderInclude includer;
|
||||
|
||||
|
||||
auto resources = getShaderResources();
|
||||
|
||||
std::string processedShader;
|
||||
auto messages = static_cast<EShMessages>(EShMsgSpvRules | EShMsgVulkanRules | EShMsgDefault | EShMsgDebugInfo);
|
||||
if (!shader.preprocess(&resources, clientVersion, ENoProfile, false, false, messages, &processedShader, includer)) {
|
||||
std::cout << "Shader preprocess failed" << std::endl;
|
||||
std::cout << shader.getInfoLog() << std::endl;
|
||||
std::cout << shader.getInfoDebugLog() << std::endl;
|
||||
throw std::runtime_error("Shader preprocess failed");
|
||||
}
|
||||
|
||||
if (!shader.parse(&resources, clientVersion, true, messages, includer)) {
|
||||
std::cout << "Shader parse failed" << std::endl;
|
||||
std::cout << shader.getInfoLog() << std::endl;
|
||||
std::cout << shader.getInfoDebugLog() << std::endl;
|
||||
throw std::runtime_error("Shader parse failed");
|
||||
}
|
||||
|
||||
program.addShader(&shader);
|
||||
|
||||
if (!program.link(messages) || !program.mapIO()) {
|
||||
throw std::runtime_error("Shader link failed");
|
||||
}
|
||||
|
||||
program.buildReflection();
|
||||
|
||||
program.dumpReflection();
|
||||
|
||||
|
||||
glslang::SpvOptions spvOptions;
|
||||
//DEBUG
|
||||
//spvOptions.generateDebugInfo = true;
|
||||
//spvOptions.disableOptimizer = true;
|
||||
//spvOptions.optimizeSize = false;
|
||||
|
||||
spvOptions.generateDebugInfo = false;
|
||||
spvOptions.disableOptimizer = false;
|
||||
spvOptions.optimizeSize = true;
|
||||
|
||||
spv::SpvBuildLogger logger;
|
||||
std::vector<uint32_t> spirv;
|
||||
GlslangToSpv(*program.getIntermediate(static_cast<EShLanguage>(language)), spirv, &logger, &spvOptions);
|
||||
|
||||
|
||||
//program.get
|
||||
|
||||
std::cout << "SPIR-V generation messages: " << logger.getAllMessages() << std::endl;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Shader::Shader(ComPtr<ID3D12Device> dev) :device(dev) {
|
||||
|
||||
}
|
||||
|
||||
ShaderPtr Shader::Create() {
|
||||
return std::shared_ptr<Shader>(nullptr);
|
||||
}
|
||||
|
||||
ShaderPtr Shader::Create(const std::filesystem::path& filePath) {
|
||||
auto shader = Create();
|
||||
shader->FromFile(AssetManager::Instance()->GetFullAssetPath(filePath));
|
||||
return shader;
|
||||
}
|
||||
|
||||
void Shader::FromFile(const std::filesystem::path& shaderFilePath) {
|
||||
|
||||
CompileSHader(shaderFilePath, ShaderType::Vs);
|
||||
//auto vsBlob = CompileSHader(shaderFilePath, "main_vs", "vs_5_0");
|
||||
//if(vsBlob != nullptr) {
|
||||
// ShaderDataPtr shader(new ShaderData());
|
||||
// shader->blob = vsBlob;
|
||||
// shaders[(int)ShaderType::Vs] = shader;
|
||||
//}
|
||||
//
|
||||
//auto psBlob = CompileSHader(shaderFilePath, "main_ps", "ps_5_0");
|
||||
//if(psBlob != nullptr) {
|
||||
// ShaderDataPtr shader(new ShaderData());
|
||||
// shader->blob = psBlob;
|
||||
// shaders[(int)ShaderType::Ps] = shader;
|
||||
//}
|
||||
}
|
||||
|
||||
ShaderDataPtr Shader::GetShader(ShaderType type) {
|
||||
return shaders[(uint32_t)type];
|
||||
}
|
||||
|
||||
ComPtr<ID3D12Device> Shader::GetDevice() {
|
||||
return device;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
#include "ForwardDeclarations.h"
|
||||
#include <d3d12.h>
|
||||
#include <d3d11.h>
|
||||
#include <wrl.h>
|
||||
#include <filesystem>
|
||||
|
||||
using namespace Microsoft::WRL;
|
||||
|
||||
class ShaderData {
|
||||
public:
|
||||
ComPtr<ID3DBlob> blob;
|
||||
|
||||
D3D12_SHADER_BYTECODE get_bytecode() {
|
||||
D3D12_SHADER_BYTECODE bytecode = {};
|
||||
bytecode.BytecodeLength = blob->GetBufferSize();
|
||||
bytecode.pShaderBytecode = blob->GetBufferPointer();
|
||||
return bytecode;
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<ShaderData> ShaderDataPtr;
|
||||
|
||||
enum class ShaderType {
|
||||
Vs,
|
||||
Ps,
|
||||
Gs,
|
||||
Hull,
|
||||
Domain,
|
||||
Count
|
||||
};
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
std::wstring class_name() const {
|
||||
return L"Shader";
|
||||
|
||||
|
||||
}
|
||||
static ShaderPtr Create();
|
||||
static ShaderPtr Create(const std::filesystem::path& filePath);
|
||||
void FromFile(const std::filesystem::path& shaderFilePath);
|
||||
ShaderDataPtr GetShader(ShaderType type);
|
||||
ComPtr<ID3D12Device> GetDevice();
|
||||
private:
|
||||
Shader(ComPtr<ID3D12Device> dev);
|
||||
ShaderDataPtr shaders[(uint32_t)ShaderType::Count];
|
||||
ComPtr<ID3D12Device> device;
|
||||
void* CompileSHader(const std::filesystem::path& filename, ShaderType stage);
|
||||
};
|
||||
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include <d3dcommon.h>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <codecvt>
|
||||
#include <locale>
|
||||
#include "File.h"
|
||||
#include <memory>
|
||||
#include <stack>
|
||||
#include <filesystem>
|
||||
|
||||
#include <SPIRV/GlslangToSpv.h>
|
||||
#include <glslang/Public/ShaderLang.h>
|
||||
|
||||
|
||||
class ShaderInclude : public glslang::TShader::Includer {
|
||||
public:
|
||||
//ShaderInclude(std::wstring localDirectory){
|
||||
// _localPathStack.push(localDirectory);
|
||||
//}
|
||||
|
||||
|
||||
|
||||
struct IncludeContext {
|
||||
std::filesystem::path path;
|
||||
std::vector<char> data;
|
||||
};
|
||||
|
||||
IncludeResult *includeLocal(const char *headerName, const char *includerName, size_t inclusionDepth) override {
|
||||
std::filesystem::path includerPath(includerName);
|
||||
auto includerDirectory = includerPath.parent_path();
|
||||
auto fullIncludePath = includerDirectory / headerName;
|
||||
|
||||
IncludeContext* context = new IncludeContext();
|
||||
FileReadAllBytes(fullIncludePath.string(), context->data);
|
||||
|
||||
IncludeResult* result = new IncludeResult(fullIncludePath.string(), context->data.data(), context->data.size(), context);
|
||||
_files.push(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
IncludeResult* includeSystem(const char* /*headerName*/, const char* /*includerName*/, size_t /*inclusionDepth*/) override {
|
||||
throw std::runtime_error("Not implemented");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void releaseInclude(IncludeResult* inc) override {
|
||||
auto* context = reinterpret_cast<IncludeContext*>(inc->userData);
|
||||
if (context != nullptr) {
|
||||
delete context;
|
||||
}
|
||||
|
||||
if (_files.top() == inc) {
|
||||
_files.pop();
|
||||
delete inc;
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Something went wrong");
|
||||
}
|
||||
}
|
||||
private:
|
||||
std::stack<IncludeResult*> _files;
|
||||
};
|
||||
@@ -0,0 +1,156 @@
|
||||
#pragma once
|
||||
|
||||
#include "Shader.h"
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include "ForwardDeclarations.h"
|
||||
|
||||
class ShaderBufferVariable {
|
||||
public:
|
||||
std::string name;
|
||||
uint32_t offset;
|
||||
uint32_t size;
|
||||
|
||||
ShaderBufferVariable() {
|
||||
|
||||
}
|
||||
|
||||
ShaderBufferVariable(const std::string& _name, uint32_t _offset, uint32_t _size)
|
||||
:name(_name), size(_size), offset(_offset){
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
class ShaderResourceVariable {
|
||||
public:
|
||||
std::string name;
|
||||
uint32_t bindPoint;
|
||||
D3D_SHADER_INPUT_TYPE type;
|
||||
};
|
||||
|
||||
class ShaderBufferContext {
|
||||
public:
|
||||
//ID3D12ResourcePtr buffer;
|
||||
size_t size;
|
||||
std::vector<ShaderBufferVariable> variables;
|
||||
std::string name;
|
||||
uint32_t bindPoint;
|
||||
D3D_SHADER_INPUT_TYPE type;
|
||||
};
|
||||
|
||||
class ShaderParametersContext {
|
||||
public:
|
||||
void Clear() {
|
||||
buffers.clear();
|
||||
resources.clear();
|
||||
}
|
||||
std::vector<ShaderBufferContext> buffers;
|
||||
std::vector<ShaderResourceVariable> resources;
|
||||
};
|
||||
|
||||
class ShaderParameter {
|
||||
public:
|
||||
virtual ~ShaderParameter()
|
||||
{
|
||||
}
|
||||
|
||||
ShaderParameter(const std::string& _name) :name(_name) {
|
||||
|
||||
}
|
||||
virtual void* GetPointer() = 0;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<ShaderParameter> ShaderParameterPtr;
|
||||
|
||||
template<typename T>
|
||||
class ShaderValueParameter : public ShaderParameter {
|
||||
public:
|
||||
ShaderValueParameter(const std::string& _name) :ShaderParameter(_name)
|
||||
{
|
||||
|
||||
}
|
||||
void* GetPointer() override {
|
||||
return &value;
|
||||
}
|
||||
T value;
|
||||
};
|
||||
|
||||
class ShaderResourceParameter : public ShaderParameter {
|
||||
public:
|
||||
ShaderResourceParameter(const std::string& _name) :ShaderParameter(_name) {
|
||||
|
||||
}
|
||||
// ShaderResourceParameter(const std::string& _name, ID3D11ShaderResourceViewPtr view) : value(view), ShaderParameter(_name) {
|
||||
// name = _name;
|
||||
// }
|
||||
//ID3D11ShaderResourceViewPtr value;
|
||||
void* GetPointer() override {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
class ShaderTextureParameter : public ShaderParameter {
|
||||
public:
|
||||
ShaderTextureParameter(const std::string& _name) :ShaderParameter(_name) {
|
||||
|
||||
}
|
||||
ShaderTextureParameter(const std::string& _name, TexturePtr view) : ShaderParameter(_name), value(view){
|
||||
name = _name;
|
||||
}
|
||||
TexturePtr value;
|
||||
void* GetPointer() override {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<ShaderResourceParameter> ShaderResourceParameterPtr;
|
||||
typedef std::shared_ptr<ShaderTextureParameter> ShaderTextureParameterPtr;
|
||||
|
||||
class ShaderParametersBlock {
|
||||
public:
|
||||
void SetTexture(const std::string& name, TexturePtr texture) {
|
||||
/*ID3D11ResourcePtr resourcePtr;
|
||||
|
||||
D3D11_TEXTURE2D_DESC desc;
|
||||
texture->GetDesc(&desc);
|
||||
texture.QueryInterface(&resourcePtr);
|
||||
SetShaderResource(name, resourcePtr);*/
|
||||
|
||||
ShaderTextureParameterPtr parameter(new ShaderTextureParameter(name, texture));
|
||||
for (auto it = parameters.begin(); it != parameters.end(); ++it) {
|
||||
if ((*it)->name == name) {
|
||||
*it = parameter;
|
||||
return;
|
||||
}
|
||||
}
|
||||
parameters.push_back(parameter);
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void SetValue(const std::string& name, const T& value) {
|
||||
std::shared_ptr<ShaderValueParameter<T>> param(new ShaderValueParameter<T>(name));
|
||||
param->value = value;
|
||||
for (auto it = parameters.begin(); it != parameters.end(); ++it) {
|
||||
if ((*it)->name == name) {
|
||||
*it = param;
|
||||
return;
|
||||
}
|
||||
}
|
||||
parameters.push_back(param);
|
||||
}
|
||||
|
||||
ShaderParameterPtr GetParameterByName(const std::string& name) {
|
||||
for (auto p : parameters) {
|
||||
if (p->name == name) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
private:
|
||||
std::vector<ShaderParameterPtr> parameters;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "LEngine.h"
|
||||
#include <memory>
|
||||
|
||||
enum class MeshTopology {
|
||||
TriangleList,
|
||||
TriangleStrip,
|
||||
LineList,
|
||||
LineStrip
|
||||
};
|
||||
|
||||
class Submesh {
|
||||
public:
|
||||
typedef uint32_t index_count_t;
|
||||
typedef uint32_t index_t;
|
||||
typedef std::vector<index_t> index_array_t;
|
||||
|
||||
Submesh()
|
||||
:_indexCount(0)
|
||||
{
|
||||
|
||||
}
|
||||
//ID3D12ResourcePtr GetIndexBuffer() const{
|
||||
// return _indexBuffer;
|
||||
//}
|
||||
|
||||
MeshTopology GetTopology() const{
|
||||
return _topology;
|
||||
}
|
||||
|
||||
D3D12_INDEX_BUFFER_VIEW GetIndexBufferView()
|
||||
{
|
||||
return _indexBufferView;
|
||||
}
|
||||
|
||||
void Set(index_array_t indices, MeshTopology topology) {
|
||||
// _indexCount = indices.size();
|
||||
// _topology = topology;
|
||||
//_indexBuffer = nullptr;
|
||||
|
||||
// if (_indexCount == 0) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// D3D11_BUFFER_DESC desc = { 0 };
|
||||
// D3D11_SUBRESOURCE_DATA data = { 0 };
|
||||
|
||||
//index buffer
|
||||
// desc.Usage = D3D11_USAGE_DEFAULT;
|
||||
// desc.ByteWidth = sizeof(indices[0]) * indices.size();
|
||||
// desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
||||
// desc.CPUAccessFlags = 0;
|
||||
// desc.MiscFlags = 0;
|
||||
|
||||
// data.pSysMem = &indices[0];
|
||||
// data.SysMemPitch = 0;
|
||||
// data.SysMemSlicePitch = 0;
|
||||
|
||||
// auto renderer = LEngine::Instance()->GetRenderer();
|
||||
// auto device = renderer->GetDevice();
|
||||
// auto cmdList = renderer->BeginCommandList();
|
||||
|
||||
//create temporal upload resource
|
||||
// ComPtr<ID3D12Resource> uploadBuffer;
|
||||
// device->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
|
||||
// D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES,
|
||||
// &CD3DX12_RESOURCE_DESC::Buffer(desc.ByteWidth),
|
||||
// D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&uploadBuffer));
|
||||
|
||||
|
||||
//upload data to temporal resource
|
||||
// void* p;
|
||||
// uploadBuffer->Map(0, nullptr, &p);
|
||||
// memcpy(p, &indices[0], desc.ByteWidth);
|
||||
// uploadBuffer->Unmap(0, nullptr);
|
||||
|
||||
// device->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
|
||||
// D3D12_HEAP_FLAG_NONE,
|
||||
// &CD3DX12_RESOURCE_DESC::Buffer(desc.ByteWidth),
|
||||
// D3D12_RESOURCE_STATE_COPY_DEST,
|
||||
// nullptr,
|
||||
// IID_PPV_ARGS(&_indexBuffer));
|
||||
|
||||
|
||||
// _indexBufferView.BufferLocation = _indexBuffer->GetGPUVirtualAddress();
|
||||
// _indexBufferView.SizeInBytes = desc.ByteWidth;
|
||||
// _indexBufferView.Format = DXGI_FORMAT_R32_UINT;
|
||||
|
||||
|
||||
// cmdList->CopyBufferRegion(_indexBuffer.Get(), 0, uploadBuffer.Get(), 0, desc.ByteWidth);
|
||||
|
||||
// CD3DX12_RESOURCE_BARRIER barriers[1] = {
|
||||
// CD3DX12_RESOURCE_BARRIER::Transition(_indexBuffer.Get(),
|
||||
// D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_INDEX_BUFFER)
|
||||
// };
|
||||
|
||||
|
||||
// cmdList->ResourceBarrier(1, barriers);
|
||||
// renderer->EndCommandList();
|
||||
// renderer->WaitCommandsQueue();
|
||||
}
|
||||
index_count_t GetIndicesCount() const {
|
||||
return _indexCount;
|
||||
}
|
||||
private:
|
||||
index_count_t _indexCount;
|
||||
MeshTopology _topology;
|
||||
//ID3D12ResourcePtr _indexBuffer;
|
||||
D3D12_INDEX_BUFFER_VIEW _indexBufferView;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<Submesh> SubmeshPtr;
|
||||
@@ -0,0 +1,122 @@
|
||||
#pragma once
|
||||
|
||||
#include "RenderOutput.h"
|
||||
#include "Renderer.h"
|
||||
|
||||
|
||||
class SwapChainOutput : public RenderOutput {
|
||||
public:
|
||||
SwapChainOutput(RendererPtr renderer, HWND window, uint32_t width, uint32_t height, bool stereo) : _activeBuffer(0){
|
||||
/*_buffersCount = 2;
|
||||
_swapChain = renderer->CreateSwapChain(window, true, width, height, _buffersCount, stereo);
|
||||
auto device = renderer->GetDevice();
|
||||
|
||||
//create RTV descriptors heap
|
||||
D3D12_DESCRIPTOR_HEAP_DESC heap_desc = {};
|
||||
heap_desc.NumDescriptors = _buffersCount;
|
||||
heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
|
||||
heap_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
|
||||
|
||||
HR_ASSERT_MSG(device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&rtvDescriptorHeap)), L"Failed to allocate RTV descriptors heap");
|
||||
rtvDescriptorHeap->SetName(L"RTV Heap");
|
||||
|
||||
rtvDescSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
|
||||
CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(rtvDescriptorHeap->GetCPUDescriptorHandleForHeapStart());
|
||||
|
||||
|
||||
//create DSV descriptors heap
|
||||
heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
|
||||
HR_ASSERT_MSG(device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&dsvDescriptorHeap)), L"Failed to allocate DSV descriptors heap");
|
||||
dsvDescriptorHeap->SetName(L"DSV Heap");
|
||||
|
||||
dsvDescSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
|
||||
CD3DX12_CPU_DESCRIPTOR_HANDLE dsvHandle(dsvDescriptorHeap->GetCPUDescriptorHandleForHeapStart());
|
||||
|
||||
for(uint32_t i = 0; i < _buffersCount; ++i){
|
||||
//get back buffer
|
||||
ID3D12ResourcePtr _backBuffer;
|
||||
if (FAILED(_swapChain->GetBuffer(i, IID_PPV_ARGS(&_backBuffer)))){
|
||||
FatalError(TEXT("Failed to get swap chain's bffer"));
|
||||
}
|
||||
device->CreateRenderTargetView(_backBuffer.Get(), nullptr, rtvHandle);
|
||||
|
||||
|
||||
|
||||
//create depth buffer
|
||||
D3D12_CLEAR_VALUE depthOptimizedClearValue = {};
|
||||
depthOptimizedClearValue.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
|
||||
depthOptimizedClearValue.DepthStencil.Depth = 1.0f;
|
||||
depthOptimizedClearValue.DepthStencil.Stencil = 0;
|
||||
|
||||
ComPtr<ID3D12Resource> depthStencilBuffer;
|
||||
device->CreateCommittedResource(
|
||||
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
|
||||
D3D12_HEAP_FLAG_NONE,
|
||||
&CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D24_UNORM_S8_UINT, width, height, 1, 0, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
|
||||
D3D12_RESOURCE_STATE_DEPTH_WRITE,
|
||||
&depthOptimizedClearValue,
|
||||
IID_PPV_ARGS(&depthStencilBuffer)
|
||||
);
|
||||
|
||||
device->CreateDepthStencilView(depthStencilBuffer.Get(), nullptr, dsvHandle);
|
||||
|
||||
rtvHandle.Offset(1, rtvDescSize);
|
||||
dsvHandle.Offset(1, dsvDescSize);
|
||||
_buffers.push_back({ _backBuffer, depthStencilBuffer });
|
||||
} */
|
||||
}
|
||||
virtual uint32_t GetWidth() override{
|
||||
return GetDesc().Width;
|
||||
}
|
||||
|
||||
virtual uint32_t GetHeight() override
|
||||
{
|
||||
return GetDesc().Height;
|
||||
}
|
||||
|
||||
// CD3DX12_CPU_DESCRIPTOR_HANDLE GetRenderTargetDescriptor(uint32_t id) override {
|
||||
// return CD3DX12_CPU_DESCRIPTOR_HANDLE(rtvDescriptorHeap->GetCPUDescriptorHandleForHeapStart(), id, rtvDescSize);
|
||||
// }
|
||||
// CD3DX12_CPU_DESCRIPTOR_HANDLE GetDepthStencilDescriptor(uint32_t id) override {
|
||||
// return CD3DX12_CPU_DESCRIPTOR_HANDLE(dsvDescriptorHeap->GetCPUDescriptorHandleForHeapStart(), id, dsvDescSize);
|
||||
// }
|
||||
|
||||
// ID3D12ResourcePtr GetRenderTarget(uint32_t id) override{
|
||||
// return _buffers[id].first;
|
||||
// }
|
||||
|
||||
// ID3D12ResourcePtr GetDepthBuffer(uint32_t id) override {
|
||||
// return _buffers[id].second;
|
||||
// }
|
||||
|
||||
virtual void Present() override{
|
||||
//_swapChain->Present(0, 0);
|
||||
_activeBuffer = (_activeBuffer + 1) % _buffersCount;
|
||||
}
|
||||
|
||||
virtual void OnPreRender(RendererPtr renderer) override{
|
||||
|
||||
}
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC1 GetDesc() {
|
||||
DXGI_SWAP_CHAIN_DESC1 desc;
|
||||
//_swapChain->GetDesc1(&desc);
|
||||
return desc;
|
||||
}
|
||||
uint32_t GetCurrentBufferIndex() override{
|
||||
return _activeBuffer;
|
||||
}
|
||||
uint32_t GetBuffersCount() override{
|
||||
return _buffersCount;
|
||||
}
|
||||
private:
|
||||
// std::vector<std::pair<ID3D12ResourcePtr, ID3D12ResourcePtr>> _buffers;
|
||||
uint32_t _activeBuffer;
|
||||
uint32_t _buffersCount;
|
||||
UINT rtvDescSize;
|
||||
UINT dsvDescSize;
|
||||
// ComPtr<ID3D12DescriptorHeap> rtvDescriptorHeap;
|
||||
// ComPtr<ID3D12DescriptorHeap> dsvDescriptorHeap;
|
||||
// IDXGISwapChain1Ptr _swapChain;
|
||||
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
class Texture
|
||||
{
|
||||
public:
|
||||
Texture(/*ID3D12ResourcePtr _resource, D3D12_CPU_DESCRIPTOR_HANDLE _srvDescriptor*/)
|
||||
/*:resource(_resource), srvDescriptor(_srvDescriptor)*/
|
||||
{
|
||||
|
||||
}
|
||||
//ID3D12ResourcePtr resource;
|
||||
//D3D12_CPU_DESCRIPTOR_HANDLE srvDescriptor;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef TextureImporter_h__
|
||||
#define TextureImporter_h__
|
||||
|
||||
#include "AssetImporter.h"
|
||||
#include <filesystem>
|
||||
|
||||
/*
|
||||
class TextureImporter : public AssetImporter {
|
||||
public:
|
||||
virtual bool can_import(const std::wstring& path) const override {
|
||||
std::tr2::sys::path file_path(path);
|
||||
if (!file_path.has_extension()) {
|
||||
return false;
|
||||
} else {
|
||||
return file_path.extension() == ".bmp";
|
||||
}
|
||||
}
|
||||
|
||||
virtual bool load_parameters(const std::wstring& params) override {
|
||||
throw std::logic_error("The method or operation is not implemented.");
|
||||
}
|
||||
|
||||
virtual std::wstring save_parameters() override {
|
||||
throw std::logic_error("The method or operation is not implemented.");
|
||||
}
|
||||
|
||||
virtual std::wstring name() override {
|
||||
return L"TextureImporter";
|
||||
}
|
||||
|
||||
};*/
|
||||
|
||||
#endif // TextureImporter_h__
|
||||
@@ -0,0 +1,94 @@
|
||||
#ifndef Transform_h__
|
||||
#define Transform_h__
|
||||
|
||||
#include <lmath/lmath.h>
|
||||
#include "Mathf.h"
|
||||
#include "ObjectsFactory.h"
|
||||
#include "Component.h"
|
||||
#include "Archive.h"
|
||||
|
||||
using namespace lm;
|
||||
|
||||
LOBJECT(Transform, Component)
|
||||
public:
|
||||
std::shared_ptr<Transform> parent;
|
||||
Quaternion_f _rotation;
|
||||
float3 _position;
|
||||
float3 _scale;
|
||||
|
||||
Transform()
|
||||
:_scale(1.0f,1.0f,1.0f), _position(0.0f,0.0f,0.0f), _rotation(0.0f,0.0f,0.0f,1.0f){
|
||||
}
|
||||
|
||||
void serialize(Archive& archive) override{
|
||||
Component::serialize(archive);
|
||||
archive << parent;
|
||||
archive << _rotation << _position << _scale;
|
||||
}
|
||||
|
||||
std::wstring class_name() const {
|
||||
return L"Transform";
|
||||
}
|
||||
|
||||
void SetLocalPosition(const float3& position) {
|
||||
_position = position;
|
||||
}
|
||||
float3 GetLocalPosition() {
|
||||
return _position;
|
||||
}
|
||||
|
||||
float3 forward() const{
|
||||
return GetGlobalTransform().z().xyz();
|
||||
}
|
||||
|
||||
float3 right() const {
|
||||
return GetGlobalTransform().x().xyz();
|
||||
}
|
||||
|
||||
float3 up() const {
|
||||
return GetGlobalTransform().y().xyz();
|
||||
}
|
||||
|
||||
void SetLocalRotation(const Quaternion_f& rotation) {
|
||||
_rotation = rotation;
|
||||
}
|
||||
|
||||
Quaternion_f GetLocalRotation() {
|
||||
return _rotation;
|
||||
}
|
||||
|
||||
void SetLocalScale(const float3& scale) {
|
||||
_scale = scale;
|
||||
}
|
||||
|
||||
float3 GetLocalScale() {
|
||||
return _scale;
|
||||
}
|
||||
|
||||
float4x4 GetLocalTransform() const{
|
||||
auto scaleMatrix = matrix4x4Scale(_scale);
|
||||
auto rotationMatrix = matrix4x4RotationQuaternion(_rotation);
|
||||
auto positionMatrix = matrix4x4Translation(_position);
|
||||
return lm::mul(lm::mul(scaleMatrix, rotationMatrix), positionMatrix);
|
||||
}
|
||||
|
||||
float4x4 GetGlobalTransform() const{
|
||||
if(parent == nullptr) {
|
||||
return GetLocalTransform();
|
||||
}
|
||||
return lm::mul(GetLocalTransform(), parent->GetGlobalTransform());
|
||||
}
|
||||
|
||||
float3 GetGlobalPosition() {
|
||||
auto tf = GetGlobalTransform();
|
||||
return float3(tf[0][3], tf[1][3], tf[2][3]);
|
||||
}
|
||||
|
||||
void RotateLocal(const Quaternion_f& rotation) {
|
||||
_rotation = lm::mul(_rotation, rotation);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // Transform_h__
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef Vertex_h__
|
||||
#define Vertex_h__
|
||||
|
||||
#include <lmath/lmath.h>
|
||||
|
||||
using namespace lm;
|
||||
struct Vertex {
|
||||
float4 position;
|
||||
float3 normal;
|
||||
float2 uv0;
|
||||
float2 uv1;
|
||||
|
||||
Vertex() {
|
||||
|
||||
}
|
||||
|
||||
Vertex(float4 _pos, float3 _norm) {
|
||||
position = _pos;
|
||||
normal = _norm;
|
||||
}
|
||||
Vertex(float4 _pos, float3 _norm, float2 _uv0) {
|
||||
position = _pos;
|
||||
normal = _norm;
|
||||
uv0 = _uv0;
|
||||
}
|
||||
};
|
||||
#endif // Vertex_h__
|
||||
@@ -0,0 +1,775 @@
|
||||
#include "Application.h"
|
||||
|
||||
|
||||
#include "VertexShader_unknown.h"
|
||||
#include "FragmentShader_unknown.h"
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include "VulkanDeviceFunctions.h"
|
||||
|
||||
Application::Application() {
|
||||
vulkan = std::make_shared<VulkanDynamicLibrary>();
|
||||
auto extensions = vulkan->getSupportedExtensions();
|
||||
auto layers = vulkan->getSupportedLayers();
|
||||
|
||||
if(std::find_if(layers.begin(), layers.end(), [](VkLayerProperties& layer){ return std::string(layer.layerName) == "VK_LAYER_KHRONOS_validation"; } ) == layers.end()){
|
||||
throw std::runtime_error("Validation layer not found");
|
||||
}
|
||||
|
||||
VulkanApplicationInfo appInfo("Test project", 1, "Test");
|
||||
|
||||
std::vector<std::string> instanceLayers;
|
||||
instanceLayers.push_back("VK_LAYER_KHRONOS_validation");
|
||||
|
||||
auto instance = vulkan->createInstance(appInfo, 0, instanceLayers, RenderWindow::getRequiredExtensionNames());
|
||||
|
||||
auto physicalDevices = instance->enumeratePhysicalDevices();
|
||||
device = physicalDevices[0];
|
||||
|
||||
|
||||
auto properties = device->getProperties();
|
||||
auto features = device->getFeatures();
|
||||
auto families = device->getQueueFamilyProperties();
|
||||
|
||||
window = new RenderWindow(instance, "VulkanWrapperSandbox");
|
||||
|
||||
std::cout << "Device queue families count: " << device->getQueueFamiliesCount() << std::endl;
|
||||
|
||||
//Find queues
|
||||
|
||||
for (uint32_t i = 0; i < device->getQueueFamiliesCount(); ++i) {
|
||||
if (presentQueueFamilyIndex == -1 && families[i].queueCount > 0 && window->canPresent(device, i)) {
|
||||
presentQueueFamilyIndex = i;
|
||||
std::cout << "Found present queue: " << i << std::endl;
|
||||
}
|
||||
|
||||
if (renderQueueFamilyIndex == -1 && families[i].isGraphics() && families[i].queueCount > 0) {
|
||||
std::cout << "Found render queue: " << i << std::endl;
|
||||
renderQueueFamilyIndex = i;
|
||||
}
|
||||
|
||||
if (renderQueueFamilyIndex != -1 && presentQueueFamilyIndex != -1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<VulkanQueueCreateInfo> queueDescriptions;
|
||||
queueDescriptions.push_back(VulkanQueueCreateInfo(presentQueueFamilyIndex, { 1.0f }));
|
||||
if (presentQueueFamilyIndex != renderQueueFamilyIndex) {
|
||||
queueDescriptions.push_back(VulkanQueueCreateInfo(renderQueueFamilyIndex, { 1.0f }));
|
||||
}
|
||||
|
||||
auto deviceExtensions = device->getExtensions();
|
||||
|
||||
std::vector<const char*> requiredDeviceExtensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
|
||||
|
||||
auto swapChainExt = std::find_if(deviceExtensions.begin(), deviceExtensions.end(), [&requiredDeviceExtensions](VkExtensionProperties ext) { return std::string(requiredDeviceExtensions[0]) == ext.extensionName; });
|
||||
if (swapChainExt == deviceExtensions.end()) {
|
||||
throw std::runtime_error("Failed to find device with swap chain extension");
|
||||
}
|
||||
|
||||
logicalDevice = IntrusivePtr<VulkanLogicalDevice>(new VulkanLogicalDevice(device, requiredDeviceExtensions, {}, queueDescriptions));
|
||||
|
||||
|
||||
VkCommandPoolCreateInfo poolCreateInfo = {};
|
||||
poolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
||||
poolCreateInfo.queueFamilyIndex = presentQueueFamilyIndex;
|
||||
poolCreateInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
||||
|
||||
if (logicalDevice->functions().vkCreateCommandPool(&poolCreateInfo, nullptr, &presentCommandPool) != VK_SUCCESS) {
|
||||
std::cerr << "failed to create command queue for presentation queue family" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
std::cout << "created command pool for presentation queue family" << std::endl;
|
||||
}
|
||||
|
||||
if (renderQueueFamilyIndex == presentQueueFamilyIndex) {
|
||||
renderCommandPool = presentCommandPool;
|
||||
}
|
||||
else {
|
||||
poolCreateInfo.queueFamilyIndex = renderQueueFamilyIndex;
|
||||
if (logicalDevice->functions().vkCreateCommandPool(&poolCreateInfo, nullptr, &renderCommandPool) != VK_SUCCESS) {
|
||||
std::cerr << "failed to create command queue for render queue family" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
std::cout << "created command pool for render queue family" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
queues = logicalDevice->queues();
|
||||
|
||||
std::vector<Vertex> vertexData =
|
||||
{
|
||||
{ { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f } },
|
||||
{ { -1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f } },
|
||||
{ { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } }
|
||||
};
|
||||
_vertexBuffer = createBuffer(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, vertexData.data(), sizeof(vertexData[0]) * vertexData.size(), logicalDevice, device, queues[0], renderCommandPool);
|
||||
|
||||
std::vector<uint32_t> indexData = { 0, 1, 2 };
|
||||
_indexBuffer = createBuffer(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, indexData.data(), sizeof(indexData[0]) * indexData.size(), logicalDevice, device, queues[0], renderCommandPool);
|
||||
|
||||
createSwapChain();
|
||||
|
||||
//for(std::size_t i = 0; i < swapChain->images)
|
||||
//_frameBuffers
|
||||
|
||||
//logicalDevice->functions().vkGetDeviceQueue()
|
||||
|
||||
for (auto image : swapChain->images()) {
|
||||
_imageViews.push_back(IntrusivePtr<VulkanImageView>(new VulkanImageView(logicalDevice, VulkanImageViewCreateInfo::image2D(image, swapChain->format()))));
|
||||
}
|
||||
|
||||
presentCommandBuffers.resize(swapChain->images().size());
|
||||
VkCommandBufferAllocateInfo allocInfo = {};
|
||||
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||
allocInfo.commandPool = presentCommandPool;
|
||||
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||
allocInfo.commandBufferCount = static_cast<uint32_t>(swapChain->images().size());
|
||||
if (logicalDevice->functions().vkAllocateCommandBuffers(&allocInfo, presentCommandBuffers.data()) != VK_SUCCESS) {
|
||||
std::cerr << "failed to allocate presentation command buffers" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
VkFenceCreateInfo fenceInfo{};
|
||||
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
||||
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
||||
_cmdBufferFences.resize(swapChain->images().size());
|
||||
for(std::size_t i = 0; i < swapChain->images().size(); ++i){
|
||||
if(logicalDevice->functions().vkCreateFence(&fenceInfo, nullptr, &_cmdBufferFences[i]) != VK_SUCCESS) {
|
||||
throw std::runtime_error("failed to create synchronization objects for a frame!");
|
||||
}
|
||||
|
||||
_uniformBuffers.push_back(IntrusivePtr<UniformBuffer>(new UniformBuffer(logicalDevice, sizeof(UboPerObjectData))));
|
||||
}
|
||||
//
|
||||
|
||||
|
||||
|
||||
compileShaders();
|
||||
|
||||
|
||||
|
||||
VkAttachmentDescription colorAttachment = {};
|
||||
colorAttachment.format = swapChain->format();
|
||||
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||||
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
||||
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
|
||||
VkAttachmentReference colorAttachmentRef = {};
|
||||
colorAttachmentRef.attachment = 0;
|
||||
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
|
||||
VkSubpassDescription subpass = {};
|
||||
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
||||
subpass.colorAttachmentCount = 1;
|
||||
subpass.pColorAttachments = &colorAttachmentRef;
|
||||
|
||||
VkRenderPassCreateInfo renderPassInfo = {};
|
||||
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
||||
renderPassInfo.attachmentCount = 1;
|
||||
renderPassInfo.pAttachments = &colorAttachment;
|
||||
renderPassInfo.subpassCount = 1;
|
||||
renderPassInfo.pSubpasses = &subpass;
|
||||
|
||||
|
||||
//Create render pass
|
||||
if (logicalDevice->functions().vkCreateRenderPass(&renderPassInfo, nullptr, &_renderPass) != VK_SUCCESS) {
|
||||
throw std::runtime_error("failed to create render pass!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (auto& imageView : _imageViews) {
|
||||
VkImageView attachments[] = {
|
||||
imageView->native()
|
||||
};
|
||||
|
||||
VkFramebufferCreateInfo framebufferInfo{};
|
||||
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
||||
framebufferInfo.renderPass = _renderPass;
|
||||
framebufferInfo.attachmentCount = 1;
|
||||
framebufferInfo.pAttachments = attachments;
|
||||
framebufferInfo.width = swapChain->extent().width;
|
||||
framebufferInfo.height = swapChain->extent().height;
|
||||
framebufferInfo.layers = 1;
|
||||
|
||||
//
|
||||
_frameBuffers.push_back(IntrusivePtr<VulkanFrameBuffer>(new VulkanFrameBuffer(logicalDevice, framebufferInfo)));
|
||||
}
|
||||
|
||||
|
||||
//Descriptor set layout for UBO
|
||||
VkDescriptorSetLayoutBinding uboLayoutBinding{};
|
||||
uboLayoutBinding.binding = 0;
|
||||
uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
uboLayoutBinding.descriptorCount = 1;
|
||||
uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
||||
uboLayoutBinding.pImmutableSamplers = nullptr; // Optional
|
||||
|
||||
VkDescriptorSetLayoutCreateInfo layoutInfo{};
|
||||
layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
||||
layoutInfo.bindingCount = 1;
|
||||
layoutInfo.pBindings = &uboLayoutBinding;
|
||||
|
||||
_uboDescriptorSetLayout = IntrusivePtr<VulkanDescriptorSetLayout>(new VulkanDescriptorSetLayout(logicalDevice, layoutInfo));
|
||||
|
||||
//Create pipeline
|
||||
createPipeline();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Application::createPipeline(){
|
||||
VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
|
||||
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
||||
vertShaderStageInfo.module = vs->native();
|
||||
vertShaderStageInfo.pName = "main";
|
||||
vertShaderStageInfo.pSpecializationInfo = nullptr;
|
||||
|
||||
VkPipelineShaderStageCreateInfo fragShaderStageInfo{};
|
||||
fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||
fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
||||
fragShaderStageInfo.module = ps->native();
|
||||
fragShaderStageInfo.pName = "main";
|
||||
|
||||
VkPipelineShaderStageCreateInfo shaderStages[] = {vertShaderStageInfo, fragShaderStageInfo};
|
||||
|
||||
|
||||
//Bind vertex data
|
||||
VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
|
||||
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
||||
|
||||
auto bindingDescription = Vertex::getBindingDescription();
|
||||
vertexInputInfo.vertexBindingDescriptionCount = 1;
|
||||
vertexInputInfo.pVertexBindingDescriptions = &bindingDescription; // Optional
|
||||
|
||||
auto attributeDescriptions = Vertex::getAttributeDescriptions();
|
||||
vertexInputInfo.vertexAttributeDescriptionCount = attributeDescriptions.size();
|
||||
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data(); // Optional
|
||||
|
||||
//Input assembly
|
||||
VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
|
||||
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
||||
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
||||
inputAssembly.primitiveRestartEnable = VK_FALSE;
|
||||
|
||||
//Viewport
|
||||
VkViewport viewport = {};
|
||||
viewport.x = 0.0f;
|
||||
viewport.y = 0.0f;
|
||||
viewport.width = (float)swapChain->extent().width;
|
||||
viewport.height = (float)swapChain->extent().height;
|
||||
viewport.minDepth = 0.0f;
|
||||
viewport.maxDepth = 1.0f;
|
||||
|
||||
//Scissor
|
||||
VkRect2D scissor = {};
|
||||
scissor.offset = { 0, 0 };
|
||||
scissor.extent = swapChain->extent();
|
||||
|
||||
//Pipeline Viewport State
|
||||
VkPipelineViewportStateCreateInfo viewportState = {};
|
||||
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
||||
viewportState.viewportCount = 1;
|
||||
viewportState.pViewports = &viewport;
|
||||
viewportState.scissorCount = 1;
|
||||
viewportState.pScissors = &scissor;
|
||||
|
||||
//Rasterizer
|
||||
VkPipelineRasterizationStateCreateInfo rasterizer = {};
|
||||
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
||||
rasterizer.depthClampEnable = VK_FALSE;
|
||||
rasterizer.rasterizerDiscardEnable = VK_FALSE;
|
||||
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
|
||||
rasterizer.lineWidth = 1.0f;
|
||||
rasterizer.cullMode = VK_CULL_MODE_NONE; //VK_CULL_MODE_BACK_BIT;
|
||||
rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
|
||||
rasterizer.depthBiasEnable = VK_FALSE;
|
||||
rasterizer.depthBiasConstantFactor = 0.0f; // Optional
|
||||
rasterizer.depthBiasClamp = 0.0f; // Optional
|
||||
rasterizer.depthBiasSlopeFactor = 0.0f; // Optional
|
||||
|
||||
//Multisample
|
||||
VkPipelineMultisampleStateCreateInfo multisampling = {};
|
||||
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
||||
multisampling.sampleShadingEnable = VK_FALSE;
|
||||
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
||||
multisampling.minSampleShading = 1.0f; // Optional
|
||||
multisampling.pSampleMask = nullptr; // Optional
|
||||
multisampling.alphaToCoverageEnable = VK_FALSE; // Optional
|
||||
multisampling.alphaToOneEnable = VK_FALSE; // Optional
|
||||
|
||||
//Pipeline layout
|
||||
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
|
||||
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||
pipelineLayoutInfo.setLayoutCount = 1;
|
||||
VkDescriptorSetLayout layout = _uboDescriptorSetLayout->native();
|
||||
pipelineLayoutInfo.pSetLayouts = &layout;
|
||||
pipelineLayoutInfo.pushConstantRangeCount = 0;
|
||||
|
||||
auto pipelineLayout = IntrusivePtr<VulkanPipelineLayout>(new VulkanPipelineLayout(logicalDevice, pipelineLayoutInfo));
|
||||
|
||||
//Blend state attachment
|
||||
VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
|
||||
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
|
||||
colorBlendAttachment.blendEnable = VK_FALSE;
|
||||
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE; // Optional
|
||||
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional
|
||||
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD; // Optional
|
||||
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; // Optional
|
||||
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional
|
||||
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD; // Optional
|
||||
|
||||
//Blend state
|
||||
VkPipelineColorBlendStateCreateInfo colorBlending{};
|
||||
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
||||
colorBlending.logicOpEnable = VK_FALSE;
|
||||
colorBlending.logicOp = VK_LOGIC_OP_COPY;
|
||||
colorBlending.attachmentCount = 1;
|
||||
colorBlending.pAttachments = &colorBlendAttachment;
|
||||
colorBlending.blendConstants[0] = 0.0f;
|
||||
colorBlending.blendConstants[1] = 0.0f;
|
||||
colorBlending.blendConstants[2] = 0.0f;
|
||||
colorBlending.blendConstants[3] = 0.0f;
|
||||
|
||||
|
||||
//Pipeline
|
||||
VkGraphicsPipelineCreateInfo pipelineInfo{};
|
||||
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
||||
pipelineInfo.stageCount = 2;
|
||||
pipelineInfo.pStages = shaderStages;
|
||||
pipelineInfo.pVertexInputState = &vertexInputInfo;
|
||||
pipelineInfo.pInputAssemblyState = &inputAssembly;
|
||||
pipelineInfo.pViewportState = &viewportState;
|
||||
pipelineInfo.pRasterizationState = &rasterizer;
|
||||
pipelineInfo.pMultisampleState = &multisampling;
|
||||
pipelineInfo.pColorBlendState = &colorBlending;
|
||||
pipelineInfo.pDynamicState = nullptr;
|
||||
pipelineInfo.layout = pipelineLayout->native();
|
||||
pipelineInfo.renderPass = _renderPass;
|
||||
pipelineInfo.subpass = 0;
|
||||
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
|
||||
|
||||
_pipeline = IntrusivePtr<VulkanPipeline>(new VulkanPipeline(logicalDevice, pipelineLayout, pipelineInfo));
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Application::compileShaders() {
|
||||
auto vertexBinary = ShaderCompiler::compile(vert, ShaderCompilerStage::vert);
|
||||
|
||||
|
||||
spv_reflect::ShaderModule sm(vertexBinary.binary.size(), vertexBinary.binary.data());
|
||||
auto name = sm.GetEntryPointName();
|
||||
|
||||
|
||||
|
||||
uint32_t count = 0;
|
||||
auto res = sm.EnumerateDescriptorSets(&count, nullptr);
|
||||
std::vector<SpvReflectDescriptorSet*> descriptorSets(count);
|
||||
res = sm.EnumerateDescriptorSets(&count, descriptorSets.data());
|
||||
|
||||
for(uint32_t i = 0; i < count; ++i){
|
||||
SpvReflectDescriptorSet* v = descriptorSets[i];
|
||||
std::cout << "V" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
auto fragmentBinary = ShaderCompiler::compile(frag, ShaderCompilerStage::frag);
|
||||
|
||||
|
||||
|
||||
vs = IntrusivePtr<VulkanShaderModule>(new VulkanShaderModule(logicalDevice, vertexBinary.binary.data(), vertexBinary.binary.size()));
|
||||
ps = IntrusivePtr<VulkanShaderModule>(new VulkanShaderModule(logicalDevice, fragmentBinary.binary.data(), fragmentBinary.binary.size()));
|
||||
}
|
||||
|
||||
void Application::createSwapChain() {
|
||||
auto formats = VulkanSwapChain::getSurfaceFormats(device, window->getSurface());
|
||||
auto caps = VulkanSwapChain::getSurfaceCapabilities(device, window->getSurface());
|
||||
auto presentModes = VulkanSwapChain::getPresentModes(device, window->getSurface());
|
||||
|
||||
VkSwapchainCreateInfoKHR createInfo = {};
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
||||
createInfo.pNext = NULL;
|
||||
createInfo.surface = window->getSurface();
|
||||
createInfo.imageFormat = formats[0].format;
|
||||
createInfo.imageColorSpace = formats[0].colorSpace;
|
||||
createInfo.minImageCount = caps.minImageCount + 1;
|
||||
createInfo.imageExtent = caps.currentExtent;
|
||||
createInfo.preTransform = caps.currentTransform;
|
||||
createInfo.presentMode = VK_PRESENT_MODE_MAILBOX_KHR;
|
||||
createInfo.imageArrayLayers = 1;
|
||||
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
||||
|
||||
std::vector<uint32_t> swapChainQueueFamilies;
|
||||
swapChainQueueFamilies.push_back(renderQueueFamilyIndex);
|
||||
swapChainQueueFamilies.push_back(presentQueueFamilyIndex);
|
||||
|
||||
if (presentQueueFamilyIndex == renderQueueFamilyIndex) {
|
||||
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
createInfo.queueFamilyIndexCount = 0; // Optional
|
||||
createInfo.pQueueFamilyIndices = nullptr; // Optional
|
||||
}
|
||||
else {
|
||||
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
||||
createInfo.queueFamilyIndexCount = 2;
|
||||
createInfo.pQueueFamilyIndices = &swapChainQueueFamilies[0];
|
||||
}
|
||||
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
||||
createInfo.clipped = VK_TRUE;
|
||||
createInfo.oldSwapchain = VK_NULL_HANDLE;
|
||||
|
||||
|
||||
swapChain = new VulkanSwapChain(logicalDevice, createInfo);
|
||||
}
|
||||
|
||||
void Application::recordCommandBuffer(VkCommandBuffer cmdBuffer, uint32_t cmdBufferIndex, uint32_t imageIndex){
|
||||
VkCommandBufferBeginInfo beginInfo = {};
|
||||
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
|
||||
|
||||
VkImageSubresourceRange subResourceRange = {};
|
||||
subResourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
subResourceRange.baseMipLevel = 0;
|
||||
subResourceRange.levelCount = 1;
|
||||
subResourceRange.baseArrayLayer = 0;
|
||||
subResourceRange.layerCount = 1;
|
||||
|
||||
|
||||
VkImageMemoryBarrier presentToClearBarrier = {};
|
||||
presentToClearBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
||||
presentToClearBarrier.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
||||
presentToClearBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||
presentToClearBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
presentToClearBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
presentToClearBarrier.srcQueueFamilyIndex = presentQueueFamilyIndex;
|
||||
presentToClearBarrier.dstQueueFamilyIndex = presentQueueFamilyIndex;
|
||||
presentToClearBarrier.image = swapChain->images()[imageIndex];
|
||||
presentToClearBarrier.subresourceRange = subResourceRange;
|
||||
|
||||
// Change layout of image to be optimal for presenting
|
||||
VkImageMemoryBarrier clearToPresentBarrier = {};
|
||||
clearToPresentBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
||||
clearToPresentBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||
clearToPresentBarrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
||||
clearToPresentBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; // VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
clearToPresentBarrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
clearToPresentBarrier.srcQueueFamilyIndex = presentQueueFamilyIndex;
|
||||
clearToPresentBarrier.dstQueueFamilyIndex = presentQueueFamilyIndex;
|
||||
clearToPresentBarrier.image = swapChain->images()[imageIndex];
|
||||
clearToPresentBarrier.subresourceRange = subResourceRange;
|
||||
|
||||
// Record command buffer
|
||||
logicalDevice->functions().vkBeginCommandBuffer(presentCommandBuffers[imageIndex], &beginInfo);
|
||||
|
||||
logicalDevice->functions().vkCmdPipelineBarrier(presentCommandBuffers[imageIndex], VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &presentToClearBarrier);
|
||||
|
||||
//logicalDevice->functions().vkCmdClearColorImage(presentCommandBuffers[i], swapChain->images()[i], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clearColors[i % 3], 1, &subResourceRange);
|
||||
|
||||
VkClearColorValue clearColors[3] = {
|
||||
{{ 1.0f, 0, 0, 1.0f }},
|
||||
{{ 0, 1, 0, 1.0f }},
|
||||
{{ 0, 0, 1, 1.0f }}
|
||||
};
|
||||
|
||||
VkRenderPassBeginInfo renderPassInfo{};
|
||||
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||||
renderPassInfo.renderPass = _renderPass;
|
||||
renderPassInfo.framebuffer = _frameBuffers[imageIndex]->native();
|
||||
renderPassInfo.renderArea.offset = {0, 0};
|
||||
renderPassInfo.renderArea.extent = swapChain->extent();
|
||||
|
||||
VkClearValue clearColor = {{{0.0f, 0.0f, 1.0f, 1.0f}}};
|
||||
renderPassInfo.clearValueCount = 1;
|
||||
renderPassInfo.pClearValues = &clearColor;
|
||||
|
||||
logicalDevice->functions().vkCmdBeginRenderPass(cmdBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
|
||||
logicalDevice->functions().vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, _pipeline->native());
|
||||
|
||||
//VkViewport viewport{};
|
||||
//viewport.x = 0.0f;
|
||||
//viewport.y = 0.0f;
|
||||
//viewport.width = (float) swapChain->extent().width;
|
||||
//viewport.height = (float) swapChain->extent().height;
|
||||
//viewport.minDepth = 0.0f;
|
||||
//viewport.maxDepth = 1.0f;
|
||||
//logicalDevice->functions().vkCmdSetViewport(cmdBuffer, 0, 1, &viewport);
|
||||
//
|
||||
//VkRect2D scissor{};
|
||||
//scissor.offset = {0, 0};
|
||||
//scissor.extent = swapChain->extent();
|
||||
//logicalDevice->functions().vkCmdSetScissor(cmdBuffer, 0, 1, &scissor);
|
||||
|
||||
VkBuffer vertexBuffers[] = {_vertexBuffer->native()};
|
||||
VkDeviceSize offsets[] = {0};
|
||||
logicalDevice->functions().vkCmdBindVertexBuffers(cmdBuffer, 0, 1, vertexBuffers, offsets);
|
||||
|
||||
logicalDevice->functions().vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, _pipeline->layout()->native(), 0, 1, &descriptorSets[cmdBufferIndex], 0, nullptr);
|
||||
|
||||
auto vertexCount = _vertexBuffer->size() / sizeof(Vertex);
|
||||
logicalDevice->functions().vkCmdDraw(cmdBuffer, static_cast<uint32_t>(vertexCount), 1, 0, 0);
|
||||
logicalDevice->functions().vkCmdEndRenderPass(cmdBuffer);
|
||||
|
||||
|
||||
|
||||
logicalDevice->functions().vkCmdPipelineBarrier(presentCommandBuffers[imageIndex], VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1, &clearToPresentBarrier);
|
||||
|
||||
if (logicalDevice->functions().vkEndCommandBuffer(presentCommandBuffers[imageIndex]) != VK_SUCCESS) {
|
||||
std::cerr << "failed to record command buffer" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
std::cout << "recorded command buffer for image " << imageIndex << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Application::updateUniformBuffer(uint32_t currentImage){
|
||||
static auto startTime = std::chrono::high_resolution_clock::now();
|
||||
auto currentTime = std::chrono::high_resolution_clock::now();
|
||||
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
|
||||
|
||||
UboPerObjectData ubo{};
|
||||
ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
ubo.view = glm::lookAtLH(glm::vec3(0.0f, 2.0f, -2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
ubo.proj = glm::perspectiveLH(glm::radians(90.0f), swapChain->extent().width / (float) swapChain->extent().height, 0.1f, 10.0f);
|
||||
ubo.proj[1][1] *= -1;
|
||||
|
||||
memcpy(_uniformBuffers[currentImage]->mappedMemory(), &ubo, sizeof(ubo));
|
||||
}
|
||||
|
||||
void Application::run() {
|
||||
auto swapChainImageCount = swapChain->images().size();
|
||||
|
||||
std::vector<IntrusivePtr<VulkanSemaphore>> imageAvailableSemaphores;
|
||||
std::vector<IntrusivePtr<VulkanSemaphore>> renderingFinishedSemaphores;
|
||||
|
||||
for(std::size_t i = 0; i < _cmdBufferFences.size(); ++i){
|
||||
imageAvailableSemaphores.push_back(IntrusivePtr<VulkanSemaphore>(new VulkanSemaphore(logicalDevice)));
|
||||
renderingFinishedSemaphores.push_back(IntrusivePtr<VulkanSemaphore>(new VulkanSemaphore(logicalDevice)));
|
||||
}
|
||||
|
||||
auto uboDescriptorPool = createDescriptorPool(_cmdBufferFences.size());
|
||||
std::vector<VkDescriptorSetLayout> layouts(_cmdBufferFences.size(), _uboDescriptorSetLayout->native());
|
||||
VkDescriptorSetAllocateInfo allocInfo{};
|
||||
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
||||
allocInfo.descriptorPool = uboDescriptorPool;
|
||||
allocInfo.descriptorSetCount = static_cast<uint32_t>(_cmdBufferFences.size());
|
||||
allocInfo.pSetLayouts = layouts.data();
|
||||
|
||||
descriptorSets.resize(_cmdBufferFences.size());
|
||||
if (logicalDevice->functions().vkAllocateDescriptorSets(&allocInfo, descriptorSets.data()) != VK_SUCCESS) {
|
||||
throw std::runtime_error("failed to allocate descriptor sets!");
|
||||
}
|
||||
for (size_t i = 0; i < _cmdBufferFences.size(); i++) {
|
||||
VkDescriptorBufferInfo bufferInfo{};
|
||||
bufferInfo.buffer = _uniformBuffers[i]->buffer()->native();
|
||||
bufferInfo.offset = 0;
|
||||
bufferInfo.range = sizeof(UboPerObjectData);
|
||||
|
||||
VkWriteDescriptorSet descriptorWrite{};
|
||||
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
descriptorWrite.dstSet = descriptorSets[i];
|
||||
descriptorWrite.dstBinding = 0;
|
||||
descriptorWrite.dstArrayElement = 0;
|
||||
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
descriptorWrite.descriptorCount = 1;
|
||||
descriptorWrite.pBufferInfo = &bufferInfo;
|
||||
descriptorWrite.pImageInfo = nullptr; // Optional
|
||||
descriptorWrite.pTexelBufferView = nullptr; // Optional
|
||||
logicalDevice->functions().vkUpdateDescriptorSets(1, &descriptorWrite, 0, nullptr);
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::cout << "allocated presentation command buffers" << std::endl;
|
||||
|
||||
int cmdBufferIndex = 0;
|
||||
|
||||
while (window->update()) {
|
||||
//Wait cmd buffer to finish it's job to use it again
|
||||
logicalDevice->functions().vkWaitForFences(1, &_cmdBufferFences[cmdBufferIndex], VK_TRUE, UINT64_MAX);
|
||||
|
||||
|
||||
std::uint32_t imageIndex = 0;
|
||||
auto result = logicalDevice->functions().vkAcquireNextImageKHR(swapChain->native(), UINT64_MAX, imageAvailableSemaphores[cmdBufferIndex]->native(), nullptr, &imageIndex);
|
||||
if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
|
||||
std::cerr << "failed to acquire image" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
updateUniformBuffer(cmdBufferIndex);
|
||||
|
||||
logicalDevice->functions().vkResetFences(1, &_cmdBufferFences[cmdBufferIndex]);
|
||||
|
||||
logicalDevice->functions().vkResetCommandBuffer(presentCommandBuffers[cmdBufferIndex], /*VkCommandBufferResetFlagBits*/ 0);
|
||||
recordCommandBuffer(presentCommandBuffers[cmdBufferIndex], cmdBufferIndex, imageIndex);
|
||||
|
||||
|
||||
|
||||
VkSubmitInfo submitInfo = {};
|
||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||
|
||||
submitInfo.waitSemaphoreCount = 1;
|
||||
submitInfo.pWaitSemaphores = &imageAvailableSemaphores[cmdBufferIndex]->native();
|
||||
submitInfo.signalSemaphoreCount = 1;
|
||||
submitInfo.pSignalSemaphores = &renderingFinishedSemaphores[cmdBufferIndex]->native();
|
||||
|
||||
// This is the stage where the queue should wait on the semaphore (it doesn't have to wait with drawing, for example)
|
||||
VkPipelineStageFlags waitDstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
||||
submitInfo.pWaitDstStageMask = &waitDstStageMask;
|
||||
|
||||
submitInfo.commandBufferCount = 1;
|
||||
submitInfo.pCommandBuffers = &presentCommandBuffers[cmdBufferIndex];
|
||||
|
||||
if (logicalDevice->functions().vkQueueSubmit(queues[0].queue, 1, &submitInfo, _cmdBufferFences[cmdBufferIndex]) != VK_SUCCESS) {
|
||||
std::cerr << "failed to submit draw command buffer" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// std::cout << "submitted draw command buffer" << std::endl;
|
||||
|
||||
// Present drawn image
|
||||
// Note: semaphore here is not strictly necessary, because commands are processed in submission order within a single queue
|
||||
VkPresentInfoKHR presentInfo = {};
|
||||
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
||||
presentInfo.waitSemaphoreCount = 1;
|
||||
presentInfo.pWaitSemaphores = &renderingFinishedSemaphores[cmdBufferIndex]->native();
|
||||
|
||||
auto nativeSwapChain = swapChain->native();
|
||||
presentInfo.swapchainCount = 1;
|
||||
presentInfo.pSwapchains = &nativeSwapChain;
|
||||
presentInfo.pImageIndices = &imageIndex;
|
||||
|
||||
auto res = logicalDevice->functions().vkQueuePresentKHR(queues[0].queue, &presentInfo);
|
||||
|
||||
if (res != VK_SUCCESS) {
|
||||
std::cerr << "failed to submit present command buffer" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
cmdBufferIndex = (cmdBufferIndex + 1) % _cmdBufferFences.size();
|
||||
//std::cout << "submitted presentation command buffer for image " << imageIndex << std::endl;
|
||||
|
||||
|
||||
|
||||
//std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
|
||||
std::cout << "Test" << std::endl;
|
||||
}
|
||||
|
||||
void Application::copyBufferHostToDevice(IntrusivePtr<VulkanLogicalDevice> device, VulkanQueue queue, VkCommandPool pool, IntrusivePtr<VulkanBuffer> hostBuffer, IntrusivePtr<VulkanBuffer> deviceBuffer) {
|
||||
VkCommandBuffer cmdBuffer;
|
||||
|
||||
VkCommandBufferAllocateInfo allocInfo = {};
|
||||
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||
allocInfo.commandPool = pool;
|
||||
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||
allocInfo.commandBufferCount = 1;
|
||||
|
||||
if (device->functions().vkAllocateCommandBuffers(&allocInfo, &cmdBuffer) != VK_SUCCESS) {
|
||||
throw std::runtime_error("Failed to allocate command buffer");
|
||||
}
|
||||
|
||||
// If requested, also start the new command buffer
|
||||
VkCommandBufferBeginInfo beginInfo = {};
|
||||
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
|
||||
device->functions().vkBeginCommandBuffer(cmdBuffer, &beginInfo);
|
||||
|
||||
|
||||
VkBufferCopy copyRegion = {};
|
||||
copyRegion.size = hostBuffer->getMemoryRequirements().size;
|
||||
|
||||
device->functions().vkCmdCopyBuffer(cmdBuffer, hostBuffer->native(), deviceBuffer->native(), 1, ©Region);
|
||||
|
||||
device->functions().vkEndCommandBuffer(cmdBuffer);
|
||||
|
||||
VkSubmitInfo submitInfo = {};
|
||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||
submitInfo.commandBufferCount = 1;
|
||||
submitInfo.pCommandBuffers = &cmdBuffer;
|
||||
|
||||
// Create fence to ensure that the command buffer has finished executing
|
||||
VkFenceCreateInfo fenceCreateInfo = {};
|
||||
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
||||
fenceCreateInfo.flags = 0;
|
||||
VkFence fence;
|
||||
device->functions().vkCreateFence(&fenceCreateInfo, nullptr, &fence);
|
||||
|
||||
// Submit to the queue
|
||||
device->functions().vkQueueSubmit(queue.queue, 1, &submitInfo, fence);
|
||||
// Wait for the fence to signal that command buffer has finished executing
|
||||
device->functions().vkWaitForFences(1, &fence, VK_TRUE, DEFAULT_FENCE_TIMEOUT);
|
||||
|
||||
device->functions().vkDestroyFence(fence, nullptr);
|
||||
device->functions().vkFreeCommandBuffers(pool, 1, &cmdBuffer);
|
||||
|
||||
}
|
||||
|
||||
IntrusivePtr<VulkanBuffer> Application::createBuffer(VkBufferUsageFlagBits usage, const void* data, VkDeviceSize size, IntrusivePtr<VulkanLogicalDevice> logicalDevice, IntrusivePtr<VulkanPhysicalDevice> device, VulkanQueue queue, VkCommandPool commandPool) {
|
||||
auto memoryProps = device->getMemoryProperties();
|
||||
|
||||
auto stagingBuffer = IntrusivePtr<VulkanBuffer>(new VulkanBuffer(logicalDevice, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT));
|
||||
auto memoryRequirements = stagingBuffer->getMemoryRequirements();
|
||||
auto suitableIndices = memoryProps.findSuitableMemoryTypeIndices(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, memoryRequirements.memoryTypeBits);
|
||||
|
||||
auto stagingBufferMemory = IntrusivePtr<VulkanMemory>(new VulkanMemory(logicalDevice, memoryRequirements.size, suitableIndices[0]));
|
||||
stagingBufferMemory->write(data, size);
|
||||
|
||||
stagingBuffer->bindMemory(stagingBufferMemory);
|
||||
|
||||
|
||||
auto result = IntrusivePtr<VulkanBuffer>(new VulkanBuffer(logicalDevice, size, usage | VK_BUFFER_USAGE_TRANSFER_DST_BIT));
|
||||
memoryRequirements = result->getMemoryRequirements();
|
||||
suitableIndices = memoryProps.findSuitableMemoryTypeIndices(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, memoryRequirements.memoryTypeBits);
|
||||
auto resultMemory = IntrusivePtr<VulkanMemory>(new VulkanMemory(logicalDevice, memoryRequirements.size, suitableIndices[0]));
|
||||
result->bindMemory(resultMemory);
|
||||
|
||||
copyBufferHostToDevice(logicalDevice, queue, commandPool, stagingBuffer, result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
VkDescriptorPool Application::createDescriptorPool(std::size_t descriptorCount)
|
||||
{
|
||||
// We need to tell the API the number of max. requested descriptors per type
|
||||
VkDescriptorPoolSize typeCounts[1];
|
||||
// This example only uses one descriptor type (uniform buffer) and only requests one descriptor of this type
|
||||
typeCounts[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
typeCounts[0].descriptorCount = descriptorCount;
|
||||
// For additional types you need to add new entries in the type count list
|
||||
// E.g. for two combined image samplers :
|
||||
// typeCounts[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
// typeCounts[1].descriptorCount = 2;
|
||||
|
||||
// Create the global descriptor pool
|
||||
// All descriptors used in this example are allocated from this pool
|
||||
VkDescriptorPoolCreateInfo descriptorPoolInfo = {};
|
||||
descriptorPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
||||
descriptorPoolInfo.pNext = nullptr;
|
||||
descriptorPoolInfo.poolSizeCount = 1;
|
||||
descriptorPoolInfo.pPoolSizes = typeCounts;
|
||||
// Set the max. number of descriptor sets that can be requested from this pool (requesting beyond this limit will result in an error)
|
||||
descriptorPoolInfo.maxSets = descriptorCount;
|
||||
|
||||
VkDescriptorPool result;
|
||||
|
||||
|
||||
if (logicalDevice->functions().vkCreateDescriptorPool(&descriptorPoolInfo, nullptr, &result) != VK_SUCCESS)
|
||||
{
|
||||
throw std::runtime_error("Failed to create descriptor pool");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <vulkan/vulkan.h>
|
||||
#include <Windows.h>
|
||||
#include "DynamicLibrary.h"
|
||||
#include "VulkanInstance.h"
|
||||
#include "VulkanDynamicLibrary.h"
|
||||
#include "VulkanLogicalDevice.h"
|
||||
#include "VulkanImageView.h"
|
||||
#include "VulkanSwapChain.h"
|
||||
#include "VulkanImageView.h"
|
||||
#include "VulkanShaderModule.h"
|
||||
#include "VulkanSemaphore.h"
|
||||
#include "VulkanDescriptorSetLayout.h"
|
||||
#include "VulkanMemory.h"
|
||||
#include "VulkanBuffer.h"
|
||||
#include "VulkanPipeline.h"
|
||||
#include "VulkanFrameBuffer.h"
|
||||
#include "UboPerObjectData.h"
|
||||
#include "RenderWindow.h"
|
||||
#include "TempFile.h"
|
||||
#include "ShaderCompiler.h"
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <optional>
|
||||
#include "Vertex.h"
|
||||
#include "UniformBuffer.h"
|
||||
#include <spirv_reflect.h>
|
||||
|
||||
|
||||
#define DEFAULT_FENCE_TIMEOUT 100000000000
|
||||
|
||||
class Application {
|
||||
public:
|
||||
Application();
|
||||
|
||||
void createSwapChain();
|
||||
|
||||
void run();
|
||||
|
||||
void copyBufferHostToDevice(IntrusivePtr<VulkanLogicalDevice> device, VulkanQueue queue, VkCommandPool pool, IntrusivePtr<VulkanBuffer> hostBuffer, IntrusivePtr<VulkanBuffer> deviceBuffer);
|
||||
|
||||
IntrusivePtr<VulkanBuffer> createBuffer(VkBufferUsageFlagBits usage, const void* data, VkDeviceSize size, IntrusivePtr<VulkanLogicalDevice> logicalDevice, IntrusivePtr<VulkanPhysicalDevice> device, VulkanQueue queue, VkCommandPool commandPool);
|
||||
|
||||
VkDescriptorPool createDescriptorPool(std::size_t descriptorCount);
|
||||
|
||||
void createPipeline();
|
||||
|
||||
void recordCommandBuffer(VkCommandBuffer cmdBuffer, uint32_t cmdBufferIndex, uint32_t imageIndex);
|
||||
|
||||
void updateUniformBuffer(uint32_t currentImage);
|
||||
private:
|
||||
std::shared_ptr<VulkanDynamicLibrary> vulkan;
|
||||
IntrusivePtr<VulkanPhysicalDevice> device;
|
||||
IntrusivePtr<VulkanLogicalDevice> logicalDevice;
|
||||
std::vector<VulkanQueue> queues;
|
||||
VulkanSwapChain* swapChain;
|
||||
RenderWindow* window;
|
||||
VkCommandPool presentCommandPool;
|
||||
VkCommandPool renderCommandPool;
|
||||
uint32_t presentQueueFamilyIndex = -1;
|
||||
uint32_t renderQueueFamilyIndex = -1;
|
||||
|
||||
IntrusivePtr<VulkanShaderModule> vs;
|
||||
IntrusivePtr<VulkanShaderModule> ps;
|
||||
|
||||
IntrusivePtr<VulkanBuffer> _vertexBuffer;
|
||||
IntrusivePtr<VulkanBuffer> _indexBuffer;
|
||||
|
||||
IntrusivePtr<VulkanPipeline> _pipeline;
|
||||
|
||||
std::vector<IntrusivePtr<VulkanFrameBuffer>> _frameBuffers;
|
||||
std::vector<IntrusivePtr<VulkanImageView>> _imageViews;
|
||||
std::vector<IntrusivePtr<UniformBuffer>> _uniformBuffers;
|
||||
|
||||
IntrusivePtr<VulkanDescriptorSetLayout> _uboDescriptorSetLayout;
|
||||
|
||||
std::vector<VkDescriptorSet> descriptorSets;
|
||||
|
||||
VkRenderPass _renderPass;
|
||||
|
||||
std::vector<VkCommandBuffer> presentCommandBuffers;
|
||||
std::vector<VkFence> _cmdBufferFences;
|
||||
|
||||
void compileShaders();
|
||||
};
|
||||
@@ -0,0 +1,86 @@
|
||||
|
||||
|
||||
include(VulkanTools)
|
||||
|
||||
Vulkan_generateFunctionsFile("${CMAKE_CURRENT_LIST_DIR}")
|
||||
|
||||
add_library(VulkanWrapper INTERFACE)
|
||||
|
||||
target_compile_definitions(VulkanWrapper
|
||||
INTERFACE
|
||||
VK_NO_PROTOTYPES
|
||||
)
|
||||
|
||||
target_sources(VulkanWrapper
|
||||
INTERFACE
|
||||
Application.cpp
|
||||
Application.h
|
||||
CMakeLists.txt
|
||||
DynamicLibrary.cpp
|
||||
DynamicLibrary.h
|
||||
FragmentShader_unknown.h
|
||||
FunctionTraits.h
|
||||
IntrusivePtr.h
|
||||
main.cpp
|
||||
RefCountedObject.h
|
||||
RenderWindow.h
|
||||
RenderWindowWindows.cpp
|
||||
ShaderCompiler.h
|
||||
TempFile.h
|
||||
VertexShader_unknown.h
|
||||
VulkanApplicationInfo.h
|
||||
VulkanBuffer.h
|
||||
VulkanCommon.h
|
||||
VulkanDynamicLibrary.h
|
||||
VulkanFunctions.h
|
||||
VulkanImageView.h
|
||||
VulkanInstance.cpp
|
||||
VulkanInstance.h
|
||||
VulkanLibrary.cpp
|
||||
VulkanLibrary.h
|
||||
VulkanLogicalDevice.cpp
|
||||
VulkanLogicalDevice.h
|
||||
VulkanMemory.h
|
||||
VulkanMemoryProperties.h
|
||||
VulkanPhysicalDevice.cpp
|
||||
VulkanPhysicalDevice.h
|
||||
VulkanQueueFamilyProperties.h
|
||||
VulkanSemaphore.h
|
||||
VulkanShaderModule.h
|
||||
VulkanSwapChain.h
|
||||
WindowWindows.h
|
||||
|
||||
VulkanDeviceFunctions.h
|
||||
VulkanInstanceFunctions.h
|
||||
VulkanGlobalFunctions.h
|
||||
|
||||
Vertex.h
|
||||
VulkanPipelineLayout.h
|
||||
VulkanPipelineLayout.cpp
|
||||
|
||||
VulkanPipeline.h
|
||||
VulkanPipeline.cpp
|
||||
|
||||
VulkanFrameBuffer.h
|
||||
VulkanFrameBuffer.cpp
|
||||
|
||||
VulkanDescriptorSetLayout.h
|
||||
VulkanDescriptorSetLayout.cpp
|
||||
UboPerObjectData.h
|
||||
|
||||
UniformBuffer.h
|
||||
|
||||
${Vulkan_INCLUDE_DIR}/../Source/SPIRV-Reflect/spirv_reflect.h
|
||||
${Vulkan_INCLUDE_DIR}/../Source/SPIRV-Reflect/spirv_reflect.c
|
||||
)
|
||||
|
||||
target_include_directories(VulkanWrapper
|
||||
INTERFACE
|
||||
${Vulkan_INCLUDE_DIR}/../Source/SPIRV-Reflect
|
||||
)
|
||||
|
||||
target_link_libraries(VulkanWrapper
|
||||
INTERFACE
|
||||
Vulkan::Vulkan
|
||||
glslang::glslang
|
||||
)
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "DynamicLibrary.h"
|
||||
|
||||
|
||||
DynamicLibrary::DynamicLibrary(const std::filesystem::path& path) {
|
||||
_library = LoadLibraryW(path.c_str());
|
||||
|
||||
if (_library == NULL) {
|
||||
throw std::runtime_error("Failed to load library");
|
||||
}
|
||||
}
|
||||
|
||||
void DynamicLibrary::addSearchPath(const std::filesystem::path& path) {
|
||||
SetDllDirectoryW(path.c_str());
|
||||
}
|
||||
|
||||
void* DynamicLibrary::getFunction(const std::string& name) {
|
||||
return GetProcAddress(_library, name.c_str());
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user