From 49c809915f08139e63a40c2e0e44b990da1bd474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 29 Mar 2012 23:21:33 +0200 Subject: [PATCH] Added function to bind fragment data locations. --- src/AbstractShaderProgram.cpp | 9 +++++++++ src/AbstractShaderProgram.h | 24 +++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/AbstractShaderProgram.cpp b/src/AbstractShaderProgram.cpp index 46eb356e3..38d8a0574 100644 --- a/src/AbstractShaderProgram.cpp +++ b/src/AbstractShaderProgram.cpp @@ -59,6 +59,15 @@ void AbstractShaderProgram::bindAttribute(GLuint location, const string& name) { glBindAttribLocation(program, location, name.c_str()); } +void AbstractShaderProgram::bindFragmentDataLocation(GLuint location, const std::string& name) { + if(state != Initialized) { + Error() << "AbstractShaderProgram: fragment data location cannot be bound after linking."; + assert(0); + } + + glBindFragDataLocation(program, location, name.c_str()); +} + void AbstractShaderProgram::link() { /* Already compiled or failed, exit */ if(state != Initialized) return; diff --git a/src/AbstractShaderProgram.h b/src/AbstractShaderProgram.h index 167d059a3..60e4e92bd 100644 --- a/src/AbstractShaderProgram.h +++ b/src/AbstractShaderProgram.h @@ -63,7 +63,7 @@ typedef Attribute<2, Vector2> TextureCoords; projectionMatrixUniform = uniformLocation("projectionMatrix"); // more uniforms like light location, colors etc. @endcode - - **Uniform binding functions**, which set shader uniforms with + - **Uniform binding functions**, which set shader uniforms using setUniform() and setUniformArray() functions. Example: @code void setTransformationMatrixUniform(const Matrix4& matrix) { @@ -74,6 +74,18 @@ void setTransformationMatrixUniform(const Matrix4& matrix) { Basic workflow with AbstractShaderProgram subclasses is: instancing the class (once at the beginning), then in every frame calling use(), setting uniforms and calling Mesh::draw() (see its documentation for more). + +@section MultipleFragmentOutputs Multiple fragment shader outputs +If your shader uses multiple fragment outputs, you can use +bindFragmentDataLocation() *before linking* to bind their names to desired +location, e.g.: +@code +bindFragmentDataLocation(0, "color"); +@endcode + +You should then clearly state in the documentation which output is on what +position, so the user can set framebuffer attachments for them using +Framebuffer::mapForDraw() or Framebuffer::mapDefaultForDraw(). */ class MAGNUM_EXPORT AbstractShaderProgram { AbstractShaderProgram(const AbstractShaderProgram& other) = delete; @@ -134,6 +146,16 @@ class MAGNUM_EXPORT AbstractShaderProgram { */ void bindAttribute(GLuint location, const std::string& name); + /** + * @brief Bind fragment data to given location + * @param location Location + * @param name Fragment output variable name + * + * @note This function should be called between loadShader() calls + * and link(). + */ + void bindFragmentDataLocation(GLuint location, const std::string& name); + /** * @brief Link the shader *