| 1 |
#if defined(WITH_RHI_RSX) |
| 2 |
|
| 3 |
#include "RsxShaderProgram.h" |
| 4 |
#include "RsxShaderUniforms.h" |
| 5 |
#include "RsxBufferObject.h" |
| 6 |
#include "RsxDevice.h" |
| 7 |
|
| 8 |
#include "../../../../Main.h" |
| 9 |
#include "../../../../Shaders/Generated/ShaderCompilerTypes.h" |
| 10 |
#include "../../../../Shaders/Generated/RsxGeneratedShaders.h" |
| 11 |
|
| 12 |
#include <algorithm> |
| 13 |
#include <cstdio> |
| 14 |
#include <cstdlib> |
| 15 |
#include <cstring> |
| 16 |
|
| 17 |
#include <rsx/rsx.h> |
| 18 |
#include <rsx/commands.h> |
| 19 |
|
| 20 |
namespace nCine::RHI::RSX |
| 21 |
{ |
| 22 |
std::uint32_t RsxShaderProgram::_nextHandle = 1; |
| 23 |
|
| 24 |
RsxShaderProgram::RsxShaderProgram() |
| 25 |
: RsxShaderProgram(QueryPhase::Immediate) |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
RsxShaderProgram::RsxShaderProgram(QueryPhase queryPhase) |
| 30 |
: _handle(_nextHandle++), _status(Status::NotLinked), _introspection(Introspection::Enabled), |
| 31 |
_queryPhase(queryPhase), _batchSize(std::uint32_t(DefaultBatchSize)), _shouldLogOnErrors(true), |
| 32 |
_reflection(nullptr), _programName(nullptr), _variantName(nullptr), |
| 33 |
_vertexProgram(nullptr), _fragmentProgram(nullptr), _vertexUcode(nullptr), |
| 34 |
_uniformsSize(0), _uniformBlocksSize(0) |
| 35 |
{ |
| 36 |
} |
| 37 |
|
| 38 |
RsxShaderProgram::RsxShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection, QueryPhase queryPhase) |
| 39 |
: RsxShaderProgram(queryPhase) |
| 40 |
{ |
| 41 |
static_cast<void>(vertexFile); |
| 42 |
static_cast<void>(fragmentFile); |
| 43 |
_introspection = introspection; |
| 44 |
} |
| 45 |
|
| 46 |
RsxShaderProgram::RsxShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection) |
| 47 |
: RsxShaderProgram(vertexFile, fragmentFile, introspection, QueryPhase::Immediate) |
| 48 |
{ |
| 49 |
} |
| 50 |
|
| 51 |
RsxShaderProgram::RsxShaderProgram(StringView vertexFile, StringView fragmentFile) |
| 52 |
: RsxShaderProgram(vertexFile, fragmentFile, Introspection::Enabled, QueryPhase::Immediate) |
| 53 |
{ |
| 54 |
} |
| 55 |
|
| 56 |
RsxShaderProgram::~RsxShaderProgram() |
| 57 |
{ |
| 58 |
// The device tracks the bound program by pointer, so it has to forget this one before it goes |
| 59 |
RsxDevice::OnProgramDestroyed(this); |
| 60 |
if (_fragmentUcode.IsValid()) { |
| 61 |
RsxDevice::RetireBlock(_fragmentUcode); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
bool RsxShaderProgram::IsLinked() const |
| 66 |
{ |
| 67 |
return (_status == Status::Linked || _status == Status::LinkedWithDeferredQueries || |
| 68 |
_status == Status::LinkedWithIntrospection); |
| 69 |
} |
| 70 |
|
| 71 |
bool RsxShaderProgram::AttachShaderFromFile(ShaderStage stage, StringView filename) |
| 72 |
{ |
| 73 |
// Stage sources play no part on this backend: the microcode comes from the generated table, keyed by |
| 74 |
// the program identity. Accepted so the shared loading code needs no PS3 arm. |
| 75 |
static_cast<void>(stage); |
| 76 |
static_cast<void>(filename); |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
bool RsxShaderProgram::AttachShaderFromString(ShaderStage stage, StringView string) |
| 81 |
{ |
| 82 |
static_cast<void>(stage); |
| 83 |
static_cast<void>(string); |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
bool RsxShaderProgram::AttachShaderFromStrings(ShaderStage stage, ArrayView<const StringView> strings) |
| 88 |
{ |
| 89 |
static_cast<void>(stage); |
| 90 |
static_cast<void>(strings); |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
bool RsxShaderProgram::AttachShaderFromStringsAndFile(ShaderStage stage, ArrayView<const StringView> strings, StringView filename) |
| 95 |
{ |
| 96 |
static_cast<void>(stage); |
| 97 |
static_cast<void>(strings); |
| 98 |
static_cast<void>(filename); |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
bool RsxShaderProgram::ResolveGeneratedProgram() |
| 103 |
{ |
| 104 |
const char* programName = (_programName != nullptr ? _programName : ""); |
| 105 |
const char* variantName = (_variantName != nullptr ? _variantName : ""); |
| 106 |
|
| 107 |
const GeneratedRsxShader* generated = FindGeneratedRsxShader(programName, variantName); |
| 108 |
if (generated == nullptr) { |
| 109 |
// There is no runtime fallback to reach for: the console has no shader compiler, so a variant |
| 110 |
// missing from the table is one whose Cg the vp40/fp40 profiles could not express when the table |
| 111 |
// was generated. That is a build-time fact, so it is reported plainly and once. |
| 112 |
if (_shouldLogOnErrors) { |
| 113 |
LOGE("Shader \"{}\"{}{}{} has no compiled RSX microcode; it was not accepted by the vp40/fp40 " |
| 114 |
"profiles when the shader table was generated", programName, |
| 115 |
variantName[0] != '\0' ? " (variant \"" : "", variantName, variantName[0] != '\0' ? "\")" : ""); |
| 116 |
} |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
_vertexProgram = reinterpret_cast<const rsxVertexProgram*>(generated->VertexProgram); |
| 121 |
_fragmentProgram = reinterpret_cast<const rsxFragmentProgram*>(generated->FragmentProgram); |
| 122 |
|
| 123 |
// The vertex microcode is fetched by the command processor out of wherever it is given, so the copy |
| 124 |
// in the executable serves directly; the fragment half is not so lucky (see UploadFragmentUcode()) |
| 125 |
std::uint32_t ucodeSize = 0; |
| 126 |
void* ucode = nullptr; |
| 127 |
rsxVertexProgramGetUCode(_vertexProgram, &ucode, &ucodeSize); |
| 128 |
_vertexUcode = ucode; |
| 129 |
return (_vertexUcode != nullptr); |
| 130 |
} |
| 131 |
|
| 132 |
bool RsxShaderProgram::UploadFragmentUcode() |
| 133 |
{ |
| 134 |
std::uint32_t ucodeSize = 0; |
| 135 |
void* ucode = nullptr; |
| 136 |
rsxFragmentProgramGetUCode(_fragmentProgram, &ucode, &ucodeSize); |
| 137 |
if (ucode == nullptr || ucodeSize == 0) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
// Local memory is mandatory - the fragment engine cannot fetch microcode from main memory - and the |
| 142 |
// copy is per-program rather than shared because the RSX patches fragment constants into the |
| 143 |
// microcode itself, so two programs over one blob would overwrite each other's uniforms |
| 144 |
_fragmentUcode = RsxVram::AllocFragmentProgram(ucodeSize); |
| 145 |
if (!_fragmentUcode.IsValid()) { |
| 146 |
if (_shouldLogOnErrors) { |
| 147 |
LOGE("Cannot allocate {} bytes of local memory for the fragment microcode of shader \"{}\"", |
| 148 |
ucodeSize, _programName != nullptr ? _programName : "<unnamed>"); |
| 149 |
} |
| 150 |
return false; |
| 151 |
} |
| 152 |
std::memcpy(_fragmentUcode.Base, ucode, ucodeSize); |
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
bool RsxShaderProgram::Link(Introspection introspection) |
| 157 |
{ |
| 158 |
_introspection = introspection; |
| 159 |
|
| 160 |
if (_reflection == nullptr) { |
| 161 |
if (_shouldLogOnErrors) { |
| 162 |
LOGE("Cannot link a shader program without offline reflection on this backend"); |
| 163 |
} |
| 164 |
_status = Status::CompilationFailed; |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
if (!ResolveGeneratedProgram() || !UploadFragmentUcode()) { |
| 169 |
_status = Status::CompilationFailed; |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
return FinalizeAfterLinking(introspection); |
| 174 |
} |
| 175 |
|
| 176 |
bool RsxShaderProgram::FinalizeAfterLinking(Introspection introspection) |
| 177 |
{ |
| 178 |
_introspection = introspection; |
| 179 |
_status = Status::Linked; |
| 180 |
|
| 181 |
if (_introspection == Introspection::Disabled || _reflection == nullptr) { |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
_uniforms.clear(); |
| 186 |
_uniformBlocks.clear(); |
| 187 |
_attributes.clear(); |
| 188 |
_uniformsSize = 0; |
| 189 |
_uniformBlocksSize = 0; |
| 190 |
_vertexUniformSlots.clear(); |
| 191 |
_fragmentUniformSlots.clear(); |
| 192 |
_vertexBlockUploads.clear(); |
| 193 |
_fragmentBlockUploads.clear(); |
| 194 |
_fragmentSamplers.clear(); |
| 195 |
_stageAttributes.clear(); |
| 196 |
_resolvedUniforms.clear(); |
| 197 |
_instanceArray = RsxInstanceArray{}; |
| 198 |
|
| 199 |
ImportReflection(_introspection); |
| 200 |
ResolveStageInterface(); |
| 201 |
ResolveConstants(); |
| 202 |
|
| 203 |
_status = Status::LinkedWithIntrospection; |
| 204 |
return true; |
| 205 |
} |
| 206 |
|
| 207 |
void RsxShaderProgram::ImportReflection(Introspection introspection) |
| 208 |
{ |
| 209 |
const ShaderCompiler::ProgramVariant& reflection = *_reflection; |
| 210 |
std::int32_t nextLocation = 0; |
| 211 |
|
| 212 |
// Loose uniforms - samplers are kept in a separate reflection list but treated as loose uniforms here |
| 213 |
for (std::size_t i = 0; i < reflection.UniformCount; i++) { |
| 214 |
const ShaderCompiler::Uniform& u = reflection.Uniforms[i]; |
| 215 |
_uniforms.emplace_back(this, u.Name, u.Type, std::int32_t(u.ArraySize), nextLocation++); |
| 216 |
_uniformsSize += _uniforms.back().GetMemorySize(); |
| 217 |
} |
| 218 |
for (std::size_t i = 0; i < reflection.TextureCount; i++) { |
| 219 |
const ShaderCompiler::TextureBinding& t = reflection.Textures[i]; |
| 220 |
_uniforms.emplace_back(this, t.Name, ShaderCompiler::UniformType::Sampler2D, 1, nextLocation++); |
| 221 |
_uniformsSize += _uniforms.back().GetMemorySize(); |
| 222 |
} |
| 223 |
|
| 224 |
_uniformBlocks.reserve(reflection.BlockCount); |
| 225 |
for (std::size_t i = 0; i < reflection.BlockCount; i++) { |
| 226 |
const ShaderCompiler::UniformBlock& b = reflection.Blocks[i]; |
| 227 |
|
| 228 |
// A BATCH_SIZE-sized instance array uses the explicitly set batch size, or the same fallback the |
| 229 |
// generated microcode baked in - which on this backend is the RSX's constant-register budget |
| 230 |
// rather than the 64 KB the desktop backends assume (see RsxRhiCapabilities) |
| 231 |
std::uint32_t effectiveBatchSize = 0; |
| 232 |
std::uint32_t dataSize = b.BaseSize; |
| 233 |
if (b.InstanceStride > 0) { |
| 234 |
effectiveBatchSize = (_batchSize != std::uint32_t(DefaultBatchSize) && _batchSize > 0) |
| 235 |
? _batchSize : std::uint32_t(RsxDevice::GetMaxUniformBlockSize()) / b.InstanceStride; |
| 236 |
// Whatever the constant budget would allow, the batch cannot exceed what the device's |
| 237 |
// batched corner stream covers - that stream is what supplies the instance index, so a |
| 238 |
// larger batch would read past it. The generated microcode bakes in the same bound. |
| 239 |
if (effectiveBatchSize > RsxDevice::MaxBatchSize) { |
| 240 |
// Clamping here only bounds what this program advertises; it cannot bound what the engine |
| 241 |
// collects. That comes from the batch size the engine derived and passed to SetBatchSize(), |
| 242 |
// which is capped by the IntValues::MaxBatchSize this backend publishes - so reaching here |
| 243 |
// means the capability did not make it through, and the batcher will write past the end of |
| 244 |
// the instance array. Say so loudly rather than clamping in silence. |
| 245 |
LOGE("Uniform block \"{}\" wants a batch of {} but the backend caps it at {} - the " |
| 246 |
"IntValues::MaxBatchSize capability was not applied, and the batcher will overrun the instance array", |
| 247 |
b.Name, effectiveBatchSize, RsxDevice::MaxBatchSize); |
| 248 |
effectiveBatchSize = RsxDevice::MaxBatchSize; |
| 249 |
} |
| 250 |
dataSize += b.InstanceStride * effectiveBatchSize; |
| 251 |
} |
| 252 |
|
| 253 |
const std::uint32_t blockIndex = std::uint32_t(_uniformBlocks.size()); |
| 254 |
_uniformBlocks.emplace_back(blockIndex, b.Name, std::int32_t(dataSize)); |
| 255 |
RsxUniformBlock& block = _uniformBlocks.back(); |
| 256 |
_uniformBlocksSize += block.GetSize(); |
| 257 |
|
| 258 |
if (introspection != Introspection::NoUniformsInBlocks) { |
| 259 |
block._members.reserve(b.MemberCount); |
| 260 |
for (std::size_t j = 0; j < b.MemberCount; j++) { |
| 261 |
const ShaderCompiler::BlockMember& m = b.Members[j]; |
| 262 |
if (m.Type == ShaderCompiler::UniformType::Struct) { |
| 263 |
// Struct aggregates are never read by name; only flat leaf members matter |
| 264 |
continue; |
| 265 |
} |
| 266 |
RsxUniform member; |
| 267 |
member.SetName(m.Name); |
| 268 |
member._type = m.Type; |
| 269 |
member._size = (m.ArraySize == ShaderCompiler::SymbolicArraySize) |
| 270 |
? std::int32_t(effectiveBatchSize) |
| 271 |
: (m.ArraySize > 0 ? std::int32_t(m.ArraySize) : 1); |
| 272 |
member._blockIndex = std::int32_t(blockIndex); |
| 273 |
member._offset = std::int32_t(m.Offset); |
| 274 |
member._owner = this; |
| 275 |
block._members.push_back(member); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
for (std::size_t i = 0; i < reflection.AttributeCount; i++) { |
| 281 |
const ShaderCompiler::Attribute& a = reflection.Attributes[i]; |
| 282 |
const std::int32_t location = (a.Location >= 0 ? a.Location : std::int32_t(i)); |
| 283 |
_attributes.emplace_back(a.Name, a.Type, location); |
| 284 |
_vertexFormat[std::uint32_t(location)].Init(std::uint32_t(location), |
| 285 |
std::int32_t(UniformTypeInfo::ComponentCount(a.Type)), 0); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
void RsxShaderProgram::ResolveStageInterface() |
| 290 |
{ |
| 291 |
if (_vertexProgram == nullptr) { |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
// The compiled stage's own attribute table, not the reflection's - see RsxStageAttribute for why the |
| 296 |
// two disagree for every sprite shader |
| 297 |
const std::uint16_t attribCount = rsxVertexProgramGetNumAttrib(_vertexProgram); |
| 298 |
const rsxProgramAttrib* attribs = rsxVertexProgramGetAttribs(_vertexProgram); |
| 299 |
const char* base = reinterpret_cast<const char*>(_vertexProgram); |
| 300 |
for (std::uint16_t i = 0; i < attribCount; i++) { |
| 301 |
const char* name = base + attribs[i].name_off; |
| 302 |
// An attribute the entry point receives through an input struct is named "<struct>.<member>"; |
| 303 |
// the engine knows it by the member alone |
| 304 |
if (const char* lastDot = std::strrchr(name, '.')) { |
| 305 |
name = lastDot + 1; |
| 306 |
} |
| 307 |
_stageAttributes.push_back({name, std::uint8_t(attribs[i].index)}); |
| 308 |
} |
| 309 |
|
| 310 |
// Map every location the pipeline may bind to the register the compiled stage reads it from. The two |
| 311 |
// numbering schemes are independent - the reflection's is the engine's, the other is cgcomp's - and |
| 312 |
// binding a stream to the reflection's number leaves the stage fetching a register nothing filled, |
| 313 |
// which draws nothing at all. Only the sprite shaders' synthetic corner attributes were resolved |
| 314 |
// this way before, so the shaders that do supply real per-vertex data (the tile-layer meshes) were |
| 315 |
// the ones left reading empty registers. |
| 316 |
for (std::uint32_t i = 0; i < MaxAttributeLocations; i++) { |
| 317 |
_attributeRegisterByLocation[i] = -1; |
| 318 |
} |
| 319 |
if (_reflection != nullptr) { |
| 320 |
for (std::size_t i = 0; i < _reflection->AttributeCount; i++) { |
| 321 |
const ShaderCompiler::Attribute& a = _reflection->Attributes[i]; |
| 322 |
const std::int32_t location = (a.Location >= 0 ? a.Location : std::int32_t(i)); |
| 323 |
if (location < 0 || std::uint32_t(location) >= MaxAttributeLocations) { |
| 324 |
continue; |
| 325 |
} |
| 326 |
const std::int32_t reg = GetStageAttributeRegister(a.Name); |
| 327 |
if (reg >= 0) { |
| 328 |
_attributeRegisterByLocation[location] = std::int8_t(reg); |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
if (_fragmentProgram == nullptr || _reflection == nullptr) { |
| 334 |
return; |
| 335 |
} |
| 336 |
|
| 337 |
// Samplers: the reflection assigns the engine unit, the microcode reports the hardware one. They |
| 338 |
// normally agree (the Cg emitter tags each sampler with the reflection's TEXUNIT<n>), but resolving |
| 339 |
// both means a mismatch shows up as a warning rather than as a shader sampling the wrong texture. |
| 340 |
for (std::size_t i = 0; i < _reflection->TextureCount; i++) { |
| 341 |
const ShaderCompiler::TextureBinding& t = _reflection->Textures[i]; |
| 342 |
const std::int32_t engineUnit = (t.Unit >= 0 ? t.Unit : std::int32_t(i)); |
| 343 |
// Through GetConst() rather than the GetConstIndex() rsx_program.h also declares: librsx declares |
| 344 |
// that one but implements no such symbol, so calling it does not link |
| 345 |
const rsxProgramConst* hardwareConst = rsxFragmentProgramGetConst(_fragmentProgram, t.Name); |
| 346 |
const std::int32_t hardwareIndex = (hardwareConst != nullptr ? std::int32_t(hardwareConst->index) : -1); |
| 347 |
const std::uint32_t hardwareUnit = (hardwareIndex >= 0 ? std::uint32_t(hardwareIndex) : std::uint32_t(engineUnit)); |
| 348 |
if (hardwareIndex >= 0 && hardwareUnit != std::uint32_t(engineUnit)) { |
| 349 |
LOGW("Shader \"{}\" samples \"{}\" from texture unit {} but the pipeline binds it to {}", |
| 350 |
_programName != nullptr ? _programName : "<unnamed>", t.Name, hardwareUnit, engineUnit); |
| 351 |
} |
| 352 |
_fragmentSamplers.push_back({t.Name, std::uint32_t(engineUnit), hardwareUnit}); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
void RsxShaderProgram::ResolveConstants() |
| 357 |
{ |
| 358 |
if (_reflection == nullptr) { |
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
// Loose uniforms. A name that resolves in neither stage is not an error - a shader is free to ignore |
| 363 |
// a uniform the engine offers - so it is simply left out of both slot lists and never uploaded. |
| 364 |
for (std::size_t i = 0; i < _reflection->UniformCount; i++) { |
| 365 |
const ShaderCompiler::Uniform& u = _reflection->Uniforms[i]; |
| 366 |
const std::uint32_t byteSize = UniformTypeInfo::ComponentCount(u.Type) * 4u * |
| 367 |
(u.ArraySize > 0 ? u.ArraySize : 1u); |
| 368 |
|
| 369 |
if (_vertexProgram != nullptr) { |
| 370 |
if (const rsxProgramConst* param = rsxVertexProgramGetConst(_vertexProgram, u.Name)) { |
| 371 |
_vertexUniformSlots.push_back({u.Name, param, byteSize}); |
| 372 |
} |
| 373 |
} |
| 374 |
if (_fragmentProgram != nullptr) { |
| 375 |
if (const rsxProgramConst* param = rsxFragmentProgramGetConst(_fragmentProgram, u.Name)) { |
| 376 |
_fragmentUniformSlots.push_back({u.Name, param, byteSize}); |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
// Uniform block members, hoisted to top-level constants by the Cg emitter |
| 382 |
for (std::size_t blockIndex = 0; blockIndex < _reflection->BlockCount; blockIndex++) { |
| 383 |
const ShaderCompiler::UniformBlock& b = _reflection->Blocks[blockIndex]; |
| 384 |
bool reached = false; |
| 385 |
|
| 386 |
for (std::size_t j = 0; j < b.MemberCount; j++) { |
| 387 |
const ShaderCompiler::BlockMember& m = b.Members[j]; |
| 388 |
if (m.Type == ShaderCompiler::UniformType::Struct) { |
| 389 |
// The batched instance array: the reflection describes it as one opaque struct, so its |
| 390 |
// field layout is recovered from the compiled microcode instead (see ResolveInstanceArray) |
| 391 |
if (m.ArraySize == ShaderCompiler::SymbolicArraySize && b.InstanceStride > 0) { |
| 392 |
std::uint32_t elements = (_batchSize != std::uint32_t(DefaultBatchSize) && _batchSize > 0 |
| 393 |
? _batchSize : std::uint32_t(RsxDevice::GetMaxUniformBlockSize()) / b.InstanceStride); |
| 394 |
if (elements > RsxDevice::MaxBatchSize) { |
| 395 |
elements = RsxDevice::MaxBatchSize; |
| 396 |
} |
| 397 |
ResolveInstanceArray(std::uint32_t(blockIndex), m.Name, b.InstanceStride, elements); |
| 398 |
reached |= _instanceArray.Valid; |
| 399 |
} |
| 400 |
continue; |
| 401 |
} |
| 402 |
|
| 403 |
const bool batched = (m.ArraySize == ShaderCompiler::SymbolicArraySize); |
| 404 |
static_cast<void>(batched); |
| 405 |
const std::uint32_t elementCount = (m.ArraySize > 0 ? m.ArraySize : 1u); |
| 406 |
const std::uint32_t componentBytes = UniformTypeInfo::ComponentCount(m.Type) * 4u; |
| 407 |
|
| 408 |
RsxBlockUpload upload; |
| 409 |
upload.Param = nullptr; |
| 410 |
upload.BindingIndex = std::uint32_t(blockIndex); |
| 411 |
upload.SourceOffset = m.Offset; |
| 412 |
upload.MaxByteSize = componentBytes * elementCount; |
| 413 |
upload.Name = m.Name; |
| 414 |
|
| 415 |
if (_vertexProgram != nullptr) { |
| 416 |
if (const rsxProgramConst* param = rsxVertexProgramGetConst(_vertexProgram, m.Name)) { |
| 417 |
upload.Param = param; |
| 418 |
_vertexBlockUploads.push_back(upload); |
| 419 |
reached = true; |
| 420 |
} |
| 421 |
} |
| 422 |
if (_fragmentProgram != nullptr) { |
| 423 |
if (const rsxProgramConst* param = rsxFragmentProgramGetConst(_fragmentProgram, m.Name)) { |
| 424 |
upload.Param = param; |
| 425 |
_fragmentBlockUploads.push_back(upload); |
| 426 |
reached = true; |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
// A uniform block that resolved in neither stage means every draw of this program reads |
| 432 |
// undefined instance data - the failure mode that looks like garbled sprites rather than an error |
| 433 |
if (!reached) { |
| 434 |
LOGW("Shader \"{}\" uniform block \"{}\" reaches neither stage; its draws will read undefined uniforms", |
| 435 |
_programName != nullptr ? _programName : "<unnamed>", b.Name); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
void RsxShaderProgram::ResolveInstanceArray(std::uint32_t blockIndex, const char* memberName, |
| 441 |
std::uint32_t sourceStride, std::uint32_t elementCount) |
| 442 |
{ |
| 443 |
_instanceArray = RsxInstanceArray{}; |
| 444 |
if (_vertexProgram == nullptr || memberName == nullptr || sourceStride == 0 || elementCount == 0) { |
| 445 |
return; |
| 446 |
} |
| 447 |
|
| 448 |
// cgcomp names each element's fields individually - "instances[0].modelMatrix", "instances[1]..." - |
| 449 |
// so element 0's entries are what describe the layout, and the rest only confirm the element stride |
| 450 |
char prefix[RsxUniform::MaxNameLength]; |
| 451 |
const std::int32_t prefixLength = std::snprintf(prefix, sizeof(prefix), "%s[0].", memberName); |
| 452 |
if (prefixLength <= 0 || std::size_t(prefixLength) >= sizeof(prefix)) { |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
const std::uint16_t constCount = rsxVertexProgramGetNumConst(_vertexProgram); |
| 457 |
const rsxProgramConst* consts = rsxVertexProgramGetConsts(_vertexProgram); |
| 458 |
const char* base = reinterpret_cast<const char*>(_vertexProgram); |
| 459 |
|
| 460 |
// One entry per REGISTER is emitted for a multi-register field (a mat4 appears four times under the |
| 461 |
// same name), so the lowest index per distinct name is the field's base and the count is its width |
| 462 |
struct FieldScratch { const char* Name; std::uint32_t FirstRegister; std::uint32_t RegisterCount; std::uint8_t Type; }; |
| 463 |
SmallVector<FieldScratch, 8> fields; |
| 464 |
std::uint32_t elementBase = 0xFFFFFFFFu; |
| 465 |
|
| 466 |
for (std::uint16_t i = 0; i < constCount; i++) { |
| 467 |
const char* name = base + consts[i].name_off; |
| 468 |
if (std::strncmp(name, prefix, std::size_t(prefixLength)) != 0) { |
| 469 |
continue; |
| 470 |
} |
| 471 |
const char* fieldName = name + prefixLength; |
| 472 |
const std::uint32_t reg = consts[i].index; |
| 473 |
if (reg < elementBase) { |
| 474 |
elementBase = reg; |
| 475 |
} |
| 476 |
|
| 477 |
bool found = false; |
| 478 |
for (FieldScratch& f : fields) { |
| 479 |
if (std::strcmp(f.Name, fieldName) == 0) { |
| 480 |
if (reg < f.FirstRegister) { |
| 481 |
f.FirstRegister = reg; |
| 482 |
} |
| 483 |
f.RegisterCount++; |
| 484 |
found = true; |
| 485 |
break; |
| 486 |
} |
| 487 |
} |
| 488 |
if (!found) { |
| 489 |
fields.push_back({fieldName, reg, 1, consts[i].type}); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
if (fields.empty() || elementBase == 0xFFFFFFFFu) { |
| 494 |
LOGW("Shader \"{}\" declares a batched instance array, but its microcode has no \"{}[0].*\" constants", |
| 495 |
_programName != nullptr ? _programName : "<unnamed>", memberName); |
| 496 |
return; |
| 497 |
} |
| 498 |
|
| 499 |
// Sort by register so the std140 walk below follows the shader's own field order |
| 500 |
std::sort(fields.begin(), fields.end(), [](const FieldScratch& a, const FieldScratch& b) { |
| 501 |
return a.FirstRegister < b.FirstRegister; |
| 502 |
}); |
| 503 |
|
| 504 |
// The engine's side is std140, whose packing the offline reflection does not spell out for a struct. |
| 505 |
// It is recovered here from each field's register width, which is what distinguishes the cases that |
| 506 |
// matter: a mat4 (four registers, four aligned vec4s) from a vec4, and both from the scalars std140 |
| 507 |
// packs together but the register file does not. |
| 508 |
// The width has to come from the field's declared TYPE, not from how many registers it occupies: |
| 509 |
// the register file gives a vec2 and a trailing float one register each, while std140 aligns the |
| 510 |
// vec2 to 8 and packs the float into the same 16-byte slot behind it. Deriving the offsets from |
| 511 |
// registers would put every field after the first scalar 8 bytes late, for the whole batch. |
| 512 |
std::uint32_t sourceOffset = 0; |
| 513 |
for (const FieldScratch& f : fields) { |
| 514 |
std::uint32_t alignment = 16, size = 16, copyBytes = 16; |
| 515 |
switch (f.Type) { |
| 516 |
case PARAM_FLOAT: alignment = 4; size = 4; copyBytes = 4; break; |
| 517 |
case PARAM_FLOAT1: alignment = 4; size = 4; copyBytes = 4; break; |
| 518 |
case PARAM_FLOAT2: alignment = 8; size = 8; copyBytes = 8; break; |
| 519 |
case PARAM_FLOAT3: alignment = 16; size = 12; copyBytes = 12; break; |
| 520 |
case PARAM_FLOAT4: alignment = 16; size = 16; copyBytes = 16; break; |
| 521 |
case PARAM_FLOAT3x3: alignment = 16; size = 48; copyBytes = 48; break; |
| 522 |
case PARAM_FLOAT4x4: alignment = 16; size = 64; copyBytes = 64; break; |
| 523 |
default: |
| 524 |
// Fall back to the register width, which is right for everything vec4-shaped |
| 525 |
alignment = 16; size = f.RegisterCount * 16; copyBytes = size; |
| 526 |
break; |
| 527 |
} |
| 528 |
sourceOffset = (sourceOffset + alignment - 1) & ~(alignment - 1); |
| 529 |
_instanceArray.Fields.push_back({sourceOffset, f.FirstRegister - elementBase, f.RegisterCount, copyBytes, f.Name}); |
| 530 |
sourceOffset += size; |
| 531 |
} |
| 532 |
// std140 rounds the whole element up to its largest member alignment, which is always 16 here |
| 533 |
sourceOffset = (sourceOffset + 15u) & ~15u; |
| 534 |
|
| 535 |
// Element stride from two consecutive elements, which is authoritative; the field walk above only |
| 536 |
// has to agree with the engine's std140 stride, and a disagreement means the two sides would drift |
| 537 |
// element by element - so it is reported rather than silently mis-rendered |
| 538 |
std::uint32_t registersPerElement = 0; |
| 539 |
{ |
| 540 |
char nextPrefix[RsxUniform::MaxNameLength]; |
| 541 |
const std::int32_t nextLength = std::snprintf(nextPrefix, sizeof(nextPrefix), "%s[1].", memberName); |
| 542 |
if (nextLength > 0 && std::size_t(nextLength) < sizeof(nextPrefix)) { |
| 543 |
std::uint32_t nextBase = 0xFFFFFFFFu; |
| 544 |
for (std::uint16_t i = 0; i < constCount; i++) { |
| 545 |
const char* name = base + consts[i].name_off; |
| 546 |
if (std::strncmp(name, nextPrefix, std::size_t(nextLength)) == 0 && consts[i].index < nextBase) { |
| 547 |
nextBase = consts[i].index; |
| 548 |
} |
| 549 |
} |
| 550 |
if (nextBase != 0xFFFFFFFFu && nextBase > elementBase) { |
| 551 |
registersPerElement = nextBase - elementBase; |
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
if (registersPerElement == 0) { |
| 556 |
registersPerElement = sourceOffset / 16; |
| 557 |
} |
| 558 |
|
| 559 |
if (sourceOffset != sourceStride) { |
| 560 |
LOGW("Shader \"{}\" batched element is {} bytes by the microcode's field layout but {} by the " |
| 561 |
"engine's; instance data past the first element will be misread", |
| 562 |
_programName != nullptr ? _programName : "<unnamed>", sourceOffset, sourceStride); |
| 563 |
} |
| 564 |
|
| 565 |
_instanceArray.Valid = true; |
| 566 |
_instanceArray.BindingIndex = blockIndex; |
| 567 |
_instanceArray.BaseRegister = elementBase; |
| 568 |
_instanceArray.RegistersPerElement = registersPerElement; |
| 569 |
_instanceArray.SourceStride = sourceStride; |
| 570 |
_instanceArray.ElementCount = elementCount; |
| 571 |
|
| 572 |
LOGD("Shader \"{}\" batched instance array: {} fields, {} registers per element, {} elements", |
| 573 |
_programName != nullptr ? _programName : "<unnamed>", _instanceArray.Fields.size(), |
| 574 |
registersPerElement, elementCount); |
| 575 |
} |
| 576 |
|
| 577 |
void RsxShaderProgram::Use() |
| 578 |
{ |
| 579 |
RsxDevice::BindProgram(this); |
| 580 |
} |
| 581 |
|
| 582 |
bool RsxShaderProgram::HasAttribute(const char* name) const |
| 583 |
{ |
| 584 |
for (const RsxAttribute& attribute : _attributes) { |
| 585 |
if (std::strcmp(attribute.GetName(), name) == 0) { |
| 586 |
return true; |
| 587 |
} |
| 588 |
} |
| 589 |
return false; |
| 590 |
} |
| 591 |
|
| 592 |
bool RsxShaderProgram::IsIntegerAttribute(const char* name) const |
| 593 |
{ |
| 594 |
for (const RsxAttribute& attribute : _attributes) { |
| 595 |
if (std::strcmp(attribute.GetName(), name) != 0) { |
| 596 |
continue; |
| 597 |
} |
| 598 |
switch (attribute.GetType()) { |
| 599 |
case ShaderCompiler::UniformType::Int: |
| 600 |
case ShaderCompiler::UniformType::IVec2: |
| 601 |
case ShaderCompiler::UniformType::IVec3: |
| 602 |
case ShaderCompiler::UniformType::IVec4: |
| 603 |
case ShaderCompiler::UniformType::UInt: |
| 604 |
case ShaderCompiler::UniformType::UVec2: |
| 605 |
case ShaderCompiler::UniformType::UVec3: |
| 606 |
case ShaderCompiler::UniformType::UVec4: |
| 607 |
return true; |
| 608 |
default: |
| 609 |
return false; |
| 610 |
} |
| 611 |
} |
| 612 |
return false; |
| 613 |
} |
| 614 |
|
| 615 |
RsxVertexFormat::Attribute* RsxShaderProgram::GetAttribute(const char* name) |
| 616 |
{ |
| 617 |
for (const RsxAttribute& attribute : _attributes) { |
| 618 |
if (std::strcmp(attribute.GetName(), name) == 0) { |
| 619 |
return &_vertexFormat[std::uint32_t(attribute.GetLocation())]; |
| 620 |
} |
| 621 |
} |
| 622 |
return nullptr; |
| 623 |
} |
| 624 |
|
| 625 |
std::int32_t RsxShaderProgram::GetStageAttributeRegister(const char* name) const |
| 626 |
{ |
| 627 |
for (const RsxStageAttribute& attribute : _stageAttributes) { |
| 628 |
if (std::strcmp(attribute.Name, name) == 0) { |
| 629 |
return std::int32_t(attribute.RegIndex); |
| 630 |
} |
| 631 |
} |
| 632 |
return -1; |
| 633 |
} |
| 634 |
|
| 635 |
void RsxShaderProgram::DefineVertexFormat(const RsxBufferObject* vbo, const RsxBufferObject* ibo, std::uint32_t vboOffset) |
| 636 |
{ |
| 637 |
for (std::uint32_t i = 0; i < _vertexFormat.GetAttributeCount(); i++) { |
| 638 |
RsxVertexFormat::Attribute& attribute = _vertexFormat[i]; |
| 639 |
if (attribute.IsEnabled()) { |
| 640 |
attribute.setVbo(vbo); |
| 641 |
attribute.SetBaseOffset(vboOffset); |
| 642 |
} |
| 643 |
} |
| 644 |
_vertexFormat.SetIbo(ibo); |
| 645 |
} |
| 646 |
|
| 647 |
void RsxShaderProgram::Reset() |
| 648 |
{ |
| 649 |
_uniforms.clear(); |
| 650 |
_uniformBlocks.clear(); |
| 651 |
_attributes.clear(); |
| 652 |
_uniformsSize = 0; |
| 653 |
_uniformBlocksSize = 0; |
| 654 |
_vertexUniformSlots.clear(); |
| 655 |
_fragmentUniformSlots.clear(); |
| 656 |
_vertexBlockUploads.clear(); |
| 657 |
_fragmentBlockUploads.clear(); |
| 658 |
_fragmentSamplers.clear(); |
| 659 |
_stageAttributes.clear(); |
| 660 |
_resolvedUniforms.clear(); |
| 661 |
_vertexFormat.Reset(); |
| 662 |
|
| 663 |
if (_fragmentUcode.IsValid()) { |
| 664 |
RsxDevice::RetireBlock(_fragmentUcode); |
| 665 |
} |
| 666 |
_vertexProgram = nullptr; |
| 667 |
_fragmentProgram = nullptr; |
| 668 |
_vertexUcode = nullptr; |
| 669 |
_status = Status::NotLinked; |
| 670 |
} |
| 671 |
|
| 672 |
void RsxShaderProgram::SetObjectLabel(StringView label) |
| 673 |
{ |
| 674 |
static_cast<void>(label); |
| 675 |
} |
| 676 |
|
| 677 |
void RsxShaderProgram::SetResolvedUniform(const char* name, const std::uint8_t* data) |
| 678 |
{ |
| 679 |
for (auto& entry : _resolvedUniforms) { |
| 680 |
if (std::strcmp(entry.first, name) == 0) { |
| 681 |
entry.second = data; |
| 682 |
return; |
| 683 |
} |
| 684 |
} |
| 685 |
_resolvedUniforms.emplace_back(name, data); |
| 686 |
} |
| 687 |
|
| 688 |
const std::uint8_t* RsxShaderProgram::ResolveUniform(const char* name) const |
| 689 |
{ |
| 690 |
for (const auto& entry : _resolvedUniforms) { |
| 691 |
if (std::strcmp(entry.first, name) == 0) { |
| 692 |
return entry.second; |
| 693 |
} |
| 694 |
} |
| 695 |
return nullptr; |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
#endif |