-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathpalShaderLibrary.h
277 lines (249 loc) · 13.6 KB
/
palShaderLibrary.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
/*
***********************************************************************************************************************
*
* Copyright (c) 2014-2024 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.
*
**********************************************************************************************************************/
/**
***********************************************************************************************************************
* @file palShaderLibrary.h
* @brief Defines the Platform Abstraction Library (PAL) IShaderLibrary interface and related types.
***********************************************************************************************************************
*/
#pragma once
#include "pal.h"
#include "palDestroyable.h"
#include "palStringView.h"
#include "palSpan.h"
namespace Pal
{
struct GpuMemSubAllocInfo;
/// Common flags controlling creation of shader libraries.
union LibraryCreateFlags
{
struct
{
uint32 clientInternal : 1; ///< Internal library not created by the application.
uint32 isGraphics : 1; ///< Whether it is a graphics library
uint32 reserved : 30; ///< Reserved for future use.
};
uint32 u32All; ///< Flags packed as 32-bit uint.
};
/// Specifies properties about an indirect function belonging to a @ref IShaderLibrary object. Part of the input
/// structure to IDevice::CreateShaderLibrary().
struct ShaderLibraryFunctionInfo
{
#if PAL_CLIENT_INTERFACE_MAJOR_VERSION < 827
const char* pSymbolName; ///< ELF Symbol name for the associated function. Must not be null.
#else
Util::StringView<char> symbolName; ///< ELF Symbol name for the associated function.
#endif
gpusize gpuVirtAddr; ///< [out] GPU virtual address of the function. This is computed by PAL during
/// library creation.
};
/// Specifies a shader sub type / ShaderKind.
enum class ShaderSubType : uint32
{
Unknown = 0,
Traversal,
RayGeneration,
Intersection,
AnyHit,
ClosestHit,
Miss,
Callable,
LaunchKernel, ///< Raytracing launch kernel
Count
};
/// Specifies properties for creation of a compute @ref IShaderLibrary object. Input structure to
/// IDevice::CreateShaderLibrary().
struct ShaderLibraryCreateInfo
{
LibraryCreateFlags flags; ///< Library creation flags
const void* pCodeObject; ///< Pointer to code-object ELF binary implementing the Pipeline ABI interface.
/// The code-object ELF contains pre-compiled shaders, register values, and
/// additional metadata.
size_t codeObjectSize; ///< Size of code object in bytes.
#if PAL_CLIENT_INTERFACE_MAJOR_VERSION < 827
/// List of functions for PAL to compute virtual addresses for during library creation. These GPU addresses can
/// then be passed as shader arguments to a later dispatch operation to allow a compute pipeline's shaders to jump
/// to the corresponding function(s). This behaves similarly to a function pointer, but on the GPU. PAL will
/// guarantee that these GPU virtual addresses remain valid for the lifetime of the @ref IShaderLibrary object and
/// that the backing GPU memory will remain resident.
ShaderLibraryFunctionInfo* pFuncList;
uint32 funcCount; ///< Number of entries in the pFuncList array. Must be zero if pFuncList
/// is nullptr.
#endif
};
/// Reports properties of a compiled library.
struct LibraryInfo
{
PipelineHash internalLibraryHash; ///< 128-bit identifier extracted from this library's ELF binary, composed of
/// the state the compiler decided was appropriate to identify the compiled
/// library. The lower 64 bits are "stable"; the upper 64 bits are "unique".
};
/// Reports shader stats. Multiple bits set in the shader stage mask indicates that multiple shaders have been combined
/// due to HW support. The same information will be repeated for both the constituent shaders in this case.
struct ShaderLibStats
{
ShaderHash shaderHash; ///< Shader hash.
CommonShaderStats common; ///< The shader compilation parameters for this shader.
/// Maximum number of VGPRs the compiler was allowed to use for this shader. This limit will be the minimum
/// of any architectural restriction and any client-requested limit intended to increase the number of waves in
/// flight.
uint32 numAvailableVgprs;
/// Maximum number of SGPRs the compiler was allowed to use for this shader. This limit will be the minimum
/// of any architectural restriction and any client-requested limit intended to increase the number of waves in
/// flight.
uint32 numAvailableSgprs;
size_t isaSizeInBytes; ///< Size of the shader ISA disassembly for this shader.
PipelineHash palInternalLibraryHash; ///< Internal hash of the shader compilation data used by PAL.
uint32 stackFrameSizeInBytes; ///< Shader function stack frame size
ShaderSubType shaderSubType; ///< ShaderSubType / Shader Kind
CompilerStackSizes cpsStackSizes; ///< Stack used in Continuation
};
/**
***********************************************************************************************************************
* @interface IShaderLibrary
* @brief Object containing one or more shader functions stored in GPU memory. These shader functions are callable
* from the shaders contained within IPipeline objects.
*
* Before a pipeline which calls into this library is bound to a command buffer (using @ref ICmdBuffer::BindPipeline),
* the client must call @ref IPipeline::LinkWithLibraries() and specify this library in the list of linked libraries.
* Failure to comply with this requirement is an error and will result in undefined behavior.
*
* @see IDevice::CreateShaderLibrary()
* @see IPipeline::LinkWithLibraries()
***********************************************************************************************************************
*/
class IShaderLibrary : public IDestroyable
{
public:
/// Returns properties of this library and its corresponding shader functions.
///
/// @returns Property structure describing this library.
virtual const LibraryInfo& GetInfo() const = 0;
/// Returns a list of GPU memory allocations used by this library.
///
/// @param [in,out] pNumEntries Input value specifies the available size in pAllocInfoList; output value
/// reports the number of GPU memory allocations.
/// @param [out] pAllocInfoList If pAllocInfoList=nullptr, then pNumEntries is ignored on input. On output it
/// will reflect the number of allocations that make up this pipeline. If
/// pAllocInfoList!=nullptr, then on input pNumEntries is assumed to be the number
/// of entries in the pAllocInfoList array. On output, pNumEntries reflects the
/// number of entries in pAllocInfoList that are valid.
/// @returns Success if the allocation info was successfully written to the buffer.
/// + ErrorInvalidValue if the caller provides a buffer size that is different from the size needed.
/// + ErrorInvalidPointer if pNumEntries is nullptr.
virtual Result QueryAllocationInfo(
size_t* pNumEntries,
GpuMemSubAllocInfo* const pAllocInfoList) const = 0;
/// Obtains the binary code object for this library.
///
/// @param [in, out] pSize Represents the size of the shader ISA code.
///
/// @param [out] pBuffer If non-null, the library ELF is written in the buffer. If null, the size required
/// for the library ELF is given out in the location pSize.
///
/// @returns Success if the library binary was fetched successfully.
/// +ErrorUnavailable if the library binary was not fetched successfully.
virtual Result GetCodeObject(
uint32* pSize,
void* pBuffer) const = 0;
/// Returns the value of the associated arbitrary client data pointer.
/// Can be used to associate arbitrary data with a particular PAL object.
///
/// @returns Pointer to client data.
void* GetClientData() const { return m_pClientData; }
/// Sets the value of the associated arbitrary client data pointer.
/// Can be used to associate arbitrary data with a particular PAL object.
///
/// @param [in] pClientData A pointer to arbitrary client data.
void SetClientData(
void* pClientData)
{
m_pClientData = pClientData;
}
/// Obtains the compiled shader ISA code for the shader function specified.
///
/// @param [in] pShaderExportName The shader exported name
///
/// @param [in, out] pSize Represents the size of the shader ISA code.
///
/// @param [out] pBuffer If non-null, the shader ISA code is written in the buffer. If null, the size required
/// for the shader ISA is given out in the location pSize.
///
/// @returns Success if the shader ISA code was fetched successfully.
/// +ErrorUnavailable if the shader ISA code was not fetched successfully.
virtual Result GetShaderFunctionCode(
#if PAL_CLIENT_INTERFACE_MAJOR_VERSION >= 852
Util::StringView<char> shaderExportName,
#else
const char* pShaderExportName,
#endif
size_t* pSize,
void* pBuffer) const = 0;
/// Obtains the shader pre and post compilation stats/params for the specified shader.
///
/// @param [in] pShaderExportName The shader exported name
///
/// @param [out] pShaderStats Pointer to the ShaderStats structure which will be filled with the shader stats for
/// the shader stage mentioned in shaderType. This cannot be nullptr.
/// @param [in] getDisassemblySize If set to true performs disassembly on the shader binary code and reports the
/// size of the disassembly string in ShaderStats::isaSizeInBytes. Else reports 0.
/// @returns Success if the stats were successfully obtained for this shader, including the shader disassembly size.
/// +ErrorUnavailable if a wrong shader stage for this pipeline was specified, or if some internal error
/// occured.
virtual Result GetShaderFunctionStats(
Util::StringView<char> shaderExportName,
ShaderLibStats* pShaderStats) const = 0;
/// Returns the function list owned by this shader library
///
/// @returns A list of ShaderLibraryFunctionInfo.
virtual const Util::Span<const ShaderLibraryFunctionInfo> GetShaderLibFunctionInfos() const = 0;
#if PAL_CLIENT_INTERFACE_MAJOR_VERSION < 827
/// Returns the function list owned by this shader library
///
/// @returns A list of ShaderLibraryFunctionInfo if number of functions is not zero.
/// Null is number of functions is zero.
const ShaderLibraryFunctionInfo* GetShaderLibFunctionList() const { return GetShaderLibFunctionInfos().Data(); }
/// Returns the function count owned by this shader library
///
/// @returns function count
uint32 GetShaderLibFunctionCount() const { return static_cast<uint32>(GetShaderLibFunctionInfos().NumElements()); }
#endif
protected:
/// @internal Constructor. Prevent use of new operator on this interface. Client must create objects by explicitly
/// called the proper create method.
IShaderLibrary() : m_pClientData(nullptr) { }
/// @internal Destructor. Prevent use of delete operator on this interface. Client must destroy objects by
/// explicitly calling IDestroyable::Destroy() and is responsible for freeing the system memory allocated for the
/// object on their own.
virtual ~IShaderLibrary() { }
private:
/// @internal Client data pointer. This can have an arbitrary value and can be returned by calling GetClientData()
/// and set via SetClientData().
/// For non-top-layer objects, this will point to the layer above the current object.
void* m_pClientData;
IShaderLibrary(const IShaderLibrary&) = delete;
IShaderLibrary& operator=(const IShaderLibrary&) = delete;
};
} // Pal