Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding DX12 support to OpenSubdiv #938

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ option(NO_OPENCL "Disable OpenCL backend" OFF)
option(NO_CLEW "Disable CLEW wrapper library" OFF)
option(NO_OPENGL "Disable OpenGL support")
option(NO_METAL "Disable Metal support" OFF)
option(NO_DX "Disable DirectX support")
option(NO_DX11 "Disable DirectX 11 support")
option(NO_DX12 "Disable DirectX 12 support")
option(NO_TESTS "Disable all tests")
option(NO_GLTESTS "Disable GL tests")

Expand Down Expand Up @@ -356,7 +357,18 @@ if (OPENGL_FOUND AND NOT IOS)
endif()
endif()

if (WIN32 AND NOT NO_DX)
if (WIN32 AND NOT NO_DX12)
find_package(DX12SDK)
if(DX12SDK_FOUND)
# The DX12 SDK (Win 10 SDK) is a super-set of the DXSDK
set(DXSDK_FOUND TRUE)

set(DXSDK_INCLUDE_DIR ${D3D12_INCLUDE_DIRS})
set(DXSDK_LIBRARIES ${D3D12_LIBRARIES})
endif()
endif()

if (WIN32 AND NOT NO_DX11 AND NOT DXSDK_FOUND)
find_package(DXSDK)
endif()

Expand Down Expand Up @@ -476,7 +488,7 @@ if(OPENCL_FOUND)
endif()
endif()

if (DXSDK_FOUND AND NOT NO_DX)
if (DXSDK_FOUND AND NOT NO_DX11)
if (OPENCL_CL_D3D11_H_FOUND)
set(OPENCL_D3D11_INTEROP_FOUND "YES")
add_definitions(
Expand Down Expand Up @@ -566,13 +578,19 @@ if (WIN32)
-DGLEW_STATIC
)
endif()

if(DX12SDK_FOUND AND NOT NO_DX12)
add_definitions(
-DOPENSUBDIV_HAS_DX12
)
endif()

if (DXSDK_FOUND AND NOT NO_DX)
if (DXSDK_FOUND AND NOT NO_DX11)
add_definitions(
-DOPENSUBDIV_HAS_DX11SDK
)
set(OSD_GPU TRUE)
elseif(NOT NO_DX)
elseif(NOT NO_DX11)
message(WARNING
"DirectX11 SDK was not found. "
"If you do have DXSDK installed and see this message, "
Expand Down
60 changes: 60 additions & 0 deletions cmake/FindDX12SDK.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Find the win10 SDK path.
get_filename_component(WIN10_SDK_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Microsoft SDKs\\Windows\\v10.0;InstallationFolder]" ABSOLUTE CACHE)
get_filename_component(TEMP_WIN10_SDK_VERSION "[HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Microsoft SDKs\\Windows\\v10.0;ProductVersion]" ABSOLUTE CACHE)

get_filename_component(WIN10_SDK_VERSION ${TEMP_WIN10_SDK_VERSION} NAME)

# WIN10_SDK_PATH will be something like C:\Program Files (x86)\Windows Kits\10

# WIN10_SDK_VERSION will be something like 10.0.14393 or 10.0.14393.0; we need the
# one that matches the directory name.
if (IS_DIRECTORY "${WIN10_SDK_PATH}/Include/${WIN10_SDK_VERSION}.0")
set(WIN10_SDK_VERSION "${WIN10_SDK_VERSION}.0")
endif (IS_DIRECTORY "${WIN10_SDK_PATH}/Include/${WIN10_SDK_VERSION}.0")


# Find the d3d12 and dxgi include path, it will typically look something like this.
# C:\Program Files (x86)\Windows Kits\10\Include\10.0.10586.0\um\d3d12.h
# C:\Program Files (x86)\Windows Kits\10\Include\10.0.10586.0\shared\dxgi1_4.h
find_path(D3D12_INCLUDE_DIR # Set variable D3D12_INCLUDE_DIR
d3d12.h # Find a path with d3d12.h
HINTS "${WIN10_SDK_PATH}/Include/${WIN10_SDK_VERSION}/um"
DOC "path to WIN10 SDK header files"
HINTS
)

find_path(DXGI_INCLUDE_DIR # Set variable DXGI_INCLUDE_DIR
dxgi1_4.h # Find a path with dxgi1_4.h
HINTS "${WIN10_SDK_PATH}/Include/${WIN10_SDK_VERSION}/shared"
DOC "path to WIN10 SDK header files"
HINTS
)

foreach(DX_LIB d3d12 d3d11 d3dcompiler dxgi)
if (CMAKE_GENERATOR MATCHES "Visual Studio.*Win64" )
find_library(D3D12_${DX_LIB}_LIBRARY NAMES ${DX_LIB}.lib
HINTS ${WIN10_SDK_PATH}/Lib/${WIN10_SDK_VERSION}/um/x64 )
elseif (CMAKE_GENERATOR MATCHES "Visual Studio.*ARM" )
find_library(D3D12_${DX_LIB}_LIBRARY NAMES ${DX_LIB}.lib
HINTS ${WIN10_SDK_PATH}/Lib/${WIN10_SDK_VERSION}/um/arm )
elseif (CMAKE_GENERATOR MATCHES "Visual Studio.*ARM64" )
find_library(D3D12_${DX_LIB}_LIBRARY NAMES ${DX_LIB}.lib
HINTS ${WIN10_SDK_PATH}/Lib/${WIN10_SDK_VERSION}/um/arm64 )
else (CMAKE_GENERATOR MATCHES "Visual Studio.*Win32" )
find_library(D3D12_${DX_LIB}_LIBRARY NAMES ${DX_LIB}.lib
HINTS ${WIN10_SDK_PATH}/Lib/${WIN10_SDK_VERSION}/um/x86 )
endif (CMAKE_GENERATOR MATCHES "Visual Studio.*Win64" )

list(APPEND D3D12_LIBRARIES ${D3D12_${DX_LIB}_LIBRARY})
endforeach(DX_LIB)

set(D3D12_INCLUDE_DIRS ${D3D12_INCLUDE_DIR} ${DXGI_INCLUDE_DIR})


include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set DX12SDK_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(DX12SDK DEFAULT_MSG
D3D12_INCLUDE_DIRS D3D12_LIBRARIES)

mark_as_advanced(D3D12_INCLUDE_DIRS D3D12_LIBRARIES)
2 changes: 1 addition & 1 deletion cmake/FindDXSDK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ if (WIN32)

set(DXSDK_LIBRARY_DIR ${LIBRARY_DIR})

foreach(DX_LIB d3d11 d3dcompiler)
foreach(DX_LIB d3d11 d3dcompiler dxgi)

find_library(DXSDK_${DX_LIB}_LIBRARY
NAMES
Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ if (NOT NO_OPENGL)
endif()
endif()

if (DXSDK_FOUND AND NOT NO_DX)
if (DXSDK_FOUND AND NOT NO_DX11)

add_subdirectory(dxViewer)

Expand Down
4 changes: 4 additions & 0 deletions examples/common/d3d11ControlMeshDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ D3D11ControlMeshDisplay::createProgram() {

device->CreateBuffer(&cbDesc, NULL, &_constantBuffer);
assert(_constantBuffer);

SAFE_RELEASE(device);
return true;
}

Expand Down Expand Up @@ -264,5 +266,7 @@ D3D11ControlMeshDisplay::SetTopology(
srvDesc.Buffer.NumElements = _numEdges;
hr = device->CreateShaderResourceView(_edgeSharpness, &srvDesc, &_edgeSharpnessSRV);
assert(_edgeSharpnessSRV);

SAFE_RELEASE(device);
}

45 changes: 29 additions & 16 deletions examples/common/d3d11Hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ D3D11hud::D3D11hud(ID3D11DeviceContext *deviceContext)
: _deviceContext(deviceContext),
_vbo(0), _staticVbo(0), _fontTexture(0), _inputLayout(0),
_shaderResourceView(0), _samplerState(0), _vertexShader(0),
_pixelShader(0), _rasterizerState(0)
_pixelShader(0), _rasterizerState(0), _vboBufferSize(0)
{
}

Expand All @@ -83,6 +83,7 @@ D3D11hud::~D3D11hud()
SAFE_RELEASE(_vertexShader);
SAFE_RELEASE(_pixelShader);
SAFE_RELEASE(_rasterizerState);
SAFE_RELEASE(_constantBuffer);
}

void
Expand Down Expand Up @@ -187,6 +188,8 @@ D3D11hud::Init(int width, int height, int frameBufferWidth, int frameBufferHeigh

device->CreateBuffer(&cbDesc, NULL, &_constantBuffer);
assert(_constantBuffer);

SAFE_RELEASE(device);
}

void
Expand Down Expand Up @@ -216,6 +219,8 @@ D3D11hud::Rebuild(int width, int height, int framebufferWidth, int framebufferHe
HRESULT hr = device->CreateBuffer(&bufferDesc, &subData, &_staticVbo);
assert(_staticVbo);
_staticVboCount = size / 7;

SAFE_RELEASE(device);
}
}

Expand All @@ -226,25 +231,33 @@ D3D11hud::Flush()
return false;

// update dynamic text
D3D11_BUFFER_DESC bufferDesc;
bufferDesc.ByteWidth = (int)getVboSource().size() * sizeof(float);
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
bufferDesc.StructureByteStride = 4*sizeof(float);
static int vboSourceSizeInBytes = (int)(getVboSource().size() * sizeof(float));
if (_vbo == nullptr || _vboBufferSize < vboSourceSizeInBytes)
{
_vboBufferSize = vboSourceSizeInBytes;

D3D11_SUBRESOURCE_DATA subData;
subData.pSysMem = &getVboSource()[0];
subData.SysMemPitch = 0;
subData.SysMemSlicePitch = 0;
D3D11_BUFFER_DESC bufferDesc;
bufferDesc.ByteWidth = vboSourceSizeInBytes;
bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
bufferDesc.MiscFlags = 0;
bufferDesc.StructureByteStride = 4 * sizeof(float);

SAFE_RELEASE(_vbo);
SAFE_RELEASE(_vbo);

ID3D11Device *device = NULL;
_deviceContext->GetDevice(&device);
HRESULT hr = device->CreateBuffer(&bufferDesc, &subData, &_vbo);
ID3D11Device *device = NULL;
_deviceContext->GetDevice(&device);
HRESULT hr = device->CreateBuffer(&bufferDesc, nullptr, &_vbo);

SAFE_RELEASE(device);
}
assert(_vbo);
D3D11_MAPPED_SUBRESOURCE MappedVBO;
_deviceContext->Map(_vbo, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedVBO);
memcpy(MappedVBO.pData, &getVboSource()[0], getVboSource().size() * sizeof(float));
_deviceContext->Unmap(_vbo, 0);

int numVertices = (int)getVboSource().size()/7; /* (x, y, r, g, b, u, v) = 7*/

// reserved space of the vector remains for the next frame.
Expand Down
1 change: 1 addition & 0 deletions examples/common/d3d11Hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class D3D11hud : public Hud
ID3D11RasterizerState *_rasterizerState;
ID3D11Buffer* _constantBuffer;
int _staticVboCount;
int _vboBufferSize;
};

#endif // OPENSUBDIV_EXAMPLES_D3D11_HUD_H
Loading