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

Blend modes! #185

Merged
merged 10 commits into from Oct 13, 2017

Conversation

@termhn
Copy link
Contributor

commented Oct 8, 2017

An easy to use API (basically the same as LOVE's) to switch the blend mode of the current shader program. The main limitation is that it requires you to provide a list of blend modes that you want your shader program to support when you create it (or None to apply only the default Alpha blend mode). The default shader program comes with all of them by default. This allows us to do things like this :D (the updated shadows example).

image

TODO:

  • Still need to figure out how to implement the Lighten and Darken blend modes using gfx's API--for now they're just placeholders. All the rest of the blend modes work, though.
  • Perhaps restructure where a few things live; I'm not sure it makes sense to have the graphics::set_blend_mode() function inside pixelshader.rs
@icefoxen

This comment has been minimized.

Copy link
Contributor

commented Oct 8, 2017

Nice. Is it possible to use this to set blend modes for arbitrary images without custom shaders being involved as well? I vaguely recall doing something for that but don't remember what it looks like.

@termhn

This comment has been minimized.

Copy link
Contributor Author

commented Oct 8, 2017

Hmm... you could just switch the blend mode, draw the image, then switch it back to the default blend mode, but I don't think it's possible (at least with this) to have an image have a different blend mode. From what I've gathered from kvark, the blend mode is an immutable piece of the gfx PSO, and so to change blend modes we create multiple PSOs (one for each blend mode) and then have it use the correct one based on the requested blend mode. We could add a piece of state on the ggez Image type that would automatically attempt to switch the blend mode when it is drawn, though.

I've made the default shader program have PSOs for all of the blend modes, so you can set any blend mode when you have the default shader active. For user shaders, the user has to specify all the blend modes that they want to be able to use with that shader.

@nlordell

This comment has been minimized.

Copy link
Contributor

commented Oct 8, 2017

This is super cool!

@termhn

This comment has been minimized.

Copy link
Contributor Author

commented Oct 9, 2017

Alright, all the TODOs are now implemented (hopefully) and I experimentally added in the ability for you to individually set the blend mode of any Drawable, which will override the general blend mode that is set, based on @icefoxen 's comment.

@termhn

This comment has been minimized.

Copy link
Contributor Author

commented Oct 13, 2017

@nlordell Can you test this on your macbook? I hadn't until now and it seems as though something is being messed up with the new high dpi stuff on mine. I probably messed something up when rebasing it and didn't notice until now heh.

@icefoxen I think this will be ready to merge once I get the above-mentioned bug fixed

@icefoxen

This comment has been minimized.

Copy link
Contributor

commented Oct 13, 2017

This looks generally fantastic! I just need to grok what the Pso structs are doing more fully.

@termhn

This comment has been minimized.

Copy link
Contributor Author

commented Oct 13, 2017

Basically, in gfx, because Vulkan and DX are more strict about how blend modes work than GL is, blend modes are a baked in piece of state for a PSO and you can't change it dynamically. After chatting with @kvark on IRC and looking how he does it in three-rs, the best way to change blend modes is to just make multiple PSOs with a blend mode baked in. The PsoSet struct is basically just a hash map for easily storing each shader set's PSOs and then retrieving them based on a BlendMode

This is fine for when you only have one shader program, but since shaders are also baked into a PSO, if you want to have multiple shaders in which each supports all the blend modes, you also have to make a PSO per blend mode per shader.

Since creating all the blend mode PSOs for multiple shaders would be a bit ridiculous when you probably aren't going to use all of them, for custom shaders you have to specify when you create the shader a list of BlendModes that you want to be able to use with that shader. If you supply None then it will only create one PSO with the default Alpha blend mode.

@icefoxen

This comment has been minimized.

Copy link
Contributor

commented Oct 13, 2017

Makes sense. Sounds, as I suspected from the placement and organization of the code, rather similar to the SamplerCache. But thanks for the excellent explanation. ...can you add it as a comment to the source? :-P

@termhn

This comment has been minimized.

Copy link
Contributor Author

commented Oct 13, 2017

Yes, it's basically a modified SamplerCache, ahah. And yeah, I can!

@termhn

This comment has been minimized.

Copy link
Contributor Author

commented Oct 13, 2017

Done! Ready to merge if you are I think.

@icefoxen icefoxen merged commit ce05784 into ggez:master Oct 13, 2017

0 of 2 checks passed

continuous-integration/travis-ci/pr The Travis CI build could not complete due to an error
Details
continuous-integration/appveyor/pr AppVeyor build failed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.