// // Copyright (c) 2010-2012 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Alexandros Frantzis <[email protected]> // Jesse Barker <[email protected]> // #include <istream> #include <memory> #include "shader-source.h" #include "log.h" #include "vec.h" #include "util.h" namespace { bool is_valid_precision_value(ShaderSource::PrecisionValue precision_value) { … } bool is_valid_shader_type(ShaderSource::ShaderType shader_type) { … } } /** * Holds default precision values for all shader types * (even the unknown type, which is hardwired to default precision values) */ std::vector<ShaderSource::Precision> ShaderSource::default_precision_(ShaderSource::ShaderTypeUnknown + 1); /** * Loads the contents of a file into a string. * * @param filename the name of the file * @param str the string to put the contents of the file into */ bool ShaderSource::load_file(const std::string& filename, std::string& str) { … } /** * Appends a string to the shader source. * * @param str the string to append */ void ShaderSource::append(const std::string &str) { … } /** * Appends the contents of a file to the shader source. * * @param filename the name of the file to append */ void ShaderSource::append_file(const std::string &filename) { … } /** * Replaces a string in the source with another string. * * @param remove the string to replace * @param insert the string to replace with */ void ShaderSource::replace(const std::string &remove, const std::string &insert) { … } /** * Replaces a string in the source with the contents of a file. * * @param remove the string to replace * @param filename the name of the file to read from */ void ShaderSource::replace_with_file(const std::string &remove, const std::string &filename) { … } /** * Adds a string (usually containing a constant definition) at * global (per shader) scope. * * The string is placed after any default precision qualifiers. * * @param str the string to add */ void ShaderSource::add_global(const std::string &str) { … } /** * Adds a string (usually containing a constant definition) at * global (per shader) scope. * * The string is placed after any default precision qualifiers. * * @param function the function to add the string into * @param str the string to add */ void ShaderSource::add_local(const std::string &str, const std::string &function) { … } /** * Adds a string (usually containing a constant definition) to a shader source * * If the function parameter is empty, the string will be added to global * scope, after any precision definitions. * * @param str the string to add * @param function if not empty, the function to add the string into */ void ShaderSource::add(const std::string &str, const std::string &function) { … } /** * Adds a float constant definition. * * @param name the name of the constant * @param f the value of the constant * @param function if not empty, the function to put the definition in */ void ShaderSource::add_const(const std::string &name, float f, const std::string &function) { … } /** * Adds a float array constant definition. * * Note that various GLSL versions (including ES) don't support * array constants. * * @param name the name of the constant * @param v the value of the constant * @param function if not empty, the function to put the definition in */ void ShaderSource::add_const(const std::string &name, std::vector<float> &array, const std::string &function) { … } /** * Adds a vec2 constant definition. * * @param name the name of the constant * @param v the value of the constant * @param function if not empty, the function to put the definition in */ void ShaderSource::add_const(const std::string &name, const LibMatrix::vec2 &v, const std::string &function) { … } /** * Adds a vec3 constant definition. * * @param name the name of the constant * @param v the value of the constant * @param function if not empty, the function to put the definition in */ void ShaderSource::add_const(const std::string &name, const LibMatrix::vec3 &v, const std::string &function) { … } /** * Adds a vec4 constant definition. * * @param name the name of the constant * @param v the value of the constant * @param function if not empty, the function to put the definition in */ void ShaderSource::add_const(const std::string &name, const LibMatrix::vec4 &v, const std::string &function) { … } /** * Adds a mat3 constant definition. * * @param name the name of the constant * @param v the value of the constant * @param function if not empty, the function to put the definition in */ void ShaderSource::add_const(const std::string &name, const LibMatrix::mat3 &m, const std::string &function) { … } /** * Adds a float array declaration and initialization. * * @param name the name of the array * @param array the array values * @param init_function the function to put the initialization in * @param decl_function if not empty, the function to put the declaration in */ void ShaderSource::add_array(const std::string &name, std::vector<float> &array, const std::string &init_function, const std::string &decl_function) { … } /** * Gets the ShaderType for this ShaderSource. * * If the ShaderType is unknown, an attempt is made to infer * the type from the shader source contents. * * @return the ShaderType */ ShaderSource::ShaderType ShaderSource::type() { … } /** * Helper function that emits a precision statement. * * @param ss the stringstream to add the statement to * @param val the precision value * @param type_str the variable type to apply the precision value to */ void ShaderSource::emit_precision(std::stringstream& ss, ShaderSource::PrecisionValue val, const std::string& type_str) { … } /** * Gets a string containing the complete shader source. * * Precision statements are applied at this point. * * @return the shader source */ std::string ShaderSource::str() { … } /** * Sets the precision that will be used for this shader. * * This overrides any default values set with ShaderSource::default_*_precision(). * * @param precision the precision to set */ void ShaderSource::precision(const ShaderSource::Precision& precision) { … } /** * Gets the precision that will be used for this shader. * * @return the precision */ const ShaderSource::Precision& ShaderSource::precision() { … } /** * Sets the default precision that will be used for a shaders type. * * If type is ShaderTypeUnknown the supplied precision is used for all * shader types. * * This can be overriden per ShaderSource object by using ::precision(). * * @param precision the default precision to set * @param type the ShaderType to use the precision for */ void ShaderSource::default_precision(const ShaderSource::Precision& precision, ShaderSource::ShaderType type) { … } /** * Gets the default precision that will be used for a shader type. * * It is valid to use a type of ShaderTypeUnknown. This will always * return a Precision with default values. * * @param type the ShaderType to get the precision of * * @return the precision */ const ShaderSource::Precision& ShaderSource::default_precision(ShaderSource::ShaderType type) { … } /**************************************** * ShaderSource::Precision constructors * ****************************************/ /** * Creates a ShaderSource::Precision with default precision values. */ ShaderSource::Precision::Precision() : … { … } /** * Creates a ShaderSource::Precision using the supplied precision values. */ ShaderSource::Precision::Precision(ShaderSource::PrecisionValue int_p, ShaderSource::PrecisionValue float_p, ShaderSource::PrecisionValue sampler2d_p, ShaderSource::PrecisionValue samplercube_p) : … { … } /** * Creates a ShaderSource::Precision from a string representation of * precision values. * * The string format is: * "<int>,<float>,<sampler2d>,<samplercube>" * * Each precision value is one of "high", "medium", "low" or "default". * * @param precision_values the string representation of the precision values */ ShaderSource::Precision::Precision(const std::string& precision_values) : … { … }