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

[core] update shaders for line property functions #6658

Merged
merged 4 commits into from
Nov 17, 2016
Merged

[core] update shaders for line property functions #6658

merged 4 commits into from
Nov 17, 2016

Conversation

mollymerp
Copy link
Contributor

@mollymerp mollymerp commented Oct 11, 2016

attempting to make native work with the shaders update at mapbox/mapbox-gl-shaders#24

Currently the macOS build is 'succeeding' but it freezes/times out and the actual app never opens so I'm definitely doing something wrong -- possibly that you can't #define macros like that in OpenGL or something? 🔧 fixed

I'm planning on having the device pixel ratio not be hard-coded in the build-shaders script, but I figured I'd try to get that working as a first step 💭 🔧 fixed

cc @lucaswoj

@mollymerp mollymerp added the ⚠️ DO NOT MERGE Work in progress, proof of concept, or on hold label Oct 11, 2016
@mention-bot
Copy link

@mollymerp, thanks for your PR! By analyzing the history of the files in this pull request, we identified @kkaefer, @jfirebaugh and @ansis to be potential reviewers.

@mollymerp mollymerp added the Core The cross-platform C++ core, aka mbgl label Oct 11, 2016
@mollymerp mollymerp changed the title update shaders for line property functions [core] update shaders for line property functions Oct 11, 2016
@@ -33,12 +33,15 @@ var data = data.split('\n').map(function(line) {
} else {
return params[1] + params[3] + 'p ' + params[4] + ' ' + params[5] + ' = u_' + params[5] + ';';
}
} else if (line.match(pixel_ratio_regex)){
return '\n#define DEVICE_PIXEL_RATIO 2.0\n' + line;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line replaces any occurrences of /(.*)#define(.*)DEVICE_PIXEL_RATIO(.*)/ in the shader source with #define DEVICE_PIXEL_RATIO 2.0. Are there occurrences of /(.*)#define(.*)DEVICE_PIXEL_RATIO(.*)/ in the shader source?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually inserts the hardcoded definition before (#define DEVICE_PIXEL_RATIO 2.0) before the matching line in the line-*.vertex shaders. so the output is

#define DEVICE_PIXEL_RATIO 2.0
#define ANTIALIASING 1.0 / DEVICE_PIXEL_RATIO / 2.0

I've confirmed the build-shaders script is producing the .hpp shaders with the expected result but at least one of the shaders is not compiling and I can't find a helpful error message ☹️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mollymerp There are some shaders which use the DEVICE_PIXEL_RATIO outside of #define statements. I suggest following the precedent set in GL JS and adding this #define to the top of all shaders.

@mollymerp
Copy link
Contributor Author

Ok I fixed a bug in mapbox/mapbox-gl-shaders#24 and now everything is compiling/building, and I think all I have left to do is figure out how to access view.getPixelRatio() from src/mbgl/gl/shader.cpp so that I can dynamically #define the device pixel ratio instead of hard-coding it at 2.0.

@mollymerp
Copy link
Contributor Author

Soooo this is more or less working, but the render tests show that (I think) antialiasing is messed up and maybe color too? I'll need to investigate that more.

: name(name_),
program(context.createProgram()),
vertexShader(context.createVertexShader()),
fragmentShader(context.createFragmentShader()) {
util::stopwatch stopwatch("shader compilation", Event::Shader);

if (!compileShader(vertexShader, vertexSource)) {
std::string vertex(vertexSource);
vertex.replace(vertex.find_first_of('\n'), 1,"\n#define DEVICE_PIXEL_RATIO " + std::to_string(shaderParameters.pixelRatio) + "\n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure hard-coding the device pixel ratio is a good idea. On desktop, we can switch windows between screens with differing pixel ratio. We don't reload any sprites, but we still do a best effort to render them at the correct size.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take that back; we are initializing Map with a constant pixel ratio that can't be changed after the fact anyway.

std::string fragment(fragmentSource);
if (defines & Defines::Overdraw) {
fragment.replace(fragment.find_first_of('\n'), 1,"\n#define DEVICE_PIXEL_RATIO "+ std::to_string(shaderParameters.pixelRatio) + "\n");
if ((strcmp(name, "collision_box") != 0) & shaderParameters.overdraw) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this hard-coded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I couldn't figure out how to modify things such that the collision_box_shader is always initialized with overdraw = false 😳

#ifndef NDEBUG
shaderParametersOverdraw = gl::ShaderParameters{ frame.pixelRatio, true };
overdrawShaders = std::make_unique<Shaders>(context, shaderParametersOverdraw);
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#if/endif should not be indented; and the code it surrounds needs to be unindented. You can use clang-format to apply our standard code formatting.

shaders = std::make_unique<Shaders>(context, shaderParameters);
#ifndef NDEBUG
shaderParametersOverdraw = gl::ShaderParameters{ frame.pixelRatio, true };
overdrawShaders = std::make_unique<Shaders>(context, shaderParametersOverdraw);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you move this to the render() method? This means the shaders are initialized on every frame, which isn't efficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I put it originally in the Painter initialization, DEVICE_PIXEL_RATIO was getting set to 0 all the time 😬

@@ -151,6 +153,8 @@ class Painter : private util::noncopyable {
LineAtlas* lineAtlas = nullptr;

FrameHistory frameHistory;
gl::ShaderParameters shaderParameters;
gl::ShaderParameters shaderParametersOverdraw;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ShaderParameters are only used to initialize the shaders; there's no need to keep them around.

@@ -31,7 +32,7 @@ class Shader : private util::noncopyable {
const char* vertex,
const char* fragment,
Context&,
Defines defines = Defines::None);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Defines isn't used anymore, you should also remove the enum definition.

@kkaefer
Copy link
Member

kkaefer commented Nov 4, 2016

There seem to be a few commits in here that track your progress rather than individual self-contained changes (and some commented out code added, then removed subsequently). Can you please merge/split commits so that the changes represent self-contained units? Our general rule of thumb is that every commit in gl-native should have all tests passing. This helps bigly when using git bisect to find regressions later on.

@mollymerp
Copy link
Contributor Author

Thanks for the review @kkaefer ! I will squash my commits when this is closer to merging because I don't think any of these commits have all tests passing so far.

@mollymerp mollymerp force-pushed the dpr branch 3 times, most recently from 51871b7 to 162da32 Compare November 11, 2016 01:35
@mollymerp
Copy link
Contributor Author

Ok I think this is finally ready for another glance @kkaefer @jfirebaugh 👀

Copy link
Contributor

@jfirebaugh jfirebaugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, pretty much just formatting/style nits at this point.

@@ -20,10 +20,9 @@
"csscolorparser": "^1.0.2",
"ejs": "^2.4.1",
"express": "^4.11.1",
"lodash": "^4.16.4",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert the lodash removal -- this is still used in a couple places.

@@ -40,7 +40,7 @@ Context::~Context() {

UniqueShader Context::createShader(ShaderType type, const std::string& source) {
UniqueShader result { MBGL_CHECK_ERROR(glCreateShader(static_cast<GLenum>(type))), { this } };

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert the whitespace changes in this file.

@@ -12,13 +12,11 @@ static_assert(sizeof(LineAttributes::Vertex) == 8, "expected LineVertex size");

template <class Values, class...Args>
Values makeValues(const style::LinePaintProperties& properties,
float pixelRatio,
const RenderTile& tile,
const TransformState& state,
Args&&... args) {
// the distance over which the line edge fades out.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the comments here; they were attached to the line that was deleted.

std::string fragment = Shaders::fragmentSource;
if (defines == ProgramDefines::Overdraw) {

fragment.replace(fragment.find_first_of('\n'), 1,"\n#define DEVICE_PIXEL_RATIO "+ std::to_string(programParameters.pixelRatio) + "\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of finding the first newline here? Could it instead simply prepend "#define DEVICE_PIXEL_RATIO ...\n" to the source?

symbolGlyph(context, defines),
debug(context, ProgramDefines::None),
collisionBox(context, ProgramDefines::None) {
Programs(gl::Context& context, ProgramParameters& programParameters)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const ProgramParameters&


namespace mbgl {



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this added whitespace.

@@ -21,6 +22,7 @@
#include <mbgl/geometry/line_atlas.hpp>
#include <mbgl/text/glyph_atlas.hpp>

#include <mbgl/programs/program_parameters.hpp>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate #include.


namespace mbgl {

class ProgramParameters {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting nits:

class ProgramParameters {
public:
    ProgramParameters(float pixelRatio_ = 1.0, bool overdraw_ = false)
      : pixelRatio(pixelRatio_),
        overdraw(overdraw_) {}

    float pixelRatio;
    bool overdraw;
};

@@ -151,6 +152,7 @@ class Painter : private util::noncopyable {

FrameHistory frameHistory;


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert whitespace change.

: pixelsToGLUnits },
uniforms::u_devicepixelratio::Value{ frame.pixelRatio },
},
: pixelsToGLUnits } },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting nit:

                : pixelsToGLUnits }
         },
         *bucket.vertexBuffer,

@mollymerp mollymerp force-pushed the dpr branch 2 times, most recently from 740dced to 4e6da9e Compare November 12, 2016 00:16
@mollymerp
Copy link
Contributor Author

Thanks @jfirebaugh -- fixed and squashed!

@mollymerp
Copy link
Contributor Author

std::string fragment = Shaders::fragmentSource;
if (defines == ProgramDefines::Overdraw) {

fragment.insert(0, "\n#define DEVICE_PIXEL_RATIO "+ std::to_string(programParameters.pixelRatio) + "\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static std::string pixelRatioDefine(const ProgramParameters& parameters) {
    return std::string("#define DEVICE_PIXEL_RATIO ") + std::to_string(parameters.pixelRatio) + "\n";
}

static std::string fragmentSource(const ProgramParameters& parameters) {
    std::string source = pixelRatioDefine(parameters) + Shaders::fragmentSource;
    ...
}

static std::string vertexSource(const ProgramParameters& parameters) {
    return pixelRatioDefine(parameters) + Shaders::vertexSource;
}

@mollymerp mollymerp force-pushed the dpr branch 2 times, most recently from 1e221cd to 91c64d4 Compare November 15, 2016 22:23
@mollymerp
Copy link
Contributor Author

@jfirebaugh

@jfirebaugh jfirebaugh removed the ⚠️ DO NOT MERGE Work in progress, proof of concept, or on hold label Nov 15, 2016
@jfirebaugh
Copy link
Contributor

@kkaefer Have all your requested changes been addressed?

Copy link
Member

@kkaefer kkaefer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup!

@@ -86,9 +87,12 @@ Painter::Painter(gl::Context& context_, const TransformState& state_)
gl::debugging::enable();
#endif

programs = std::make_unique<Programs>(context);
ProgramParameters programParameters = ProgramParameters{ pixelRatio, false };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be shortened to ProgramParameters programParameters{ pixelRatio, false };

Molly Lloyd added 4 commits November 16, 2016 16:59
update deps, define device pixel ratio for all shaders

[core] create ShaderParameter struct to store pixel ratio and overdraw param

repair rebase errs

update shaders to include pixel ratio

make sure collision_box never overdraws

update test suite, move shaders to Painter::render so the correct pixel ratio is applied

move shader compiling back to the Painter constructor

rebase from shader --> program refactor

re-factor parameters for collisionBox and debug programs

remove unused vars from line-program, move blur math to shader

update core files

remove unecessary files

update shaders PR, remove comments

bump test suite sha

fix formatting, incorporate feedback

refactor program.hpp
@kkaefer
Copy link
Member

kkaefer commented Dec 5, 2016

Looks like the mapbox-gl-shaders repository branch didn't get into master after this branch got merged?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Core The cross-platform C++ core, aka mbgl
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants