This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
forked from GPUOpen-Drivers/pal
-
Notifications
You must be signed in to change notification settings - Fork 2
/
palTimeGraphImpl.h
289 lines (245 loc) · 11.3 KB
/
palTimeGraphImpl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
***********************************************************************************************************************
*
* Copyright (c) 2016-2021 Advanced Micro Devices, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**********************************************************************************************************************/
#pragma once
#include "palCmdBuffer.h"
#include "palDevice.h"
#include "palFormatInfo.h"
#include "palGpuMemory.h"
#include "palInlineFuncs.h"
#include "palPipeline.h"
#include "palSysMemory.h"
#include "palTimeGraph.h"
#include "timeGraph/g_timeGraphComputePipelineInitImpl.h"
namespace GpuUtil
{
// =====================================================================================================================
template <typename Allocator>
TimeGraph<Allocator>::TimeGraph(
Pal::IDevice* pDevice,
Allocator* pAllocator)
:
m_pDevice(pDevice),
m_pAllocator(pAllocator),
m_pPipeline(nullptr),
m_maxSrdSize(0)
{
memset(&m_deviceProps, 0, sizeof(m_deviceProps));
memset(m_memHeapProps, 0, sizeof(m_memHeapProps));
}
// =====================================================================================================================
template <typename Allocator>
TimeGraph<Allocator>::~TimeGraph()
{
if (m_pPipeline != nullptr)
{
m_pPipeline->Destroy();
PAL_SAFE_FREE(m_pPipeline, m_pAllocator);
}
}
// =====================================================================================================================
// Initializes the TimeGraph class:
// - Stores the device and GPU memory heap properties for later reference.
// - Create the pipeline and GPU memory for the draw text pipeline.
// - Create the GPU memory for the constant binary font data.
// - Add all GPU memory references to the device and mark them as always resident.
template <typename Allocator>
Pal::Result TimeGraph<Allocator>::Init()
{
Pal::Result result = m_pDevice->GetProperties(&m_deviceProps);
if (result == Pal::Result::Success)
{
m_maxSrdSize = Util::Max(Util::Max(m_deviceProps.gfxipProperties.srdSizes.bufferView,
m_deviceProps.gfxipProperties.srdSizes.imageView),
Util::Max(m_deviceProps.gfxipProperties.srdSizes.fmaskView,
m_deviceProps.gfxipProperties.srdSizes.sampler));
result = m_pDevice->GetGpuMemoryHeapProperties(m_memHeapProps);
}
if (result == Pal::Result::Success)
{
result = TimeGraphDraw::CreateTimeGraphComputePipelines(m_pDevice, m_pAllocator, &m_pPipeline);
}
return result;
}
// =====================================================================================================================
// Creates a GpuMemory object using the given memory requirements.
template <typename Allocator>
Pal::Result TimeGraph<Allocator>::CreateGpuMemory(
Pal::GpuMemoryRequirements* pMemReqs,
Pal::IGpuMemory** ppGpuMemory,
Pal::gpusize* pOffset)
{
// Translate the memory requirements into a GpuMemory create info.
Pal::GpuMemoryCreateInfo createInfo = {};
createInfo.size = pMemReqs->size;
createInfo.alignment = pMemReqs->alignment;
createInfo.vaRange = Pal::VaRange::Default;
createInfo.priority = Pal::GpuMemPriority::VeryLow;
createInfo.heapCount = pMemReqs->heapCount;
memcpy(createInfo.heaps, pMemReqs->heaps, createInfo.heapCount);
Pal::Result result = Pal::Result::Success;
const size_t objectSize = m_pDevice->GetGpuMemorySize(createInfo, &result);
if (result == Pal::Result::Success)
{
void* pMemory = PAL_MALLOC(objectSize, m_pAllocator, Util::SystemAllocType::AllocInternal);
if (pMemory != nullptr)
{
result = m_pDevice->CreateGpuMemory(createInfo, pMemory, reinterpret_cast<Pal::IGpuMemory**>(ppGpuMemory));
if (result != Pal::Result::Success)
{
PAL_SAFE_FREE(pMemory, m_pAllocator);
}
}
else
{
result = Pal::Result::ErrorOutOfMemory;
}
}
return result;
}
// =====================================================================================================================
// Executes a line draw (using a dispatch) onto the destination image.
template <typename Allocator>
void TimeGraph<Allocator>::DrawGraphLine(
const Pal::IImage& dstImage, // Desintation image.
Pal::ICmdBuffer* pCmdBuffer, // Command buffer for drawing graph
const Pal::uint32* pTimeData, // Scaled Time Values
Pal::uint32 xPosition, // X drawing offset
Pal::uint32 yPosition, // Y drawing offset
const Pal::uint32* pLineColor, // Color of the line
Pal::uint32 numDataPoints // Number of data points
) const
{
ColorInfo colorInfo = {};
// Pack the raw draw colors into the destination format.
const Pal::SwizzledFormat imgFormat = dstImage.GetImageCreateInfo().swizzledFormat;
Pal::uint32 swizzledLineColor[4] = {};
Pal::Formats::SwizzleColor(imgFormat, pLineColor, swizzledLineColor);
Pal::Formats::PackRawClearColor(imgFormat, swizzledLineColor, &colorInfo.lineColor[0]);
Pal::gpusize dataAddr = 0;
Pal::uint32* pData = pCmdBuffer->CmdAllocateEmbeddedData(numDataPoints * sizeof(float), 1, &dataAddr);
memcpy(pData, pTimeData, numDataPoints * sizeof(float));
Pal::BufferViewInfo bufferViewInfo = {};
// Create an SRD for the Time data.
bufferViewInfo.gpuAddr = dataAddr;
bufferViewInfo.range = numDataPoints * sizeof(float);
bufferViewInfo.stride = sizeof(float);
bufferViewInfo.swizzledFormat = Pal::UndefinedSwizzledFormat;
Pal::uint32 bufferViewSrd[4] = {};
m_pDevice->CreateUntypedBufferViewSrds(1, &bufferViewInfo, &bufferViewSrd[0]);
// Bind a buffer view for the Scaled Time Data in user data #0-3.
pCmdBuffer->CmdSetUserData(Pal::PipelineBindPoint::Compute, 0, 4, &bufferViewSrd[0]);
// Construct an embedded descriptor table. The first entry is an image view for the target image and the second is
// for the color data .
const Pal::uint32 srdDwords = m_maxSrdSize / sizeof(Pal::uint32);
Pal::gpusize tableGpuAddr = 0;
Pal::uint32* pTable = pCmdBuffer->CmdAllocateEmbeddedData(srdDwords + (sizeof(ColorInfo) / sizeof(Pal::uint32)),
1,
&tableGpuAddr);
CreateImageView(&dstImage, pTable);
pTable += srdDwords;
memcpy(pTable, &colorInfo, sizeof(ColorInfo));
const Pal::uint32 tableGpuAddrLo = Util::LowPart(tableGpuAddr);
// Bind that descriptor table to user data #4.
pCmdBuffer->CmdSetUserData(Pal::PipelineBindPoint::Compute, 4, 1, &tableGpuAddrLo);
const Pal::uint32 constantInfo[3] =
{
xPosition,
yPosition,
numDataPoints
};
pCmdBuffer->CmdSetUserData(Pal::PipelineBindPoint::Compute, 5, 3, &constantInfo[0]);
// Bind the pipeline and issue one thread group.
pCmdBuffer->CmdBindPipeline({ Pal::PipelineBindPoint::Compute, m_pPipeline, Pal::InternalApiPsoHash, });
pCmdBuffer->CmdDispatch(32, 1, 1);
}
// =====================================================================================================================
// Creates an internal image view for the provided pImage.
template <typename Allocator>
void TimeGraph<Allocator>::CreateImageView(
const Pal::IImage* pImage,
void* pOut
) const
{
const auto& createInfo = pImage->GetImageCreateInfo();
Pal::ImageViewInfo imgViewInfo = {};
imgViewInfo.pImage = pImage;
imgViewInfo.viewType = Pal::ImageViewType::Tex2d;
imgViewInfo.swizzledFormat = GetRawFormat(createInfo.swizzledFormat.format);
// This is only used in a compute shader write, but will probably be immediately followed by a present
imgViewInfo.possibleLayouts.engines = Pal::EngineTypeUniversal | Pal::EngineTypeCompute;
imgViewInfo.possibleLayouts.usages = Pal::LayoutShaderWrite | Pal::LayoutShaderRead |
Pal::LayoutPresentWindowed | Pal::LayoutPresentFullscreen;
#if PAL_CLIENT_INTERFACE_MAJOR_VERSION < 642
imgViewInfo.subresRange.startSubres.aspect = Pal::ImageAspect::Color;
imgViewInfo.subresRange.startSubres.arraySlice = 0;
imgViewInfo.subresRange.startSubres.mipLevel = 0;
#else
imgViewInfo.subresRange.numPlanes = 1;
#endif
imgViewInfo.subresRange.numSlices = createInfo.arraySize;
imgViewInfo.subresRange.numMips = createInfo.mipLevels;
m_pDevice->CreateImageViewSrds(1, &imgViewInfo, pOut);
}
// =====================================================================================================================
// Gets a raw Uint format that matches the bit depth of the provided format.
template <typename Allocator>
Pal::SwizzledFormat TimeGraph<Allocator>::GetRawFormat(
Pal::ChNumFormat oldFmt)
{
Pal::SwizzledFormat retFmt = Pal::UndefinedSwizzledFormat;
switch (Pal::Formats::BitsPerPixel(oldFmt))
{
case 8:
retFmt.format = Pal::ChNumFormat::X8_Uint;
retFmt.swizzle =
{ Pal::ChannelSwizzle::X, Pal::ChannelSwizzle::Zero, Pal::ChannelSwizzle::Zero, Pal::ChannelSwizzle::One };
break;
case 16:
retFmt.format = Pal::ChNumFormat::X16_Uint;
retFmt.swizzle =
{ Pal::ChannelSwizzle::X, Pal::ChannelSwizzle::Zero, Pal::ChannelSwizzle::Zero, Pal::ChannelSwizzle::One };
break;
case 32:
retFmt.format = Pal::ChNumFormat::X32_Uint;
retFmt.swizzle =
{ Pal::ChannelSwizzle::X, Pal::ChannelSwizzle::Zero, Pal::ChannelSwizzle::Zero, Pal::ChannelSwizzle::One };
break;
case 64:
retFmt.format = Pal::ChNumFormat::X32Y32_Uint;
retFmt.swizzle =
{ Pal::ChannelSwizzle::X, Pal::ChannelSwizzle::Y, Pal::ChannelSwizzle::Zero, Pal::ChannelSwizzle::One };
break;
case 128:
retFmt.format = Pal::ChNumFormat::X32Y32Z32W32_Uint;
retFmt.swizzle =
{ Pal::ChannelSwizzle::X, Pal::ChannelSwizzle::Y, Pal::ChannelSwizzle::Z, Pal::ChannelSwizzle::W };
break;
default:
retFmt = Pal::UndefinedSwizzledFormat;
break;
}
return retFmt;
}
} // GpuUtil