Skip to content

Commit 217e3d6

Browse files
committed
July 2017
1 parent b3df7d1 commit 217e3d6

File tree

207 files changed

+6683
-898
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

207 files changed

+6683
-898
lines changed

Kits/ATGTK/ControllerHelp.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ namespace ATG
6767
public:
6868
Help(_In_z_ const wchar_t* title, _In_z_ const wchar_t* description,
6969
_In_count_(buttonCount) const HelpButtonAssignment* buttons, size_t buttonCount, bool linearColors = false);
70+
71+
Help(Help&&) = default;
72+
Help& operator= (Help&&) = default;
73+
74+
Help(Help const&) = delete;
75+
Help& operator= (Help const&) = delete;
76+
7077
~Help();
7178

7279
#if defined(__d3d12_h__) || defined(__d3d12_x_h__)

Kits/ATGTK/PBREffect/PBREffect_Math.hlsli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
static const float PI = 3.14159265f;
1414

15+
float3 BiasX2(float3 x)
16+
{
17+
return 2.0f * x - 1.0f;
18+
}
19+
1520
// Given a local normal, transform it into a tangent space given by surface normal and tangent
1621
float3 PeturbNormal(float3 localNormal, float3 surfaceNormalWS, float3 surfaceTangentWS)
1722
{

Kits/ATGTK/PBREffect/PBREffect_PSTextured.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ float4 PSTextured(PSInputPixelLightingTxTangent pin) : SV_Target0
1818
const float3 L = normalize(-PBR_LightDirection[0]); // light vector ("to light" oppositve of light's direction)
1919

2020
// Before lighting, peturb the surface's normal by the one given in normal map.
21-
float3 localNormal = (PBR_NormalTexture.Sample(PBR_SurfaceSampler, pin.TexCoord).xyz * 2) - 1;
21+
float3 localNormal = BiasX2(PBR_NormalTexture.Sample(PBR_SurfaceSampler, pin.TexCoord).xyz);
2222

2323
float3 N = PeturbNormal( localNormal, pin.NormalWS, pin.TangentWS);
2424

Kits/ATGTK/RenderTexture.cpp

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
//--------------------------------------------------------------------------------------
2+
// File: RenderTexture.cpp
3+
//
4+
// Helper for managing offscreen render targets
5+
//
6+
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
7+
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
8+
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
9+
// PARTICULAR PURPOSE.
10+
//
11+
// Copyright (c) Microsoft Corporation. All rights reserved.
12+
//-------------------------------------------------------------------------------------
13+
14+
#pragma once
15+
16+
#include "pch.h"
17+
#include "RenderTexture.h"
18+
19+
#include "DirectXHelpers.h"
20+
21+
#include <algorithm>
22+
#include <stdio.h>
23+
#include <stdexcept>
24+
25+
#include <wrl/client.h>
26+
27+
using namespace DirectX;
28+
using namespace DX;
29+
30+
using Microsoft::WRL::ComPtr;
31+
32+
#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
33+
//======================================================================================
34+
// Direct3D 12
35+
//======================================================================================
36+
RenderTexture::RenderTexture(DXGI_FORMAT format) :
37+
m_format(format),
38+
m_width(0),
39+
m_height(0),
40+
m_state(D3D12_RESOURCE_STATE_COMMON),
41+
m_srvDescriptor{},
42+
m_rtvDescriptor{},
43+
m_clearColor{}
44+
{
45+
}
46+
47+
void RenderTexture::SetDevice(_In_ ID3D12Device* device, D3D12_CPU_DESCRIPTOR_HANDLE srvDescriptor, D3D12_CPU_DESCRIPTOR_HANDLE rtvDescriptor)
48+
{
49+
if (device == m_device.Get()
50+
&& srvDescriptor.ptr == m_srvDescriptor.ptr
51+
&& rtvDescriptor.ptr == m_rtvDescriptor.ptr)
52+
return;
53+
54+
if (m_device)
55+
{
56+
ReleaseDevice();
57+
}
58+
59+
{
60+
D3D12_FEATURE_DATA_FORMAT_SUPPORT formatSupport = { m_format };
61+
if (FAILED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &formatSupport, sizeof(formatSupport))))
62+
{
63+
throw std::exception("CheckFeatureSupport");
64+
}
65+
66+
UINT required = D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE | D3D12_FORMAT_SUPPORT1_RENDER_TARGET;
67+
if ((formatSupport.Support1 & required) != required)
68+
{
69+
#ifdef _DEBUG
70+
char buff[128] = {};
71+
sprintf_s(buff, "RenderTexture: Device does not support the requested format (%u)!\n", m_format);
72+
OutputDebugStringA(buff);
73+
#endif
74+
throw std::exception("RenderTexture");
75+
}
76+
}
77+
78+
if (!srvDescriptor.ptr || !rtvDescriptor.ptr)
79+
{
80+
throw std::exception("Invalid descriptors");
81+
}
82+
83+
m_device = device;
84+
85+
m_srvDescriptor = srvDescriptor;
86+
m_rtvDescriptor = rtvDescriptor;
87+
}
88+
89+
void RenderTexture::SizeResources(size_t width, size_t height)
90+
{
91+
if (width == m_width && height == m_height)
92+
return;
93+
94+
if (m_width > UINT32_MAX || m_height > UINT32_MAX)
95+
{
96+
throw std::out_of_range("Invalid width/height");
97+
}
98+
99+
if (!m_device)
100+
return;
101+
102+
m_width = m_height = 0;
103+
104+
auto heapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
105+
106+
D3D12_RESOURCE_DESC desc = CD3DX12_RESOURCE_DESC::Tex2D(m_format,
107+
static_cast<UINT64>(width),
108+
static_cast<UINT>(height),
109+
1, 1, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
110+
111+
D3D12_CLEAR_VALUE clearValue = { m_format };
112+
memcpy(clearValue.Color, m_clearColor, sizeof(clearValue.Color));
113+
114+
m_state = D3D12_RESOURCE_STATE_RENDER_TARGET;
115+
116+
// Create a render target
117+
ThrowIfFailed(
118+
m_device->CreateCommittedResource(&heapProperties, D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES,
119+
&desc,
120+
m_state, &clearValue,
121+
IID_GRAPHICS_PPV_ARGS(m_resource.ReleaseAndGetAddressOf()))
122+
);
123+
124+
SetDebugObjectName(m_resource.Get(), L"RenderTexture RT");
125+
126+
// Create RTV.
127+
m_device->CreateRenderTargetView(m_resource.Get(), nullptr, m_rtvDescriptor);
128+
129+
// Create SRV.
130+
m_device->CreateShaderResourceView(m_resource.Get(), nullptr, m_srvDescriptor);
131+
132+
m_width = width;
133+
m_height = height;
134+
}
135+
136+
void RenderTexture::ReleaseDevice()
137+
{
138+
m_resource.Reset();
139+
m_device.Reset();
140+
141+
m_state = D3D12_RESOURCE_STATE_COMMON;
142+
m_width = m_height = 0;
143+
144+
m_srvDescriptor.ptr = m_rtvDescriptor.ptr = 0;
145+
}
146+
147+
void RenderTexture::TransitionTo(_In_ ID3D12GraphicsCommandList* commandList, D3D12_RESOURCE_STATES afterState)
148+
{
149+
TransitionResource(commandList, m_resource.Get(), m_state, afterState);
150+
m_state = afterState;
151+
}
152+
153+
#else
154+
//======================================================================================
155+
// Direct3D 11
156+
//======================================================================================
157+
RenderTexture::RenderTexture(DXGI_FORMAT format) :
158+
m_format(format),
159+
m_width(0),
160+
m_height(0)
161+
{
162+
}
163+
164+
void RenderTexture::SetDevice(_In_ ID3D11Device* device)
165+
{
166+
if (device == m_device.Get())
167+
return;
168+
169+
if (m_device)
170+
{
171+
ReleaseDevice();
172+
}
173+
174+
{
175+
UINT formatSupport = 0;
176+
if (FAILED(device->CheckFormatSupport(m_format, &formatSupport)))
177+
{
178+
throw std::exception("CheckFormatSupport");
179+
}
180+
181+
UINT32 required = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_SHADER_SAMPLE | D3D11_FORMAT_SUPPORT_RENDER_TARGET;
182+
if ((formatSupport & required) != required)
183+
{
184+
#ifdef _DEBUG
185+
char buff[128] = {};
186+
sprintf_s(buff, "RenderTexture: Device does not support the requested format (%u)!\n", m_format);
187+
OutputDebugStringA(buff);
188+
#endif
189+
throw std::exception("RenderTexture");
190+
}
191+
}
192+
193+
m_device = device;
194+
195+
#if defined(_XBOX_ONE) && defined(_TITLE)
196+
m_fastSemantics = (device->GetCreationFlags() & D3D11_CREATE_DEVICE_IMMEDIATE_CONTEXT_FAST_SEMANTICS) != 0;
197+
#endif
198+
}
199+
200+
201+
void RenderTexture::SizeResources(size_t width, size_t height)
202+
{
203+
if (width == m_width && height == m_height)
204+
return;
205+
206+
if (m_width > UINT32_MAX || m_height > UINT32_MAX)
207+
{
208+
throw std::out_of_range("Invalid width/height");
209+
}
210+
211+
if (!m_device)
212+
return;
213+
214+
m_width = m_height = 0;
215+
216+
// Create a render target
217+
CD3D11_TEXTURE2D_DESC renderTargetDesc(
218+
m_format,
219+
static_cast<UINT>(width),
220+
static_cast<UINT>(height),
221+
1, // The render target view has only one texture.
222+
1, // Use a single mipmap level.
223+
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE,
224+
D3D11_USAGE_DEFAULT,
225+
0,
226+
1
227+
);
228+
229+
ThrowIfFailed(m_device->CreateTexture2D(
230+
&renderTargetDesc,
231+
nullptr,
232+
m_renderTarget.ReleaseAndGetAddressOf()
233+
));
234+
235+
SetDebugObjectName(m_renderTarget.Get(), "RenderTexture RT");
236+
237+
// Create RTV.
238+
CD3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc(D3D11_RTV_DIMENSION_TEXTURE2D, m_format);
239+
240+
ThrowIfFailed(m_device->CreateRenderTargetView(
241+
m_renderTarget.Get(),
242+
&renderTargetViewDesc,
243+
m_renderTargetView.ReleaseAndGetAddressOf()
244+
));
245+
246+
SetDebugObjectName(m_renderTargetView.Get(), "RenderTexture RTV");
247+
248+
// Create SRV.
249+
CD3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc(D3D11_SRV_DIMENSION_TEXTURE2D, m_format);
250+
251+
ThrowIfFailed(m_device->CreateShaderResourceView(
252+
m_renderTarget.Get(),
253+
&shaderResourceViewDesc,
254+
m_shaderResourceView.ReleaseAndGetAddressOf()
255+
));
256+
257+
SetDebugObjectName(m_shaderResourceView.Get(), "RenderTexture SRV");
258+
259+
m_width = width;
260+
m_height = height;
261+
}
262+
263+
264+
void RenderTexture::ReleaseDevice()
265+
{
266+
m_renderTargetView.Reset();
267+
m_shaderResourceView.Reset();
268+
m_renderTarget.Reset();
269+
270+
m_device.Reset();
271+
272+
m_width = m_height = 0;
273+
}
274+
275+
#if defined(_XBOX_ONE) && defined(_TITLE)
276+
void RenderTexture::EndScene(_In_ ID3D11DeviceContextX* context)
277+
{
278+
if (m_fastSemantics)
279+
{
280+
context->FlushGpuCacheRange(
281+
D3D11_FLUSH_ENSURE_CB0_COHERENCY
282+
| D3D11_FLUSH_COLOR_BLOCK_INVALIDATE
283+
| D3D11_FLUSH_TEXTURE_L1_INVALIDATE
284+
| D3D11_FLUSH_TEXTURE_L2_INVALIDATE,
285+
nullptr, D3D11_FLUSH_GPU_CACHE_RANGE_ALL);
286+
context->DecompressResource(m_renderTarget.Get(), 0, nullptr,
287+
m_renderTarget.Get(), 0, nullptr,
288+
m_format, D3D11X_DECOMPRESS_PROPAGATE_COLOR_CLEAR);
289+
}
290+
}
291+
#endif
292+
293+
#endif
294+
295+
void RenderTexture::SetWindow(const RECT& output)
296+
{
297+
// Determine the render target size in pixels.
298+
size_t width = std::max<size_t>(output.right - output.left, 1);
299+
size_t height = std::max<size_t>(output.bottom - output.top, 1);
300+
301+
SizeResources(width, height);
302+
}
303+

0 commit comments

Comments
 (0)