| 1 |
#include "SwShaderProgram.h" |
| 2 |
#include "SwDevice.h" |
| 3 |
#include "../../RenderResources.h" |
| 4 |
|
| 5 |
#include <cstring> |
| 6 |
|
| 7 |
#include <Asserts.h> |
| 8 |
|
| 9 |
namespace nCine::RHI::Software |
| 10 |
{ |
| 11 |
namespace |
| 12 |
{ |
| 13 |
/** @brief One row of the exact-name effect table: a shader label and everything derived from it */ |
| 14 |
struct EffectMapping |
| 15 |
{ |
| 16 |
const char* Label; |
| 17 |
SwEffect Effect; |
| 18 |
// The dithering variant is baked into the shader name ("...Dither" is a separate program), |
| 19 |
// so the flag travels with the table entry instead of being re-derived by substring |
| 20 |
bool Dither; |
| 21 |
}; |
| 22 |
|
| 23 |
// Every shader label with a hand-written software effect, mapped by exact name. The label set is |
| 24 |
// closed: ContentResolver::CompileShader() bakes each variant into the name and RenderResources |
| 25 |
// registers the built-in defaults, so nothing else reaches SetObjectLabel(). Substring matching |
| 26 |
// made every shader rename a silent behavior change; a label absent from this table stays |
| 27 |
// Unknown and takes the offline-transpiled generated-fragment path (or a logged, skipped draw). |
| 28 |
constexpr EffectMapping EffectMappings[] = { |
| 29 |
// Default sprite programs registered by RenderResources; the mesh variants deliberately keep |
| 30 |
// the generated-fragment path |
| 31 |
{ "Sprite", SwEffect::DefaultSprite, false }, |
| 32 |
{ "Sprite_NoTexture", SwEffect::DefaultSpriteNoTexture, false }, |
| 33 |
{ "Batched_Sprites", SwEffect::DefaultBatchedSprites, false }, |
| 34 |
{ "Batched_Sprites_NoTexture", SwEffect::DefaultBatchedSpritesNoTexture, false }, |
| 35 |
|
| 36 |
// Precompiled shaders registered by ContentResolver::CompileShaders(). The plain and water |
| 37 |
// compositor variants share the base composite in C++. |
| 38 |
{ "Combine", SwEffect::Combine, false }, |
| 39 |
{ "CombineWithWater", SwEffect::Combine, false }, |
| 40 |
{ "CombineWithWaterLow", SwEffect::Combine, false }, |
| 41 |
{ "TexturedBackground", SwEffect::TexturedBackground, false }, |
| 42 |
{ "TexturedBackgroundDither", SwEffect::TexturedBackground, true }, |
| 43 |
{ "TexturedBackgroundCircle", SwEffect::TexturedBackgroundCircle, false }, |
| 44 |
{ "TexturedBackgroundCircleDither", SwEffect::TexturedBackgroundCircle, true }, |
| 45 |
{ "PaletteRemap", SwEffect::PaletteRemap, false }, |
| 46 |
{ "BatchedPaletteRemap", SwEffect::BatchedPaletteRemap, false }, |
| 47 |
}; |
| 48 |
|
| 49 |
const EffectMapping* FindEffectMapping(const char* label) |
| 50 |
{ |
| 51 |
// A linear scan is fine - the lookup runs once per program load, not per draw |
| 52 |
for (const EffectMapping& mapping : EffectMappings) { |
| 53 |
if (std::strcmp(mapping.Label, label) == 0) { |
| 54 |
return &mapping; |
| 55 |
} |
| 56 |
} |
| 57 |
return nullptr; |
| 58 |
} |
| 59 |
|
| 60 |
#if defined(DEATH_DEBUG) |
| 61 |
bool Contains(StringView haystack, const char* needle) |
| 62 |
{ |
| 63 |
return haystack.contains(needle); |
| 64 |
} |
| 65 |
|
| 66 |
// The retired substring classifier, kept only to cross-check the table above: debug builds |
| 67 |
// assert the two agree for every classified label. The parity check ships for one release, |
| 68 |
// then this function is deleted (see Docs/FixedFunctionShaderDesign.md, migration plan phase 1). |
| 69 |
SwEffect ClassifyEffectBySubstrings(StringView label) |
| 70 |
{ |
| 71 |
// Palette family: recolors an R8/RG8 index sprite through the shared palette texture. Checked |
| 72 |
// before the sprite family - "BatchedPaletteRemap" contains "Batched" but not "Sprite". |
| 73 |
if (Contains(label, "PaletteRemap")) { |
| 74 |
return Contains(label, "Batched") ? SwEffect::BatchedPaletteRemap : SwEffect::PaletteRemap; |
| 75 |
} |
| 76 |
// Animated background (planar tunnel and its circular variant) |
| 77 |
if (Contains(label, "TexturedBackground")) { |
| 78 |
return Contains(label, "Circle") ? SwEffect::TexturedBackgroundCircle : SwEffect::TexturedBackground; |
| 79 |
} |
| 80 |
// Viewport compositor (the plain and water variants share the base composite in C++) |
| 81 |
if (Contains(label, "Combine")) { |
| 82 |
return SwEffect::Combine; |
| 83 |
} |
| 84 |
|
| 85 |
// No-texture solid-colour sprite family (labels "Sprite_NoTexture" / "Batched_Sprites_NoTexture"). |
| 86 |
// The block omits texRect, so it needs a dedicated fast path (different std140 offsets) rather than |
| 87 |
// the generated fragment. The mesh no-texture variants keep the generated-fragment path. |
| 88 |
if (Contains(label, "Sprite") && Contains(label, "NoTexture") && !Contains(label, "Mesh")) { |
| 89 |
return Contains(label, "Batched") ? SwEffect::DefaultBatchedSpritesNoTexture : SwEffect::DefaultSpriteNoTexture; |
| 90 |
} |
| 91 |
|
| 92 |
// Default textured sprite family |
| 93 |
const bool isSprite = Contains(label, "Sprite") && !Contains(label, "Mesh") && !Contains(label, "NoTexture"); |
| 94 |
if (isSprite && Contains(label, "Batched")) { |
| 95 |
return SwEffect::DefaultBatchedSprites; |
| 96 |
} |
| 97 |
if (isSprite && !Contains(label, "Batched")) { |
| 98 |
return SwEffect::DefaultSprite; |
| 99 |
} |
| 100 |
return SwEffect::Unknown; |
| 101 |
} |
| 102 |
#endif |
| 103 |
} |
| 104 |
|
| 105 |
std::uint32_t SwShaderProgram::_nextHandle = 1; |
| 106 |
|
| 107 |
SwShaderProgram::SwShaderProgram() |
| 108 |
: SwShaderProgram(QueryPhase::Immediate) |
| 109 |
{ |
| 110 |
} |
| 111 |
|
| 112 |
SwShaderProgram::SwShaderProgram(QueryPhase queryPhase) |
| 113 |
: _handle(_nextHandle++), _status(Status::NotLinked), _introspection(Introspection::Disabled), _queryPhase(queryPhase), |
| 114 |
_batchSize(DefaultBatchSize), _shouldLogOnErrors(true), _uniformsSize(0), _uniformBlocksSize(0), |
| 115 |
_reflection(nullptr), _effectReflection(nullptr), _effect(SwEffect::Unknown), _ditherVariant(false), |
| 116 |
_boundVbo(nullptr), _boundIbo(nullptr) |
| 117 |
{ |
| 118 |
} |
| 119 |
|
| 120 |
SwShaderProgram::SwShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection, QueryPhase queryPhase) |
| 121 |
: SwShaderProgram(queryPhase) |
| 122 |
{ |
| 123 |
static_cast<void>(vertexFile); |
| 124 |
static_cast<void>(fragmentFile); |
| 125 |
static_cast<void>(introspection); |
| 126 |
} |
| 127 |
|
| 128 |
SwShaderProgram::SwShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection) |
| 129 |
: SwShaderProgram(vertexFile, fragmentFile, introspection, QueryPhase::Immediate) |
| 130 |
{ |
| 131 |
} |
| 132 |
|
| 133 |
SwShaderProgram::SwShaderProgram(StringView vertexFile, StringView fragmentFile) |
| 134 |
: SwShaderProgram(vertexFile, fragmentFile, Introspection::Enabled, QueryPhase::Immediate) |
| 135 |
{ |
| 136 |
} |
| 137 |
|
| 138 |
SwShaderProgram::~SwShaderProgram() |
| 139 |
{ |
| 140 |
// The pipeline keys per-shader camera uniform data on the program pointer; drop this program's |
| 141 |
// entry so RenderResources::Dispose() finds the map empty (mirrors GLShaderProgram's destructor) |
| 142 |
RenderResources::RemoveCameraUniformData(this); |
| 143 |
} |
| 144 |
|
| 145 |
bool SwShaderProgram::IsLinked() const |
| 146 |
{ |
| 147 |
return (_status == Status::Linked || _status == Status::LinkedWithDeferredQueries || _status == Status::LinkedWithIntrospection); |
| 148 |
} |
| 149 |
|
| 150 |
bool SwShaderProgram::AttachShaderFromFile(ShaderStage stage, StringView filename) |
| 151 |
{ |
| 152 |
static_cast<void>(stage); |
| 153 |
static_cast<void>(filename); |
| 154 |
return true; |
| 155 |
} |
| 156 |
|
| 157 |
bool SwShaderProgram::AttachShaderFromString(ShaderStage stage, StringView string) |
| 158 |
{ |
| 159 |
static_cast<void>(stage); |
| 160 |
static_cast<void>(string); |
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
bool SwShaderProgram::AttachShaderFromStrings(ShaderStage stage, ArrayView<const StringView> strings) |
| 165 |
{ |
| 166 |
static_cast<void>(stage); |
| 167 |
static_cast<void>(strings); |
| 168 |
return true; |
| 169 |
} |
| 170 |
|
| 171 |
bool SwShaderProgram::AttachShaderFromStringsAndFile(ShaderStage stage, ArrayView<const StringView> strings, StringView filename) |
| 172 |
{ |
| 173 |
static_cast<void>(stage); |
| 174 |
static_cast<void>(strings); |
| 175 |
static_cast<void>(filename); |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
bool SwShaderProgram::Link(Introspection introspection) |
| 180 |
{ |
| 181 |
return FinalizeAfterLinking(introspection); |
| 182 |
} |
| 183 |
|
| 184 |
bool SwShaderProgram::FinalizeAfterLinking(Introspection introspection) |
| 185 |
{ |
| 186 |
_introspection = introspection; |
| 187 |
PerformIntrospection(); |
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
void SwShaderProgram::Use() |
| 192 |
{ |
| 193 |
SwDevice::BindProgram(this); |
| 194 |
} |
| 195 |
|
| 196 |
void SwShaderProgram::PerformIntrospection() |
| 197 |
{ |
| 198 |
if (_introspection != Introspection::Disabled && _status != Status::LinkedWithIntrospection) { |
| 199 |
_uniformsSize = 0; |
| 200 |
_uniformBlocksSize = 0; |
| 201 |
|
| 202 |
if (_reflection != nullptr) { |
| 203 |
_effectReflection = _reflection; |
| 204 |
ImportReflection(); |
| 205 |
} |
| 206 |
_status = Status::LinkedWithIntrospection; |
| 207 |
} |
| 208 |
// The reflection is consumed by introspection (a copy of its layout is kept in _effectReflection) |
| 209 |
_reflection = nullptr; |
| 210 |
} |
| 211 |
|
| 212 |
void SwShaderProgram::ImportReflection() |
| 213 |
{ |
| 214 |
const ShaderCompiler::ProgramVariant& reflection = *_reflection; |
| 215 |
std::int32_t nextLocation = 0; |
| 216 |
|
| 217 |
// Loose uniforms - samplers are kept in a separate reflection list but treated as loose uniforms here |
| 218 |
for (std::size_t i = 0; i < reflection.UniformCount; i++) { |
| 219 |
const ShaderCompiler::Uniform& u = reflection.Uniforms[i]; |
| 220 |
_uniforms.emplace_back(this, u.Name, u.Type, std::int32_t(u.ArraySize), nextLocation++); |
| 221 |
_uniformsSize += _uniforms.back().GetMemorySize(); |
| 222 |
} |
| 223 |
for (std::size_t i = 0; i < reflection.TextureCount; i++) { |
| 224 |
const ShaderCompiler::TextureBinding& t = reflection.Textures[i]; |
| 225 |
_uniforms.emplace_back(this, t.Name, ShaderCompiler::UniformType::Sampler2D, 1, nextLocation++); |
| 226 |
_uniformsSize += _uniforms.back().GetMemorySize(); |
| 227 |
} |
| 228 |
|
| 229 |
_uniformBlocks.reserve(reflection.BlockCount); |
| 230 |
for (std::size_t i = 0; i < reflection.BlockCount; i++) { |
| 231 |
const ShaderCompiler::UniformBlock& b = reflection.Blocks[i]; |
| 232 |
|
| 233 |
// A BATCH_SIZE-sized instance array uses the explicitly set batch size, or the same 64 KB-based |
| 234 |
// fallback the in-shader "#ifndef BATCH_SIZE" defaults assume when no size is injected |
| 235 |
std::uint32_t effectiveBatchSize = 0; |
| 236 |
std::uint32_t dataSize = b.BaseSize; |
| 237 |
if (b.InstanceStride > 0) { |
| 238 |
effectiveBatchSize = (_batchSize != std::uint32_t(DefaultBatchSize) && _batchSize > 0) |
| 239 |
? _batchSize : (64u * 1024u) / b.InstanceStride; |
| 240 |
dataSize += b.InstanceStride * effectiveBatchSize; |
| 241 |
} |
| 242 |
|
| 243 |
const std::uint32_t blockIndex = std::uint32_t(_uniformBlocks.size()); |
| 244 |
_uniformBlocks.emplace_back(blockIndex, b.Name, std::int32_t(dataSize)); |
| 245 |
SwUniformBlock& block = _uniformBlocks.back(); |
| 246 |
_uniformBlocksSize += block.GetSize(); |
| 247 |
|
| 248 |
if (_introspection != Introspection::NoUniformsInBlocks) { |
| 249 |
block._members.reserve(b.MemberCount); |
| 250 |
for (std::size_t j = 0; j < b.MemberCount; j++) { |
| 251 |
const ShaderCompiler::BlockMember& m = b.Members[j]; |
| 252 |
if (m.Type == ShaderCompiler::UniformType::Struct) { |
| 253 |
// Struct aggregates are never read by name; only flat leaf members matter |
| 254 |
continue; |
| 255 |
} |
| 256 |
SwUniform member; |
| 257 |
member.SetName(m.Name); |
| 258 |
member._type = m.Type; |
| 259 |
member._size = (m.ArraySize == ShaderCompiler::SymbolicArraySize) |
| 260 |
? std::int32_t(effectiveBatchSize) |
| 261 |
: (m.ArraySize > 0 ? std::int32_t(m.ArraySize) : 1); |
| 262 |
member._blockIndex = std::int32_t(blockIndex); |
| 263 |
member._offset = std::int32_t(m.Offset); |
| 264 |
member._owner = this; |
| 265 |
block._members.push_back(member); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
for (std::size_t i = 0; i < reflection.AttributeCount; i++) { |
| 271 |
const ShaderCompiler::Attribute& a = reflection.Attributes[i]; |
| 272 |
const std::int32_t location = (a.Location >= 0 ? a.Location : std::int32_t(i)); |
| 273 |
_attributes.emplace_back(a.Name, a.Type, location); |
| 274 |
_vertexFormat[std::uint32_t(location)].Init(std::uint32_t(location), std::int32_t(UniformTypeInfo::ComponentCount(a.Type)), 0); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
bool SwShaderProgram::HasAttribute(const char* name) const |
| 279 |
{ |
| 280 |
for (const SwAttribute& a : _attributes) { |
| 281 |
if (std::strcmp(a.GetName(), name) == 0) { |
| 282 |
return true; |
| 283 |
} |
| 284 |
} |
| 285 |
return false; |
| 286 |
} |
| 287 |
|
| 288 |
SwVertexFormat::Attribute* SwShaderProgram::GetAttribute(const char* name) |
| 289 |
{ |
| 290 |
for (const SwAttribute& a : _attributes) { |
| 291 |
if (std::strcmp(a.GetName(), name) == 0 && a.GetLocation() >= 0) { |
| 292 |
return &_vertexFormat[std::uint32_t(a.GetLocation())]; |
| 293 |
} |
| 294 |
} |
| 295 |
return nullptr; |
| 296 |
} |
| 297 |
|
| 298 |
void SwShaderProgram::DefineVertexFormat(const SwBuffer* vbo, const SwBuffer* ibo, std::uint32_t vboOffset) |
| 299 |
{ |
| 300 |
_boundVbo = vbo; |
| 301 |
_boundIbo = ibo; |
| 302 |
if (vbo != nullptr) { |
| 303 |
for (const SwAttribute& a : _attributes) { |
| 304 |
if (a.GetLocation() >= 0) { |
| 305 |
SwVertexFormat::Attribute& attr = _vertexFormat[std::uint32_t(a.GetLocation())]; |
| 306 |
attr.setVbo(vbo); |
| 307 |
attr.SetBaseOffset(vboOffset); |
| 308 |
} |
| 309 |
} |
| 310 |
_vertexFormat.SetIbo(ibo); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
void SwShaderProgram::Reset() |
| 315 |
{ |
| 316 |
_uniforms.clear(); |
| 317 |
_uniformBlocks.clear(); |
| 318 |
_attributes.clear(); |
| 319 |
_resolvedUniforms.clear(); |
| 320 |
_vertexFormat.Reset(); |
| 321 |
_status = Status::NotLinked; |
| 322 |
_batchSize = DefaultBatchSize; |
| 323 |
_reflection = nullptr; |
| 324 |
_effectReflection = nullptr; |
| 325 |
} |
| 326 |
|
| 327 |
void SwShaderProgram::SetObjectLabel(StringView label) |
| 328 |
{ |
| 329 |
// The effect is looked up by the program's exact object label (the shader name registered by |
| 330 |
// ContentResolver::CompileShader, which bakes the variant into the name, e.g. "...Dither"). |
| 331 |
// Labels with no table entry stay Unknown and take the generated-fragment path. |
| 332 |
_label = label; |
| 333 |
const EffectMapping* mapping = FindEffectMapping(_label.data()); |
| 334 |
_effect = (mapping != nullptr ? mapping->Effect : SwEffect::Unknown); |
| 335 |
_ditherVariant = (mapping != nullptr && mapping->Dither); |
| 336 |
#if defined(DEATH_DEBUG) |
| 337 |
// Migration parity check (Docs/FixedFunctionShaderDesign.md, phase 1): the table must reproduce |
| 338 |
// the substring classification for every label either side classifies. Ships for one release, |
| 339 |
// then ClassifyEffectBySubstrings() is deleted. |
| 340 |
SwEffect bySubstrings = ClassifyEffectBySubstrings(label); |
| 341 |
if (mapping != nullptr || bySubstrings != SwEffect::Unknown) { |
| 342 |
DEATH_DEBUG_ASSERT(_effect == bySubstrings, |
| 343 |
("Effect table disagrees with the substring classifier for shader label \"{}\"", _label.data())); |
| 344 |
DEATH_DEBUG_ASSERT(_ditherVariant == label.contains("Dither"), |
| 345 |
("Dither flag disagrees with the substring derivation for shader label \"{}\"", _label.data())); |
| 346 |
} |
| 347 |
#endif |
| 348 |
} |
| 349 |
|
| 350 |
const SwUniformBlock* SwShaderProgram::FindBlock(const char* name) const |
| 351 |
{ |
| 352 |
for (const SwUniformBlock& block : _uniformBlocks) { |
| 353 |
if (std::strcmp(block.GetName(), name) == 0) { |
| 354 |
return █ |
| 355 |
} |
| 356 |
} |
| 357 |
return nullptr; |
| 358 |
} |
| 359 |
|
| 360 |
void SwShaderProgram::SetResolvedUniform(const char* name, const std::uint8_t* data) |
| 361 |
{ |
| 362 |
for (ResolvedUniform& r : _resolvedUniforms) { |
| 363 |
if (r.Name == name) { |
| 364 |
r.Data = data; |
| 365 |
return; |
| 366 |
} |
| 367 |
} |
| 368 |
_resolvedUniforms.push_back({name, data}); |
| 369 |
} |
| 370 |
|
| 371 |
const std::uint8_t* SwShaderProgram::ResolveUniform(const char* name) const |
| 372 |
{ |
| 373 |
for (const ResolvedUniform& r : _resolvedUniforms) { |
| 374 |
if (r.Name == name) { |
| 375 |
return r.Data; |
| 376 |
} |
| 377 |
} |
| 378 |
return nullptr; |
| 379 |
} |
| 380 |
} |