summaryrefslogtreecommitdiff
path: root/src/mbgl/gl/program.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mbgl/gl/program.hpp')
-rw-r--r--src/mbgl/gl/program.hpp118
1 files changed, 59 insertions, 59 deletions
diff --git a/src/mbgl/gl/program.hpp b/src/mbgl/gl/program.hpp
index ffa3e07391..9028ffdf53 100644
--- a/src/mbgl/gl/program.hpp
+++ b/src/mbgl/gl/program.hpp
@@ -1,80 +1,79 @@
#pragma once
+#include <mbgl/gfx/program.hpp>
#include <mbgl/gl/types.hpp>
#include <mbgl/gl/object.hpp>
#include <mbgl/gl/context.hpp>
+#include <mbgl/gl/draw_scope_resource.hpp>
#include <mbgl/gfx/vertex_buffer.hpp>
#include <mbgl/gfx/index_buffer.hpp>
+#include <mbgl/gfx/uniform.hpp>
#include <mbgl/gl/vertex_array.hpp>
#include <mbgl/gl/attribute.hpp>
#include <mbgl/gl/uniform.hpp>
#include <mbgl/gl/texture.hpp>
-
+#include <mbgl/gl/features.hpp>
+#include <mbgl/gl/binary_program.hpp>
#include <mbgl/util/io.hpp>
+
#include <mbgl/util/logging.hpp>
-#include <mbgl/programs/binary_program.hpp>
#include <mbgl/programs/program_parameters.hpp>
-#include <mbgl/shaders/shaders.hpp>
+#include <mbgl/programs/gl/shaders.hpp>
#include <string>
namespace mbgl {
namespace gl {
-template <class P, class As, class Us, class TextureList>
-class Program {
+template <class Name>
+class Program final : public gfx::Program<Name> {
public:
- using Primitive = P;
- using Attributes = As;
- using Uniforms = Us;
-
- using UniformValues = typename Uniforms::Values;
- using AttributeBindings = typename Attributes::Bindings;
- using TextureBindings = gfx::TextureBindings<TextureList>;
-
+ using AttributeList = typename Name::AttributeList;
+ using UniformList = typename Name::UniformList;
+ using TextureList = typename Name::TextureList;
+
Program(Context& context, const std::string& vertexSource, const std::string& fragmentSource)
: program(
context.createProgram(context.createShader(ShaderType::Vertex, vertexSource),
context.createShader(ShaderType::Fragment, fragmentSource))),
- uniformsState((context.linkProgram(program), Uniforms::bindLocations(program))),
- attributeLocations(Attributes::bindLocations(context, program)) {
- // Re-link program after manually binding only active attributes in Attributes::bindLocations
+ attributeLocations(context, program) {
+ // Re-link program after manually binding only active attributes in Attributes::queryLocations
context.linkProgram(program);
// We have to re-initialize the uniforms state from the bindings as the uniform locations
// get shifted on some implementations
- uniformsState = Uniforms::bindLocations(program);
+ uniformStates.queryLocations(program);
// Texture units are specified via uniforms as well, so we need query their locations
- textures.queryLocations(program);
+ textureStates.queryLocations(program);
}
template <class BinaryProgram>
Program(Context& context, const BinaryProgram& binaryProgram)
: program(context.createProgram(binaryProgram.format(), binaryProgram.code())),
- uniformsState(Uniforms::loadNamedLocations(binaryProgram)),
- attributeLocations(Attributes::loadNamedLocations(binaryProgram)) {
- textures.loadNamedLocations(binaryProgram);
+ attributeLocations(binaryProgram) {
+ uniformStates.loadNamedLocations(binaryProgram);
+ textureStates.loadNamedLocations(binaryProgram);
}
-
- static Program createProgram(gl::Context& context,
- const ProgramParameters& programParameters,
- const char* name,
- const char* vertexSource_,
- const char* fragmentSource_) {
- const std::string vertexSource = shaders::vertexSource(programParameters, vertexSource_);
- const std::string fragmentSource = shaders::fragmentSource(programParameters, fragmentSource_);
+
+ static std::unique_ptr<Program> createProgram(gl::Context& context,
+ const ProgramParameters& programParameters,
+ const char* name,
+ const char* vertexSource_,
+ const char* fragmentSource_) {
+ const std::string vertexSource = programs::gl::vertexSource(programParameters, vertexSource_);
+ const std::string fragmentSource = programs::gl::fragmentSource(programParameters, fragmentSource_);
#if MBGL_HAS_BINARY_PROGRAMS
optional<std::string> cachePath = programParameters.cachePath(name);
if (cachePath && context.supportsProgramBinaries()) {
- const std::string identifier = shaders::programIdentifier(vertexSource, fragmentSource);
+ const std::string identifier = programs::gl::programIdentifier(vertexSource, fragmentSource);
try {
if (auto cachedBinaryProgram = util::readFile(*cachePath)) {
const BinaryProgram binaryProgram(std::move(*cachedBinaryProgram));
if (binaryProgram.identifier() == identifier) {
- return Program { context, binaryProgram };
+ return std::make_unique<Program>(context, binaryProgram);
} else {
Log::Warning(Event::OpenGL,
"Cached program %s changed. Recompilation required.",
@@ -87,11 +86,11 @@ public:
}
// Compile the shader
- Program result{ context, vertexSource, fragmentSource };
+ auto result = std::make_unique<Program>(context, vertexSource, fragmentSource);
try {
if (const auto binaryProgram =
- result.template get<BinaryProgram>(context, identifier)) {
+ result->template get<BinaryProgram>(context, identifier)) {
util::write_file(*cachePath, binaryProgram->serialize());
Log::Warning(Event::OpenGL, "Caching program in: %s", (*cachePath).c_str());
}
@@ -104,37 +103,37 @@ public:
#endif
(void)name;
- return Program { context, vertexSource, fragmentSource };
+ return std::make_unique<Program>(context, vertexSource, fragmentSource);
}
template <class BinaryProgram>
optional<BinaryProgram> get(Context& context, const std::string& identifier) const {
if (auto binaryProgram = context.getBinaryProgram(program)) {
- return BinaryProgram{ binaryProgram->first, std::move(binaryProgram->second),
- identifier, Attributes::getNamedLocations(attributeLocations),
- Uniforms::getNamedLocations(uniformsState),
- textures.getNamedLocations() };
+ return BinaryProgram{ binaryProgram->first,
+ std::move(binaryProgram->second),
+ identifier,
+ attributeLocations.getNamedLocations(),
+ uniformStates.getNamedLocations(),
+ textureStates.getNamedLocations() };
}
return {};
}
- template <class DrawMode>
- void draw(Context& context,
- DrawMode drawMode,
- gfx::DepthMode depthMode,
- gfx::StencilMode stencilMode,
- gfx::ColorMode colorMode,
- gfx::CullFaceMode cullFaceMode,
- const UniformValues& uniformValues,
- gl::VertexArray& vertexArray,
- const AttributeBindings& attributeBindings,
- const TextureBindings& textureBindings,
+ void draw(gfx::Context& genericContext,
+ const gfx::DrawMode& drawMode,
+ const gfx::DepthMode& depthMode,
+ const gfx::StencilMode& stencilMode,
+ const gfx::ColorMode& colorMode,
+ const gfx::CullFaceMode& cullFaceMode,
+ const gfx::UniformValues<UniformList>& uniformValues,
+ gfx::DrawScope& drawScope,
+ const gfx::AttributeBindings<AttributeList>& attributeBindings,
+ const gfx::TextureBindings<TextureList>& textureBindings,
const gfx::IndexBuffer& indexBuffer,
std::size_t indexOffset,
- std::size_t indexLength) {
- static_assert(std::is_same<Primitive, typename DrawMode::Primitive>::value, "incompatible draw mode");
+ std::size_t indexLength) override {
+ auto& context = reinterpret_cast<gl::Context&>(genericContext);
- context.setDrawMode(drawMode);
context.setDepthMode(depthMode);
context.setStencilMode(stencilMode);
context.setColorMode(colorMode);
@@ -142,15 +141,16 @@ public:
context.program = program;
- Uniforms::bind(uniformsState, uniformValues);
+ uniformStates.bind(uniformValues);
- textures.bind(context, textureBindings);
+ textureStates.bind(context, textureBindings);
+ auto& vertexArray = reinterpret_cast<gl::DrawScopeResource&>(*drawScope.resource).vertexArray;
vertexArray.bind(context,
indexBuffer,
- Attributes::toBindingArray(attributeLocations, attributeBindings));
+ attributeLocations.toBindingArray(attributeBindings));
- context.draw(drawMode.primitiveType,
+ context.draw(drawMode,
indexOffset,
indexLength);
}
@@ -158,9 +158,9 @@ public:
private:
UniqueProgram program;
- typename Uniforms::State uniformsState;
- typename Attributes::Locations attributeLocations;
- gl::Textures<TextureList> textures;
+ gl::AttributeLocations<AttributeList> attributeLocations;
+ gl::UniformStates<UniformList> uniformStates;
+ gl::TextureStates<TextureList> textureStates;
};
} // namespace gl