Skip to content

Commit

Permalink
[Impeller] OpenGLES: Ensure frag/vert textures are bound with unique …
Browse files Browse the repository at this point in the history
…texture units.
  • Loading branch information
bdero committed Oct 23, 2023
1 parent 94f504f commit 8a5fe45
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
30 changes: 18 additions & 12 deletions impeller/renderer/backend/gles/buffer_bindings_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,15 @@ bool BufferBindingsGLES::BindUniformData(
}
}

if (!BindTextures(gl, vertex_bindings, ShaderStage::kVertex)) {
std::optional<size_t> next_unit_index =
BindTextures(gl, vertex_bindings, ShaderStage::kVertex);
if (!next_unit_index.has_value()) {
return false;
}

if (!BindTextures(gl, fragment_bindings, ShaderStage::kFragment)) {
if (!BindTextures(gl, fragment_bindings, ShaderStage::kFragment,
*next_unit_index)
.has_value()) {
return false;
}

Expand Down Expand Up @@ -295,23 +299,25 @@ bool BufferBindingsGLES::BindUniformBuffer(const ProcTableGLES& gl,
return true;
}

bool BufferBindingsGLES::BindTextures(const ProcTableGLES& gl,
const Bindings& bindings,
ShaderStage stage) const {
size_t active_index = 0;
std::optional<size_t> BufferBindingsGLES::BindTextures(
const ProcTableGLES& gl,
const Bindings& bindings,
ShaderStage stage,
size_t unit_start_index) const {
size_t active_index = unit_start_index;
for (const auto& data : bindings.sampled_images) {
const auto& texture_gles = TextureGLES::Cast(*data.second.texture.resource);
if (data.second.texture.GetMetadata() == nullptr) {
VALIDATION_LOG << "No metadata found for texture binding.";
return false;
return std::nullopt;
}

const auto uniform_key =
CreateUniformMemberKey(data.second.texture.GetMetadata()->name);
auto uniform = uniform_locations_.find(uniform_key);
if (uniform == uniform_locations_.end()) {
VALIDATION_LOG << "Could not find uniform for key: " << uniform_key;
return false;
return std::nullopt;
}

//--------------------------------------------------------------------------
Expand All @@ -320,15 +326,15 @@ bool BufferBindingsGLES::BindTextures(const ProcTableGLES& gl,
if (active_index >= gl.GetCapabilities()->GetMaxTextureUnits(stage)) {
VALIDATION_LOG << "Texture units specified exceed the capabilities for "
"this shader stage.";
return false;
return std::nullopt;
}
gl.ActiveTexture(GL_TEXTURE0 + active_index);

//--------------------------------------------------------------------------
/// Bind the texture.
///
if (!texture_gles.Bind()) {
return false;
return std::nullopt;
}

//--------------------------------------------------------------------------
Expand All @@ -337,7 +343,7 @@ bool BufferBindingsGLES::BindTextures(const ProcTableGLES& gl,
///
const auto& sampler_gles = SamplerGLES::Cast(*data.second.sampler.resource);
if (!sampler_gles.ConfigureBoundTexture(texture_gles, gl)) {
return false;
return std::nullopt;
}

//--------------------------------------------------------------------------
Expand All @@ -350,7 +356,7 @@ bool BufferBindingsGLES::BindTextures(const ProcTableGLES& gl,
///
active_index++;
}
return true;
return active_index;
}

} // namespace impeller
7 changes: 4 additions & 3 deletions impeller/renderer/backend/gles/buffer_bindings_gles.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ class BufferBindingsGLES {
Allocator& transients_allocator,
const BufferResource& buffer) const;

bool BindTextures(const ProcTableGLES& gl,
const Bindings& bindings,
ShaderStage stage) const;
std::optional<size_t> BindTextures(const ProcTableGLES& gl,
const Bindings& bindings,
ShaderStage stage,
size_t unit_start_index = 0) const;

FML_DISALLOW_COPY_AND_ASSIGN(BufferBindingsGLES);
};
Expand Down

0 comments on commit 8a5fe45

Please sign in to comment.