├── Images
├── rates.PNG
├── simple.PNG
└── combine.PNG
├── hellovrs_simple
├── Vulkan
│ ├── vkHelloTriangle.h
│ ├── small.ico
│ ├── vkHelloTriangle.ico
│ ├── vkHelloTriangle.rc
│ ├── CompileShaders.bat
│ ├── vkHelloTriangle.vcxproj.user
│ ├── pch.cpp
│ ├── VS.glsl
│ ├── targetver.h
│ ├── framework.h
│ ├── PS.glsl
│ ├── Resource.h
│ ├── pch.h
│ ├── vkHelloTriangle.sln
│ ├── vkHelloTriangle.vcxproj.filters
│ └── vkHelloTriangle.vcxproj
└── D3D12
│ ├── HelloTriangle
│ ├── stdafx.cpp
│ ├── Main.cpp
│ ├── Win32Application.h
│ ├── shaders.hlsl
│ ├── stdafx.h
│ ├── D3D12HelloTriangle.vcxproj.user
│ ├── DXSample.h
│ ├── D3D12HelloTriangle.vcxproj.filters
│ ├── D3D12HelloTriangle.h
│ ├── DXSample.cpp
│ ├── DXSampleHelper.h
│ ├── Win32Application.cpp
│ ├── D3D12HelloTriangle.vcxproj
│ └── D3D12HelloTriangle.cpp
│ └── D3D12HelloWorld.sln
├── hellovrs_arrowkeys_tier2
├── HelloTriangle
│ ├── PS.hlsl
│ ├── BuildShaders.cmd
│ ├── stdafx.cpp
│ ├── VS.hlsl
│ ├── Main.cpp
│ ├── Win32Application.h
│ ├── Fallback.hlsl
│ ├── stdafx.h
│ ├── D3D12HelloTriangle.vcxproj.user
│ ├── DXSample.h
│ ├── D3D12HelloTriangle.vcxproj.filters
│ ├── D3D12HelloTriangle.h
│ ├── DXSample.cpp
│ ├── DXSampleHelper.h
│ ├── Win32Application.cpp
│ ├── D3D12HelloTriangle.vcxproj
│ └── D3D12HelloTriangle.cpp
└── D3D12HelloWorld.sln
├── hellovrs_rates
├── HelloTriangle
│ ├── stdafx.cpp
│ ├── Main.cpp
│ ├── Win32Application.h
│ ├── shaders.hlsl
│ ├── stdafx.h
│ ├── D3D12HelloTriangle.vcxproj.user
│ ├── DXSample.h
│ ├── D3D12HelloTriangle.vcxproj.filters
│ ├── D3D12HelloTriangle.h
│ ├── DXSample.cpp
│ ├── DXSampleHelper.h
│ ├── Win32Application.cpp
│ ├── D3D12HelloTriangle.vcxproj
│ └── D3D12HelloTriangle.cpp
└── D3D12HelloWorld.sln
├── .gitignore
├── LICENSE
└── README.md
/Images/rates.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/clandrew/hellovrs/HEAD/Images/rates.PNG
--------------------------------------------------------------------------------
/Images/simple.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/clandrew/hellovrs/HEAD/Images/simple.PNG
--------------------------------------------------------------------------------
/Images/combine.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/clandrew/hellovrs/HEAD/Images/combine.PNG
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/vkHelloTriangle.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "resource.h"
4 |
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/small.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/clandrew/hellovrs/HEAD/hellovrs_simple/Vulkan/small.ico
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/vkHelloTriangle.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/clandrew/hellovrs/HEAD/hellovrs_simple/Vulkan/vkHelloTriangle.ico
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/vkHelloTriangle.rc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/clandrew/hellovrs/HEAD/hellovrs_simple/Vulkan/vkHelloTriangle.rc
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/CompileShaders.bat:
--------------------------------------------------------------------------------
1 | D:\VulkanSDK\1.2.198.1\Bin\glslc.exe -fshader-stage=vertex VS.glsl -o VS.spv
2 | D:\VulkanSDK\1.2.198.1\Bin\glslc.exe -fshader-stage=fragment PS.glsl -o PS.spv
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/vkHelloTriangle.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/pch.cpp:
--------------------------------------------------------------------------------
1 | // pch.cpp: source file corresponding to the pre-compiled header
2 |
3 | #include "pch.h"
4 |
5 | // When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
6 |
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/VS.glsl:
--------------------------------------------------------------------------------
1 | #version 450
2 |
3 | vec2 positions[3] = vec2[](
4 | vec2(-1, -1), // top left
5 | vec2(1, -1), // top right
6 | vec2(-1, 1) // bottom left
7 | );
8 |
9 | void main()
10 | {
11 | gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
12 | }
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/targetver.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | // // Including SDKDDKVer.h defines the highest available Windows platform.
4 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
5 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
6 | #include
7 |
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/framework.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "targetver.h"
4 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
5 | // Windows Header Files
6 | #include
7 | // C RunTime Header Files
8 | #include
9 | #include
10 | #include
11 | #include
12 |
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/PS.glsl:
--------------------------------------------------------------------------------
1 | #version 450
2 |
3 | layout(location = 0) out vec4 outColor;
4 |
5 | void main()
6 | {
7 | vec4 position = gl_FragCoord;
8 |
9 | vec4 color = vec4(
10 | sin(position.x / 10.0f),
11 | sin(position.y / 10.0f),
12 | cos((position.x + position.y) / 10.0f),
13 | 1);
14 | outColor = color;
15 | }
16 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/PS.hlsl:
--------------------------------------------------------------------------------
1 | struct PSInput
2 | {
3 | float4 position : SV_POSITION;
4 | float4 color : COLOR;
5 | uint shadingRate : SV_SHADINGRATE;
6 | };
7 |
8 | float4 PSMain(PSInput input) : SV_TARGET
9 | {
10 | float4 color = float4(sin(input.position.x / 10.0f), sin(input.position.y / 10.0f), cos((input.position.x + input.position.y) / 10.0f), 1);
11 |
12 | return color;
13 | }
14 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/BuildShaders.cmd:
--------------------------------------------------------------------------------
1 | :: Please run this from a Developer Command Prompt for VS 2019.
2 |
3 | :: Compile shaders using 6_4 profile
4 | dxc /Zi -Od -Tvs_6_4 -EVSMain -FhVSMain.h shaders.hlsl
5 | dxc /Zi -Od -Tps_6_4 -EPSMain -FhPSMain.h shaders.hlsl
6 |
7 | :: Compile shaders to use if no VRS Tier 2 support is found
8 | dxc /Zi -Od -Tvs_5_1 -EVSFallback -FhVSFallback.h fallback.hlsl
9 | dxc /Zi -Od -Tps_5_1 -EPSFallback -FhPSFallback.h fallback.hlsl
10 |
11 |
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/stdafx.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/stdafx.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/stdafx.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /hellovrs_simple/D3D12/HelloTriangle/bin/*
2 | /hellovrs_simple/D3D12/HelloTriangle/obj/*
3 | /hellovrs_simple/D3D12/.vs/*
4 | /hellovrs_simple/Vulkan/.vs/*
5 | /hellovrs_simple/Vulkan/x64/*
6 |
7 | /hellovrs_rates/HelloTriangle/bin/*
8 | /hellovrs_arrowkeys_tier2/HelloTriangle/bin/*
9 | /hellovrs_rates/HelloTriangle/obj/*
10 | /hellovrs_arrowkeys_tier2/HelloTriangle/obj/*
11 | /hellovrs_rates/.vs/*
12 | /hellovrs_arrowkeys_tier2/.vs/*
13 | /hellovrs_arrowkeys_tier2/D3D12HelloWorld/.vs/*
14 | /hellovrs_arrowkeys_tier2/HelloTriangle/x64/*
15 | /hellovrs_arrowkeys_tier2/x64/*
16 | *.spv
17 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/VS.hlsl:
--------------------------------------------------------------------------------
1 | struct PSInput
2 | {
3 | float4 position : SV_POSITION;
4 | float4 color : COLOR;
5 | uint shadingRate : SV_SHADINGRATE;
6 | };
7 |
8 | PSInput VSMain(float4 position : POSITION, float4 color : COLOR)
9 | {
10 | PSInput result;
11 |
12 | result.position = position;
13 | result.color = color;
14 |
15 | /*
16 |
17 | typedef enum D3D12_SHADING_RATE
18 | {
19 | D3D12_SHADING_RATE_1X1 = 0,
20 | D3D12_SHADING_RATE_1X2 = 0x1,
21 | D3D12_SHADING_RATE_2X1 = 0x4,
22 | D3D12_SHADING_RATE_2X2 = 0x5,
23 | D3D12_SHADING_RATE_2X4 = 0x6,
24 | D3D12_SHADING_RATE_4X2 = 0x9,
25 | D3D12_SHADING_RATE_4X4 = 0xa
26 | } D3D12_SHADING_RATE;
27 |
28 |
29 | */
30 | result.shadingRate = 0xa;
31 |
32 | return result;
33 | }
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/Main.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 | #include "D3D12HelloTriangle.h"
14 |
15 | _Use_decl_annotations_
16 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
17 | {
18 | D3D12HelloTriangle sample(1000, 1000, L"D3D12 Hello Triangle");
19 | return Win32Application::Run(&sample, hInstance, nCmdShow);
20 | }
21 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/Main.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 | #include "D3D12HelloTriangle.h"
14 |
15 | _Use_decl_annotations_
16 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
17 | {
18 | D3D12HelloTriangle sample(1280, 720, L"D3D12 Hello Triangle");
19 | return Win32Application::Run(&sample, hInstance, nCmdShow);
20 | }
21 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/Main.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 | #include "D3D12HelloTriangle.h"
14 |
15 | _Use_decl_annotations_
16 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
17 | {
18 | D3D12HelloTriangle sample(1280, 720, L"D3D12 Hello Triangle");
19 | return Win32Application::Run(&sample, hInstance, nCmdShow);
20 | }
21 |
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/Resource.h:
--------------------------------------------------------------------------------
1 | //{{NO_DEPENDENCIES}}
2 | // Microsoft Visual C++ generated include file.
3 | // Used by vkHelloTriangle.rc
4 |
5 | #define IDS_APP_TITLE 103
6 |
7 | #define IDR_MAINFRAME 128
8 | #define IDD_VKHELLOTRIANGLE_DIALOG 102
9 | #define IDD_ABOUTBOX 103
10 | #define IDM_ABOUT 104
11 | #define IDM_EXIT 105
12 | #define IDI_VKHELLOTRIANGLE 107
13 | #define IDI_SMALL 108
14 | #define IDC_VKHELLOTRIANGLE 109
15 | #define IDC_MYICON 2
16 | #ifndef IDC_STATIC
17 | #define IDC_STATIC -1
18 | #endif
19 | // Next default values for new objects
20 | //
21 | #ifdef APSTUDIO_INVOKED
22 | #ifndef APSTUDIO_READONLY_SYMBOLS
23 |
24 | #define _APS_NO_MFC 130
25 | #define _APS_NEXT_RESOURCE_VALUE 129
26 | #define _APS_NEXT_COMMAND_VALUE 32771
27 | #define _APS_NEXT_CONTROL_VALUE 1000
28 | #define _APS_NEXT_SYMED_VALUE 110
29 | #endif
30 | #endif
31 |
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/Win32Application.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #pragma once
13 |
14 | #include "DXSample.h"
15 |
16 | class DXSample;
17 |
18 | class Win32Application
19 | {
20 | public:
21 | static int Run(DXSample* pSample, HINSTANCE hInstance, int nCmdShow);
22 | static HWND GetHwnd() { return m_hwnd; }
23 |
24 | protected:
25 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
26 |
27 | private:
28 | static HWND m_hwnd;
29 | };
30 |
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/pch.h:
--------------------------------------------------------------------------------
1 | // pch.h: This is a precompiled header file.
2 | // Files listed below are compiled only once, improving build performance for future builds.
3 | // This also affects IntelliSense performance, including code completion and many code browsing features.
4 | // However, files listed here are ALL re-compiled if any one of them is updated between builds.
5 | // Do not add files here that you will be updating frequently as this negates the performance advantage.
6 |
7 | #ifndef PCH_H
8 | #define PCH_H
9 |
10 | // add headers that you want to pre-compile here
11 | #include "framework.h"
12 |
13 | #define VK_USE_PLATFORM_WIN32_KHR
14 | #include
15 |
16 | #define GLM_FORCE_RADIANS
17 | #define GLM_FORCE_DEPTH_ZERO_TO_ONE
18 | #include
19 | #include
20 |
21 | #include
22 | #include
23 | #include
24 | #include
25 |
26 | #endif //PCH_H
27 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/Win32Application.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #pragma once
13 |
14 | #include "DXSample.h"
15 |
16 | class DXSample;
17 |
18 | class Win32Application
19 | {
20 | public:
21 | static int Run(DXSample* pSample, HINSTANCE hInstance, int nCmdShow);
22 | static HWND GetHwnd() { return m_hwnd; }
23 |
24 | protected:
25 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
26 |
27 | private:
28 | static HWND m_hwnd;
29 | };
30 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/Win32Application.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #pragma once
13 |
14 | #include "DXSample.h"
15 |
16 | class DXSample;
17 |
18 | class Win32Application
19 | {
20 | public:
21 | static int Run(DXSample* pSample, HINSTANCE hInstance, int nCmdShow);
22 | static HWND GetHwnd() { return m_hwnd; }
23 |
24 | protected:
25 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
26 |
27 | private:
28 | static HWND m_hwnd;
29 | };
30 |
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/shaders.hlsl:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | struct PSInput
13 | {
14 | float4 position : SV_POSITION;
15 | float4 color : COLOR;
16 | };
17 |
18 | PSInput VSMain(float4 position : POSITION, float4 color : COLOR)
19 | {
20 | PSInput result;
21 |
22 | result.position = position;
23 | result.color = color;
24 |
25 | return result;
26 | }
27 |
28 | float4 PSMain(PSInput input) : SV_TARGET
29 | {
30 | float4 color = float4(sin(input.position.x / 10.0f), sin(input.position.y / 10.0f), cos((input.position.x + input.position.y) / 10.0f), 1);
31 |
32 | return color;
33 | }
34 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/shaders.hlsl:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | struct PSInput
13 | {
14 | float4 position : SV_POSITION;
15 | float4 color : COLOR;
16 | };
17 |
18 | PSInput VSMain(float4 position : POSITION, float4 color : COLOR)
19 | {
20 | PSInput result;
21 |
22 | result.position = position;
23 | result.color = color;
24 |
25 | return result;
26 | }
27 |
28 | float4 PSMain(PSInput input) : SV_TARGET
29 | {
30 | float4 color = float4(sin(input.position.x / 10.0f), sin(input.position.y / 10.0f), cos((input.position.x + input.position.y) / 10.0f), 1);
31 |
32 | return color;
33 | }
34 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/Fallback.hlsl:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | struct PSInput
13 | {
14 | float4 position : SV_POSITION;
15 | float4 color : COLOR;
16 | };
17 |
18 | PSInput VSFallback(float4 position : POSITION, float4 color : COLOR)
19 | {
20 | PSInput result;
21 |
22 | result.position = position;
23 | result.color = color;
24 |
25 | return result;
26 | }
27 |
28 | float4 PSFallback(PSInput input) : SV_TARGET
29 | {
30 | float4 color = float4(sin(input.position.x / 10.0f), sin(input.position.y / 10.0f), cos((input.position.x + input.position.y) / 10.0f), 1);
31 |
32 | return color;
33 | }
34 |
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/stdafx.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | // stdafx.h : include file for standard system include files,
13 | // or project specific include files that are used frequently, but
14 | // are changed infrequently.
15 |
16 | #pragma once
17 |
18 | #ifndef WIN32_LEAN_AND_MEAN
19 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers.
20 | #endif
21 |
22 | #include
23 |
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include "d3dx12.h"
29 |
30 | #include
31 | #include
32 | #include
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/stdafx.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | // stdafx.h : include file for standard system include files,
13 | // or project specific include files that are used frequently, but
14 | // are changed infrequently.
15 |
16 | #pragma once
17 |
18 | #ifndef WIN32_LEAN_AND_MEAN
19 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers.
20 | #endif
21 |
22 | #include
23 |
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include "d3dx12.h"
29 |
30 | #include
31 | #include
32 | #include
33 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 ClAndrew
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/stdafx.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | // stdafx.h : include file for standard system include files,
13 | // or project specific include files that are used frequently, but
14 | // are changed infrequently.
15 |
16 | #pragma once
17 |
18 | #ifndef WIN32_LEAN_AND_MEAN
19 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers.
20 | #endif
21 |
22 | #include
23 |
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include "d3dx12.h"
29 |
30 | #include
31 | #include
32 | #include
33 | #include
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/D3D12HelloWorld.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.30225.117
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "D3D12HelloTriangle", "HelloTriangle\D3D12HelloTriangle.vcxproj", "{BA127827-56C8-492A-8437-532BFB24B290}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|ARM64 = Debug|ARM64
11 | Debug|x64 = Debug|x64
12 | Release|ARM64 = Release|ARM64
13 | Release|x64 = Release|x64
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {BA127827-56C8-492A-8437-532BFB24B290}.Debug|ARM64.ActiveCfg = Debug|Win32
17 | {BA127827-56C8-492A-8437-532BFB24B290}.Debug|x64.ActiveCfg = Debug|x64
18 | {BA127827-56C8-492A-8437-532BFB24B290}.Debug|x64.Build.0 = Debug|x64
19 | {BA127827-56C8-492A-8437-532BFB24B290}.Release|ARM64.ActiveCfg = Release|Win32
20 | {BA127827-56C8-492A-8437-532BFB24B290}.Release|x64.ActiveCfg = Release|x64
21 | {BA127827-56C8-492A-8437-532BFB24B290}.Release|x64.Build.0 = Release|x64
22 | EndGlobalSection
23 | GlobalSection(SolutionProperties) = preSolution
24 | HideSolutionNode = FALSE
25 | EndGlobalSection
26 | GlobalSection(ExtensibilityGlobals) = postSolution
27 | SolutionGuid = {E02EF943-90FC-41AA-B9AF-12C38B9342B9}
28 | EndGlobalSection
29 | EndGlobal
30 |
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/D3D12HelloTriangle.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | WindowsLocalDebugger
5 | DESKTOP-0ACEOOM
6 | \\clandrew-5810\Samples\Desktop\D3D12HelloWorld\src\HelloTriangle\bin\x64\Debug\D3D12HelloTriangle.exe
7 | \\clandrew-5810\Samples\Desktop\D3D12HelloWorld\src\HelloTriangle\bin\x64\Debug
8 | RemoteWithoutAuthentication
9 |
10 |
11 | WindowsRemoteDebugger
12 | \\clandrew-5810\Share\Tmp\samples\d3d12\variablerateshading\D3D12VariableRateShading.exe
13 | \\clandrew-5810\Share\Tmp\samples\d3d12\variablerateshading\
14 | DESKTOP-K7J412I
15 | RemoteWithAuthentication
16 | NativeOnly
17 |
18 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/D3D12HelloTriangle.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | WindowsLocalDebugger
5 | DESKTOP-0ACEOOM
6 | \\clandrew-5810\Samples\Desktop\D3D12HelloWorld\src\HelloTriangle\bin\x64\Debug\D3D12HelloTriangle.exe
7 | \\clandrew-5810\Samples\Desktop\D3D12HelloWorld\src\HelloTriangle\bin\x64\Debug
8 | RemoteWithoutAuthentication
9 |
10 |
11 | WindowsRemoteDebugger
12 | \\clandrew-5810\Share\Tmp\samples\d3d12\variablerateshading\D3D12VariableRateShading.exe
13 | \\clandrew-5810\Share\Tmp\samples\d3d12\variablerateshading\
14 | DESKTOP-K7J412I
15 | RemoteWithAuthentication
16 | NativeOnly
17 |
18 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/D3D12HelloTriangle.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | WindowsLocalDebugger
5 | DESKTOP-0ACEOOM
6 | \\clandrew-5810\Samples\Desktop\D3D12HelloWorld\src\HelloTriangle\bin\x64\Debug\D3D12HelloTriangle.exe
7 | \\clandrew-5810\Samples\Desktop\D3D12HelloWorld\src\HelloTriangle\bin\x64\Debug
8 | RemoteWithoutAuthentication
9 |
10 |
11 | WindowsRemoteDebugger
12 | \\clandrew-5810\Share\Tmp\samples\d3d12\variablerateshading\D3D12VariableRateShading.exe
13 | \\clandrew-5810\Share\Tmp\samples\d3d12\variablerateshading\
14 | DESKTOP-K7J412I
15 | RemoteWithAuthentication
16 | NativeOnly
17 |
18 |
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/vkHelloTriangle.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.31728.308
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "vkHelloTriangle", "vkHelloTriangle.vcxproj", "{BE279FC3-DC18-4D1D-A565-9FA975C687BA}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {BE279FC3-DC18-4D1D-A565-9FA975C687BA}.Debug|x64.ActiveCfg = Debug|x64
17 | {BE279FC3-DC18-4D1D-A565-9FA975C687BA}.Debug|x64.Build.0 = Debug|x64
18 | {BE279FC3-DC18-4D1D-A565-9FA975C687BA}.Debug|x86.ActiveCfg = Debug|Win32
19 | {BE279FC3-DC18-4D1D-A565-9FA975C687BA}.Debug|x86.Build.0 = Debug|Win32
20 | {BE279FC3-DC18-4D1D-A565-9FA975C687BA}.Release|x64.ActiveCfg = Release|x64
21 | {BE279FC3-DC18-4D1D-A565-9FA975C687BA}.Release|x64.Build.0 = Release|x64
22 | {BE279FC3-DC18-4D1D-A565-9FA975C687BA}.Release|x86.ActiveCfg = Release|Win32
23 | {BE279FC3-DC18-4D1D-A565-9FA975C687BA}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {68B00328-031E-45B2-8E67-EB10485FCDC0}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/hellovrs_rates/D3D12HelloWorld.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27130.2020
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "D3D12HelloTriangle", "HelloTriangle\D3D12HelloTriangle.vcxproj", "{5018F6A3-6533-4744-B1FD-727D199FD2E9}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|ARM64 = Debug|ARM64
11 | Debug|x64 = Debug|x64
12 | Release|ARM64 = Release|ARM64
13 | Release|x64 = Release|x64
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Debug|ARM64.ActiveCfg = Debug|ARM64
17 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Debug|ARM64.Build.0 = Debug|ARM64
18 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Debug|x64.ActiveCfg = Debug|x64
19 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Debug|x64.Build.0 = Debug|x64
20 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Release|ARM64.ActiveCfg = Release|ARM64
21 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Release|ARM64.Build.0 = Release|ARM64
22 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Release|x64.ActiveCfg = Release|x64
23 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Release|x64.Build.0 = Release|x64
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {E02EF943-90FC-41AA-B9AF-12C38B9342B9}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/D3D12HelloWorld.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27130.2020
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "D3D12HelloTriangle", "HelloTriangle\D3D12HelloTriangle.vcxproj", "{5018F6A3-6533-4744-B1FD-727D199FD2E9}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|ARM64 = Debug|ARM64
11 | Debug|x64 = Debug|x64
12 | Release|ARM64 = Release|ARM64
13 | Release|x64 = Release|x64
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Debug|ARM64.ActiveCfg = Debug|ARM64
17 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Debug|ARM64.Build.0 = Debug|ARM64
18 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Debug|x64.ActiveCfg = Debug|x64
19 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Debug|x64.Build.0 = Debug|x64
20 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Release|ARM64.ActiveCfg = Release|ARM64
21 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Release|ARM64.Build.0 = Release|ARM64
22 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Release|x64.ActiveCfg = Release|x64
23 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}.Release|x64.Build.0 = Release|x64
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {E02EF943-90FC-41AA-B9AF-12C38B9342B9}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/DXSample.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #pragma once
13 |
14 | #include "DXSampleHelper.h"
15 | #include "Win32Application.h"
16 |
17 | class DXSample
18 | {
19 | public:
20 | DXSample(UINT width, UINT height, std::wstring name);
21 | virtual ~DXSample();
22 |
23 | virtual void OnInit() = 0;
24 | virtual void OnUpdate() = 0;
25 | virtual void OnRender() = 0;
26 | virtual void OnDestroy() = 0;
27 |
28 | // Samples override the event handlers to handle specific messages.
29 | virtual void OnKeyDown(UINT8 /*key*/) {}
30 | virtual void OnKeyUp(UINT8 /*key*/) {}
31 |
32 | // Accessors.
33 | UINT GetWidth() const { return m_width; }
34 | UINT GetHeight() const { return m_height; }
35 | const WCHAR* GetTitle() const { return m_title.c_str(); }
36 |
37 | void ParseCommandLineArgs(_In_reads_(argc) WCHAR* argv[], int argc);
38 |
39 | protected:
40 | std::wstring GetAssetFullPath(LPCWSTR assetName);
41 | void GetHardwareAdapter(_In_ IDXGIFactory2* pFactory, _Outptr_result_maybenull_ IDXGIAdapter1** ppAdapter);
42 | void SetCustomWindowText(LPCWSTR text);
43 |
44 | // Viewport dimensions.
45 | UINT m_width;
46 | UINT m_height;
47 | float m_aspectRatio;
48 |
49 | // Adapter info.
50 | bool m_useWarpDevice;
51 |
52 | private:
53 | // Root assets path.
54 | std::wstring m_assetsPath;
55 |
56 | // Window title.
57 | std::wstring m_title;
58 | };
59 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/DXSample.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #pragma once
13 |
14 | #include "DXSampleHelper.h"
15 | #include "Win32Application.h"
16 |
17 | class DXSample
18 | {
19 | public:
20 | DXSample(UINT width, UINT height, std::wstring name);
21 | virtual ~DXSample();
22 |
23 | virtual void OnInit() = 0;
24 | virtual void OnUpdate() = 0;
25 | virtual void OnRender() = 0;
26 | virtual void OnDestroy() = 0;
27 |
28 | // Samples override the event handlers to handle specific messages.
29 | virtual void OnKeyDown(UINT8 /*key*/) {}
30 | virtual void OnKeyUp(UINT8 /*key*/) {}
31 |
32 | // Accessors.
33 | UINT GetWidth() const { return m_width; }
34 | UINT GetHeight() const { return m_height; }
35 | const WCHAR* GetTitle() const { return m_title.c_str(); }
36 |
37 | void ParseCommandLineArgs(_In_reads_(argc) WCHAR* argv[], int argc);
38 |
39 | protected:
40 | std::wstring GetAssetFullPath(LPCWSTR assetName);
41 | void GetHardwareAdapter(_In_ IDXGIFactory2* pFactory, _Outptr_result_maybenull_ IDXGIAdapter1** ppAdapter);
42 | void SetCustomWindowText(LPCWSTR text);
43 |
44 | // Viewport dimensions.
45 | UINT m_width;
46 | UINT m_height;
47 | float m_aspectRatio;
48 |
49 | // Adapter info.
50 | bool m_useWarpDevice;
51 |
52 | private:
53 | // Root assets path.
54 | std::wstring m_assetsPath;
55 |
56 | // Window title.
57 | std::wstring m_title;
58 | };
59 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/DXSample.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #pragma once
13 |
14 | #include "DXSampleHelper.h"
15 | #include "Win32Application.h"
16 |
17 | class DXSample
18 | {
19 | public:
20 | DXSample(UINT width, UINT height, std::wstring name);
21 | virtual ~DXSample();
22 |
23 | virtual void OnInit() = 0;
24 | virtual void OnUpdate() = 0;
25 | virtual void OnRender() = 0;
26 | virtual void OnDestroy() = 0;
27 |
28 | // Samples override the event handlers to handle specific messages.
29 | virtual void OnKeyDown(UINT8 /*key*/) {}
30 | virtual void OnKeyUp(UINT8 /*key*/) {}
31 |
32 | // Accessors.
33 | UINT GetWidth() const { return m_width; }
34 | UINT GetHeight() const { return m_height; }
35 | const WCHAR* GetTitle() const { return m_title.c_str(); }
36 |
37 | void ParseCommandLineArgs(_In_reads_(argc) WCHAR* argv[], int argc);
38 |
39 | protected:
40 | std::wstring GetAssetFullPath(LPCWSTR assetName);
41 | void GetHardwareAdapter(_In_ IDXGIFactory2* pFactory, _Outptr_result_maybenull_ IDXGIAdapter1** ppAdapter);
42 | void SetCustomWindowText(LPCWSTR text);
43 |
44 | // Viewport dimensions.
45 | UINT m_width;
46 | UINT m_height;
47 | float m_aspectRatio;
48 |
49 | // Adapter info.
50 | bool m_useWarpDevice;
51 |
52 | private:
53 | // Root assets path.
54 | std::wstring m_assetsPath;
55 |
56 | // Window title.
57 | std::wstring m_title;
58 | };
59 |
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/D3D12HelloTriangle.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {67bebf86-9539-4f7f-b9e9-a376519ec1f1}
6 |
7 |
8 | {954a8f61-1c3e-419b-baca-c5e2b83c689a}
9 |
10 |
11 | {09095d68-bcd7-45c4-b007-5c8ddb5d60a4}
12 |
13 |
14 | {8b0cb0c9-e0d8-4107-91c8-11654fac88e7}
15 |
16 |
17 |
18 |
19 | Header Files
20 |
21 |
22 | Header Files
23 |
24 |
25 | Header Files
26 |
27 |
28 | Header Files
29 |
30 |
31 | Header Files
32 |
33 |
34 | Header Files
35 |
36 |
37 |
38 |
39 | Source Files
40 |
41 |
42 | Source Files
43 |
44 |
45 | Source Files
46 |
47 |
48 | Source Files
49 |
50 |
51 | Source Files
52 |
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/D3D12HelloTriangle.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {67bebf86-9539-4f7f-b9e9-a376519ec1f1}
6 |
7 |
8 | {954a8f61-1c3e-419b-baca-c5e2b83c689a}
9 |
10 |
11 | {09095d68-bcd7-45c4-b007-5c8ddb5d60a4}
12 |
13 |
14 | {8b0cb0c9-e0d8-4107-91c8-11654fac88e7}
15 |
16 |
17 |
18 |
19 | Header Files
20 |
21 |
22 | Header Files
23 |
24 |
25 | Header Files
26 |
27 |
28 | Header Files
29 |
30 |
31 | Header Files
32 |
33 |
34 | Header Files
35 |
36 |
37 |
38 |
39 | Source Files
40 |
41 |
42 | Source Files
43 |
44 |
45 | Source Files
46 |
47 |
48 | Source Files
49 |
50 |
51 | Source Files
52 |
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/vkHelloTriangle.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | Header Files
20 |
21 |
22 | Header Files
23 |
24 |
25 | Header Files
26 |
27 |
28 | Header Files
29 |
30 |
31 | Header Files
32 |
33 |
34 |
35 |
36 | Source Files
37 |
38 |
39 | Source Files
40 |
41 |
42 |
43 |
44 | Resource Files
45 |
46 |
47 |
48 |
49 | Resource Files
50 |
51 |
52 | Resource Files
53 |
54 |
55 |
56 |
57 | Source Files
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # VRS Samples
2 | This repo contains some simple, standalone test applications that exercise Direct3D12 Variable Rate Shading.
3 |
4 |
5 | ## hellovrs_simple
6 | A triangle straightforwardly shaded. The shading rate is specified through [RSSetShadingRate](https://docs.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12graphicscommandlist5-rssetshadingrate) API. Tier 1 compatible.
7 |
8 | Controls: Use the left and right arrow keys to select a shading rate, shown in the title bar. The default is 1x1.
9 |
10 | 
11 |
12 | This sample has both D3D12 and Vulkan versions.
13 |
14 | ## hellovrs_rates
15 | A bunch of shaded quads, one for each shading rate. Their shading rate is specified through RSSetShadingRate API. Tier 1 compatible.
16 |
17 | Controls: none
18 |
19 | 
20 |
21 | ## hellovrs_arrowkeys_tier2
22 | One triangle shaded while a screenspace image is also bound. There is combined use of the base shading rate through RSSetShadingRate, binding of the image through [RSSetShadingRateImage](https://docs.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12graphicscommandlist5-rssetshadingrateimage), and a per-primitive rate using SV_ShadingRate system value.
23 |
24 | The base shading rate is used in two places:
25 | * as input to RSSetShadingRate
26 | * and as a value in the screenspace image.
27 | More specifically the screenspace-image is fixed to have the base shading rate in the upper right, and fine shading (1x1) in the lower left.
28 |
29 | Controls: Use the left and right arrow keys to select a shading rate.
30 | Use the up and down arrow keys to select a combiner. There's three choices:
31 | * AlwaysScreenspace (default)
32 | * AlwaysPerPrimitive
33 | * Min
34 |
35 | 
36 |
37 | ## Build and usage
38 | These samples were built using Visual Studio 2019, version 142 of the build tools. They run on x86-64 architecture.
39 | The shaders for hellovrs_arrowkeys_tier2 are built using the Developer Command Prompt for Visual Studio 2019.
40 |
41 | The samples are set up to run against the default hardware adapter; use of VRS requires a compatible system.
42 |
43 | The Vulkan sample requires the Vulkan SDK. You'll have to edit the project file and CompileShaders.bat to point it to your SDK.
44 |
45 | ## See also
46 | The spec for Variable Rate Shading can be viewed [here](https://github.com/microsoft/DirectX-Specs/blob/master/d3d/VariableRateShading.md).
47 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/D3D12HelloTriangle.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #pragma once
13 |
14 | #include "DXSample.h"
15 |
16 | using namespace DirectX;
17 |
18 | // Note that while ComPtr is used to manage the lifetime of resources on the CPU,
19 | // it has no understanding of the lifetime of resources on the GPU. Apps must account
20 | // for the GPU lifetime of resources to avoid destroying objects that may still be
21 | // referenced by the GPU.
22 | // An example of this can be found in the class method: OnDestroy().
23 | using Microsoft::WRL::ComPtr;
24 |
25 | class D3D12HelloTriangle : public DXSample
26 | {
27 | public:
28 | D3D12HelloTriangle(UINT width, UINT height, std::wstring name);
29 |
30 | virtual void OnInit();
31 | virtual void OnUpdate();
32 | virtual void OnRender();
33 | virtual void OnDestroy();
34 |
35 | virtual void OnKeyUp(UINT8 key) override;
36 |
37 | private:
38 | static const UINT FrameCount = 2;
39 |
40 | struct Vertex
41 | {
42 | XMFLOAT3 position;
43 | XMFLOAT4 color;
44 | };
45 |
46 | // Pipeline objects.
47 | CD3DX12_VIEWPORT m_viewport;
48 | CD3DX12_RECT m_scissorRect;
49 | ComPtr m_swapChain;
50 | ComPtr m_device;
51 | ComPtr m_renderTargets[FrameCount];
52 | ComPtr m_commandAllocator;
53 | ComPtr m_commandQueue;
54 | ComPtr m_rootSignature;
55 | ComPtr m_rtvHeap;
56 | ComPtr m_pipelineState;
57 | ComPtr m_commandList;
58 | ComPtr m_commandList5;
59 | UINT m_rtvDescriptorSize;
60 |
61 | // App resources.
62 | ComPtr m_vertexBuffer;
63 | D3D12_VERTEX_BUFFER_VIEW m_vertexBufferView;
64 |
65 | // Synchronization objects.
66 | UINT m_frameIndex;
67 | HANDLE m_fenceEvent;
68 | ComPtr m_fence;
69 | UINT64 m_fenceValue;
70 |
71 | UINT m_shadingRateIndex;
72 | bool m_additionalRatesSupported;
73 | bool m_platformSupportsVRS;
74 |
75 | void LoadPipeline();
76 | void LoadAssets();
77 | void PopulateCommandList();
78 | void WaitForPreviousFrame();
79 |
80 | void UpdateTitleText();
81 | };
82 |
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/D3D12HelloTriangle.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #pragma once
13 |
14 | #include "DXSample.h"
15 |
16 | #include
17 |
18 | using namespace DirectX;
19 |
20 | // Note that while ComPtr is used to manage the lifetime of resources on the CPU,
21 | // it has no understanding of the lifetime of resources on the GPU. Apps must account
22 | // for the GPU lifetime of resources to avoid destroying objects that may still be
23 | // referenced by the GPU.
24 | // An example of this can be found in the class method: OnDestroy().
25 | using Microsoft::WRL::ComPtr;
26 |
27 | class D3D12HelloTriangle : public DXSample
28 | {
29 | public:
30 | D3D12HelloTriangle(UINT width, UINT height, std::wstring name);
31 |
32 | virtual void OnInit();
33 | virtual void OnUpdate();
34 | virtual void OnRender();
35 | virtual void OnDestroy();
36 |
37 | private:
38 | static const UINT FrameCount = 2;
39 |
40 | struct Vertex
41 | {
42 | XMFLOAT3 position;
43 | XMFLOAT4 color;
44 | };
45 |
46 | // Pipeline objects.
47 | CD3DX12_VIEWPORT m_viewport;
48 | CD3DX12_RECT m_scissorRect;
49 | ComPtr m_swapChain;
50 | ComPtr m_device;
51 | ComPtr m_renderTargets[FrameCount];
52 | ComPtr m_commandAllocator;
53 | ComPtr m_commandQueue;
54 | ComPtr m_rootSignature;
55 | ComPtr m_rtvHeap;
56 | ComPtr m_pipelineState;
57 | ComPtr m_commandList;
58 | ComPtr m_commandList5;
59 | UINT m_rtvDescriptorSize;
60 |
61 | // App resources.
62 | ComPtr m_vertexBuffer;
63 | D3D12_VERTEX_BUFFER_VIEW m_vertexBufferView;
64 | std::vector m_triangleVertices;
65 | bool m_platformSupportsVRS;
66 | bool m_additionalShadingRatesSupported;
67 |
68 | // Synchronization objects.
69 | UINT m_frameIndex;
70 | HANDLE m_fenceEvent;
71 | ComPtr m_fence;
72 | UINT64 m_fenceValue;
73 |
74 | void LoadPipeline();
75 | void LoadAssets();
76 | void PopulateCommandList();
77 | void WaitForPreviousFrame();
78 |
79 | void AddQuad(float left, float bottom, float quadSize);
80 |
81 | void UpdateTitleText();
82 | };
83 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/D3D12HelloTriangle.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {67bebf86-9539-4f7f-b9e9-a376519ec1f1}
6 |
7 |
8 | {954a8f61-1c3e-419b-baca-c5e2b83c689a}
9 |
10 |
11 | {09095d68-bcd7-45c4-b007-5c8ddb5d60a4}
12 |
13 |
14 | {8b0cb0c9-e0d8-4107-91c8-11654fac88e7}
15 |
16 |
17 | {1327f3c1-c22c-4d88-bfde-2e752116f2b6}
18 |
19 |
20 |
21 |
22 | Source Files
23 |
24 |
25 | Source Files
26 |
27 |
28 | Source Files
29 |
30 |
31 | Source Files
32 |
33 |
34 | Source Files
35 |
36 |
37 | Source Files
38 |
39 |
40 | Source Files
41 |
42 |
43 | Source Files
44 |
45 |
46 | Source Files
47 |
48 |
49 | Source Files
50 |
51 |
52 |
53 |
54 | Source Files
55 |
56 |
57 | Source Files
58 |
59 |
60 | Source Files
61 |
62 |
63 | Source Files
64 |
65 |
66 | Source Files
67 |
68 |
69 |
70 |
71 | Shaders
72 |
73 |
74 | Shaders
75 |
76 |
77 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/D3D12HelloTriangle.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #pragma once
13 |
14 | #include "DXSample.h"
15 |
16 | using namespace DirectX;
17 |
18 | // Note that while ComPtr is used to manage the lifetime of resources on the CPU,
19 | // it has no understanding of the lifetime of resources on the GPU. Apps must account
20 | // for the GPU lifetime of resources to avoid destroying objects that may still be
21 | // referenced by the GPU.
22 | // An example of this can be found in the class method: OnDestroy().
23 | using Microsoft::WRL::ComPtr;
24 |
25 | class D3D12HelloTriangle : public DXSample
26 | {
27 | public:
28 | D3D12HelloTriangle(UINT width, UINT height, std::wstring name);
29 |
30 | virtual void OnInit();
31 | virtual void OnUpdate();
32 | virtual void OnRender();
33 | virtual void OnDestroy();
34 |
35 | virtual void OnKeyUp(UINT8 key) override;
36 |
37 | private:
38 | static const UINT FrameCount = 2;
39 |
40 | struct Vertex
41 | {
42 | XMFLOAT3 position;
43 | XMFLOAT4 color;
44 | };
45 |
46 | // Pipeline objects.
47 | CD3DX12_VIEWPORT m_viewport;
48 | CD3DX12_RECT m_scissorRect;
49 | ComPtr m_swapChain;
50 | ComPtr m_device;
51 | ComPtr m_renderTargets[FrameCount];
52 | ComPtr m_commandAllocator;
53 | ComPtr m_commandQueue;
54 | ComPtr m_rootSignature;
55 | ComPtr m_rtvHeap;
56 | ComPtr m_pipelineState;
57 | ComPtr m_commandList;
58 | ComPtr m_commandList5;
59 | UINT m_rtvDescriptorSize;
60 |
61 | bool m_platformSupportsTier2VRS;
62 | ComPtr m_spScreenspaceImage;
63 | ComPtr m_spScreenspaceImageUpload;
64 |
65 | // App resources.
66 | ComPtr m_vertexBuffer;
67 | D3D12_VERTEX_BUFFER_VIEW m_vertexBufferView;
68 |
69 | // Synchronization objects.
70 | UINT m_frameIndex;
71 | HANDLE m_fenceEvent;
72 | ComPtr m_fence;
73 | UINT64 m_fenceValue;
74 |
75 | UINT m_shadingRateIndex;
76 | UINT m_combinerTypeIndex;
77 |
78 | void LoadPipeline();
79 | void LoadAssets();
80 | void PopulateCommandList();
81 | void WaitForPreviousFrame();
82 |
83 | void UpdateTitleText();
84 | void AllocateScreenspaceImage(UINT width = 1, UINT height = 1);
85 | void SetScreenspaceImageData(ID3D12GraphicsCommandList5* cl, BYTE* data, size_t dataSize);
86 | };
87 |
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/DXSample.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 | #include "DXSample.h"
14 |
15 | using namespace Microsoft::WRL;
16 |
17 | DXSample::DXSample(UINT width, UINT height, std::wstring name) :
18 | m_width(width),
19 | m_height(height),
20 | m_title(name),
21 | m_useWarpDevice(false)
22 | {
23 | WCHAR assetsPath[512];
24 | GetAssetsPath(assetsPath, _countof(assetsPath));
25 | m_assetsPath = assetsPath;
26 |
27 | m_aspectRatio = static_cast(width) / static_cast(height);
28 | }
29 |
30 | DXSample::~DXSample()
31 | {
32 | }
33 |
34 | // Helper function for resolving the full path of assets.
35 | std::wstring DXSample::GetAssetFullPath(LPCWSTR assetName)
36 | {
37 | return m_assetsPath + assetName;
38 | }
39 |
40 | // Helper function for acquiring the first available hardware adapter that supports Direct3D 12.
41 | // If no such adapter can be found, *ppAdapter will be set to nullptr.
42 | _Use_decl_annotations_
43 | void DXSample::GetHardwareAdapter(IDXGIFactory2* pFactory, IDXGIAdapter1** ppAdapter)
44 | {
45 | ComPtr adapter;
46 | *ppAdapter = nullptr;
47 |
48 | for (UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != pFactory->EnumAdapters1(adapterIndex, &adapter); ++adapterIndex)
49 | {
50 | DXGI_ADAPTER_DESC1 desc;
51 | adapter->GetDesc1(&desc);
52 |
53 | if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
54 | {
55 | // Don't select the Basic Render Driver adapter.
56 | // If you want a software adapter, pass in "/warp" on the command line.
57 | continue;
58 | }
59 |
60 | // Check to see if the adapter supports Direct3D 12, but don't create the
61 | // actual device yet.
62 | if (SUCCEEDED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), nullptr)))
63 | {
64 | break;
65 | }
66 | }
67 |
68 | *ppAdapter = adapter.Detach();
69 | }
70 |
71 | // Helper function for setting the window's title text.
72 | void DXSample::SetCustomWindowText(LPCWSTR text)
73 | {
74 | std::wstring windowText = m_title + L": " + text;
75 | SetWindowText(Win32Application::GetHwnd(), windowText.c_str());
76 | }
77 |
78 | // Helper function for parsing any supplied command line args.
79 | _Use_decl_annotations_
80 | void DXSample::ParseCommandLineArgs(WCHAR* argv[], int argc)
81 | {
82 | for (int i = 1; i < argc; ++i)
83 | {
84 | if (_wcsnicmp(argv[i], L"-warp", wcslen(argv[i])) == 0 ||
85 | _wcsnicmp(argv[i], L"/warp", wcslen(argv[i])) == 0)
86 | {
87 | m_useWarpDevice = true;
88 | m_title = m_title + L" (WARP)";
89 | }
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/DXSample.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 | #include "DXSample.h"
14 |
15 | using namespace Microsoft::WRL;
16 |
17 | DXSample::DXSample(UINT width, UINT height, std::wstring name) :
18 | m_width(width),
19 | m_height(height),
20 | m_title(name),
21 | m_useWarpDevice(false)
22 | {
23 | WCHAR assetsPath[512];
24 | GetAssetsPath(assetsPath, _countof(assetsPath));
25 | m_assetsPath = assetsPath;
26 |
27 | m_aspectRatio = static_cast(width) / static_cast(height);
28 | }
29 |
30 | DXSample::~DXSample()
31 | {
32 | }
33 |
34 | // Helper function for resolving the full path of assets.
35 | std::wstring DXSample::GetAssetFullPath(LPCWSTR assetName)
36 | {
37 | return m_assetsPath + assetName;
38 | }
39 |
40 | // Helper function for acquiring the first available hardware adapter that supports Direct3D 12.
41 | // If no such adapter can be found, *ppAdapter will be set to nullptr.
42 | _Use_decl_annotations_
43 | void DXSample::GetHardwareAdapter(IDXGIFactory2* pFactory, IDXGIAdapter1** ppAdapter)
44 | {
45 | ComPtr adapter;
46 | *ppAdapter = nullptr;
47 |
48 | for (UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != pFactory->EnumAdapters1(adapterIndex, &adapter); ++adapterIndex)
49 | {
50 | DXGI_ADAPTER_DESC1 desc;
51 | adapter->GetDesc1(&desc);
52 |
53 | if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
54 | {
55 | // Don't select the Basic Render Driver adapter.
56 | // If you want a software adapter, pass in "/warp" on the command line.
57 | continue;
58 | }
59 |
60 | // Check to see if the adapter supports Direct3D 12, but don't create the
61 | // actual device yet.
62 | if (SUCCEEDED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), nullptr)))
63 | {
64 | break;
65 | }
66 | }
67 |
68 | *ppAdapter = adapter.Detach();
69 | }
70 |
71 | // Helper function for setting the window's title text.
72 | void DXSample::SetCustomWindowText(LPCWSTR text)
73 | {
74 | std::wstring windowText = m_title + L": " + text;
75 | SetWindowText(Win32Application::GetHwnd(), windowText.c_str());
76 | }
77 |
78 | // Helper function for parsing any supplied command line args.
79 | _Use_decl_annotations_
80 | void DXSample::ParseCommandLineArgs(WCHAR* argv[], int argc)
81 | {
82 | for (int i = 1; i < argc; ++i)
83 | {
84 | if (_wcsnicmp(argv[i], L"-warp", wcslen(argv[i])) == 0 ||
85 | _wcsnicmp(argv[i], L"/warp", wcslen(argv[i])) == 0)
86 | {
87 | m_useWarpDevice = true;
88 | m_title = m_title + L" (WARP)";
89 | }
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/DXSample.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 | #include "DXSample.h"
14 |
15 | using namespace Microsoft::WRL;
16 |
17 | DXSample::DXSample(UINT width, UINT height, std::wstring name) :
18 | m_width(width),
19 | m_height(height),
20 | m_title(name),
21 | m_useWarpDevice(false)
22 | {
23 | WCHAR assetsPath[512];
24 | GetAssetsPath(assetsPath, _countof(assetsPath));
25 | m_assetsPath = assetsPath;
26 |
27 | m_aspectRatio = static_cast(width) / static_cast(height);
28 | }
29 |
30 | DXSample::~DXSample()
31 | {
32 | }
33 |
34 | // Helper function for resolving the full path of assets.
35 | std::wstring DXSample::GetAssetFullPath(LPCWSTR assetName)
36 | {
37 | return m_assetsPath + assetName;
38 | }
39 |
40 | // Helper function for acquiring the first available hardware adapter that supports Direct3D 12.
41 | // If no such adapter can be found, *ppAdapter will be set to nullptr.
42 | _Use_decl_annotations_
43 | void DXSample::GetHardwareAdapter(IDXGIFactory2* pFactory, IDXGIAdapter1** ppAdapter)
44 | {
45 | ComPtr adapter;
46 | *ppAdapter = nullptr;
47 |
48 | for (UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != pFactory->EnumAdapters1(adapterIndex, &adapter); ++adapterIndex)
49 | {
50 | DXGI_ADAPTER_DESC1 desc;
51 | adapter->GetDesc1(&desc);
52 |
53 | if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
54 | {
55 | // Don't select the Basic Render Driver adapter.
56 | // If you want a software adapter, pass in "/warp" on the command line.
57 | continue;
58 | }
59 |
60 | // Check to see if the adapter supports Direct3D 12, but don't create the
61 | // actual device yet.
62 | if (SUCCEEDED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), nullptr)))
63 | {
64 | break;
65 | }
66 | }
67 |
68 | *ppAdapter = adapter.Detach();
69 | }
70 |
71 | // Helper function for setting the window's title text.
72 | void DXSample::SetCustomWindowText(LPCWSTR text)
73 | {
74 | std::wstring windowText = m_title + L": " + text;
75 | SetWindowText(Win32Application::GetHwnd(), windowText.c_str());
76 | }
77 |
78 | // Helper function for parsing any supplied command line args.
79 | _Use_decl_annotations_
80 | void DXSample::ParseCommandLineArgs(WCHAR* argv[], int argc)
81 | {
82 | for (int i = 1; i < argc; ++i)
83 | {
84 | if (_wcsnicmp(argv[i], L"-warp", wcslen(argv[i])) == 0 ||
85 | _wcsnicmp(argv[i], L"/warp", wcslen(argv[i])) == 0)
86 | {
87 | m_useWarpDevice = true;
88 | m_title = m_title + L" (WARP)";
89 | }
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/DXSampleHelper.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #pragma once
13 |
14 | inline void ThrowIfFailed(HRESULT hr)
15 | {
16 | if (FAILED(hr))
17 | {
18 | throw std::exception();
19 | }
20 | }
21 |
22 | inline void GetAssetsPath(_Out_writes_(pathSize) WCHAR* path, UINT pathSize)
23 | {
24 | if (path == nullptr)
25 | {
26 | throw std::exception();
27 | }
28 |
29 | DWORD size = GetModuleFileName(nullptr, path, pathSize);
30 | if (size == 0 || size == pathSize)
31 | {
32 | // Method failed or path was truncated.
33 | throw std::exception();
34 | }
35 |
36 | WCHAR* lastSlash = wcsrchr(path, L'\\');
37 | if (lastSlash)
38 | {
39 | *(lastSlash + 1) = L'\0';
40 | }
41 | }
42 |
43 | inline HRESULT ReadDataFromFile(LPCWSTR filename, byte** data, UINT* size)
44 | {
45 | using namespace Microsoft::WRL;
46 |
47 | CREATEFILE2_EXTENDED_PARAMETERS extendedParams = {};
48 | extendedParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
49 | extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
50 | extendedParams.dwFileFlags = FILE_FLAG_SEQUENTIAL_SCAN;
51 | extendedParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
52 | extendedParams.lpSecurityAttributes = nullptr;
53 | extendedParams.hTemplateFile = nullptr;
54 |
55 | Wrappers::FileHandle file(CreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, &extendedParams));
56 | if (file.Get() == INVALID_HANDLE_VALUE)
57 | {
58 | throw std::exception();
59 | }
60 |
61 | FILE_STANDARD_INFO fileInfo = {};
62 | if (!GetFileInformationByHandleEx(file.Get(), FileStandardInfo, &fileInfo, sizeof(fileInfo)))
63 | {
64 | throw std::exception();
65 | }
66 |
67 | if (fileInfo.EndOfFile.HighPart != 0)
68 | {
69 | throw std::exception();
70 | }
71 |
72 | *data = reinterpret_cast(malloc(fileInfo.EndOfFile.LowPart));
73 | *size = fileInfo.EndOfFile.LowPart;
74 |
75 | if (!ReadFile(file.Get(), *data, fileInfo.EndOfFile.LowPart, nullptr, nullptr))
76 | {
77 | throw std::exception();
78 | }
79 |
80 | return S_OK;
81 | }
82 |
83 | // Assign a name to the object to aid with debugging.
84 | #if defined(_DEBUG)
85 | inline void SetName(ID3D12Object* pObject, LPCWSTR name)
86 | {
87 | pObject->SetName(name);
88 | }
89 | inline void SetNameIndexed(ID3D12Object* pObject, LPCWSTR name, UINT index)
90 | {
91 | WCHAR fullName[50];
92 | if (swprintf_s(fullName, L"%s[%u]", name, index) > 0)
93 | {
94 | pObject->SetName(fullName);
95 | }
96 | }
97 | #else
98 | inline void SetName(ID3D12Object*, LPCWSTR)
99 | {
100 | }
101 | inline void SetNameIndexed(ID3D12Object*, LPCWSTR, UINT)
102 | {
103 | }
104 | #endif
105 |
106 | // Naming helper for ComPtr.
107 | // Assigns the name of the variable as the name of the object.
108 | // The indexed variant will include the index in the name of the object.
109 | #define NAME_D3D12_OBJECT(x) SetName(x.Get(), L#x)
110 | #define NAME_D3D12_OBJECT_INDEXED(x, n) SetNameIndexed(x[n].Get(), L#x, n)
111 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/DXSampleHelper.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #pragma once
13 |
14 | inline void ThrowIfFailed(HRESULT hr)
15 | {
16 | if (FAILED(hr))
17 | {
18 | throw std::exception();
19 | }
20 | }
21 |
22 | inline void GetAssetsPath(_Out_writes_(pathSize) WCHAR* path, UINT pathSize)
23 | {
24 | if (path == nullptr)
25 | {
26 | throw std::exception();
27 | }
28 |
29 | DWORD size = GetModuleFileName(nullptr, path, pathSize);
30 | if (size == 0 || size == pathSize)
31 | {
32 | // Method failed or path was truncated.
33 | throw std::exception();
34 | }
35 |
36 | WCHAR* lastSlash = wcsrchr(path, L'\\');
37 | if (lastSlash)
38 | {
39 | *(lastSlash + 1) = L'\0';
40 | }
41 | }
42 |
43 | inline HRESULT ReadDataFromFile(LPCWSTR filename, byte** data, UINT* size)
44 | {
45 | using namespace Microsoft::WRL;
46 |
47 | CREATEFILE2_EXTENDED_PARAMETERS extendedParams = {};
48 | extendedParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
49 | extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
50 | extendedParams.dwFileFlags = FILE_FLAG_SEQUENTIAL_SCAN;
51 | extendedParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
52 | extendedParams.lpSecurityAttributes = nullptr;
53 | extendedParams.hTemplateFile = nullptr;
54 |
55 | Wrappers::FileHandle file(CreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, &extendedParams));
56 | if (file.Get() == INVALID_HANDLE_VALUE)
57 | {
58 | throw std::exception();
59 | }
60 |
61 | FILE_STANDARD_INFO fileInfo = {};
62 | if (!GetFileInformationByHandleEx(file.Get(), FileStandardInfo, &fileInfo, sizeof(fileInfo)))
63 | {
64 | throw std::exception();
65 | }
66 |
67 | if (fileInfo.EndOfFile.HighPart != 0)
68 | {
69 | throw std::exception();
70 | }
71 |
72 | *data = reinterpret_cast(malloc(fileInfo.EndOfFile.LowPart));
73 | *size = fileInfo.EndOfFile.LowPart;
74 |
75 | if (!ReadFile(file.Get(), *data, fileInfo.EndOfFile.LowPart, nullptr, nullptr))
76 | {
77 | throw std::exception();
78 | }
79 |
80 | return S_OK;
81 | }
82 |
83 | // Assign a name to the object to aid with debugging.
84 | #if defined(_DEBUG)
85 | inline void SetName(ID3D12Object* pObject, LPCWSTR name)
86 | {
87 | pObject->SetName(name);
88 | }
89 | inline void SetNameIndexed(ID3D12Object* pObject, LPCWSTR name, UINT index)
90 | {
91 | WCHAR fullName[50];
92 | if (swprintf_s(fullName, L"%s[%u]", name, index) > 0)
93 | {
94 | pObject->SetName(fullName);
95 | }
96 | }
97 | #else
98 | inline void SetName(ID3D12Object*, LPCWSTR)
99 | {
100 | }
101 | inline void SetNameIndexed(ID3D12Object*, LPCWSTR, UINT)
102 | {
103 | }
104 | #endif
105 |
106 | // Naming helper for ComPtr.
107 | // Assigns the name of the variable as the name of the object.
108 | // The indexed variant will include the index in the name of the object.
109 | #define NAME_D3D12_OBJECT(x) SetName(x.Get(), L#x)
110 | #define NAME_D3D12_OBJECT_INDEXED(x, n) SetNameIndexed(x[n].Get(), L#x, n)
111 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/DXSampleHelper.h:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #pragma once
13 |
14 | inline void ThrowIfFailed(HRESULT hr)
15 | {
16 | if (FAILED(hr))
17 | {
18 | throw std::exception();
19 | }
20 | }
21 |
22 | inline void GetAssetsPath(_Out_writes_(pathSize) WCHAR* path, UINT pathSize)
23 | {
24 | if (path == nullptr)
25 | {
26 | throw std::exception();
27 | }
28 |
29 | DWORD size = GetModuleFileName(nullptr, path, pathSize);
30 | if (size == 0 || size == pathSize)
31 | {
32 | // Method failed or path was truncated.
33 | throw std::exception();
34 | }
35 |
36 | WCHAR* lastSlash = wcsrchr(path, L'\\');
37 | if (lastSlash)
38 | {
39 | *(lastSlash + 1) = L'\0';
40 | }
41 | }
42 |
43 | inline HRESULT ReadDataFromFile(LPCWSTR filename, byte** data, UINT* size)
44 | {
45 | using namespace Microsoft::WRL;
46 |
47 | CREATEFILE2_EXTENDED_PARAMETERS extendedParams = {};
48 | extendedParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
49 | extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
50 | extendedParams.dwFileFlags = FILE_FLAG_SEQUENTIAL_SCAN;
51 | extendedParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
52 | extendedParams.lpSecurityAttributes = nullptr;
53 | extendedParams.hTemplateFile = nullptr;
54 |
55 | Wrappers::FileHandle file(CreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, &extendedParams));
56 | if (file.Get() == INVALID_HANDLE_VALUE)
57 | {
58 | throw std::exception();
59 | }
60 |
61 | FILE_STANDARD_INFO fileInfo = {};
62 | if (!GetFileInformationByHandleEx(file.Get(), FileStandardInfo, &fileInfo, sizeof(fileInfo)))
63 | {
64 | throw std::exception();
65 | }
66 |
67 | if (fileInfo.EndOfFile.HighPart != 0)
68 | {
69 | throw std::exception();
70 | }
71 |
72 | *data = reinterpret_cast(malloc(fileInfo.EndOfFile.LowPart));
73 | *size = fileInfo.EndOfFile.LowPart;
74 |
75 | if (!ReadFile(file.Get(), *data, fileInfo.EndOfFile.LowPart, nullptr, nullptr))
76 | {
77 | throw std::exception();
78 | }
79 |
80 | return S_OK;
81 | }
82 |
83 | // Assign a name to the object to aid with debugging.
84 | #if defined(_DEBUG)
85 | inline void SetName(ID3D12Object* pObject, LPCWSTR name)
86 | {
87 | pObject->SetName(name);
88 | }
89 | inline void SetNameIndexed(ID3D12Object* pObject, LPCWSTR name, UINT index)
90 | {
91 | WCHAR fullName[50];
92 | if (swprintf_s(fullName, L"%s[%u]", name, index) > 0)
93 | {
94 | pObject->SetName(fullName);
95 | }
96 | }
97 | #else
98 | inline void SetName(ID3D12Object*, LPCWSTR)
99 | {
100 | }
101 | inline void SetNameIndexed(ID3D12Object*, LPCWSTR, UINT)
102 | {
103 | }
104 | #endif
105 |
106 | // Naming helper for ComPtr.
107 | // Assigns the name of the variable as the name of the object.
108 | // The indexed variant will include the index in the name of the object.
109 | #define NAME_D3D12_OBJECT(x) SetName(x.Get(), L#x)
110 | #define NAME_D3D12_OBJECT_INDEXED(x, n) SetNameIndexed(x[n].Get(), L#x, n)
111 |
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/Win32Application.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 | #include "Win32Application.h"
14 |
15 | HWND Win32Application::m_hwnd = nullptr;
16 |
17 | int Win32Application::Run(DXSample* pSample, HINSTANCE hInstance, int nCmdShow)
18 | {
19 | // Parse the command line parameters
20 | int argc = 0;
21 | LPWSTR* argv = nullptr;
22 |
23 | // Initialize the window class.
24 | WNDCLASSEX windowClass = { 0 };
25 | windowClass.cbSize = sizeof(WNDCLASSEX);
26 | windowClass.style = CS_HREDRAW | CS_VREDRAW;
27 | windowClass.lpfnWndProc = WindowProc;
28 | windowClass.hInstance = hInstance;
29 | windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
30 | windowClass.lpszClassName = L"DXSampleClass";
31 | RegisterClassEx(&windowClass);
32 |
33 | RECT windowRect = { 0, 0, static_cast(pSample->GetWidth()), static_cast(pSample->GetHeight()) };
34 | AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);
35 |
36 | // Create the window and store a handle to it.
37 | m_hwnd = CreateWindow(
38 | windowClass.lpszClassName,
39 | pSample->GetTitle(),
40 | WS_OVERLAPPEDWINDOW,
41 | CW_USEDEFAULT,
42 | CW_USEDEFAULT,
43 | windowRect.right - windowRect.left,
44 | windowRect.bottom - windowRect.top,
45 | nullptr, // We have no parent window.
46 | nullptr, // We aren't using menus.
47 | hInstance,
48 | pSample);
49 |
50 | // Initialize the sample. OnInit is defined in each child-implementation of DXSample.
51 | pSample->OnInit();
52 |
53 | ShowWindow(m_hwnd, nCmdShow);
54 |
55 | // Main sample loop.
56 | MSG msg = {};
57 | while (msg.message != WM_QUIT)
58 | {
59 | // Process any messages in the queue.
60 | if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
61 | {
62 | TranslateMessage(&msg);
63 | DispatchMessage(&msg);
64 | }
65 | }
66 |
67 | pSample->OnDestroy();
68 |
69 | // Return this part of the WM_QUIT message to Windows.
70 | return static_cast(msg.wParam);
71 | }
72 |
73 | // Main message handler for the sample.
74 | LRESULT CALLBACK Win32Application::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
75 | {
76 | DXSample* pSample = reinterpret_cast(GetWindowLongPtr(hWnd, GWLP_USERDATA));
77 |
78 | switch (message)
79 | {
80 | case WM_CREATE:
81 | {
82 | // Save the DXSample* passed in to CreateWindow.
83 | LPCREATESTRUCT pCreateStruct = reinterpret_cast(lParam);
84 | SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast(pCreateStruct->lpCreateParams));
85 | }
86 | return 0;
87 |
88 | case WM_KEYDOWN:
89 | if (pSample)
90 | {
91 | pSample->OnKeyDown(static_cast(wParam));
92 | }
93 | return 0;
94 |
95 | case WM_KEYUP:
96 | if (pSample)
97 | {
98 | pSample->OnKeyUp(static_cast(wParam));
99 | }
100 | return 0;
101 |
102 | case WM_PAINT:
103 | if (pSample)
104 | {
105 | pSample->OnUpdate();
106 | pSample->OnRender();
107 | }
108 | return 0;
109 |
110 | case WM_DESTROY:
111 | PostQuitMessage(0);
112 | return 0;
113 | }
114 |
115 | // Handle any messages the switch statement didn't.
116 | return DefWindowProc(hWnd, message, wParam, lParam);
117 | }
118 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/Win32Application.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 | #include "Win32Application.h"
14 |
15 | HWND Win32Application::m_hwnd = nullptr;
16 |
17 | int Win32Application::Run(DXSample* pSample, HINSTANCE hInstance, int nCmdShow)
18 | {
19 | // Parse the command line parameters
20 | int argc = 0;
21 | LPWSTR* argv = nullptr;
22 |
23 | // Initialize the window class.
24 | WNDCLASSEX windowClass = { 0 };
25 | windowClass.cbSize = sizeof(WNDCLASSEX);
26 | windowClass.style = CS_HREDRAW | CS_VREDRAW;
27 | windowClass.lpfnWndProc = WindowProc;
28 | windowClass.hInstance = hInstance;
29 | windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
30 | windowClass.lpszClassName = L"DXSampleClass";
31 | RegisterClassEx(&windowClass);
32 |
33 | RECT windowRect = { 0, 0, static_cast(pSample->GetWidth()), static_cast(pSample->GetHeight()) };
34 | AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);
35 |
36 | // Create the window and store a handle to it.
37 | m_hwnd = CreateWindow(
38 | windowClass.lpszClassName,
39 | pSample->GetTitle(),
40 | WS_OVERLAPPEDWINDOW,
41 | CW_USEDEFAULT,
42 | CW_USEDEFAULT,
43 | windowRect.right - windowRect.left,
44 | windowRect.bottom - windowRect.top,
45 | nullptr, // We have no parent window.
46 | nullptr, // We aren't using menus.
47 | hInstance,
48 | pSample);
49 |
50 | // Initialize the sample. OnInit is defined in each child-implementation of DXSample.
51 | pSample->OnInit();
52 |
53 | ShowWindow(m_hwnd, nCmdShow);
54 |
55 | // Main sample loop.
56 | MSG msg = {};
57 | while (msg.message != WM_QUIT)
58 | {
59 | // Process any messages in the queue.
60 | if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
61 | {
62 | TranslateMessage(&msg);
63 | DispatchMessage(&msg);
64 | }
65 | }
66 |
67 | pSample->OnDestroy();
68 |
69 | // Return this part of the WM_QUIT message to Windows.
70 | return static_cast(msg.wParam);
71 | }
72 |
73 | // Main message handler for the sample.
74 | LRESULT CALLBACK Win32Application::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
75 | {
76 | DXSample* pSample = reinterpret_cast(GetWindowLongPtr(hWnd, GWLP_USERDATA));
77 |
78 | switch (message)
79 | {
80 | case WM_CREATE:
81 | {
82 | // Save the DXSample* passed in to CreateWindow.
83 | LPCREATESTRUCT pCreateStruct = reinterpret_cast(lParam);
84 | SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast(pCreateStruct->lpCreateParams));
85 | }
86 | return 0;
87 |
88 | case WM_KEYDOWN:
89 | if (pSample)
90 | {
91 | pSample->OnKeyDown(static_cast(wParam));
92 | }
93 | return 0;
94 |
95 | case WM_KEYUP:
96 | if (pSample)
97 | {
98 | pSample->OnKeyUp(static_cast(wParam));
99 | }
100 | return 0;
101 |
102 | case WM_PAINT:
103 | if (pSample)
104 | {
105 | pSample->OnUpdate();
106 | pSample->OnRender();
107 | }
108 | return 0;
109 |
110 | case WM_DESTROY:
111 | PostQuitMessage(0);
112 | return 0;
113 | }
114 |
115 | // Handle any messages the switch statement didn't.
116 | return DefWindowProc(hWnd, message, wParam, lParam);
117 | }
118 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/Win32Application.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 | #include "Win32Application.h"
14 |
15 | HWND Win32Application::m_hwnd = nullptr;
16 |
17 | int Win32Application::Run(DXSample* pSample, HINSTANCE hInstance, int nCmdShow)
18 | {
19 | // Parse the command line parameters
20 | int argc = 0;
21 | LPWSTR* argv = nullptr;
22 |
23 | // Initialize the window class.
24 | WNDCLASSEX windowClass = { 0 };
25 | windowClass.cbSize = sizeof(WNDCLASSEX);
26 | windowClass.style = CS_HREDRAW | CS_VREDRAW;
27 | windowClass.lpfnWndProc = WindowProc;
28 | windowClass.hInstance = hInstance;
29 | windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
30 | windowClass.lpszClassName = L"DXSampleClass";
31 | RegisterClassEx(&windowClass);
32 |
33 | RECT windowRect = { 0, 0, static_cast(pSample->GetWidth()), static_cast(pSample->GetHeight()) };
34 | AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);
35 |
36 | // Create the window and store a handle to it.
37 | m_hwnd = CreateWindow(
38 | windowClass.lpszClassName,
39 | pSample->GetTitle(),
40 | WS_OVERLAPPEDWINDOW,
41 | CW_USEDEFAULT,
42 | CW_USEDEFAULT,
43 | windowRect.right - windowRect.left,
44 | windowRect.bottom - windowRect.top,
45 | nullptr, // We have no parent window.
46 | nullptr, // We aren't using menus.
47 | hInstance,
48 | pSample);
49 |
50 | // Initialize the sample. OnInit is defined in each child-implementation of DXSample.
51 | pSample->OnInit();
52 |
53 | ShowWindow(m_hwnd, nCmdShow);
54 |
55 | // Main sample loop.
56 | MSG msg = {};
57 | while (msg.message != WM_QUIT)
58 | {
59 | // Process any messages in the queue.
60 | if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
61 | {
62 | TranslateMessage(&msg);
63 | DispatchMessage(&msg);
64 | }
65 | }
66 |
67 | pSample->OnDestroy();
68 |
69 | // Return this part of the WM_QUIT message to Windows.
70 | return static_cast(msg.wParam);
71 | }
72 |
73 | // Main message handler for the sample.
74 | LRESULT CALLBACK Win32Application::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
75 | {
76 | DXSample* pSample = reinterpret_cast(GetWindowLongPtr(hWnd, GWLP_USERDATA));
77 |
78 | switch (message)
79 | {
80 | case WM_CREATE:
81 | {
82 | // Save the DXSample* passed in to CreateWindow.
83 | LPCREATESTRUCT pCreateStruct = reinterpret_cast(lParam);
84 | SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast(pCreateStruct->lpCreateParams));
85 | }
86 | return 0;
87 |
88 | case WM_KEYDOWN:
89 | if (pSample)
90 | {
91 | pSample->OnKeyDown(static_cast(wParam));
92 | }
93 | return 0;
94 |
95 | case WM_KEYUP:
96 | if (pSample)
97 | {
98 | pSample->OnKeyUp(static_cast(wParam));
99 | }
100 | return 0;
101 |
102 | case WM_PAINT:
103 | if (pSample)
104 | {
105 | pSample->OnUpdate();
106 | pSample->OnRender();
107 | }
108 | return 0;
109 |
110 | case WM_DESTROY:
111 | PostQuitMessage(0);
112 | return 0;
113 | }
114 |
115 | // Handle any messages the switch statement didn't.
116 | return DefWindowProc(hWnd, message, wParam, lParam);
117 | }
118 |
--------------------------------------------------------------------------------
/hellovrs_simple/Vulkan/vkHelloTriangle.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 16.0
23 | Win32Proj
24 | {be279fc3-dc18-4d1d-a565-9fa975c687ba}
25 | vkHelloTriangle
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v142
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v142
39 | true
40 | Unicode
41 |
42 |
43 | Application
44 | true
45 | v142
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v142
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 | true
75 |
76 |
77 | false
78 |
79 |
80 | true
81 |
82 |
83 | false
84 |
85 |
86 |
87 | Level3
88 | true
89 | WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)
90 | true
91 | Use
92 | pch.h
93 | D:\VulkanSDK\1.2.198.1\Include;D:\VulkanSDK\1.2.198.1\Third-Party\Include\;%(AdditionalIncludeDirectories)
94 | 26812
95 |
96 |
97 | Windows
98 | true
99 | D:\VulkanSDK\1.2.198.1\Lib32;%(AdditionalLibraryDirectories)
100 | vulkan-1.lib;%(AdditionalDependencies)
101 |
102 |
103 | $(ProjectDir)CompileShaders.bat
104 |
105 |
106 |
107 |
108 | Level3
109 | true
110 | true
111 | true
112 | WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)
113 | true
114 | Use
115 | pch.h
116 | D:\VulkanSDK\1.2.198.1\Include;D:\VulkanSDK\1.2.198.1\Third-Party\Include\;%(AdditionalIncludeDirectories)
117 | 26812
118 |
119 |
120 | Windows
121 | true
122 | true
123 | true
124 | D:\VulkanSDK\1.2.198.1\Lib32;%(AdditionalLibraryDirectories)
125 | vulkan-1.lib;%(AdditionalDependencies)
126 |
127 |
128 | $(ProjectDir)CompileShaders.bat
129 |
130 |
131 |
132 |
133 | Level3
134 | true
135 | _DEBUG;_WINDOWS;%(PreprocessorDefinitions)
136 | true
137 | Use
138 | pch.h
139 | D:\VulkanSDK\1.2.198.1\Include;D:\VulkanSDK\1.2.198.1\Third-Party\Include\;%(AdditionalIncludeDirectories)
140 | 26812
141 |
142 |
143 | Windows
144 | true
145 | D:\VulkanSDK\1.2.198.1\Lib;%(AdditionalLibraryDirectories)
146 | vulkan-1.lib;%(AdditionalDependencies)
147 |
148 |
149 | $(ProjectDir)CompileShaders.bat
150 |
151 |
152 |
153 |
154 | Level3
155 | true
156 | true
157 | true
158 | NDEBUG;_WINDOWS;%(PreprocessorDefinitions)
159 | true
160 | Use
161 | pch.h
162 | D:\VulkanSDK\1.2.198.1\Include;D:\VulkanSDK\1.2.198.1\Third-Party\Include\;%(AdditionalIncludeDirectories)
163 | 26812
164 |
165 |
166 | Windows
167 | true
168 | true
169 | true
170 | D:\VulkanSDK\1.2.198.1\Lib;%(AdditionalLibraryDirectories)
171 | vulkan-1.lib;%(AdditionalDependencies)
172 |
173 |
174 | $(ProjectDir)CompileShaders.bat
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 | Create
187 | Create
188 | Create
189 | Create
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/D3D12HelloTriangle.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | ARM64
7 |
8 |
9 | Debug
10 | x64
11 |
12 |
13 | Release
14 | ARM64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}
23 | Win32Proj
24 | D3D12HelloTriangle
25 | D3D12HelloTriangle
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v142
33 | Unicode
34 |
35 |
36 | Application
37 | true
38 | v142
39 | Unicode
40 |
41 |
42 | Application
43 | false
44 | v142
45 | true
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v142
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 | true
73 | bin\$(Platform)\$(Configuration)\
74 | obj\$(Platform)\$(Configuration)\
75 |
76 |
77 | true
78 | bin\$(Platform)\$(Configuration)\
79 | obj\$(Platform)\$(Configuration)\
80 |
81 |
82 | false
83 | bin\$(Platform)\$(Configuration)\
84 | obj\$(Platform)\$(Configuration)\
85 |
86 |
87 | false
88 | bin\$(Platform)\$(Configuration)\
89 | obj\$(Platform)\$(Configuration)\
90 |
91 |
92 |
93 | Use
94 | Level3
95 | Disabled
96 | WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)
97 | true
98 | %(AdditionalIncludeDirectories)
99 | false
100 |
101 |
102 | Windows
103 | true
104 | d3d12.lib;dxgi.lib;d3dcompiler.lib;%(AdditionalDependencies)
105 | d3d12.dll
106 |
107 |
108 | true
109 |
110 |
111 | copy %(Identity) "$(OutDir)" > NUL
112 | $(OutDir)\%(Identity)
113 | true
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 | Use
122 | Level3
123 | Disabled
124 | WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)
125 | true
126 | %(AdditionalIncludeDirectories)
127 | false
128 |
129 |
130 | Windows
131 | true
132 | d3d12.lib;dxgi.lib;d3dcompiler.lib;%(AdditionalDependencies)
133 | d3d12.dll
134 |
135 |
136 | true
137 |
138 |
139 | copy %(Identity) "$(OutDir)" > NUL
140 | $(OutDir)\%(Identity)
141 | true
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 | Level3
151 | Use
152 | MaxSpeed
153 | true
154 | true
155 | WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)
156 | true
157 | %(AdditionalIncludeDirectories)
158 | false
159 |
160 |
161 | Windows
162 | true
163 | true
164 | true
165 | d3d12.lib;dxgi.lib;d3dcompiler.lib;%(AdditionalDependencies)
166 | d3d12.dll
167 |
168 |
169 | true
170 |
171 |
172 | copy %(Identity) "$(OutDir)" > NUL
173 | $(OutDir)\%(Identity)
174 | true
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 | Level3
183 | Use
184 | MaxSpeed
185 | true
186 | true
187 | WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)
188 | true
189 | %(AdditionalIncludeDirectories)
190 | false
191 |
192 |
193 | Windows
194 | true
195 | true
196 | true
197 | d3d12.lib;dxgi.lib;d3dcompiler.lib;%(AdditionalDependencies)
198 | d3d12.dll
199 |
200 |
201 | true
202 |
203 |
204 | copy %(Identity) "$(OutDir)" > NUL
205 | $(OutDir)\%(Identity)
206 | true
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 | Create
228 | Create
229 | Create
230 | Create
231 |
232 |
233 |
234 |
235 | Document
236 | true
237 |
238 |
239 |
240 |
241 |
242 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/D3D12HelloTriangle.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | ARM64
7 |
8 |
9 | Debug
10 | x64
11 |
12 |
13 | Release
14 | ARM64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | {5018F6A3-6533-4744-B1FD-727D199FD2E9}
23 | Win32Proj
24 | D3D12HelloTriangle
25 | D3D12HelloTriangle
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v142
33 | Unicode
34 |
35 |
36 | Application
37 | true
38 | v142
39 | Unicode
40 |
41 |
42 | Application
43 | false
44 | v142
45 | true
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v142
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 | true
73 | bin\$(Platform)\$(Configuration)\
74 | obj\$(Platform)\$(Configuration)\
75 |
76 |
77 | true
78 | bin\$(Platform)\$(Configuration)\
79 | obj\$(Platform)\$(Configuration)\
80 |
81 |
82 | false
83 | bin\$(Platform)\$(Configuration)\
84 | obj\$(Platform)\$(Configuration)\
85 |
86 |
87 | false
88 | bin\$(Platform)\$(Configuration)\
89 | obj\$(Platform)\$(Configuration)\
90 |
91 |
92 |
93 | Use
94 | Level3
95 | Disabled
96 | WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)
97 | true
98 | %(AdditionalIncludeDirectories)
99 | false
100 |
101 |
102 | Windows
103 | true
104 | d3d12.lib;dxgi.lib;d3dcompiler.lib;%(AdditionalDependencies)
105 | d3d12.dll
106 |
107 |
108 | true
109 |
110 |
111 | copy %(Identity) "$(OutDir)" > NUL
112 | $(OutDir)\%(Identity)
113 | true
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 | Use
122 | Level3
123 | Disabled
124 | WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)
125 | true
126 | %(AdditionalIncludeDirectories)
127 | false
128 |
129 |
130 | Windows
131 | true
132 | d3d12.lib;dxgi.lib;d3dcompiler.lib;%(AdditionalDependencies)
133 | d3d12.dll
134 |
135 |
136 | true
137 |
138 |
139 | copy %(Identity) "$(OutDir)" > NUL
140 | $(OutDir)\%(Identity)
141 | true
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 | Level3
151 | Use
152 | MaxSpeed
153 | true
154 | true
155 | WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)
156 | true
157 | %(AdditionalIncludeDirectories)
158 | false
159 |
160 |
161 | Windows
162 | true
163 | true
164 | true
165 | d3d12.lib;dxgi.lib;d3dcompiler.lib;%(AdditionalDependencies)
166 | d3d12.dll
167 |
168 |
169 | true
170 |
171 |
172 | copy %(Identity) "$(OutDir)" > NUL
173 | $(OutDir)\%(Identity)
174 | true
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 | Level3
183 | Use
184 | MaxSpeed
185 | true
186 | true
187 | WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)
188 | true
189 | %(AdditionalIncludeDirectories)
190 | false
191 |
192 |
193 | Windows
194 | true
195 | true
196 | true
197 | d3d12.lib;dxgi.lib;d3dcompiler.lib;%(AdditionalDependencies)
198 | d3d12.dll
199 |
200 |
201 | true
202 |
203 |
204 | copy %(Identity) "$(OutDir)" > NUL
205 | $(OutDir)\%(Identity)
206 | true
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 | Create
228 | Create
229 | Create
230 | Create
231 |
232 |
233 |
234 |
235 | Document
236 | true
237 |
238 |
239 |
240 |
241 |
242 |
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/D3D12HelloTriangle.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 | PSMain
43 | 6.4
44 | PSMain
45 | 6.4
46 | PSMain
47 | 6.4
48 | PSMain
49 | 6.4
50 | Pixel
51 | Pixel
52 | Pixel
53 | Pixel
54 | -Qembed_debug
55 | -Qembed_debug
56 | -Qembed_debug
57 | -Qembed_debug
58 | PSMain.h
59 | PSMain.h
60 | PSMain.h
61 | PSMain.h
62 |
63 |
64 | Vertex
65 | Vertex
66 | Vertex
67 | Vertex
68 | VSMain
69 | 6.4
70 | VSMain
71 | 6.4
72 | VSMain
73 | 6.4
74 | VSMain
75 | 6.4
76 | -Qembed_debug
77 | -Qembed_debug
78 | -Qembed_debug
79 | -Qembed_debug
80 | VSMain.h
81 | VSMain.h
82 | VSMain.h
83 | VSMain.h
84 |
85 |
86 |
87 | 16.0
88 | Win32Proj
89 | {ba127827-56c8-492a-8437-532bfb24b290}
90 | D3D12HelloWorld
91 | 10.0
92 |
93 |
94 |
95 | Application
96 | true
97 | v142
98 | Unicode
99 |
100 |
101 | Application
102 | false
103 | v142
104 | true
105 | Unicode
106 |
107 |
108 | Application
109 | true
110 | v142
111 | Unicode
112 |
113 |
114 | Application
115 | false
116 | v142
117 | true
118 | Unicode
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 | true
140 |
141 |
142 | false
143 |
144 |
145 | true
146 |
147 |
148 | false
149 |
150 |
151 |
152 | Level3
153 | true
154 | WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)
155 | true
156 |
157 |
158 | Windows
159 | true
160 | d3d12.lib;dxgi.lib;d3dcompiler.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
161 |
162 |
163 |
164 |
165 | Level3
166 | true
167 | true
168 | true
169 | WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)
170 | true
171 |
172 |
173 | Windows
174 | true
175 | true
176 | true
177 | d3d12.lib;dxgi.lib;d3dcompiler.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
178 |
179 |
180 |
181 |
182 | Level3
183 | true
184 | _DEBUG;_WINDOWS;%(PreprocessorDefinitions)
185 | true
186 |
187 |
188 | Windows
189 | true
190 | d3d12.lib;dxgi.lib;d3dcompiler.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
191 |
192 |
193 |
194 |
195 | Level3
196 | true
197 | true
198 | true
199 | NDEBUG;_WINDOWS;%(PreprocessorDefinitions)
200 | true
201 |
202 |
203 | Windows
204 | true
205 | true
206 | true
207 | d3d12.lib;dxgi.lib;d3dcompiler.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
208 |
209 |
210 |
211 |
212 |
213 |
--------------------------------------------------------------------------------
/hellovrs_simple/D3D12/HelloTriangle/D3D12HelloTriangle.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 | #include "D3D12HelloTriangle.h"
14 |
15 | D3D12HelloTriangle::D3D12HelloTriangle(UINT width, UINT height, std::wstring name) :
16 | DXSample(width, height, name),
17 | m_frameIndex(0),
18 | m_viewport(0.0f, 0.0f, static_cast(width), static_cast(height)),
19 | m_scissorRect(0, 0, static_cast(width), static_cast(height)),
20 | m_rtvDescriptorSize(0),
21 | m_shadingRateIndex(0)
22 | {
23 | }
24 |
25 | void D3D12HelloTriangle::OnInit()
26 | {
27 | LoadPipeline();
28 | LoadAssets();
29 |
30 | UpdateTitleText();
31 | }
32 |
33 | // Load the rendering pipeline dependencies.
34 | void D3D12HelloTriangle::LoadPipeline()
35 | {
36 | UINT dxgiFactoryFlags = 0;
37 |
38 | #if defined(_DEBUG)
39 | // Enable the debug layer (requires the Graphics Tools "optional feature").
40 | // NOTE: Enabling the debug layer after device creation will invalidate the active device.
41 | {
42 | ComPtr debugController;
43 | if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController))))
44 | {
45 | debugController->EnableDebugLayer();
46 |
47 | // Enable additional debug layers.
48 | dxgiFactoryFlags |= DXGI_CREATE_FACTORY_DEBUG;
49 | }
50 | }
51 | #endif
52 |
53 | ComPtr factory;
54 | ThrowIfFailed(CreateDXGIFactory2(dxgiFactoryFlags, IID_PPV_ARGS(&factory)));
55 |
56 | if (m_useWarpDevice)
57 | {
58 | ComPtr warpAdapter;
59 | ThrowIfFailed(factory->EnumWarpAdapter(IID_PPV_ARGS(&warpAdapter)));
60 |
61 | ThrowIfFailed(D3D12CreateDevice(
62 | warpAdapter.Get(),
63 | D3D_FEATURE_LEVEL_11_0,
64 | IID_PPV_ARGS(&m_device)
65 | ));
66 | }
67 | else
68 | {
69 | ComPtr hardwareAdapter;
70 | GetHardwareAdapter(factory.Get(), &hardwareAdapter);
71 |
72 | ThrowIfFailed(D3D12CreateDevice(
73 | hardwareAdapter.Get(),
74 | D3D_FEATURE_LEVEL_11_0,
75 | IID_PPV_ARGS(&m_device)
76 | ));
77 | }
78 |
79 | const D3D12_FEATURE D3D12_FEATURE_D3D12_OPTIONS6 = (D3D12_FEATURE)30;
80 | D3D12_FEATURE_DATA_D3D12_OPTIONS6 options6{};
81 | m_platformSupportsVRS = false;
82 | m_additionalRatesSupported = false;
83 | if (SUCCEEDED(m_device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS6, &options6, sizeof(options6))))
84 | {
85 | m_additionalRatesSupported = !!options6.AdditionalShadingRatesSupported;
86 | m_platformSupportsVRS = options6.VariableShadingRateTier != D3D12_VARIABLE_SHADING_RATE_TIER_NOT_SUPPORTED;
87 | }
88 |
89 | // Describe and create the command queue.
90 | D3D12_COMMAND_QUEUE_DESC queueDesc = {};
91 | queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
92 | queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
93 |
94 | ThrowIfFailed(m_device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&m_commandQueue)));
95 |
96 | // Describe and create the swap chain.
97 | DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
98 | swapChainDesc.BufferCount = FrameCount;
99 | swapChainDesc.Width = m_width;
100 | swapChainDesc.Height = m_height;
101 | swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
102 | swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
103 | swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
104 | swapChainDesc.SampleDesc.Count = 1;
105 |
106 | ComPtr swapChain;
107 | ThrowIfFailed(factory->CreateSwapChainForHwnd(
108 | m_commandQueue.Get(), // Swap chain needs the queue so that it can force a flush on it.
109 | Win32Application::GetHwnd(),
110 | &swapChainDesc,
111 | nullptr,
112 | nullptr,
113 | &swapChain
114 | ));
115 |
116 | // This sample does not support fullscreen transitions.
117 | ThrowIfFailed(factory->MakeWindowAssociation(Win32Application::GetHwnd(), DXGI_MWA_NO_ALT_ENTER));
118 |
119 | ThrowIfFailed(swapChain.As(&m_swapChain));
120 | m_frameIndex = m_swapChain->GetCurrentBackBufferIndex();
121 |
122 | // Create descriptor heaps.
123 | {
124 | // Describe and create a render target view (RTV) descriptor heap.
125 | D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
126 | rtvHeapDesc.NumDescriptors = FrameCount;
127 | rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
128 | rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
129 | ThrowIfFailed(m_device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&m_rtvHeap)));
130 |
131 | m_rtvDescriptorSize = m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
132 | }
133 |
134 | // Create frame resources.
135 | {
136 | CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart());
137 |
138 | // Create a RTV for each frame.
139 | for (UINT n = 0; n < FrameCount; n++)
140 | {
141 | ThrowIfFailed(m_swapChain->GetBuffer(n, IID_PPV_ARGS(&m_renderTargets[n])));
142 | m_device->CreateRenderTargetView(m_renderTargets[n].Get(), nullptr, rtvHandle);
143 | rtvHandle.Offset(1, m_rtvDescriptorSize);
144 | }
145 | }
146 |
147 | ThrowIfFailed(m_device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&m_commandAllocator)));
148 | }
149 |
150 | void
151 | CompileAndCheckErrors(_In_ LPCWSTR pFileName,
152 | _In_reads_opt_(_Inexpressible_(pDefines->Name != NULL)) CONST D3D_SHADER_MACRO* pDefines,
153 | _In_opt_ ID3DInclude* pInclude,
154 | _In_ LPCSTR pEntrypoint,
155 | _In_ LPCSTR pTarget,
156 | _In_ UINT Flags1,
157 | _In_ UINT Flags2,
158 | _Out_ ID3DBlob** ppCode)
159 | {
160 | ComPtr errorMsgs;
161 | HRESULT hr = D3DCompileFromFile(pFileName, pDefines, pInclude, pEntrypoint, pTarget, Flags1, Flags2, ppCode, &errorMsgs);
162 | if (FAILED(hr))
163 | {
164 | char const* errorMsg = reinterpret_cast(errorMsgs->GetBufferPointer());
165 | MessageBoxA(nullptr, errorMsg, "Failed to compile shader", MB_OK);
166 | ThrowIfFailed(hr);
167 | }
168 | }
169 |
170 | // Load the sample assets.
171 | void D3D12HelloTriangle::LoadAssets()
172 | {
173 | // Create an empty root signature.
174 | {
175 | CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc;
176 | rootSignatureDesc.Init(0, nullptr, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
177 |
178 | ComPtr signature;
179 | ComPtr error;
180 | ThrowIfFailed(D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error));
181 | ThrowIfFailed(m_device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&m_rootSignature)));
182 | }
183 |
184 | // Create the pipeline state, which includes compiling and loading shaders.
185 | {
186 | ComPtr vertexShader;
187 | ComPtr pixelShader;
188 |
189 | #if defined(_DEBUG)
190 | // Enable better shader debugging with the graphics debugging tools.
191 | UINT compileFlags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
192 | #else
193 | UINT compileFlags = 0;
194 | #endif
195 |
196 | CompileAndCheckErrors(GetAssetFullPath(L"shaders.hlsl").c_str(), nullptr, nullptr, "VSMain", "vs_5_0", compileFlags, 0, &vertexShader);
197 | CompileAndCheckErrors(GetAssetFullPath(L"shaders.hlsl").c_str(), nullptr, nullptr, "PSMain", "ps_5_0", compileFlags, 0, &pixelShader);
198 |
199 | // Define the vertex input layout.
200 | D3D12_INPUT_ELEMENT_DESC inputElementDescs[] =
201 | {
202 | { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
203 | { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }
204 | };
205 |
206 | // Describe and create the graphics pipeline state object (PSO).
207 | D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
208 | psoDesc.InputLayout = { inputElementDescs, _countof(inputElementDescs) };
209 | psoDesc.pRootSignature = m_rootSignature.Get();
210 | psoDesc.VS = CD3DX12_SHADER_BYTECODE(vertexShader.Get());
211 | psoDesc.PS = CD3DX12_SHADER_BYTECODE(pixelShader.Get());
212 | psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
213 | psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
214 | psoDesc.DepthStencilState.DepthEnable = FALSE;
215 | psoDesc.DepthStencilState.StencilEnable = FALSE;
216 | psoDesc.SampleMask = UINT_MAX;
217 | psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
218 | psoDesc.NumRenderTargets = 1;
219 | psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
220 | psoDesc.SampleDesc.Count = 1;
221 | ThrowIfFailed(m_device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&m_pipelineState)));
222 | }
223 |
224 | // Create the command list.
225 | ThrowIfFailed(m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_commandAllocator.Get(), m_pipelineState.Get(), IID_PPV_ARGS(&m_commandList)));
226 |
227 | m_commandList.As(&m_commandList5);
228 |
229 | // Command lists are created in the recording state, but there is nothing
230 | // to record yet. The main loop expects it to be closed, so close it now.
231 | ThrowIfFailed(m_commandList->Close());
232 |
233 | // Create the vertex buffer.
234 | {
235 | // Define the geometry for a triangle.
236 | Vertex triangleVertices[] =
237 | {
238 | { {-1, 1, 0.0f }, { 0.0f, 0.0f, 0.0f, 1.0f } },
239 | { { 0.25f, 1, 0.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } },
240 | { { -1, -0.25f * m_aspectRatio, 0.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } }
241 | };
242 |
243 | const UINT vertexBufferSize = sizeof(triangleVertices);
244 |
245 | // Note: using upload heaps to transfer static data like vert buffers is not
246 | // recommended. Every time the GPU needs it, the upload heap will be marshalled
247 | // over. Please read up on Default Heap usage. An upload heap is used here for
248 | // code simplicity and because there are very few verts to actually transfer.
249 | ThrowIfFailed(m_device->CreateCommittedResource(
250 | &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
251 | D3D12_HEAP_FLAG_NONE,
252 | &CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize),
253 | D3D12_RESOURCE_STATE_GENERIC_READ,
254 | nullptr,
255 | IID_PPV_ARGS(&m_vertexBuffer)));
256 |
257 | // Copy the triangle data to the vertex buffer.
258 | UINT8* pVertexDataBegin;
259 | CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
260 | ThrowIfFailed(m_vertexBuffer->Map(0, &readRange, reinterpret_cast(&pVertexDataBegin)));
261 | memcpy(pVertexDataBegin, triangleVertices, sizeof(triangleVertices));
262 | m_vertexBuffer->Unmap(0, nullptr);
263 |
264 | // Initialize the vertex buffer view.
265 | m_vertexBufferView.BufferLocation = m_vertexBuffer->GetGPUVirtualAddress();
266 | m_vertexBufferView.StrideInBytes = sizeof(Vertex);
267 | m_vertexBufferView.SizeInBytes = vertexBufferSize;
268 | }
269 |
270 | // Create synchronization objects and wait until assets have been uploaded to the GPU.
271 | {
272 | ThrowIfFailed(m_device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_fence)));
273 | m_fenceValue = 1;
274 |
275 | // Create an event handle to use for frame synchronization.
276 | m_fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
277 | if (m_fenceEvent == nullptr)
278 | {
279 | ThrowIfFailed(HRESULT_FROM_WIN32(GetLastError()));
280 | }
281 |
282 | // Wait for the command list to execute; we are reusing the same command
283 | // list in our main loop but for now, we just want to wait for setup to
284 | // complete before continuing.
285 | WaitForPreviousFrame();
286 | }
287 | }
288 |
289 | // Update frame-based values.
290 | void D3D12HelloTriangle::OnUpdate()
291 | {
292 | }
293 |
294 | // Render the scene.
295 | void D3D12HelloTriangle::OnRender()
296 | {
297 | // Record all the commands we need to render the scene into the command list.
298 | PopulateCommandList();
299 |
300 | // Execute the command list.
301 | ID3D12CommandList* ppCommandLists[] = { m_commandList.Get() };
302 | m_commandQueue->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists);
303 |
304 | // Present the frame.
305 | ThrowIfFailed(m_swapChain->Present(1, 0));
306 |
307 | WaitForPreviousFrame();
308 | }
309 |
310 | void D3D12HelloTriangle::OnDestroy()
311 | {
312 | // Ensure that the GPU is no longer referencing resources that are about to be
313 | // cleaned up by the destructor.
314 | WaitForPreviousFrame();
315 |
316 | CloseHandle(m_fenceEvent);
317 | }
318 |
319 | void D3D12HelloTriangle::PopulateCommandList()
320 | {
321 | // Command list allocators can only be reset when the associated
322 | // command lists have finished execution on the GPU; apps should use
323 | // fences to determine GPU execution progress.
324 | ThrowIfFailed(m_commandAllocator->Reset());
325 |
326 | // However, when ExecuteCommandList() is called on a particular command
327 | // list, that command list can then be reset at any time and must be before
328 | // re-recording.
329 | ThrowIfFailed(m_commandList->Reset(m_commandAllocator.Get(), m_pipelineState.Get()));
330 |
331 | // Set necessary state.
332 | m_commandList->SetGraphicsRootSignature(m_rootSignature.Get());
333 | m_commandList->RSSetViewports(1, &m_viewport);
334 | m_commandList->RSSetScissorRects(1, &m_scissorRect);
335 |
336 | // Indicate that the back buffer will be used as a render target.
337 | m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));
338 |
339 | CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart(), m_frameIndex, m_rtvDescriptorSize);
340 | m_commandList->OMSetRenderTargets(1, &rtvHandle, FALSE, nullptr);
341 |
342 | if (m_commandList5)
343 | {
344 | static const D3D12_SHADING_RATE shadingRates[] = {
345 | D3D12_SHADING_RATE_1X1, D3D12_SHADING_RATE_1X2,D3D12_SHADING_RATE_2X1, D3D12_SHADING_RATE_2X2, D3D12_SHADING_RATE_2X4, D3D12_SHADING_RATE_4X2, D3D12_SHADING_RATE_4X4 };
346 |
347 | D3D12_SHADING_RATE shadingRate = shadingRates[m_shadingRateIndex];
348 |
349 | D3D12_SHADING_RATE_COMBINER combiners[2] = { D3D12_SHADING_RATE_COMBINER_PASSTHROUGH, D3D12_SHADING_RATE_COMBINER_PASSTHROUGH };
350 | m_commandList5->RSSetShadingRate(shadingRate, combiners);
351 | }
352 |
353 | // Record commands.
354 | const float clearColor[] = { 0.0f, 0.2f, 0.4f, 1.0f };
355 | m_commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
356 | m_commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
357 | m_commandList->IASetVertexBuffers(0, 1, &m_vertexBufferView);
358 | m_commandList->DrawInstanced(3, 1, 0, 0);
359 |
360 | // Indicate that the back buffer will now be used to present.
361 | m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT));
362 |
363 | ThrowIfFailed(m_commandList->Close());
364 | }
365 |
366 | void D3D12HelloTriangle::WaitForPreviousFrame()
367 | {
368 | // WAITING FOR THE FRAME TO COMPLETE BEFORE CONTINUING IS NOT BEST PRACTICE.
369 | // This is code implemented as such for simplicity. The D3D12HelloFrameBuffering
370 | // sample illustrates how to use fences for efficient resource usage and to
371 | // maximize GPU utilization.
372 |
373 | // Signal and increment the fence value.
374 | const UINT64 fence = m_fenceValue;
375 | ThrowIfFailed(m_commandQueue->Signal(m_fence.Get(), fence));
376 | m_fenceValue++;
377 |
378 | // Wait until the previous frame is finished.
379 | if (m_fence->GetCompletedValue() < fence)
380 | {
381 | ThrowIfFailed(m_fence->SetEventOnCompletion(fence, m_fenceEvent));
382 | WaitForSingleObject(m_fenceEvent, INFINITE);
383 | }
384 |
385 | m_frameIndex = m_swapChain->GetCurrentBackBufferIndex();
386 | }
387 |
388 | void D3D12HelloTriangle::OnKeyUp(UINT8 key)
389 | {
390 | if (!m_platformSupportsVRS)
391 | return;
392 |
393 | UINT allowedShadingRateCount = m_additionalRatesSupported ? 7 : 4;
394 |
395 | if (key == 37 && m_shadingRateIndex > 0) // left
396 | {
397 | m_shadingRateIndex--;
398 | }
399 | if (key == 39 && m_shadingRateIndex < allowedShadingRateCount - 1) // right
400 | {
401 | m_shadingRateIndex++;
402 | }
403 |
404 | UpdateTitleText();
405 | }
406 |
407 | void D3D12HelloTriangle::UpdateTitleText()
408 | {
409 | wchar_t const* titleText = L"";
410 |
411 | if (m_platformSupportsVRS)
412 | {
413 | switch (m_shadingRateIndex)
414 | {
415 | case 0: titleText = L"1x1"; break;
416 | case 1: titleText = L"1x2"; break;
417 | case 2: titleText = L"2x1"; break;
418 | case 3: titleText = L"2x2"; break;
419 | case 4: titleText = L"2x4"; break;
420 | case 5: titleText = L"4x2"; break;
421 | case 6: titleText = L"4x4"; break;
422 | }
423 | }
424 | else
425 | {
426 | titleText = L"{Platform does not support VRS}";
427 | }
428 |
429 |
430 | SetWindowText(Win32Application::GetHwnd(), titleText);
431 |
432 | }
--------------------------------------------------------------------------------
/hellovrs_rates/HelloTriangle/D3D12HelloTriangle.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 | #include "D3D12HelloTriangle.h"
14 |
15 | D3D12HelloTriangle::D3D12HelloTriangle(UINT width, UINT height, std::wstring name) :
16 | DXSample(width, height, name),
17 | m_frameIndex(0),
18 | m_viewport(0.0f, 0.0f, static_cast(width), static_cast(height)),
19 | m_scissorRect(0, 0, static_cast(width), static_cast(height)),
20 | m_rtvDescriptorSize(0)
21 | {
22 | }
23 |
24 | void D3D12HelloTriangle::OnInit()
25 | {
26 | LoadPipeline();
27 | LoadAssets();
28 | UpdateTitleText();
29 | }
30 |
31 | // Load the rendering pipeline dependencies.
32 | void D3D12HelloTriangle::LoadPipeline()
33 | {
34 | UINT dxgiFactoryFlags = 0;
35 |
36 | #if defined(_DEBUG)
37 | // Enable the debug layer (requires the Graphics Tools "optional feature").
38 | // NOTE: Enabling the debug layer after device creation will invalidate the active device.
39 | {
40 | ComPtr debugController;
41 | if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController))))
42 | {
43 | debugController->EnableDebugLayer();
44 |
45 | // Enable additional debug layers.
46 | dxgiFactoryFlags |= DXGI_CREATE_FACTORY_DEBUG;
47 | }
48 | }
49 | #endif
50 |
51 | ComPtr factory;
52 | ThrowIfFailed(CreateDXGIFactory2(dxgiFactoryFlags, IID_PPV_ARGS(&factory)));
53 |
54 | if (m_useWarpDevice)
55 | {
56 | ComPtr warpAdapter;
57 | ThrowIfFailed(factory->EnumWarpAdapter(IID_PPV_ARGS(&warpAdapter)));
58 |
59 | ThrowIfFailed(D3D12CreateDevice(
60 | warpAdapter.Get(),
61 | D3D_FEATURE_LEVEL_11_0,
62 | IID_PPV_ARGS(&m_device)
63 | ));
64 | }
65 | else
66 | {
67 | ComPtr hardwareAdapter;
68 | GetHardwareAdapter(factory.Get(), &hardwareAdapter);
69 |
70 | ThrowIfFailed(D3D12CreateDevice(
71 | hardwareAdapter.Get(),
72 | D3D_FEATURE_LEVEL_11_0,
73 | IID_PPV_ARGS(&m_device)
74 | ));
75 | }
76 |
77 | // Describe and create the command queue.
78 | D3D12_COMMAND_QUEUE_DESC queueDesc = {};
79 | queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
80 | queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
81 |
82 | ThrowIfFailed(m_device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&m_commandQueue)));
83 |
84 | // Describe and create the swap chain.
85 | DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
86 | swapChainDesc.BufferCount = FrameCount;
87 | swapChainDesc.Width = m_width;
88 | swapChainDesc.Height = m_height;
89 | swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
90 | swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
91 | swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
92 | swapChainDesc.SampleDesc.Count = 1;
93 |
94 | ComPtr swapChain;
95 | ThrowIfFailed(factory->CreateSwapChainForHwnd(
96 | m_commandQueue.Get(), // Swap chain needs the queue so that it can force a flush on it.
97 | Win32Application::GetHwnd(),
98 | &swapChainDesc,
99 | nullptr,
100 | nullptr,
101 | &swapChain
102 | ));
103 |
104 | // This sample does not support fullscreen transitions.
105 | ThrowIfFailed(factory->MakeWindowAssociation(Win32Application::GetHwnd(), DXGI_MWA_NO_ALT_ENTER));
106 |
107 | ThrowIfFailed(swapChain.As(&m_swapChain));
108 | m_frameIndex = m_swapChain->GetCurrentBackBufferIndex();
109 |
110 | // Create descriptor heaps.
111 | {
112 | // Describe and create a render target view (RTV) descriptor heap.
113 | D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
114 | rtvHeapDesc.NumDescriptors = FrameCount;
115 | rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
116 | rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
117 | ThrowIfFailed(m_device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&m_rtvHeap)));
118 |
119 | m_rtvDescriptorSize = m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
120 | }
121 |
122 | // Create frame resources.
123 | {
124 | CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart());
125 |
126 | // Create a RTV for each frame.
127 | for (UINT n = 0; n < FrameCount; n++)
128 | {
129 | ThrowIfFailed(m_swapChain->GetBuffer(n, IID_PPV_ARGS(&m_renderTargets[n])));
130 | m_device->CreateRenderTargetView(m_renderTargets[n].Get(), nullptr, rtvHandle);
131 | rtvHandle.Offset(1, m_rtvDescriptorSize);
132 | }
133 | }
134 |
135 | ThrowIfFailed(m_device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&m_commandAllocator)));
136 | }
137 |
138 | void
139 | CompileAndCheckErrors(_In_ LPCWSTR pFileName,
140 | _In_reads_opt_(_Inexpressible_(pDefines->Name != NULL)) CONST D3D_SHADER_MACRO* pDefines,
141 | _In_opt_ ID3DInclude* pInclude,
142 | _In_ LPCSTR pEntrypoint,
143 | _In_ LPCSTR pTarget,
144 | _In_ UINT Flags1,
145 | _In_ UINT Flags2,
146 | _Out_ ID3DBlob** ppCode)
147 | {
148 | ComPtr errorMsgs;
149 | HRESULT hr = D3DCompileFromFile(pFileName, pDefines, pInclude, pEntrypoint, pTarget, Flags1, Flags2, ppCode, &errorMsgs);
150 | if (FAILED(hr))
151 | {
152 | char const* errorMsg = reinterpret_cast(errorMsgs->GetBufferPointer());
153 | MessageBoxA(nullptr, errorMsg, "Failed to compile shader", MB_OK);
154 | ThrowIfFailed(hr);
155 | }
156 | }
157 |
158 | void D3D12HelloTriangle::AddQuad(float left, float bottom, float quadSize)
159 | {
160 | float right = left + quadSize;
161 | float top = bottom + quadSize;
162 |
163 | m_triangleVertices.push_back({ { left, top, 0.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } }); // Top left
164 | m_triangleVertices.push_back({ { right, top, 0.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } }); // Top right
165 | m_triangleVertices.push_back({ { left, bottom, 0.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } }); // Bottom left
166 |
167 | m_triangleVertices.push_back({ { left, bottom, 0.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } }); // Bottom left
168 | m_triangleVertices.push_back({ { right, top, 0.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } }); // Top right
169 | m_triangleVertices.push_back({ { right, bottom, 0.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } }); // Bottom right
170 | }
171 |
172 | // Load the sample assets.
173 | void D3D12HelloTriangle::LoadAssets()
174 | {
175 | // Create an empty root signature.
176 | {
177 | CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc;
178 | rootSignatureDesc.Init(0, nullptr, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
179 |
180 | ComPtr signature;
181 | ComPtr error;
182 | ThrowIfFailed(D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error));
183 | ThrowIfFailed(m_device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&m_rootSignature)));
184 | }
185 |
186 | // Create the pipeline state, which includes compiling and loading shaders.
187 | {
188 | ComPtr vertexShader;
189 | ComPtr pixelShader;
190 |
191 | #if defined(_DEBUG)
192 | // Enable better shader debugging with the graphics debugging tools.
193 | UINT compileFlags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
194 | #else
195 | UINT compileFlags = 0;
196 | #endif
197 |
198 | CompileAndCheckErrors(GetAssetFullPath(L"shaders.hlsl").c_str(), nullptr, nullptr, "VSMain", "vs_5_0", compileFlags, 0, &vertexShader);
199 | CompileAndCheckErrors(GetAssetFullPath(L"shaders.hlsl").c_str(), nullptr, nullptr, "PSMain", "ps_5_0", compileFlags, 0, &pixelShader);
200 |
201 | // Define the vertex input layout.
202 | D3D12_INPUT_ELEMENT_DESC inputElementDescs[] =
203 | {
204 | { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
205 | { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }
206 | };
207 |
208 | // Describe and create the graphics pipeline state object (PSO).
209 | D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
210 | psoDesc.InputLayout = { inputElementDescs, _countof(inputElementDescs) };
211 | psoDesc.pRootSignature = m_rootSignature.Get();
212 | psoDesc.VS = CD3DX12_SHADER_BYTECODE(vertexShader.Get());
213 | psoDesc.PS = CD3DX12_SHADER_BYTECODE(pixelShader.Get());
214 | psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
215 | psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
216 | psoDesc.DepthStencilState.DepthEnable = FALSE;
217 | psoDesc.DepthStencilState.StencilEnable = FALSE;
218 | psoDesc.SampleMask = UINT_MAX;
219 | psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
220 | psoDesc.NumRenderTargets = 1;
221 | psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
222 | psoDesc.SampleDesc.Count = 1;
223 | ThrowIfFailed(m_device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&m_pipelineState)));
224 | }
225 |
226 | // Create the command list.
227 | ThrowIfFailed(m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_commandAllocator.Get(), m_pipelineState.Get(), IID_PPV_ARGS(&m_commandList)));
228 |
229 | m_platformSupportsVRS = false;
230 | m_additionalShadingRatesSupported = false;
231 | D3D12_FEATURE_DATA_D3D12_OPTIONS6 options6;
232 | if (SUCCEEDED(m_device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS6, &options6, sizeof(options6))))
233 | {
234 | m_platformSupportsVRS = options6.VariableShadingRateTier > D3D12_VARIABLE_SHADING_RATE_TIER_NOT_SUPPORTED;
235 | if (m_platformSupportsVRS)
236 | {
237 | // Can use variable rate shading
238 | m_commandList.As(&m_commandList5);
239 | m_additionalShadingRatesSupported = !!options6.AdditionalShadingRatesSupported;
240 | }
241 | }
242 |
243 | // Command lists are created in the recording state, but there is nothing
244 | // to record yet. The main loop expects it to be closed, so close it now.
245 | ThrowIfFailed(m_commandList->Close());
246 |
247 | // Create the vertex buffer.
248 | {
249 | AddQuad(-1, 0.6f, 0.4f);
250 |
251 | AddQuad(-1, 0.1f, 0.4f);
252 | AddQuad(-1 + 0.5f, 0.1f, 0.4f);
253 | AddQuad(-1 + 0.5f + 0.5f, 0.1f, 0.4f);
254 |
255 | AddQuad(-1, -0.4f, 0.4f);
256 | AddQuad(-1 + 0.5f, -0.4f, 0.4f);
257 | AddQuad(-1 + 0.5f + 0.5f, -0.4f, 0.4f);
258 |
259 | const UINT vertexBufferSize = sizeof(Vertex) * m_triangleVertices.size();
260 |
261 | // Note: using upload heaps to transfer static data like vert buffers is not
262 | // recommended. Every time the GPU needs it, the upload heap will be marshalled
263 | // over. Please read up on Default Heap usage. An upload heap is used here for
264 | // code simplicity and because there are very few verts to actually transfer.
265 | ThrowIfFailed(m_device->CreateCommittedResource(
266 | &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
267 | D3D12_HEAP_FLAG_NONE,
268 | &CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize),
269 | D3D12_RESOURCE_STATE_GENERIC_READ,
270 | nullptr,
271 | IID_PPV_ARGS(&m_vertexBuffer)));
272 |
273 | // Copy the triangle data to the vertex buffer.
274 | UINT8* pVertexDataBegin;
275 | CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
276 | ThrowIfFailed(m_vertexBuffer->Map(0, &readRange, reinterpret_cast(&pVertexDataBegin)));
277 | memcpy(pVertexDataBegin, m_triangleVertices.data(), vertexBufferSize);
278 | m_vertexBuffer->Unmap(0, nullptr);
279 |
280 | // Initialize the vertex buffer view.
281 | m_vertexBufferView.BufferLocation = m_vertexBuffer->GetGPUVirtualAddress();
282 | m_vertexBufferView.StrideInBytes = sizeof(Vertex);
283 | m_vertexBufferView.SizeInBytes = vertexBufferSize;
284 | }
285 |
286 | // Create synchronization objects and wait until assets have been uploaded to the GPU.
287 | {
288 | ThrowIfFailed(m_device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_fence)));
289 | m_fenceValue = 1;
290 |
291 | // Create an event handle to use for frame synchronization.
292 | m_fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
293 | if (m_fenceEvent == nullptr)
294 | {
295 | ThrowIfFailed(HRESULT_FROM_WIN32(GetLastError()));
296 | }
297 |
298 | // Wait for the command list to execute; we are reusing the same command
299 | // list in our main loop but for now, we just want to wait for setup to
300 | // complete before continuing.
301 | WaitForPreviousFrame();
302 | }
303 | }
304 |
305 | // Update frame-based values.
306 | void D3D12HelloTriangle::OnUpdate()
307 | {
308 | }
309 |
310 | // Render the scene.
311 | void D3D12HelloTriangle::OnRender()
312 | {
313 | // Record all the commands we need to render the scene into the command list.
314 | PopulateCommandList();
315 |
316 | // Execute the command list.
317 | ID3D12CommandList* ppCommandLists[] = { m_commandList.Get() };
318 | m_commandQueue->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists);
319 |
320 | // Present the frame.
321 | ThrowIfFailed(m_swapChain->Present(1, 0));
322 |
323 | WaitForPreviousFrame();
324 | }
325 |
326 | void D3D12HelloTriangle::OnDestroy()
327 | {
328 | // Ensure that the GPU is no longer referencing resources that are about to be
329 | // cleaned up by the destructor.
330 | WaitForPreviousFrame();
331 |
332 | CloseHandle(m_fenceEvent);
333 | }
334 |
335 | void D3D12HelloTriangle::PopulateCommandList()
336 | {
337 | // Command list allocators can only be reset when the associated
338 | // command lists have finished execution on the GPU; apps should use
339 | // fences to determine GPU execution progress.
340 | ThrowIfFailed(m_commandAllocator->Reset());
341 |
342 | // However, when ExecuteCommandList() is called on a particular command
343 | // list, that command list can then be reset at any time and must be before
344 | // re-recording.
345 | ThrowIfFailed(m_commandList->Reset(m_commandAllocator.Get(), m_pipelineState.Get()));
346 |
347 | // Set necessary state.
348 | m_commandList->SetGraphicsRootSignature(m_rootSignature.Get());
349 | m_commandList->RSSetViewports(1, &m_viewport);
350 | m_commandList->RSSetScissorRects(1, &m_scissorRect);
351 |
352 | // Indicate that the back buffer will be used as a render target.
353 | m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));
354 |
355 | CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart(), m_frameIndex, m_rtvDescriptorSize);
356 | m_commandList->OMSetRenderTargets(1, &rtvHandle, FALSE, nullptr);
357 |
358 | // Record commands.
359 | const float clearColor[] = { 0.0f, 0.2f, 0.4f, 1.0f };
360 | m_commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
361 | m_commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
362 | m_commandList->IASetVertexBuffers(0, 1, &m_vertexBufferView);
363 |
364 |
365 | UINT triangleCount = m_triangleVertices.size() / 6;
366 | for (UINT i = 0; i < triangleCount; ++i)
367 | {
368 | if (!m_platformSupportsVRS && i > 0)
369 | continue;
370 |
371 | if (i >= 4 && !m_additionalShadingRatesSupported)
372 | continue;
373 |
374 | if (m_commandList5)
375 | {
376 | static const D3D12_SHADING_RATE shadingRates[] = {
377 | D3D12_SHADING_RATE_1X1,
378 | D3D12_SHADING_RATE_1X2,D3D12_SHADING_RATE_2X1, D3D12_SHADING_RATE_2X2,
379 | D3D12_SHADING_RATE_2X4, D3D12_SHADING_RATE_4X2, D3D12_SHADING_RATE_4X4 };
380 |
381 | D3D12_SHADING_RATE shadingRate = shadingRates[i];
382 | D3D12_SHADING_RATE_COMBINER combiners[2] = { D3D12_SHADING_RATE_COMBINER_PASSTHROUGH, D3D12_SHADING_RATE_COMBINER_PASSTHROUGH };
383 | m_commandList5->RSSetShadingRate(shadingRate, combiners);
384 | }
385 |
386 | m_commandList->DrawInstanced(6, 1, i * 6, 0);
387 | }
388 |
389 | // Indicate that the back buffer will now be used to present.
390 | m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT));
391 |
392 | ThrowIfFailed(m_commandList->Close());
393 | }
394 |
395 | void D3D12HelloTriangle::WaitForPreviousFrame()
396 | {
397 | // WAITING FOR THE FRAME TO COMPLETE BEFORE CONTINUING IS NOT BEST PRACTICE.
398 | // This is code implemented as such for simplicity. The D3D12HelloFrameBuffering
399 | // sample illustrates how to use fences for efficient resource usage and to
400 | // maximize GPU utilization.
401 |
402 | // Signal and increment the fence value.
403 | const UINT64 fence = m_fenceValue;
404 | ThrowIfFailed(m_commandQueue->Signal(m_fence.Get(), fence));
405 | m_fenceValue++;
406 |
407 | // Wait until the previous frame is finished.
408 | if (m_fence->GetCompletedValue() < fence)
409 | {
410 | ThrowIfFailed(m_fence->SetEventOnCompletion(fence, m_fenceEvent));
411 | WaitForSingleObject(m_fenceEvent, INFINITE);
412 | }
413 |
414 | m_frameIndex = m_swapChain->GetCurrentBackBufferIndex();
415 | }
416 |
417 |
418 | void D3D12HelloTriangle::UpdateTitleText()
419 | {
420 | if (!m_platformSupportsVRS)
421 | {
422 | wchar_t const* titleText = L"{Platform does not support VRS}";
423 | SetWindowText(Win32Application::GetHwnd(), titleText);
424 | }
425 | }
--------------------------------------------------------------------------------
/hellovrs_arrowkeys_tier2/HelloTriangle/D3D12HelloTriangle.cpp:
--------------------------------------------------------------------------------
1 | //*********************************************************
2 | //
3 | // Copyright (c) Microsoft. All rights reserved.
4 | // This code is licensed under the MIT License (MIT).
5 | // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 | // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 | // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 | // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 | //
10 | //*********************************************************
11 |
12 | #include "stdafx.h"
13 | #include "D3D12HelloTriangle.h"
14 | #include "VSMain.h"
15 | #include "PSMain.h"
16 | #include "VSFallback.h"
17 | #include "PSFallback.h"
18 | #include
19 |
20 | D3D12HelloTriangle::D3D12HelloTriangle(UINT width, UINT height, std::wstring name) :
21 | DXSample(width, height, name),
22 | m_frameIndex(0),
23 | m_viewport(0.0f, 0.0f, static_cast(width), static_cast(height)),
24 | m_scissorRect(0, 0, static_cast(width), static_cast(height)),
25 | m_rtvDescriptorSize(0),
26 | m_shadingRateIndex(0),
27 | m_combinerTypeIndex(0)
28 | {
29 | }
30 |
31 | void D3D12HelloTriangle::OnInit()
32 | {
33 | LoadPipeline();
34 | LoadAssets();
35 |
36 | if (m_platformSupportsTier2VRS)
37 | AllocateScreenspaceImage(256u, 256u);
38 |
39 | m_shadingRateIndex = 0;
40 | UpdateTitleText();
41 | }
42 |
43 | // Load the rendering pipeline dependencies.
44 | void D3D12HelloTriangle::LoadPipeline()
45 | {
46 | UINT dxgiFactoryFlags = 0;
47 |
48 | D3D12EnableExperimentalFeatures(1, &D3D12ExperimentalShaderModels, NULL, NULL);
49 |
50 | #if defined(_DEBUG)
51 | // Enable the debug layer (requires the Graphics Tools "optional feature").
52 | // NOTE: Enabling the debug layer after device creation will invalidate the active device.
53 | {
54 | ComPtr debugController;
55 | if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController))))
56 | {
57 | debugController->EnableDebugLayer();
58 |
59 | // Enable additional debug layers.
60 | dxgiFactoryFlags |= DXGI_CREATE_FACTORY_DEBUG;
61 | }
62 | }
63 | #endif
64 |
65 | ComPtr factory;
66 | ThrowIfFailed(CreateDXGIFactory2(dxgiFactoryFlags, IID_PPV_ARGS(&factory)));
67 |
68 | if (m_useWarpDevice)
69 | {
70 | ComPtr warpAdapter;
71 | ThrowIfFailed(factory->EnumWarpAdapter(IID_PPV_ARGS(&warpAdapter)));
72 |
73 | ThrowIfFailed(D3D12CreateDevice(
74 | warpAdapter.Get(),
75 | D3D_FEATURE_LEVEL_11_0,
76 | IID_PPV_ARGS(&m_device)
77 | ));
78 | }
79 | else
80 | {
81 | ComPtr hardwareAdapter;
82 | GetHardwareAdapter(factory.Get(), &hardwareAdapter);
83 |
84 | ThrowIfFailed(D3D12CreateDevice(
85 | nullptr,
86 | D3D_FEATURE_LEVEL_11_0,
87 | IID_PPV_ARGS(&m_device)
88 | ));
89 | }
90 |
91 | const D3D12_FEATURE D3D12_FEATURE_D3D12_OPTIONS6 = (D3D12_FEATURE)30;
92 | D3D12_FEATURE_DATA_D3D12_OPTIONS6 options6;
93 | m_platformSupportsTier2VRS = false;
94 | if (SUCCEEDED(m_device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS6, &options6, sizeof(options6))))
95 | {
96 | m_platformSupportsTier2VRS = options6.VariableShadingRateTier >= D3D12_VARIABLE_SHADING_RATE_TIER_2;
97 | }
98 |
99 | // Describe and create the command queue.
100 | D3D12_COMMAND_QUEUE_DESC queueDesc = {};
101 | queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
102 | queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
103 |
104 | ThrowIfFailed(m_device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&m_commandQueue)));
105 |
106 | // Describe and create the swap chain.
107 | DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
108 | swapChainDesc.BufferCount = FrameCount;
109 | swapChainDesc.Width = m_width;
110 | swapChainDesc.Height = m_height;
111 | swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
112 | swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
113 | swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
114 | swapChainDesc.SampleDesc.Count = 1;
115 |
116 | ComPtr swapChain;
117 | ThrowIfFailed(factory->CreateSwapChainForHwnd(
118 | m_commandQueue.Get(), // Swap chain needs the queue so that it can force a flush on it.
119 | Win32Application::GetHwnd(),
120 | &swapChainDesc,
121 | nullptr,
122 | nullptr,
123 | &swapChain
124 | ));
125 |
126 | // This sample does not support fullscreen transitions.
127 | ThrowIfFailed(factory->MakeWindowAssociation(Win32Application::GetHwnd(), DXGI_MWA_NO_ALT_ENTER));
128 |
129 | ThrowIfFailed(swapChain.As(&m_swapChain));
130 | m_frameIndex = m_swapChain->GetCurrentBackBufferIndex();
131 |
132 | // Create descriptor heaps.
133 | {
134 | // Describe and create a render target view (RTV) descriptor heap.
135 | D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
136 | rtvHeapDesc.NumDescriptors = FrameCount;
137 | rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
138 | rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
139 | ThrowIfFailed(m_device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&m_rtvHeap)));
140 |
141 | m_rtvDescriptorSize = m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
142 | }
143 |
144 | // Create frame resources.
145 | {
146 | CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart());
147 |
148 | // Create a RTV for each frame.
149 | for (UINT n = 0; n < FrameCount; n++)
150 | {
151 | ThrowIfFailed(m_swapChain->GetBuffer(n, IID_PPV_ARGS(&m_renderTargets[n])));
152 | m_device->CreateRenderTargetView(m_renderTargets[n].Get(), nullptr, rtvHandle);
153 | rtvHandle.Offset(1, m_rtvDescriptorSize);
154 | }
155 | }
156 |
157 | ThrowIfFailed(m_device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&m_commandAllocator)));
158 | }
159 |
160 | void D3D12HelloTriangle::AllocateScreenspaceImage(UINT width, UINT height)
161 | {
162 | CD3DX12_RESOURCE_DESC screenspaceImageDesc = CD3DX12_RESOURCE_DESC::Tex2D(
163 | DXGI_FORMAT_R8_UINT,
164 | static_cast(width),
165 | height,
166 | 1 /*arraySize*/,
167 | 1 /*mipLevels*/);
168 |
169 | D3D12_RESOURCE_STATES initialState = D3D12_RESOURCE_STATE_SHADING_RATE_SOURCE;
170 |
171 | CD3DX12_HEAP_PROPERTIES heapProperties(D3D12_HEAP_TYPE_DEFAULT);
172 | ThrowIfFailed(m_device->CreateCommittedResource(
173 | &heapProperties,
174 | D3D12_HEAP_FLAG_NONE,
175 | &screenspaceImageDesc,
176 | initialState,
177 | nullptr,
178 | IID_PPV_ARGS(&m_spScreenspaceImage)));
179 |
180 | UINT64 uploadSize;
181 | m_device->GetCopyableFootprints(&screenspaceImageDesc, 0, 1, 0, nullptr, nullptr, nullptr, &uploadSize);
182 |
183 | ThrowIfFailed(m_device->CreateCommittedResource(
184 | &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
185 | D3D12_HEAP_FLAG_NONE,
186 | &CD3DX12_RESOURCE_DESC::Buffer(uploadSize),
187 | D3D12_RESOURCE_STATE_GENERIC_READ,
188 | nullptr,
189 | IID_PPV_ARGS(&m_spScreenspaceImageUpload)));
190 | }
191 |
192 | void D3D12HelloTriangle::SetScreenspaceImageData(ID3D12GraphicsCommandList5* cl, BYTE* data, size_t dataSize)
193 | {
194 | // Map the screenspace image upload buffer and write the value
195 | UINT8* pMappedRange;
196 | CD3DX12_RANGE readRange(0, 0);
197 | ThrowIfFailed(m_spScreenspaceImageUpload->Map(0, &readRange, reinterpret_cast(&pMappedRange)));
198 | memcpy(pMappedRange, data, dataSize);
199 | m_spScreenspaceImageUpload->Unmap(0, nullptr);
200 |
201 | // Copy from the upload buffer to the screenspace image
202 | CD3DX12_TEXTURE_COPY_LOCATION Src;
203 | Src.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
204 | Src.pResource = m_spScreenspaceImageUpload.Get();
205 | auto desc = m_spScreenspaceImage->GetDesc();
206 | m_device->GetCopyableFootprints(&desc, 0, 1, 0, &Src.PlacedFootprint, nullptr, nullptr, nullptr);
207 |
208 | CD3DX12_TEXTURE_COPY_LOCATION Dst;
209 | Dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
210 | Dst.pResource = m_spScreenspaceImage.Get();
211 | Dst.SubresourceIndex = 0;
212 |
213 | D3D12_RESOURCE_STATES previousState = D3D12_RESOURCE_STATE_SHADING_RATE_SOURCE;
214 |
215 | cl->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_spScreenspaceImage.Get(), previousState, D3D12_RESOURCE_STATE_COPY_DEST));
216 | cl->CopyTextureRegion(&Dst, 0, 0, 0, &Src, nullptr);
217 | cl->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_spScreenspaceImage.Get(), D3D12_RESOURCE_STATE_COPY_DEST, previousState));
218 | }
219 |
220 | void
221 | CompileAndCheckErrors(_In_ LPCWSTR pFileName,
222 | _In_reads_opt_(_Inexpressible_(pDefines->Name != NULL)) CONST D3D_SHADER_MACRO* pDefines,
223 | _In_opt_ ID3DInclude* pInclude,
224 | _In_ LPCSTR pEntrypoint,
225 | _In_ LPCSTR pTarget,
226 | _In_ UINT Flags1,
227 | _In_ UINT Flags2,
228 | _Out_ ID3DBlob** ppCode)
229 | {
230 | ComPtr errorMsgs;
231 | HRESULT hr = D3DCompileFromFile(pFileName, pDefines, pInclude, pEntrypoint, pTarget, Flags1, Flags2, ppCode, &errorMsgs);
232 | if (FAILED(hr))
233 | {
234 | char const* errorMsg = reinterpret_cast(errorMsgs->GetBufferPointer());
235 | MessageBoxA(nullptr, errorMsg, "Failed to compile shader", MB_OK);
236 | ThrowIfFailed(hr);
237 | }
238 | }
239 |
240 | // Load the sample assets.
241 | void D3D12HelloTriangle::LoadAssets()
242 | {
243 | // Create an empty root signature.
244 | {
245 | CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc;
246 | rootSignatureDesc.Init(0, nullptr, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
247 |
248 | ComPtr signature;
249 | ComPtr error;
250 | ThrowIfFailed(D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error));
251 | ThrowIfFailed(m_device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&m_rootSignature)));
252 | }
253 |
254 | // Create the pipeline state, which includes compiling and loading shaders.
255 | {
256 | ComPtr vertexShader;
257 | ComPtr pixelShader;
258 |
259 | #if defined(_DEBUG)
260 | // Enable better shader debugging with the graphics debugging tools.
261 | UINT compileFlags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
262 | #else
263 | UINT compileFlags = 0;
264 | #endif
265 |
266 | // Define the vertex input layout.
267 | D3D12_INPUT_ELEMENT_DESC inputElementDescs[] =
268 | {
269 | { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
270 | { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }
271 | };
272 |
273 | // Describe and create the graphics pipeline state object (PSO).
274 | D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
275 | psoDesc.InputLayout = { inputElementDescs, _countof(inputElementDescs) };
276 | psoDesc.pRootSignature = m_rootSignature.Get();
277 | if (m_platformSupportsTier2VRS)
278 | {
279 | psoDesc.VS = CD3DX12_SHADER_BYTECODE((void *)g_VSMain, ARRAYSIZE(g_VSMain));
280 | psoDesc.PS = CD3DX12_SHADER_BYTECODE((void *)g_PSMain, ARRAYSIZE(g_PSMain));
281 | }
282 | else
283 | {
284 | psoDesc.VS = CD3DX12_SHADER_BYTECODE((void *)g_VSFallback, ARRAYSIZE(g_VSFallback));
285 | psoDesc.PS = CD3DX12_SHADER_BYTECODE((void *)g_PSFallback, ARRAYSIZE(g_PSFallback));
286 | }
287 | psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
288 | psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
289 | psoDesc.DepthStencilState.DepthEnable = FALSE;
290 | psoDesc.DepthStencilState.StencilEnable = FALSE;
291 | psoDesc.SampleMask = UINT_MAX;
292 | psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
293 | psoDesc.NumRenderTargets = 1;
294 | psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
295 | psoDesc.SampleDesc.Count = 1;
296 | ThrowIfFailed(m_device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&m_pipelineState)));
297 | }
298 |
299 | // Create the command list.
300 | ThrowIfFailed(m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_commandAllocator.Get(), m_pipelineState.Get(), IID_PPV_ARGS(&m_commandList)));
301 |
302 | m_commandList.As(&m_commandList5);
303 |
304 | // Command lists are created in the recording state, but there is nothing
305 | // to record yet. The main loop expects it to be closed, so close it now.
306 | ThrowIfFailed(m_commandList->Close());
307 |
308 | // Create the vertex buffer.
309 | {
310 | // Define the geometry for a triangle.
311 | Vertex triangleVertices[] =
312 | {
313 | { {-1, 1, 0.0f }, { 0.0f, 0.0f, 0.0f, 1.0f } },
314 | { { 0.25f, 1, 0.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } },
315 | { { -1, -0.25f * m_aspectRatio, 0.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } }
316 | };
317 |
318 | const UINT vertexBufferSize = sizeof(triangleVertices);
319 |
320 | // Note: using upload heaps to transfer static data like vert buffers is not
321 | // recommended. Every time the GPU needs it, the upload heap will be marshalled
322 | // over. Please read up on Default Heap usage. An upload heap is used here for
323 | // code simplicity and because there are very few verts to actually transfer.
324 | ThrowIfFailed(m_device->CreateCommittedResource(
325 | &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
326 | D3D12_HEAP_FLAG_NONE,
327 | &CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize),
328 | D3D12_RESOURCE_STATE_GENERIC_READ,
329 | nullptr,
330 | IID_PPV_ARGS(&m_vertexBuffer)));
331 |
332 | // Copy the triangle data to the vertex buffer.
333 | UINT8* pVertexDataBegin;
334 | CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
335 | ThrowIfFailed(m_vertexBuffer->Map(0, &readRange, reinterpret_cast(&pVertexDataBegin)));
336 | memcpy(pVertexDataBegin, triangleVertices, sizeof(triangleVertices));
337 | m_vertexBuffer->Unmap(0, nullptr);
338 |
339 | // Initialize the vertex buffer view.
340 | m_vertexBufferView.BufferLocation = m_vertexBuffer->GetGPUVirtualAddress();
341 | m_vertexBufferView.StrideInBytes = sizeof(Vertex);
342 | m_vertexBufferView.SizeInBytes = vertexBufferSize;
343 | }
344 |
345 | // Create synchronization objects and wait until assets have been uploaded to the GPU.
346 | {
347 | ThrowIfFailed(m_device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_fence)));
348 | m_fenceValue = 1;
349 |
350 | // Create an event handle to use for frame synchronization.
351 | m_fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
352 | if (m_fenceEvent == nullptr)
353 | {
354 | ThrowIfFailed(HRESULT_FROM_WIN32(GetLastError()));
355 | }
356 |
357 | // Wait for the command list to execute; we are reusing the same command
358 | // list in our main loop but for now, we just want to wait for setup to
359 | // complete before continuing.
360 | WaitForPreviousFrame();
361 | }
362 | }
363 |
364 | // Update frame-based values.
365 | void D3D12HelloTriangle::OnUpdate()
366 | {
367 | }
368 |
369 | // Render the scene.
370 | void D3D12HelloTriangle::OnRender()
371 | {
372 | // Record all the commands we need to render the scene into the command list.
373 | PopulateCommandList();
374 |
375 | // Execute the command list.
376 | ID3D12CommandList* ppCommandLists[] = { m_commandList.Get() };
377 | m_commandQueue->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists);
378 |
379 | // Present the frame.
380 | ThrowIfFailed(m_swapChain->Present(1, 0));
381 |
382 | WaitForPreviousFrame();
383 | }
384 |
385 | void D3D12HelloTriangle::OnDestroy()
386 | {
387 | // Ensure that the GPU is no longer referencing resources that are about to be
388 | // cleaned up by the destructor.
389 | WaitForPreviousFrame();
390 |
391 | CloseHandle(m_fenceEvent);
392 | }
393 |
394 | void D3D12HelloTriangle::PopulateCommandList()
395 | {
396 | // Command list allocators can only be reset when the associated
397 | // command lists have finished execution on the GPU; apps should use
398 | // fences to determine GPU execution progress.
399 | ThrowIfFailed(m_commandAllocator->Reset());
400 |
401 | // However, when ExecuteCommandList() is called on a particular command
402 | // list, that command list can then be reset at any time and must be before
403 | // re-recording.
404 | ThrowIfFailed(m_commandList->Reset(m_commandAllocator.Get(), m_pipelineState.Get()));
405 |
406 | // Set necessary state.
407 | m_commandList->SetGraphicsRootSignature(m_rootSignature.Get());
408 | m_commandList->RSSetViewports(1, &m_viewport);
409 | m_commandList->RSSetScissorRects(1, &m_scissorRect);
410 |
411 | // Indicate that the back buffer will be used as a render target.
412 | m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));
413 |
414 | CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart(), m_frameIndex, m_rtvDescriptorSize);
415 | m_commandList->OMSetRenderTargets(1, &rtvHandle, FALSE, nullptr);
416 |
417 | if (m_commandList5 && m_platformSupportsTier2VRS)
418 | {
419 | static const D3D12_SHADING_RATE shadingRates[] = {
420 | D3D12_SHADING_RATE_1X1, D3D12_SHADING_RATE_1X2,D3D12_SHADING_RATE_2X1, D3D12_SHADING_RATE_2X2, D3D12_SHADING_RATE_2X4, D3D12_SHADING_RATE_4X2, D3D12_SHADING_RATE_4X4 };
421 |
422 | D3D12_SHADING_RATE shadingRate = shadingRates[m_shadingRateIndex];
423 |
424 | std::vector screenspaceImageData;
425 | screenspaceImageData.resize(256 * 256);
426 | for (int y = 0; y < 256; ++y)
427 | {
428 | for (int x = 0; x < 256; ++x)
429 | {
430 | int index = (y * 256) + x;
431 | if (x > y)
432 | {
433 | screenspaceImageData[index] = static_cast(shadingRate);
434 | }
435 | else
436 | {
437 | screenspaceImageData[index] = static_cast(D3D12_SHADING_RATE_1X1);
438 | }
439 | }
440 | }
441 | SetScreenspaceImageData(m_commandList5.Get(), screenspaceImageData.data(), screenspaceImageData.size());
442 |
443 | if (m_combinerTypeIndex == 0)
444 | {
445 | D3D12_SHADING_RATE_COMBINER chooseScreenspaceImage[2] = { D3D12_SHADING_RATE_COMBINER_PASSTHROUGH, D3D12_SHADING_RATE_COMBINER_OVERRIDE }; // Choose screenspace image
446 | m_commandList5->RSSetShadingRate(shadingRate, chooseScreenspaceImage);
447 | }
448 | else if (m_combinerTypeIndex == 1)
449 | {
450 | D3D12_SHADING_RATE_COMBINER choosePerPrimitive[2] = { D3D12_SHADING_RATE_COMBINER_OVERRIDE, D3D12_SHADING_RATE_COMBINER_PASSTHROUGH }; // Choose per primitive
451 | m_commandList5->RSSetShadingRate(shadingRate, choosePerPrimitive);
452 | }
453 | else if (m_combinerTypeIndex == 2)
454 | {
455 | D3D12_SHADING_RATE_COMBINER chooseMin[2] = { D3D12_SHADING_RATE_COMBINER_MIN, D3D12_SHADING_RATE_COMBINER_MIN };
456 | m_commandList5->RSSetShadingRate(shadingRate, chooseMin);
457 | }
458 |
459 | m_commandList5->RSSetShadingRateImage(m_spScreenspaceImage.Get());
460 | }
461 |
462 | // Record commands.
463 | const float clearColor[] = { 0.0f, 0.2f, 0.4f, 1.0f };
464 | m_commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
465 | m_commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
466 | m_commandList->IASetVertexBuffers(0, 1, &m_vertexBufferView);
467 | m_commandList->DrawInstanced(3, 1, 0, 0);
468 |
469 | // Indicate that the back buffer will now be used to present.
470 | m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT));
471 |
472 | ThrowIfFailed(m_commandList->Close());
473 | }
474 |
475 | void D3D12HelloTriangle::WaitForPreviousFrame()
476 | {
477 | // WAITING FOR THE FRAME TO COMPLETE BEFORE CONTINUING IS NOT BEST PRACTICE.
478 | // This is code implemented as such for simplicity. The D3D12HelloFrameBuffering
479 | // sample illustrates how to use fences for efficient resource usage and to
480 | // maximize GPU utilization.
481 |
482 | // Signal and increment the fence value.
483 | const UINT64 fence = m_fenceValue;
484 | ThrowIfFailed(m_commandQueue->Signal(m_fence.Get(), fence));
485 | m_fenceValue++;
486 |
487 | // Wait until the previous frame is finished.
488 | if (m_fence->GetCompletedValue() < fence)
489 | {
490 | ThrowIfFailed(m_fence->SetEventOnCompletion(fence, m_fenceEvent));
491 | WaitForSingleObject(m_fenceEvent, INFINITE);
492 | }
493 |
494 | m_frameIndex = m_swapChain->GetCurrentBackBufferIndex();
495 | }
496 |
497 | void D3D12HelloTriangle::OnKeyUp(UINT8 key)
498 | {
499 | if (!m_platformSupportsTier2VRS)
500 | return;
501 |
502 | if (key == 37 && m_shadingRateIndex > 0) // left
503 | {
504 | m_shadingRateIndex--;
505 | }
506 | if (key == 39 && m_shadingRateIndex < 7 - 1) // right
507 | {
508 | m_shadingRateIndex++;
509 | }
510 | else if (key == 38 && m_combinerTypeIndex < 2) // up
511 | {
512 | m_combinerTypeIndex++;
513 | }
514 | else if (key == 40 && m_combinerTypeIndex > 0) // down
515 | {
516 | m_combinerTypeIndex--;
517 | }
518 |
519 | UpdateTitleText();
520 | }
521 |
522 | void D3D12HelloTriangle::UpdateTitleText()
523 | {
524 | std::wstringstream titleText;
525 |
526 | if (m_platformSupportsTier2VRS)
527 | {
528 | wchar_t const* shadingRateTitle = L"";
529 | switch (m_shadingRateIndex)
530 | {
531 | case 0: shadingRateTitle = L"1x1"; break;
532 | case 1: shadingRateTitle = L"1x2"; break;
533 | case 2: shadingRateTitle = L"2x1"; break;
534 | case 3: shadingRateTitle = L"2x2"; break;
535 | case 4: shadingRateTitle = L"2x4"; break;
536 | case 5: shadingRateTitle = L"4x2"; break;
537 | case 6: shadingRateTitle = L"4x4"; break;
538 | }
539 |
540 | wchar_t const* combinerTitle = L"";
541 | switch (m_combinerTypeIndex)
542 | {
543 | case 0: combinerTitle = L"AlwaysScreenspace"; break;
544 | case 1: combinerTitle = L"AlwaysPerPrimitive"; break;
545 | case 2: combinerTitle = L"Min"; break;
546 | }
547 |
548 | titleText << L"Screenspace image rate: " << shadingRateTitle << L" Per-primitive rate: 4x4 Combiner:" << combinerTitle;
549 | }
550 | else
551 | {
552 | titleText << L"{Platform does not support VRS Tier 2}";
553 | }
554 |
555 | SetWindowText(Win32Application::GetHwnd(), titleText.str().c_str());
556 |
557 | }
--------------------------------------------------------------------------------