#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace mbgl { namespace gl { template class Program final : public gfx::Program { public: using AttributeList = typename Name::AttributeList; using UniformList = typename Name::UniformList; using TextureList = typename Name::TextureList; Program(ProgramParameters programParameters_) : programParameters(std::move(programParameters_)) {} const ProgramParameters programParameters; class Instance { public: Instance(Context& context, const std::initializer_list& vertexSource, const std::initializer_list& fragmentSource) : program(context.createProgram(context.createShader(ShaderType::Vertex, vertexSource), context.createShader(ShaderType::Fragment, fragmentSource), attributeLocations.getFirstAttribName())) { attributeLocations.queryLocations(program); uniformStates.queryLocations(program); // Texture units are specified via uniforms as well, so we need query their locations textureStates.queryLocations(program); } static std::unique_ptr createInstance(gl::Context& context, const ProgramParameters& programParameters, const std::string& additionalDefines) { #if MLN_RENDER_BACKEND_OPENGL constexpr auto backend = gfx::Backend::Type::OpenGL; #elif MLN_RENDER_BACKEND_METAL constexpr auto backend = gfx::Backend::Type::Metal; #endif #if MLN_RENDER_BACKEND_METAL return std::make_unique(context); #else // Compile the shader std::initializer_list vertexSource = { "#version 300 es\n", programParameters.getDefinesString().c_str(), additionalDefines.c_str(), shaders::ShaderSource::vertex, programParameters.vertexSource(gfx::Backend::Type::OpenGL).c_str()}; std::initializer_list fragmentSource = { "#version 300 es\n", programParameters.getDefinesString().c_str(), additionalDefines.c_str(), shaders::ShaderSource::fragment, programParameters.fragmentSource(gfx::Backend::Type::OpenGL).c_str()}; return std::make_unique(context, vertexSource, fragmentSource); #endif } UniqueProgram program; gl::AttributeLocations attributeLocations; gl::UniformStates uniformStates; gl::TextureStates textureStates; }; void draw(gfx::Context& genericContext, gfx::RenderPass&, const gfx::DrawMode& drawMode, const gfx::DepthMode& depthMode, const gfx::StencilMode& stencilMode, const gfx::ColorMode& colorMode, const gfx::CullFaceMode& cullFaceMode, const gfx::UniformValues& uniformValues, gfx::DrawScope& drawScope, const gfx::AttributeBindings& attributeBindings, const gfx::TextureBindings& textureBindings, const gfx::IndexBuffer& indexBuffer, std::size_t indexOffset, std::size_t indexLength) override { auto& context = static_cast(genericContext); context.setDepthMode(depthMode); context.setStencilMode(stencilMode); context.setColorMode(colorMode); context.setCullFaceMode(cullFaceMode); const uint32_t key = gl::AttributeKey::compute(attributeBindings); auto it = instances.find(key); if (it == instances.end()) { try { it = instances .emplace(key, Instance::createInstance(context, programParameters, gl::AttributeKey::defines(attributeBindings))) .first; } catch (const std::runtime_error& e) { Log::Error(Event::OpenGL, e.what()); return; } } auto& instance = *it->second; context.program = instance.program; instance.uniformStates.bind(uniformValues); instance.textureStates.bind(context, textureBindings); auto& vertexArray = drawScope.getResource().vertexArray; vertexArray.bind(context, indexBuffer, instance.attributeLocations.toBindingArray(attributeBindings)); context.draw(drawMode, indexOffset, indexLength); } private: std::map> instances; }; } // namespace gl } // namespace mbgl