Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared attributes #1898

Merged
merged 7 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions include/mbgl/gfx/drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ using DrawableTweakerPtr = std::shared_ptr<DrawableTweaker>;
using IndexVectorBasePtr = std::shared_ptr<IndexVectorBase>;
using ShaderProgramBasePtr = std::shared_ptr<ShaderProgramBase>;
using Texture2DPtr = std::shared_ptr<Texture2D>;
using VertexAttributeArrayPtr = std::shared_ptr<VertexAttributeArray>;

class Drawable {
public:
Expand Down Expand Up @@ -188,16 +189,10 @@ class Drawable {
virtual void setColorMode(const gfx::ColorMode&);

/// Get the vertex attributes that override default values in the shader program
virtual const gfx::VertexAttributeArray& getVertexAttributes() const = 0;

/// Get the mutable vertex attribute array
virtual gfx::VertexAttributeArray& mutableVertexAttributes() = 0;

/// Set vertex attribute array
virtual void setVertexAttributes(const gfx::VertexAttributeArray&) = 0;
const gfx::VertexAttributeArrayPtr& getVertexAttributes() const noexcept { return vertexAttributes; }

/// Set vertex attribute array
virtual void setVertexAttributes(gfx::VertexAttributeArray&&) = 0;
void setVertexAttributes(gfx::VertexAttributeArrayPtr value) noexcept { vertexAttributes = std::move(value); }

/// Provide raw data for vertices. Incompatible with adding primitives
virtual void setVertices(std::vector<uint8_t>&&, std::size_t, AttributeDataType) = 0;
Expand Down Expand Up @@ -259,6 +254,7 @@ class Drawable {
int32_t subLayerIndex = 0;
DepthMaskType depthType; // = DepthMaskType::ReadOnly;
UniqueDrawableData drawableData{};
gfx::VertexAttributeArrayPtr vertexAttributes;

struct Impl;
std::unique_ptr<Impl> impl;
Expand Down
13 changes: 7 additions & 6 deletions include/mbgl/gfx/drawable_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class DrawableBuilder {
UniqueDrawable& getCurrentDrawable(bool createIfNone);

/// Close the current drawable, using a new one for any further work
void flush();
void flush(gfx::Context& context);

/// Get all the completed drawables
const std::vector<UniqueDrawable>& getDrawables() const { return drawables; }
Expand Down Expand Up @@ -134,9 +134,6 @@ class DrawableBuilder {
/// Set the shader to be used
void setShader(gfx::ShaderProgramBasePtr value) { shader = std::move(value); }

/// Get the vertex attributes that override default values in the shader program
const gfx::VertexAttributeArray& getVertexAttributes() const;

/// Set the name given to new drawables
void setDrawableName(std::string value) { drawableName = std::move(value); }

Expand All @@ -157,9 +154,12 @@ class DrawableBuilder {
/// Clear the tweaker collection
void clearTweakers() { tweakers.clear(); }

/// Get the vertex attributes that override default values in the shader program
VertexAttributeArrayPtr& getVertexAttributes() { return vertexAttrs; }
const VertexAttributeArrayPtr& getVertexAttributes() const { return vertexAttrs; }

/// A set of attribute values to be added for each vertex
void setVertexAttributes(const VertexAttributeArray& value);
void setVertexAttributes(VertexAttributeArray&&);
void setVertexAttributes(VertexAttributeArrayPtr attrs) { vertexAttrs = std::move(attrs); }

/// Add some vertex elements, returns the index of the first one added
std::size_t addVertices(const std::vector<std::array<int16_t, 2>>& vertices,
Expand Down Expand Up @@ -232,6 +232,7 @@ class DrawableBuilder {
std::vector<UniqueDrawable> drawables;
gfx::Drawable::Textures textures;
std::vector<DrawableTweakerPtr> tweakers;
gfx::VertexAttributeArrayPtr vertexAttrs;

class Impl;
std::unique_ptr<Impl> impl;
Expand Down
14 changes: 1 addition & 13 deletions include/mbgl/gfx/vertex_attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,23 +366,11 @@ class VertexAttributeArray {
}

VertexAttributeArray& operator=(VertexAttributeArray&&);
VertexAttributeArray& operator=(const VertexAttributeArray&);
VertexAttributeArray& operator=(const VertexAttributeArray&) = delete;

/// Clone the collection
virtual UniqueVertexAttributeArray clone() const {
auto newAttrs = std::make_unique<VertexAttributeArray>();
newAttrs->copy(*this);
return newAttrs;
}

/// Copy another collection into this one
virtual void copy(const VertexAttributeArray& other) {
for (const auto& kv : other.attrs) {
if (kv.second) {
attrs.insert(std::make_pair(kv.first, copy(*kv.second)));
}
}
}

/// Specialized DataDrivenPaintProperty reader
/// @param binders Property binders for the target shader
Expand Down
6 changes: 0 additions & 6 deletions include/mbgl/gl/drawable_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ class DrawableGL : public gfx::Drawable {

void setVertices(std::vector<uint8_t>&&, std::size_t, gfx::AttributeDataType) override;

const gfx::VertexAttributeArray& getVertexAttributes() const override;
void setVertexAttributes(const gfx::VertexAttributeArray& value) override;
void setVertexAttributes(gfx::VertexAttributeArray&& value) override;

gfx::VertexAttributeArray& mutableVertexAttributes() override;

const gfx::UniformBufferArray& getUniformBuffers() const override;
gfx::UniformBufferArray& mutableUniformBuffers() override;

Expand Down
11 changes: 1 addition & 10 deletions include/mbgl/gl/vertex_attribute_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,7 @@ class VertexAttributeArrayGL final : public gfx::VertexAttributeArray {
VertexAttributeArray::operator=(std::move(other));
return *this;
}
VertexAttributeArrayGL& operator=(const VertexAttributeArrayGL& other) {
VertexAttributeArray::operator=(other);
return *this;
}

std::unique_ptr<VertexAttributeArray> clone() const override {
auto newAttrs = std::make_unique<VertexAttributeArrayGL>();
newAttrs->copy(*this);
return newAttrs;
}
VertexAttributeArrayGL& operator=(const VertexAttributeArrayGL&) = delete;

/// Indicates whether any values have changed
bool isDirty() const override;
Expand Down
9 changes: 7 additions & 2 deletions include/mbgl/mtl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ namespace mbgl {
class ProgramParameters;
class RenderStaticData;

namespace gfx {
class VertexAttributeArray;
using VertexAttributeArrayPtr = std::shared_ptr<VertexAttributeArray>;
} // namespace gfx

namespace shaders {
struct ClipUBO;
} // namespace shaders
Expand Down Expand Up @@ -85,8 +90,6 @@ class Context final : public gfx::Context {

RenderTargetPtr createRenderTarget(const Size size, const gfx::TextureChannelDataType type) override;

// UniqueFramebuffer createFramebuffer(const gfx::Texture2D& color);

void resetState(gfx::DepthMode depthMode, gfx::ColorMode colorMode) override;

void setDirtyState() override;
Expand All @@ -104,6 +107,8 @@ class Context final : public gfx::Context {

std::unique_ptr<gfx::DrawScopeResource> createDrawScopeResource() override;

gfx::VertexAttributeArrayPtr createVertexAttributeArray() const override;

#if !defined(NDEBUG)
void visualizeStencilBuffer() override;
void visualizeDepthBuffer(float depthRangeSize) override;
Expand Down
6 changes: 0 additions & 6 deletions include/mbgl/mtl/drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ class Drawable : public gfx::Drawable {

void setVertices(std::vector<uint8_t>&&, std::size_t, gfx::AttributeDataType) override;

const gfx::VertexAttributeArray& getVertexAttributes() const override;
void setVertexAttributes(const gfx::VertexAttributeArray& value) override;
void setVertexAttributes(gfx::VertexAttributeArray&& value) override;

gfx::VertexAttributeArray& mutableVertexAttributes() override;

const gfx::UniformBufferArray& getUniformBuffers() const override;
gfx::UniformBufferArray& mutableUniformBuffers() override;

Expand Down
23 changes: 2 additions & 21 deletions include/mbgl/mtl/vertex_attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ class VertexAttribute final : public gfx::VertexAttribute {
friend VertexAttributeArray;
VertexAttribute(int index_, gfx::AttributeDataType dataType_, std::size_t count_)
: gfx::VertexAttribute(index_, dataType_, count_) {}
VertexAttribute(const VertexAttribute& other)
: gfx::VertexAttribute(other) {}
VertexAttribute(const VertexAttribute& other) = delete;
VertexAttribute(VertexAttribute&& other)
: gfx::VertexAttribute(std::move(other)) {}

public:
~VertexAttribute() override = default;

/// Get the Metal buffer, creating it if necessary
// const gfx::UniqueVertexBufferResource& getBuffer(UploadPass&, const gfx::BufferUsageType);

static const gfx::UniqueVertexBufferResource& getBuffer(gfx::VertexAttribute&,
UploadPass&,
const gfx::BufferUsageType);
Expand All @@ -45,16 +41,7 @@ class VertexAttributeArray final : public gfx::VertexAttributeArray {
gfx::VertexAttributeArray::operator=(std::move(other));
return *this;
}
VertexAttributeArray& operator=(const VertexAttributeArray& other) {
gfx::VertexAttributeArray::operator=(other);
return *this;
}

gfx::UniqueVertexAttributeArray clone() const override {
auto newAttrs = std::make_unique<VertexAttributeArray>();
newAttrs->copy(*this);
return newAttrs;
}
VertexAttributeArray& operator=(const VertexAttributeArray& other) = delete;

/// Indicates whether any values have changed
bool isDirty() const override;
Expand All @@ -63,12 +50,6 @@ class VertexAttributeArray final : public gfx::VertexAttributeArray {
gfx::UniqueVertexAttribute create(int index, gfx::AttributeDataType dataType, std::size_t count) const override {
return gfx::UniqueVertexAttribute(new VertexAttribute(index, dataType, count));
}

using gfx::VertexAttributeArray::copy;

gfx::UniqueVertexAttribute copy(const gfx::VertexAttribute& attr) const override {
return gfx::UniqueVertexAttribute(new VertexAttribute(static_cast<const VertexAttribute&>(attr)));
}
};

} // namespace mtl
Expand Down
2 changes: 0 additions & 2 deletions include/mbgl/shaders/gl/shader_program_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ class ShaderProgramGL final : public gfx::ShaderProgramBase {
protected:
gfx::UniformBlockArray& mutableUniformBlocks() override { return uniformBlocks; }

gfx::VertexAttributeArray& mutableVertexAttributes() override { return vertexAttributes; }

protected:
UniqueProgram glProgram;

Expand Down
1 change: 0 additions & 1 deletion include/mbgl/shaders/mtl/shader_program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class ShaderProgram final : public gfx::ShaderProgramBase {
std::optional<uint32_t> getSamplerLocation(const StringIdentity id) const override;

const gfx::VertexAttributeArray& getVertexAttributes() const override { return vertexAttributes; }
gfx::VertexAttributeArray& mutableVertexAttributes() override { return vertexAttributes; }

const gfx::UniformBlockArray& getUniformBlocks() const override { return uniformBlocks; }
gfx::UniformBlockArray& mutableUniformBlocks() override { return uniformBlocks; }
Expand Down
6 changes: 0 additions & 6 deletions include/mbgl/shaders/shader_program_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,8 @@ class ShaderProgramBase : public gfx::Shader {
/// Get the available vertex attributes and their default values
virtual const gfx::VertexAttributeArray& getVertexAttributes() const = 0;

template <typename T>
bool setAttribute(const std::string& name, std::size_t i, T value) {
return set(mutableVertexAttributes(), name, i, value);
}

protected:
virtual gfx::UniformBlockArray& mutableUniformBlocks() = 0;
virtual gfx::VertexAttributeArray& mutableVertexAttributes() = 0;

protected:
util::SimpleIdentity shaderProgramID;
Expand Down
31 changes: 11 additions & 20 deletions src/mbgl/gfx/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ class Drawable;
class DrawableBuilder;
class ShaderProgramBase;
class Texture2D;
class VertexAttributeArray;

using DrawablePtr = std::shared_ptr<Drawable>;
using UniqueDrawableBuilder = std::unique_ptr<DrawableBuilder>;
using UniformBufferPtr = std::shared_ptr<UniformBuffer>;
using ShaderProgramBasePtr = std::shared_ptr<ShaderProgramBase>;
using Texture2DPtr = std::shared_ptr<Texture2D>;
using UniformBufferPtr = std::shared_ptr<UniformBuffer>;
using UniqueDrawableBuilder = std::unique_ptr<DrawableBuilder>;
using VertexAttributeArrayPtr = std::shared_ptr<VertexAttributeArray>;
#endif

class Context {
Expand All @@ -57,57 +59,40 @@ class Context {
static constexpr const uint32_t minimumRequiredVertexBindingCount = 8;
const uint32_t maximumVertexBindingCount;

public:
Context(Context&&) = delete;
Context(const Context&) = delete;
Context& operator=(Context&& other) = delete;
Context& operator=(const Context& other) = delete;
virtual ~Context() = default;

public:
/// Called at the end of a frame.
virtual void performCleanup() = 0;

/// Called when the app receives a memory warning and before it goes to the background.
virtual void reduceMemoryUsage() = 0;

public:
virtual std::unique_ptr<OffscreenTexture> createOffscreenTexture(Size, TextureChannelDataType) = 0;

public:
/// Creates an empty texture with the specified dimensions.
Texture createTexture(const Size size,
TexturePixelType format = TexturePixelType::RGBA,
TextureChannelDataType type = TextureChannelDataType::UnsignedByte) {
return {size, createTextureResource(size, format, type)};
}

protected:
virtual std::unique_ptr<TextureResource> createTextureResource(Size, TexturePixelType, TextureChannelDataType) = 0;

public:
template <RenderbufferPixelType pixelType>
Renderbuffer<pixelType> createRenderbuffer(const Size size) {
return {size, createRenderbufferResource(pixelType, size)};
}

protected:
virtual std::unique_ptr<RenderbufferResource> createRenderbufferResource(RenderbufferPixelType, Size) = 0;

public:
DrawScope createDrawScope() { return DrawScope{createDrawScopeResource()}; }

protected:
virtual std::unique_ptr<DrawScopeResource> createDrawScopeResource() = 0;

public:
virtual std::unique_ptr<CommandEncoder> createCommandEncoder() = 0;

gfx::RenderingStats& renderingStats() { return stats; }
const gfx::RenderingStats& renderingStats() const { return stats; }

#if !defined(NDEBUG)
public:
virtual void visualizeStencilBuffer() = 0;
virtual void visualizeDepthBuffer(float depthRangeSize) = 0;
#endif
Expand All @@ -118,7 +103,9 @@ class Context {
virtual void setDirtyState() = 0;

#if MLN_DRAWABLE_RENDERER
public:
/// Create a new vertex attribute array
virtual gfx::VertexAttributeArrayPtr createVertexAttributeArray() const = 0;

/// Create a new drawable builder
virtual UniqueDrawableBuilder createDrawableBuilder(std::string name) = 0;

Expand Down Expand Up @@ -166,6 +153,10 @@ class Context {
#endif

protected:
virtual std::unique_ptr<TextureResource> createTextureResource(Size, TexturePixelType, TextureChannelDataType) = 0;
virtual std::unique_ptr<RenderbufferResource> createRenderbufferResource(RenderbufferPixelType, Size) = 0;
virtual std::unique_ptr<DrawScopeResource> createDrawScopeResource() = 0;

gfx::RenderingStats stats;
};

Expand Down
Loading
Loading