Refactor texture location handling to simplify tweakers (#1246)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
committed by
Bart Louwers
parent
9103d7125c
commit
f3247dd6aa
@@ -34,18 +34,8 @@ using Texture2DPtr = std::shared_ptr<Texture2D>;
|
||||
|
||||
class Drawable {
|
||||
public:
|
||||
struct TextureAttachment {
|
||||
/// @brief A Texture2D instance
|
||||
std::shared_ptr<gfx::Texture2D> texture{nullptr};
|
||||
/// @brief A sampler location to bind the texture to
|
||||
int32_t location{0};
|
||||
|
||||
TextureAttachment() = delete;
|
||||
TextureAttachment(std::shared_ptr<gfx::Texture2D> tex, int32_t loc)
|
||||
: texture(std::move(tex)),
|
||||
location(loc) {}
|
||||
};
|
||||
using Textures = std::vector<TextureAttachment>;
|
||||
/// @brief Map from sampler location to texture info
|
||||
using Textures = std::unordered_map<int32_t, gfx::Texture2DPtr>;
|
||||
|
||||
protected:
|
||||
Drawable(std::string name);
|
||||
@@ -97,20 +87,24 @@ public:
|
||||
/// @return Texture and sampler location pairs
|
||||
const Textures& getTextures() const { return textures; };
|
||||
|
||||
/// @brief Get the texture at the given sampler location.
|
||||
const gfx::Texture2DPtr& getTexture(int32_t location) const;
|
||||
|
||||
/// @brief Set the collection of textures bound to this drawable
|
||||
/// @param textures_ A Textures collection to set
|
||||
void setTextures(const Textures& textures_) noexcept { textures = textures_; }
|
||||
void setTextures(Textures&& textures_) noexcept { textures = std::move(textures_); }
|
||||
|
||||
/// @brief Attach the given texture to this drawable at the given sampler location.
|
||||
/// @param texture Texture2D instance
|
||||
/// @param location A sampler location in the shader being used with this drawable.
|
||||
void setTexture(std::shared_ptr<gfx::Texture2D> texture, int32_t location);
|
||||
void setTexture(gfx::Texture2DPtr texture, int32_t location);
|
||||
|
||||
using TexSourceFunc = std::function<gfx::Texture2DPtr()>;
|
||||
using TexSourceFunc = std::function<Textures()>;
|
||||
|
||||
/// @brief Provide a function to get the current texture
|
||||
void setTextureSource(int32_t location, TexSourceFunc);
|
||||
const TexSourceFunc& getTextureSource(int32_t location) const;
|
||||
/// @brief Provide a function to get the current textures
|
||||
void setTextureSource(TexSourceFunc value) { textureSource = std::move(value); }
|
||||
const TexSourceFunc& getTextureSource() const { return textureSource; }
|
||||
|
||||
/// @brief Provide all texture sources at once
|
||||
void setTextureSources(std::vector<TexSourceFunc>);
|
||||
@@ -189,7 +183,7 @@ protected:
|
||||
std::unique_ptr<Impl> impl;
|
||||
|
||||
Textures textures;
|
||||
std::vector<TexSourceFunc> textureSources;
|
||||
TexSourceFunc textureSource;
|
||||
std::vector<DrawableTweakerPtr> tweakers;
|
||||
};
|
||||
|
||||
|
||||
@@ -113,10 +113,8 @@ public:
|
||||
/// @param location A sampler location in the shader being used.
|
||||
void setTexture(const gfx::Texture2DPtr&, int32_t location);
|
||||
|
||||
using TexSourceFunc = std::function<gfx::Texture2DPtr()>;
|
||||
|
||||
/// @brief Provide a function to get the current texture
|
||||
void setTextureSource(int32_t location, TexSourceFunc);
|
||||
void setTextureSource(Drawable::TexSourceFunc value) { textureSource = std::move(value); }
|
||||
|
||||
/// Add a triangle
|
||||
void addTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2);
|
||||
@@ -177,7 +175,7 @@ protected:
|
||||
ColorAttrMode colorAttrMode = ColorAttrMode::PerVertex;
|
||||
VertexAttributeArray vertexAttrs;
|
||||
gfx::Drawable::Textures textures;
|
||||
std::vector<TexSourceFunc> textureSources;
|
||||
Drawable::TexSourceFunc textureSource;
|
||||
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl;
|
||||
|
||||
@@ -27,40 +27,19 @@ void Drawable::setCullFaceMode(const gfx::CullFaceMode& value) {
|
||||
impl->cullFaceMode = value;
|
||||
}
|
||||
|
||||
static const gfx::Texture2DPtr noTexture;
|
||||
|
||||
const gfx::Texture2DPtr& Drawable::getTexture(int32_t location) const {
|
||||
const auto hit = textures.find(location);
|
||||
return (hit != textures.end()) ? hit->second : noTexture;
|
||||
}
|
||||
|
||||
void Drawable::setTexture(std::shared_ptr<gfx::Texture2D> texture, int32_t location) {
|
||||
for (auto& tex : textures) {
|
||||
if (tex.location == location) {
|
||||
tex.texture = std::move(texture);
|
||||
return;
|
||||
}
|
||||
}
|
||||
textures.emplace_back(std::move(texture), location);
|
||||
textures.insert(std::make_pair(location, gfx::Texture2DPtr{})).first->second = std::move(texture);
|
||||
}
|
||||
|
||||
void Drawable::removeTexture(int32_t location) {
|
||||
for (auto it = textures.begin(); it != textures.end(); ++it) {
|
||||
if (it->location == location) {
|
||||
textures.erase(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Provide a function to get the current texture
|
||||
void Drawable::setTextureSource(int32_t location, TexSourceFunc source) {
|
||||
textureSources.resize(std::max(textureSources.size(), static_cast<size_t>(location + 1)));
|
||||
textureSources[location] = std::move(source);
|
||||
}
|
||||
|
||||
static const Drawable::TexSourceFunc noSource;
|
||||
|
||||
const Drawable::TexSourceFunc& Drawable::getTextureSource(int32_t location) const {
|
||||
return (static_cast<std::size_t>(location) < textureSources.size()) ? textureSources[location] : noSource;
|
||||
}
|
||||
|
||||
/// @brief Provide all texture sources at once
|
||||
void Drawable::setTextureSources(std::vector<TexSourceFunc> sources) {
|
||||
textureSources = std::move(sources);
|
||||
textures.erase(location);
|
||||
}
|
||||
|
||||
} // namespace gfx
|
||||
|
||||
@@ -50,7 +50,7 @@ void DrawableBuilder::flush() {
|
||||
draw->setCullFaceMode(impl->cullFaceMode);
|
||||
draw->setShader(shader);
|
||||
draw->setTextures(textures);
|
||||
draw->setTextureSources(textureSources);
|
||||
draw->setTextureSource(textureSource);
|
||||
|
||||
if (auto drawAttrs = getVertexAttributes().clone()) {
|
||||
vertexAttrs.observeAttributes([&](const std::string& iName, const VertexAttribute& iAttr) {
|
||||
@@ -104,18 +104,7 @@ void DrawableBuilder::resetDrawPriority(DrawPriority value) {
|
||||
}
|
||||
|
||||
void DrawableBuilder::setTexture(const std::shared_ptr<gfx::Texture2D>& texture, int32_t location) {
|
||||
for (auto& tex : textures) {
|
||||
if (tex.location == location) {
|
||||
tex.texture = texture;
|
||||
return;
|
||||
}
|
||||
}
|
||||
textures.emplace_back(texture, location);
|
||||
}
|
||||
|
||||
void DrawableBuilder::setTextureSource(int32_t location, TexSourceFunc source) {
|
||||
textureSources.resize(std::max(textureSources.size(), static_cast<size_t>(location + 1)));
|
||||
textureSources[location] = std::move(source);
|
||||
textures.insert(std::make_pair(location, gfx::Texture2DPtr{})).first->second = std::move(texture);
|
||||
}
|
||||
|
||||
void DrawableBuilder::addTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
|
||||
|
||||
@@ -183,14 +183,14 @@ void DrawableGL::upload(gfx::UploadPass& uploadPass) {
|
||||
};
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < textureSources.size(); ++i) {
|
||||
if (const auto& source = textureSources[i]) {
|
||||
setTexture(source(), static_cast<int32_t>(i));
|
||||
if (const auto src = getTextureSource()) {
|
||||
for (auto& pair : src()) {
|
||||
setTexture(std::move(pair.second), pair.first);
|
||||
}
|
||||
}
|
||||
|
||||
const bool texturesNeedUpload = std::any_of(
|
||||
textures.begin(), textures.end(), [](const auto& tex) { return tex.texture->needsUpload(); });
|
||||
textures.begin(), textures.end(), [](const auto& pair) { return pair.second && pair.second->needsUpload(); });
|
||||
|
||||
if (texturesNeedUpload) {
|
||||
uploadTextures();
|
||||
@@ -198,21 +198,28 @@ void DrawableGL::upload(gfx::UploadPass& uploadPass) {
|
||||
}
|
||||
|
||||
void DrawableGL::uploadTextures() const {
|
||||
for (const auto& tex : textures) {
|
||||
std::static_pointer_cast<gl::Texture2D>(tex.texture)->upload();
|
||||
for (const auto& pair : textures) {
|
||||
if (const auto& tex = pair.second) {
|
||||
std::static_pointer_cast<gl::Texture2D>(tex)->upload();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DrawableGL::bindTextures() const {
|
||||
int32_t unit = 0;
|
||||
for (const auto& tex : textures) {
|
||||
std::static_pointer_cast<gl::Texture2D>(tex.texture)->bind(tex.location, unit++);
|
||||
for (const auto& pair : textures) {
|
||||
if (const auto& tex = pair.second) {
|
||||
const auto& location = pair.first;
|
||||
std::static_pointer_cast<gl::Texture2D>(tex)->bind(location, unit++);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DrawableGL::unbindTextures() const {
|
||||
for (const auto& tex : textures) {
|
||||
std::static_pointer_cast<gl::Texture2D>(tex.texture)->unbind();
|
||||
for (const auto& pair : textures) {
|
||||
if (const auto& tex = pair.second) {
|
||||
std::static_pointer_cast<gl::Texture2D>(tex)->unbind();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -97,17 +97,12 @@ void FillLayerTweaker::execute(LayerGroupBase& layerGroup,
|
||||
Size textureSize = {0, 0};
|
||||
if (const auto shader = drawable.getShader()) {
|
||||
if (const auto index = shader->getSamplerLocation("u_image")) {
|
||||
const auto& textures = drawable.getTextures();
|
||||
const auto src = drawable.getTextureSource(*index);
|
||||
gfx::Texture2DPtr tex = src ? src() : nullptr;
|
||||
if (!tex && textures.size() > *index) {
|
||||
tex = textures[*index].texture;
|
||||
}
|
||||
if (tex) {
|
||||
if (const auto& tex = drawable.getTexture(*index)) {
|
||||
textureSize = tex->getSize();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const FillDrawableUBO drawableUBO = {
|
||||
/*.matrix=*/util::cast<float>(matrix),
|
||||
/*.scale=*/{pixelRatio, tileRatio, crossfade.fromScale, crossfade.toScale},
|
||||
|
||||
@@ -554,7 +554,9 @@ void RenderFillLayer::update(gfx::ShaderRegistry& shaders,
|
||||
|
||||
if (const auto& atlases = tile.getAtlasTextures()) {
|
||||
if (const auto samplerLocation = fillShader->getSamplerLocation("u_image")) {
|
||||
patternBuilder->setTextureSource(*samplerLocation, [=]() { return atlases->icon; });
|
||||
patternBuilder->setTextureSource([=]() {
|
||||
return gfx::Drawable::Textures{{*samplerLocation, atlases->icon}};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -571,7 +573,9 @@ void RenderFillLayer::update(gfx::ShaderRegistry& shaders,
|
||||
|
||||
if (const auto& atlases = tile.getAtlasTextures()) {
|
||||
if (const auto samplerLocation = outlineShader->getSamplerLocation("u_image")) {
|
||||
outlinePatternBuilder->setTextureSource(*samplerLocation, [=]() { return atlases->icon; });
|
||||
outlinePatternBuilder->setTextureSource([=]() {
|
||||
return gfx::Drawable::Textures{{*samplerLocation, atlases->icon}};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user