Hash :
0c4d6446
Author :
Date :
2024-01-24T10:38:45
Rework uniform block <-> uniform buffer mapping
In GLES, the shader declares which buffer binding a block (uniform,
storage or atomic counter) is bound to. For example:
layout(binding = 1) uniform ubo0 { ... };
layout(binding = 2) uniform ubo1 { ... };
layout(binding = 1) uniform ubo2 { ... };
In the above, ubo0 and ubo2 use data from the buffer bound to index 2
(through glBindBufferRange), while ubo1 uses data from the buffer bound
to index 1. For uniform blocks in particular, omitting the binding
is allowed, in which case it is implicitly bound to buffer 0.
GLES allows uniform blocks (and only uniform blocks) to remap their
bindings through calls to glUniformBlockBinding. This means that the
mapping of uniform blocks in the program (ubo0, ubo1, ubo2) to the
buffer bindings is not constant. For storage blocks and atomic counter
buffers, this binding _is_ constant and is determined at link time.
At link time, the mapping of blocks to buffers is determined based on
values specified in the shaders. This info is stored was stored in
gl::InterfaceBlock::binding (for UBOs and SSBOs), and
gl::AtomicCounterBuffer::binding. For clarity, this change renames
these members to ...::inShaderBinding.
When glUniformBlockBinding is called, the mapping is updated. Prior to
this change, gl::InterfaceBlock::binding was directly updated, trumping
the mapping determined at link time. A bug here was that after a call
to glProgramBinary, GL expects the mappings to reset to their original
link-time values, but instead ANGLE restored the mappings to what was
configured at the time the binary was retrieved.
This change tracks the uniform block -> buffer binding mapping
separately from the link results so that the original values can be
restored during glProgramBinary. In the process, the support data
structures for tracking this mapping are moved to ProgramExecutable and
the algorithms are simplified. Program Pipeline Objects maintain this
mapping identically to Programs and no longer require a special and more
costly path when a buffer state changes.
This change prepares for but does not yet fix the more fundamental bug
that the dirty bits are tracked in the program executable instead of the
context state, which makes changes not propagate to all contexts
correctly.
Bug: angleproject:8493
Change-Id: Ib0999f49be24db06ebe9a4917d06b90af899611e
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5235883
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Charlie Lao <cclao@google.com>
Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
//
// Copyright 2023 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// ProgramExecutableMtl.h: Implementation of ProgramExecutableImpl.
#ifndef LIBANGLE_RENDERER_MTL_PROGRAMEXECUTABLEMTL_H_
#define LIBANGLE_RENDERER_MTL_PROGRAMEXECUTABLEMTL_H_
#include "libANGLE/ProgramExecutable.h"
#include "libANGLE/renderer/ProgramExecutableImpl.h"
#include "libANGLE/renderer/metal/mtl_buffer_pool.h"
#include "libANGLE/renderer/metal/mtl_command_buffer.h"
#include "libANGLE/renderer/metal/mtl_common.h"
#include "libANGLE/renderer/metal/mtl_msl_utils.h"
#include "libANGLE/renderer/metal/mtl_resources.h"
#include "libANGLE/renderer/metal/mtl_state_cache.h"
namespace rx
{
class ContextMtl;
struct UBOConversionInfo
{
UBOConversionInfo(const std::vector<sh::BlockMemberInfo> &stdInfo,
const std::vector<sh::BlockMemberInfo> &metalInfo,
size_t stdSize,
size_t metalSize)
: _stdInfo(stdInfo), _metalInfo(metalInfo), _stdSize(stdSize), _metalSize(metalSize)
{
_needsConversion = _calculateNeedsConversion();
}
const std::vector<sh::BlockMemberInfo> &stdInfo() const { return _stdInfo; }
const std::vector<sh::BlockMemberInfo> &metalInfo() const { return _metalInfo; }
size_t stdSize() const { return _stdSize; }
size_t metalSize() const { return _metalSize; }
bool needsConversion() const { return _needsConversion; }
private:
std::vector<sh::BlockMemberInfo> _stdInfo, _metalInfo;
size_t _stdSize, _metalSize;
bool _needsConversion;
bool _calculateNeedsConversion()
{
if (_stdSize != _metalSize)
{
return true;
}
if (_stdInfo.size() != _metalInfo.size())
{
return true;
}
for (size_t i = 0; i < _stdInfo.size(); ++i)
{
// If the matrix is trasnposed
if (_stdInfo[i].isRowMajorMatrix)
{
return true;
}
// If we have a bool
if (gl::VariableComponentType(_stdInfo[i].type) == GL_BOOL)
{
return true;
}
// If any offset information is different
if (!(_stdInfo[i] == _metalInfo[i]))
{
return true;
}
}
return false;
}
};
struct ProgramArgumentBufferEncoderMtl
{
void reset(ContextMtl *contextMtl);
mtl::AutoObjCPtr<id<MTLArgumentEncoder>> metalArgBufferEncoder;
mtl::BufferPool bufferPool;
};
constexpr size_t kFragmentShaderVariants = 4;
// Represents a specialized shader variant. For example, a shader variant with fragment coverage
// mask enabled and a shader variant without.
struct ProgramShaderObjVariantMtl
{
void reset(ContextMtl *contextMtl);
mtl::AutoObjCPtr<id<MTLFunction>> metalShader;
// UBO's argument buffer encoder. Used when number of UBOs used exceeds number of allowed
// discrete slots, and thus needs to encode all into one argument buffer.
ProgramArgumentBufferEncoderMtl uboArgBufferEncoder;
// Store reference to the TranslatedShaderInfo to easy querying mapped textures/UBO/XFB
// bindings.
const mtl::TranslatedShaderInfo *translatedSrcInfo;
};
// State for the default uniform blocks.
struct DefaultUniformBlockMtl final : private angle::NonCopyable
{
DefaultUniformBlockMtl();
~DefaultUniformBlockMtl();
// Shadow copies of the shader uniform data.
angle::MemoryBuffer uniformData;
// Since the default blocks are laid out in std140, this tells us where to write on a call
// to a setUniform method. They are arranged in uniform location order.
std::vector<sh::BlockMemberInfo> uniformLayout;
};
class ProgramExecutableMtl : public ProgramExecutableImpl
{
public:
ProgramExecutableMtl(const gl::ProgramExecutable *executable);
~ProgramExecutableMtl() override;
void destroy(const gl::Context *context) override;
angle::Result load(ContextMtl *contextMtl, gl::BinaryInputStream *stream);
void save(gl::BinaryOutputStream *stream);
void setUniform1fv(GLint location, GLsizei count, const GLfloat *v) override;
void setUniform2fv(GLint location, GLsizei count, const GLfloat *v) override;
void setUniform3fv(GLint location, GLsizei count, const GLfloat *v) override;
void setUniform4fv(GLint location, GLsizei count, const GLfloat *v) override;
void setUniform1iv(GLint location, GLsizei count, const GLint *v) override;
void setUniform2iv(GLint location, GLsizei count, const GLint *v) override;
void setUniform3iv(GLint location, GLsizei count, const GLint *v) override;
void setUniform4iv(GLint location, GLsizei count, const GLint *v) override;
void setUniform1uiv(GLint location, GLsizei count, const GLuint *v) override;
void setUniform2uiv(GLint location, GLsizei count, const GLuint *v) override;
void setUniform3uiv(GLint location, GLsizei count, const GLuint *v) override;
void setUniform4uiv(GLint location, GLsizei count, const GLuint *v) override;
void setUniformMatrix2fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value) override;
void setUniformMatrix3fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value) override;
void setUniformMatrix4fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value) override;
void setUniformMatrix2x3fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value) override;
void setUniformMatrix3x2fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value) override;
void setUniformMatrix2x4fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value) override;
void setUniformMatrix4x2fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value) override;
void setUniformMatrix3x4fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value) override;
void setUniformMatrix4x3fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value) override;
void getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const override;
void getUniformiv(const gl::Context *context, GLint location, GLint *params) const override;
void getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const override;
bool hasFlatAttribute() const { return mProgramHasFlatAttributes; }
// Calls this before drawing, changedPipelineDesc is passed when vertex attributes desc and/or
// shader program changed.
angle::Result setupDraw(const gl::Context *glContext,
mtl::RenderCommandEncoder *cmdEncoder,
const mtl::RenderPipelineDesc &pipelineDesc,
bool pipelineDescChanged,
bool forceTexturesSetting,
bool uniformBuffersDirty);
private:
friend class ProgramMtl;
void reset(ContextMtl *context);
template <int cols, int rows>
void setUniformMatrixfv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value);
template <class T>
void getUniformImpl(GLint location, T *v, GLenum entryPointType) const;
template <typename T>
void setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType);
void saveTranslatedShaders(gl::BinaryOutputStream *stream);
void loadTranslatedShaders(gl::BinaryInputStream *stream);
void saveShaderInternalInfo(gl::BinaryOutputStream *stream);
void loadShaderInternalInfo(gl::BinaryInputStream *stream);
void saveInterfaceBlockInfo(gl::BinaryOutputStream *stream);
angle::Result loadInterfaceBlockInfo(gl::BinaryInputStream *stream);
void saveDefaultUniformBlocksInfo(gl::BinaryOutputStream *stream);
angle::Result loadDefaultUniformBlocksInfo(mtl::Context *context,
gl::BinaryInputStream *stream);
void linkUpdateHasFlatAttributes(const gl::SharedCompiledShaderState &vertexShader);
angle::Result initDefaultUniformBlocks(
mtl::Context *context,
const gl::ShaderMap<gl::SharedCompiledShaderState> &shaders);
angle::Result resizeDefaultUniformBlocksMemory(mtl::Context *context,
const gl::ShaderMap<size_t> &requiredBufferSize);
void initUniformBlocksRemapper(const gl::SharedCompiledShaderState &shader);
mtl::BufferPool *getBufferPool(ContextMtl *context, gl::ShaderType shaderType);
angle::Result getSpecializedShader(ContextMtl *context,
gl::ShaderType shaderType,
const mtl::RenderPipelineDesc &renderPipelineDesc,
id<MTLFunction> *shaderOut);
angle::Result commitUniforms(ContextMtl *context, mtl::RenderCommandEncoder *cmdEncoder);
angle::Result updateTextures(const gl::Context *glContext,
mtl::RenderCommandEncoder *cmdEncoder,
bool forceUpdate);
angle::Result updateUniformBuffers(ContextMtl *context,
mtl::RenderCommandEncoder *cmdEncoder,
const mtl::RenderPipelineDesc &pipelineDesc);
angle::Result updateXfbBuffers(ContextMtl *context,
mtl::RenderCommandEncoder *cmdEncoder,
const mtl::RenderPipelineDesc &pipelineDesc);
angle::Result legalizeUniformBufferOffsets(ContextMtl *context);
angle::Result bindUniformBuffersToDiscreteSlots(ContextMtl *context,
mtl::RenderCommandEncoder *cmdEncoder,
gl::ShaderType shaderType);
angle::Result encodeUniformBuffersInfoArgumentBuffer(ContextMtl *context,
mtl::RenderCommandEncoder *cmdEncoder,
gl::ShaderType shaderType);
bool mProgramHasFlatAttributes;
gl::ShaderMap<DefaultUniformBlockMtl> mDefaultUniformBlocks;
std::unordered_map<std::string, UBOConversionInfo> mUniformBlockConversions;
// Translated metal shaders:
gl::ShaderMap<mtl::TranslatedShaderInfo> mMslShaderTranslateInfo;
// Translated metal version for transform feedback only vertex shader:
// - Metal doesn't allow vertex shader to write to both buffers and to stage output
// (gl_Position). Need a special version of vertex shader that only writes to transform feedback
// buffers.
mtl::TranslatedShaderInfo mMslXfbOnlyVertexShaderInfo;
// Compiled native shader object variants:
// - Vertex shader: One with emulated rasterization discard, one with true rasterization
// discard, one without.
mtl::RenderPipelineRasterStateMap<ProgramShaderObjVariantMtl> mVertexShaderVariants;
// - Fragment shader: Combinations of sample coverage mask and depth write enabled states.
std::array<ProgramShaderObjVariantMtl, kFragmentShaderVariants> mFragmentShaderVariants;
// Cached references of current shader variants.
gl::ShaderMap<ProgramShaderObjVariantMtl *> mCurrentShaderVariants;
gl::ShaderBitSet mDefaultUniformBlocksDirty;
gl::ShaderBitSet mSamplerBindingsDirty;
// Scratch data:
// Legalized buffers and their offsets. For example, uniform buffer's offset=1 is not a valid
// offset, it will be converted to legal offset and the result is stored in this array.
std::vector<std::pair<mtl::BufferRef, uint32_t>> mLegalizedOffsetedUniformBuffers;
// Stores the render stages usage of each uniform buffer. Only used if the buffers are encoded
// into an argument buffer.
std::vector<uint32_t> mArgumentBufferRenderStageUsages;
uint32_t mShadowCompareModes[mtl::kMaxShaderSamplers];
gl::ShaderMap<std::unique_ptr<mtl::BufferPool>> mDefaultUniformBufferPools;
};
angle::Result CreateMslShaderLib(ContextMtl *context,
gl::InfoLog &infoLog,
mtl::TranslatedShaderInfo *translatedMslInfo,
const std::map<std::string, std::string> &substitutionMacros);
} // namespace rx
#endif // LIBANGLE_RENDERER_MTL_PROGRAMEXECUTABLEMTL_H_