|
| 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