Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Shaders refactor #6856

Merged
merged 6 commits into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
74 changes: 22 additions & 52 deletions cmake/core-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ set(MBGL_CORE_FILES

# gl
include/mbgl/gl/gl.hpp
src/mbgl/gl/attribute.cpp
src/mbgl/gl/attribute.hpp
src/mbgl/gl/color_mode.cpp
src/mbgl/gl/color_mode.hpp
Expand All @@ -64,18 +65,17 @@ set(MBGL_CORE_FILES
src/mbgl/gl/depth_mode.cpp
src/mbgl/gl/depth_mode.hpp
src/mbgl/gl/draw_mode.hpp
src/mbgl/gl/drawable.hpp
src/mbgl/gl/extension.cpp
src/mbgl/gl/extension.hpp
src/mbgl/gl/framebuffer.hpp
src/mbgl/gl/gl.cpp
src/mbgl/gl/index_buffer.hpp
src/mbgl/gl/object.cpp
src/mbgl/gl/object.hpp
src/mbgl/gl/primitives.hpp
src/mbgl/gl/program.hpp
src/mbgl/gl/renderbuffer.hpp
src/mbgl/gl/segment.hpp
src/mbgl/gl/shader.cpp
src/mbgl/gl/shader.hpp
src/mbgl/gl/state.hpp
src/mbgl/gl/stencil_mode.cpp
src/mbgl/gl/stencil_mode.hpp
Expand Down Expand Up @@ -148,6 +148,25 @@ set(MBGL_CORE_FILES
include/mbgl/platform/default/settings_json.hpp
include/mbgl/platform/default/thread_pool.hpp

# programs
src/mbgl/programs/attributes.hpp
src/mbgl/programs/circle_program.cpp
src/mbgl/programs/circle_program.hpp
src/mbgl/programs/collision_box_program.cpp
src/mbgl/programs/collision_box_program.hpp
src/mbgl/programs/debug_program.hpp
src/mbgl/programs/fill_program.cpp
src/mbgl/programs/fill_program.hpp
src/mbgl/programs/line_program.cpp
src/mbgl/programs/line_program.hpp
src/mbgl/programs/program.hpp
src/mbgl/programs/programs.hpp
src/mbgl/programs/raster_program.cpp
src/mbgl/programs/raster_program.hpp
src/mbgl/programs/symbol_program.cpp
src/mbgl/programs/symbol_program.hpp
src/mbgl/programs/uniforms.hpp

# renderer
src/mbgl/renderer/bucket.hpp
src/mbgl/renderer/circle_bucket.cpp
Expand Down Expand Up @@ -180,55 +199,6 @@ set(MBGL_CORE_FILES
src/mbgl/renderer/symbol_bucket.cpp
src/mbgl/renderer/symbol_bucket.hpp

# shader
src/mbgl/shader/circle_shader.cpp
src/mbgl/shader/circle_shader.hpp
src/mbgl/shader/circle_uniforms.hpp
src/mbgl/shader/circle_vertex.cpp
src/mbgl/shader/circle_vertex.hpp
src/mbgl/shader/collision_box_shader.cpp
src/mbgl/shader/collision_box_shader.hpp
src/mbgl/shader/collision_box_uniforms.hpp
src/mbgl/shader/collision_box_vertex.cpp
src/mbgl/shader/collision_box_vertex.hpp
src/mbgl/shader/fill_outline_pattern_shader.cpp
src/mbgl/shader/fill_outline_pattern_shader.hpp
src/mbgl/shader/fill_outline_shader.cpp
src/mbgl/shader/fill_outline_shader.hpp
src/mbgl/shader/fill_pattern_shader.cpp
src/mbgl/shader/fill_pattern_shader.hpp
src/mbgl/shader/fill_shader.cpp
src/mbgl/shader/fill_shader.hpp
src/mbgl/shader/fill_uniforms.cpp
src/mbgl/shader/fill_uniforms.hpp
src/mbgl/shader/fill_vertex.cpp
src/mbgl/shader/fill_vertex.hpp
src/mbgl/shader/line_pattern_shader.cpp
src/mbgl/shader/line_pattern_shader.hpp
src/mbgl/shader/line_sdf_shader.cpp
src/mbgl/shader/line_sdf_shader.hpp
src/mbgl/shader/line_shader.cpp
src/mbgl/shader/line_shader.hpp
src/mbgl/shader/line_uniforms.cpp
src/mbgl/shader/line_uniforms.hpp
src/mbgl/shader/line_vertex.cpp
src/mbgl/shader/line_vertex.hpp
src/mbgl/shader/raster_shader.cpp
src/mbgl/shader/raster_shader.hpp
src/mbgl/shader/raster_uniforms.hpp
src/mbgl/shader/raster_vertex.cpp
src/mbgl/shader/raster_vertex.hpp
src/mbgl/shader/shaders.hpp
src/mbgl/shader/symbol_icon_shader.cpp
src/mbgl/shader/symbol_icon_shader.hpp
src/mbgl/shader/symbol_sdf_shader.cpp
src/mbgl/shader/symbol_sdf_shader.hpp
src/mbgl/shader/symbol_uniforms.cpp
src/mbgl/shader/symbol_uniforms.hpp
src/mbgl/shader/symbol_vertex.cpp
src/mbgl/shader/symbol_vertex.hpp
src/mbgl/shader/uniforms.hpp

# sprite
include/mbgl/sprite/sprite_image.hpp
src/mbgl/sprite/sprite_atlas.cpp
Expand Down
7 changes: 3 additions & 4 deletions cmake/shaders.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ function(add_shader VAR name)
set(shader_source_prefix ${MBGL_GENERATED}/include/mbgl/shader)

add_custom_command(
OUTPUT ${shader_source_prefix}/${name}.vertex.hpp ${shader_source_prefix}/${name}.fragment.hpp
COMMAND ${shader_build_cmd} ${shader_file_prefix}/${name}.vertex.glsl ${shader_source_prefix}/${name}.vertex.hpp
COMMAND ${shader_build_cmd} ${shader_file_prefix}/${name}.fragment.glsl ${shader_source_prefix}/${name}.fragment.hpp
OUTPUT ${shader_source_prefix}/${name}.hpp
COMMAND ${shader_build_cmd} ${shader_file_prefix}/${name}.vertex.glsl ${shader_file_prefix}/${name}.fragment.glsl ${shader_source_prefix}/${name}.hpp
DEPENDS ${shader_file_prefix}/${name}.vertex.glsl
DEPENDS ${shader_file_prefix}/${name}.fragment.glsl
VERBATIM
)
set(${VAR} ${${VAR}} ${shader_source_prefix}/${name}.vertex.hpp ${shader_source_prefix}/${name}.fragment.hpp PARENT_SCOPE)
set(${VAR} ${${VAR}} ${shader_source_prefix}/${name}.hpp PARENT_SCOPE)
endfunction()

add_shader(MBGL_SHADER_FILES circle)
Expand Down
1 change: 1 addition & 0 deletions cmake/test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ target_include_directories(mbgl-test
PRIVATE test/include
PRIVATE test/src
PRIVATE platform/default
PRIVATE ${MBGL_GENERATED}/include
)

target_link_libraries(mbgl-test
Expand Down
5 changes: 0 additions & 5 deletions include/mbgl/util/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,5 @@ struct MisuseException : Exception {
MisuseException(const std::string &msg) : Exception(msg) {}
};

struct ShaderException : Exception {
ShaderException(const char *msg) : Exception(msg) {}
ShaderException(const std::string &msg) : Exception(msg) {}
};

} // namespace util
} // namespace mbgl
86 changes: 36 additions & 50 deletions scripts/build-shaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,54 @@ var path = require('path');
var fs = require('fs');
var mkdirp = require('mkdirp');

var input_file = process.argv[2]
var output_file = process.argv[3];
var vertex_file = process.argv[2];
var fragment_file = process.argv[3];
var output_file = process.argv[4];

if (!input_file || !output_file) {
if (!vertex_file || !fragment_file || !output_file) {
console.warn('No input or output file given.');
console.warn('Usage: %s [input.vertex.glsl] [output.vertex.hpp]', path.basename(process.argv[1]));
console.warn('Usage: %s input.vertex.glsl input.fragment.glsl output.hpp', path.basename(process.argv[1]));
process.exit(1);
}

var components = path.basename(input_file).split('.');

var components = path.basename(vertex_file).split('.');
var shader_name = components[0];
var shader_type = components[1];
var extension = components[2];

var data = fs.readFileSync(input_file, 'utf8');

// Replace uniform pragmas

var pragma_mapbox_regex = /(\s*)#pragma\s+mapbox\s*:\s+(define|initialize)\s+(low|medium|high)p\s+(float|vec(?:2|3|4))\s+(.*)/;

var data = data.split('\n').map(function(line) {
var params = line.match(pragma_mapbox_regex);
if (params) {
if (params[2] === 'define') {
return params[1] + 'uniform ' + params[3] + 'p ' + params[4] + ' u_' + params[5] + ';';
function source(file) {
return '\n' + fs.readFileSync(file, 'utf8').split('\n').map(function(line) {
var params = line.match(pragma_mapbox_regex);
if (params) {
if (params[2] === 'define') {
return params[1] + 'uniform ' + params[3] + 'p ' + params[4] + ' u_' + params[5] + ';';
} else {
return params[1] + params[3] + 'p ' + params[4] + ' ' + params[5] + ' = u_' + params[5] + ';';
}
} else {
return params[1] + params[3] + 'p ' + params[4] + ' ' + params[5] + ' = u_' + params[5] + ';';
return line;
}
} else {
return line;
}
}).join('\n');

}).join('\n');
}

var content =
'#pragma once\n' +
'\n' +
'// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n' +
'\n' +
'#include <mbgl/gl/gl.hpp>\n' +
'\n' +
'namespace mbgl {\n' +
'namespace shaders {\n' +
'namespace ' + shader_name + ' {\n' +
'\n' +
'#ifndef MBGL_SHADER_NAME_' + shader_name.toUpperCase() + '\n' +
'#define MBGL_SHADER_NAME_' + shader_name.toUpperCase() + '\n' +
'constexpr const char* name = "' + shader_name + '";\n' +
'#endif // MBGL_SHADER_NAME_' + shader_name.toUpperCase() + '\n' +
'\n' +
'constexpr const char* ' + shader_type + ' =\n' +
'#ifdef GL_ES_VERSION_2_0\n' +
' "precision highp float;"\n' +
'#else\n' +
' "#version 120"\n' +
'#endif\n' +
' R"MBGL_SHADER(\n' + data + ')MBGL_SHADER";\n' +
'\n' +
'} // namespace ' + shader_name + '\n' +
'} // namespace shaders\n' +
'} // namespace mbgl\n';
var content = "#pragma once\n" +
"\n" +
"// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n" +
"\n" +
"#include <mbgl/gl/gl.hpp>\n" +
"\n" +
"namespace mbgl {\n" +
"namespace shaders {\n" +
"\n" +
"class " + shader_name + " {\n" +
"public:\n" +
" static constexpr const char* name = \"" + shader_name + "\";" +
" static constexpr const char* vertexSource = R\"MBGL_SHADER(" + source(vertex_file) + ")MBGL_SHADER\";\n" +
" static constexpr const char* fragmentSource = R\"MBGL_SHADER(" + source(fragment_file) + ")MBGL_SHADER\";\n" +
"};\n" +
"\n" +
"} // namespace shaders\n" +
"} // namespace mbgl\n";

mkdirp.sync(path.dirname(output_file));
fs.writeFileSync(output_file, content);
28 changes: 28 additions & 0 deletions src/mbgl/gl/attribute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <mbgl/gl/attribute.hpp>
#include <mbgl/gl/gl.hpp>

namespace mbgl {
namespace gl {

AttributeLocation attributeLocation(ProgramID id, const char* name) {
return MBGL_CHECK_ERROR(glGetAttribLocation(id, name));
}

void bindAttribute(AttributeLocation location,
std::size_t count,
DataType type,
std::size_t vertexSize,
std::size_t vertexOffset,
std::size_t attributeOffset) {
MBGL_CHECK_ERROR(glEnableVertexAttribArray(location));
MBGL_CHECK_ERROR(glVertexAttribPointer(
location,
static_cast<GLint>(count),
static_cast<GLenum>(type),
GL_FALSE,
static_cast<GLsizei>(vertexSize),
reinterpret_cast<GLvoid*>(attributeOffset + (vertexSize * vertexOffset))));
}

} // namespace gl
} // namespace mbgl
Loading