adding shiny
parent
fb3ac6ad4a
commit
7b35b82833
@ -0,0 +1,72 @@
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
|
||||
# This is NOT intended as a stand-alone build system! Instead, you should include this from the main CMakeLists of your project.
|
||||
# Make sure to link against Ogre and boost::filesystem.
|
||||
|
||||
option(SHINY_BUILD_OGRE_PLATFORM "build the Ogre platform" ON)
|
||||
|
||||
set(SHINY_LIBRARY "shiny")
|
||||
set(SHINY_OGREPLATFORM_LIBRARY "shiny.OgrePlatform")
|
||||
|
||||
# Sources
|
||||
file(GLOB SOURCE_FILES Main/*.cpp )
|
||||
|
||||
set(SOURCE_FILES
|
||||
Main/Factory.cpp
|
||||
Main/MaterialInstance.cpp
|
||||
Main/MaterialInstancePass.cpp
|
||||
Main/MaterialInstanceTextureUnit.cpp
|
||||
Main/Platform.cpp
|
||||
Main/Preprocessor.cpp
|
||||
Main/PropertyBase.cpp
|
||||
Main/ScriptLoader.cpp
|
||||
Main/ShaderInstance.cpp
|
||||
Main/ShaderSet.cpp
|
||||
)
|
||||
|
||||
# In Debug mode, write the shader sources to the current directory
|
||||
if (DEFINED CMAKE_BUILD_TYPE)
|
||||
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
add_definitions(-DSHINY_WRITE_SHADER_DEBUG)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if (DEFINED SHINY_USE_WAVE_SYSTEM_INSTALL)
|
||||
# use system install
|
||||
else()
|
||||
list(APPEND SOURCE_FILES
|
||||
Preprocessor/aq.cpp
|
||||
Preprocessor/cpp_re.cpp
|
||||
Preprocessor/instantiate_cpp_literalgrs.cpp
|
||||
Preprocessor/instantiate_cpp_exprgrammar.cpp
|
||||
Preprocessor/instantiate_cpp_grammar.cpp
|
||||
Preprocessor/instantiate_defined_grammar.cpp
|
||||
Preprocessor/instantiate_predef_macros.cpp
|
||||
Preprocessor/instantiate_re2c_lexer.cpp
|
||||
Preprocessor/instantiate_re2c_lexer_str.cpp
|
||||
Preprocessor/token_ids.cpp
|
||||
)
|
||||
|
||||
# Don't use thread-safe boost::wave. Results in a huge speed-up for the preprocessor.
|
||||
add_definitions(-DBOOST_WAVE_SUPPORT_THREADING=0)
|
||||
endif()
|
||||
|
||||
set(OGRE_PLATFORM_SOURCE_FILES
|
||||
Platforms/Ogre/OgreGpuProgram.cpp
|
||||
Platforms/Ogre/OgreMaterial.cpp
|
||||
Platforms/Ogre/OgreMaterialSerializer.cpp
|
||||
Platforms/Ogre/OgrePass.cpp
|
||||
Platforms/Ogre/OgrePlatform.cpp
|
||||
Platforms/Ogre/OgreTextureUnitState.cpp
|
||||
)
|
||||
|
||||
file(GLOB OGRE_PLATFORM_SOURCE_FILES Platforms/Ogre/*.cpp)
|
||||
|
||||
add_library(${SHINY_LIBRARY} STATIC ${SOURCE_FILES})
|
||||
|
||||
if (SHINY_BUILD_OGRE_PLATFORM)
|
||||
add_library(${SHINY_OGREPLATFORM_LIBRARY} STATIC ${OGRE_PLATFORM_SOURCE_FILES})
|
||||
endif()
|
||||
|
||||
|
||||
link_directories(${CMAKE_CURRENT_BINARY_DIR})
|
@ -0,0 +1,32 @@
|
||||
/*!
|
||||
|
||||
\page configurations Configurations
|
||||
|
||||
A common task in shader development is to provide a different set of simpler shaders for all your materials. Some examples:
|
||||
- When rendering cubic or planar reflection maps in real-time, you will want to disable shadows.
|
||||
- For an in-game minimap render target, you don't want to have fog.
|
||||
|
||||
For this task, the library provides a \a Configuration concept.
|
||||
|
||||
A Configuration is a set of properties that can override global settings, as long as this Configuration is active.
|
||||
|
||||
Here's an example. Say you have a global setting with the name 'shadows' that controls if your materials receive shadows.
|
||||
|
||||
Now, lets create a configuration for our reflection render targets that disables shadows for all materials. Paste the following in a new file with the extension '.configuration':
|
||||
|
||||
\code
|
||||
configuration reflection_targets
|
||||
{
|
||||
shadows false
|
||||
}
|
||||
\endcode
|
||||
|
||||
\note You may also create configurations using sh::Factory::registerConfiguration.
|
||||
|
||||
The active Configuration is controlled by the active material scheme in Ogre. So, in order to use the configuration "reflection_targets" for your reflection renders, simply call
|
||||
\code
|
||||
viewport->setMaterialScheme ("reflection_targets");
|
||||
\endcode
|
||||
on the Ogre viewport of your reflection render!
|
||||
|
||||
*/
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,65 @@
|
||||
/*!
|
||||
\page getting-started Getting started
|
||||
|
||||
\section download Download the source
|
||||
|
||||
\code
|
||||
git clone git@github.com:scrawl/shiny.git
|
||||
\endcode
|
||||
|
||||
\section building Build the source
|
||||
|
||||
The source files you want to build are:
|
||||
- Main/*.cpp
|
||||
- Preprocessor/*.cpp (unless you are using the system install of boost::wave, more below)
|
||||
- Platforms/Ogre/*.cpp
|
||||
|
||||
You can either build the sources as a static library, or simply add the sources to the source tree of your project.
|
||||
|
||||
If you use CMake, you might find the included CMakeLists.txt useful. It builds static libraries with the names "shiny" and "shiny.OgrePlatform".
|
||||
|
||||
\note The CMakeLists.txt is not intended as a stand-alone build system! Instead, you should include this from the main CMakeLists of your project.
|
||||
|
||||
Make sure to link against OGRE and the boost filesystem library.
|
||||
|
||||
If your boost version is older than 1.49, you must set the SHINY_USE_WAVE_SYSTEM_INSTALL variable and additionally link against the boost wave library.
|
||||
|
||||
\code
|
||||
set(SHINY_USE_WAVE_SYSTEM_INSTALL "TRUE")
|
||||
\endcode
|
||||
|
||||
\section code Basic initialisation code
|
||||
|
||||
Add the following code to your application:
|
||||
|
||||
\code{cpp}
|
||||
|
||||
#include <shiny/Main/Factory.hpp>
|
||||
#include <shiny/Platforms/OgrePlatform/OgrePlatform.hpp>
|
||||
|
||||
....
|
||||
|
||||
sh::OgrePlatform* platform = new sh::OgrePlatform(
|
||||
"General", // OGRE Resource group to use for creating materials.
|
||||
myApplication.getDataPath() + "/" + "materials" // Path to look for materials and shaders. NOTE: This does NOT use the Ogre resource system, so you have to specify an absolute path.
|
||||
);
|
||||
|
||||
sh::Factory* factory = new sh::Factory(platform);
|
||||
|
||||
// Set a language. Valid options: CG, HLSL, GLSL
|
||||
factory->setCurrentLanguage(sh::Language_GLSL);
|
||||
|
||||
factory->loadAllFiles();
|
||||
|
||||
....
|
||||
your application runs here
|
||||
....
|
||||
|
||||
// don't forget to delete on exit
|
||||
delete factory;
|
||||
|
||||
\endcode
|
||||
|
||||
That's it! Now you can start defining materials. Refer to the page \ref defining-materials-shaders .
|
||||
|
||||
*/
|
@ -0,0 +1,49 @@
|
||||
/*!
|
||||
|
||||
\page lod Material LOD
|
||||
|
||||
\section howitworks How it works
|
||||
|
||||
When Ogre requests a technique for a specific LOD index, the Factory selects the appropriate LOD configuration which then temporarily overrides the global settings in the shaders. We can use this to disable shader features one by one at a lower LOD, resulting in simpler and faster techniques for distant objects.
|
||||
|
||||
\section howtouseit How to use it
|
||||
|
||||
- Create a file with the extension '.lod'. There you can specify shader features to disable at a specific LOD level. Higher LOD index refers to a lower LOD. Example contents:
|
||||
|
||||
\code
|
||||
lod_configuration 1
|
||||
{
|
||||
specular_mapping false
|
||||
}
|
||||
|
||||
lod_configuration 2
|
||||
{
|
||||
specular_mapping false
|
||||
normal_mapping false
|
||||
}
|
||||
|
||||
lod_configuration 3
|
||||
{
|
||||
terrain_composite_map true
|
||||
specular_mapping false
|
||||
normal_mapping false
|
||||
}
|
||||
\endcode
|
||||
|
||||
\note You can also add LOD configurations by calling \a sh::Factory::registerLodConfiguration.
|
||||
|
||||
\note Do not use an index of 0. LOD 0 refers to the highest LOD, and you will never want to disable features at the highest LOD level.
|
||||
|
||||
|
||||
- In your materials, specify the distances at which a lower LOD kicks in. Note that the actual distance might also be affected by the viewport and current entity LOD bias. In this example, the first LOD level (lod index 1) would normally be applied at a distance of 100 units, the next after 300, and the last after 1000 units.
|
||||
|
||||
\code
|
||||
material sample_material
|
||||
{
|
||||
lod_values 100 300 1000
|
||||
|
||||
... your passes, texture units etc ...
|
||||
}
|
||||
\endcode
|
||||
|
||||
*/
|
@ -0,0 +1,270 @@
|
||||
/*!
|
||||
\page macros Shader Macros
|
||||
|
||||
\tableofcontents
|
||||
|
||||
\section Shader Language
|
||||
|
||||
These macros are automatically defined, depending on the shader language that has been set by the application using sh::Factory::setCurrentLanguage.
|
||||
|
||||
- SH_GLSL
|
||||
- SH_HLSL
|
||||
- SH_CG
|
||||
|
||||
<B>Example:</B>
|
||||
|
||||
\code
|
||||
#if SH_GLSL == 1
|
||||
// glsl porting code
|
||||
#endif
|
||||
|
||||
#if SH_CG == 1 || SH_HLSL == 1
|
||||
// cg / hlsl porting code (similiar syntax)
|
||||
#endif
|
||||
\endcode
|
||||
|
||||
\note It is encouraged to use the shipped porting header (extra/core.h) by #include-ing it in your shaders. If you do that, you should not have to use the above macros directly.
|
||||
|
||||
\section vertex-fragment Vertex / fragment shader
|
||||
|
||||
These macros are automatically defined, depending on the type of shader that is currently being compiled.
|
||||
|
||||
- SH_VERTEX_SHADER
|
||||
- SH_FRAGMENT_SHADER
|
||||
|
||||
If you use the same source file for both vertex and fragment shader, then it is advised to use these macros for blending out the unused source. This will reduce your compile time.
|
||||
|
||||
\section passthrough Vertex -> Fragment passthrough
|
||||
|
||||
In shader development, a common task is to pass variables from the vertex to the fragment shader. This is no problem if you have a deterministic shader source (i.e. no #ifdefs).
|
||||
|
||||
However, as soon as you begin to have lots of permutations of the same shader source, a problem arises. All current GPUs have a limit of 8 vertex to fragment passthroughs (with 4 components each, for example a float4).
|
||||
|
||||
A common optimization is to put several individual float values together in a float4 (so-called "Packing"). But if your shader has lots of permutations and the passthrough elements you actually need are not known beforehand, it can be very tedious to pack manually. With the following macros, packing can become easier.
|
||||
|
||||
\subsection shAllocatePassthrough shAllocatePassthrough
|
||||
|
||||
<B>Usage:</B> \@shAllocatePassthrough(num_components, name)
|
||||
|
||||
<B>Example:</B>
|
||||
\code
|
||||
#if FRAGMENT_NEED_DEPTH
|
||||
@shAllocatePassthrough(1, depth)
|
||||
#endif
|
||||
\endcode
|
||||
|
||||
This is the first thing you should do before using any of the macros below.
|
||||
|
||||
\subsection shPassthroughVertexOutputs shPassthroughVertexOutputs
|
||||
|
||||
<B>Usage:</B> \@shPassthroughVertexOutputs
|
||||
|
||||
Use this in the inputs/outputs section of your vertex shader, in order to declare all the outputs that are needed for packing the variables that you want passed to the fragment.
|
||||
|
||||
\subsection shPassthroughFragmentInputs shPassthroughFragmentInputs
|
||||
|
||||
<B>Usage:</B> \@shPassthroughFragmentInputs
|
||||
|
||||
Use this in the inputs/outputs section of your fragment shader, in order to declare all the inputs that are needed for receiving the variables that you want passed to the fragment.
|
||||
|
||||
\subsection shPassthroughAssign shPassthroughAssign
|
||||
|
||||
<B>Usage:</B> \@shPassthroughAssign(name, value)
|
||||
|
||||
Use this in the vertex shader for assigning a value to the variable you want passed to the fragment.
|
||||
|
||||
<B>Example:</B>
|
||||
\code
|
||||
#if FRAGMENT_NEED_DEPTH
|
||||
@shPassthroughAssign(depth, shOutputPosition.z);
|
||||
#endif
|
||||
|
||||
\endcode
|
||||
|
||||
\subsection shPassthroughReceive shPassthroughReceive
|
||||
|
||||
<B>Usage:</B> \@shPassthroughReceive(name)
|
||||
|
||||
Use this in the fragment shader to receive the passed value.
|
||||
|
||||
<B>Example:</B>
|
||||
|
||||
\code
|
||||
#if FRAGMENT_NEED_DEPTH
|
||||
float depth = @shPassthroughReceive(depth);
|
||||
#endif
|
||||
\endcode
|
||||
|
||||
\section texUnits Texture units
|
||||
|
||||
\subsection shUseSampler shUseSampler
|
||||
|
||||
<B>Usage:</B> \@shUseSampler(samplerName)
|
||||
|
||||
Requests the texture unit with name \a samplerName to be available for use in this pass.
|
||||
|
||||
Why is this necessary? If you have a derived material that does not use all of the texture units that its parent defines (for example, if an optional asset such as a normal map is not available), there would be no way to know which texture units are actually needed and which can be skipped in the creation process (those that are never referenced in the shader).
|
||||
|
||||
\section properties Property retrieval / binding
|
||||
|
||||
\subsection shUniformProperty shUniformProperty
|
||||
|
||||
<B>Usage:</B> \@shUniformProperty<4f|3f|2f|1f|int> (uniformName, property)
|
||||
|
||||
Binds the value of \a property (from the shader_properties of the pass this shader belongs to) to the uniform with name \a uniformName.
|
||||
|
||||
The following variants are available, depending on the type of your uniform variable:
|
||||
- \@shUniformProperty4f
|
||||
- \@shUniformProperty3f
|
||||
- \@shUniformProperty2f
|
||||
- \@shUniformProperty1f
|
||||
- \@shUniformPropertyInt
|
||||
|
||||
<B>Example:</B> \@shUniformProperty1f (uFresnelScale, fresnelScale)
|
||||
|
||||
\subsection shPropertyBool shPropertyBool
|
||||
|
||||
Retrieve a boolean property of the pass that this shader belongs to, gets replaced with either 0 or 1.
|
||||
|
||||
<B>Usage:</B> \@shPropertyBool(propertyName)
|
||||
|
||||
<B>Example:</B>
|
||||
\code
|
||||
#if @shPropertyBool(has_normal_map)
|
||||
...
|
||||
#endif
|
||||
\endcode
|
||||
|
||||
\subsection shPropertyNotBool shPropertyNotBool
|
||||
|
||||
Same as shPropertyBool, but inverts the result (i.e. when shPropertyBool would return 0, this returns 1 and vice versa)
|
||||
|
||||
\subsection shPropertyString shPropertyString
|
||||
|
||||
Retrieve a string property of the pass that this shader belongs to
|
||||
|
||||
<B>Usage:</B> \@shPropertyString(propertyName)
|
||||
|
||||
\subsection shPropertyEqual shPropertyEqual
|
||||
|
||||
Check if the value of a property equals a specific value, gets replaced with either 0 or 1. This is useful because the preprocessor cannot compare strings, only numbers.
|
||||
|
||||
<B>Usage:</B> \@shPropertyEqual(propertyName, value)
|
||||
|
||||
<B>Example:</B>
|
||||
\code
|
||||
#if @shPropertyEqual(lighting_mode, phong)
|
||||
...
|
||||
#endif
|
||||
\endcode
|
||||
|
||||
\section globalSettings Global settings
|
||||
|
||||
\subsection shGlobalSettingBool shGlobalSettingBool
|
||||
|
||||
Retrieves the boolean value of a specific global setting, gets replaced with either 0 or 1. The value can be set using sh::Factory::setGlobalSetting.
|
||||
|
||||
<B>Usage:</B> \@shGlobalSettingBool(settingName)
|
||||
|
||||
\subsection shGlobalSettingEqual shGlobalSettingEqual
|
||||
|
||||
Check if the value of a global setting equals a specific value, gets replaced with either 0 or 1. This is useful because the preprocessor cannot compare strings, only numbers.
|
||||
|
||||
<B>Usage:</B> \@shGlobalSettingEqual(settingName, value)
|
||||
|
||||
\subsection shGlobalSettingString shGlobalSettingString
|
||||
|
||||
Gets replaced with the current value of a given global setting. The value can be set using sh::Factory::setGlobalSetting.
|
||||
|
||||
<B>Usage:</B> \@shGlobalSettingString(settingName)
|
||||
|
||||
\section sharedParams Shared parameters
|
||||
|
||||
\subsection shSharedParameter shSharedParameter
|
||||
|
||||
Allows you to bind a custom value to a uniform parameter.
|
||||
|
||||
<B>Usage</B>: \@shSharedParameter(sharedParameterName)
|
||||
|
||||
<B>Example</B>: \@shSharedParameter(pssmSplitPoints) - now the uniform parameter 'pssmSplitPoints' can be altered in all shaders that use it by executing sh::Factory::setSharedParameter("pssmSplitPoints", value)
|
||||
|
||||
\note You may use the same shared parameter in as many shaders as you want. But don't forget to add the \@shSharedParameter macro to every shader that uses this shared parameter.
|
||||
|
||||
\section autoconstants Auto constants
|
||||
|
||||
\subsection shAutoConstant shAutoConstant
|
||||
|
||||
<B>Usage:</B> \@shAutoConstant(uniformName, autoConstantName, [extraData])
|
||||
|
||||
<B>Example</B>: \@shAutoConstant(uModelViewMatrix, worldviewproj_matrix)
|
||||
|
||||
<B>Example</B>: \@shAutoConstant(uLightPosition4, light_position, 4)
|
||||
|
||||
Binds auto constant with name \a autoConstantName to the uniform \a uniformName. Optionally, you may specify extra data (for example the light index), as required by some auto constants.
|
||||
|
||||
The auto constant names are the same as Ogre's. Read the section "3.1.9 Using Vertex/Geometry/Fragment Programs in a Pass" of the Ogre manual for a list of all auto constant names.
|
||||
|
||||
\section misc Misc
|
||||
|
||||
\subsection shForeach shForeach
|
||||
|
||||
<B>Usage:</B> \@shForeach(n)
|
||||
|
||||
Repeats the content of this foreach block \a n times. The end of the block is marked via \@shEndForeach, and the current iteration number can be retrieved via \@shIterator.
|
||||
|
||||
\note Nested foreach blocks are currently \a not supported.
|
||||
|
||||
\note For technical reasons, you can only use constant numbers, properties (\@shPropertyString) or global settings (\@shGlobalSettingString) as \a n parameter.
|
||||
|
||||
<B>Example:</B>
|
||||
|
||||
\code
|
||||
@shForeach(3)
|
||||
this is iteration number @shIterator
|
||||
@shEndForeach
|
||||
|
||||
Gets replaced with:
|
||||
|
||||
this is iteration number 0
|
||||
this is iteration number 1
|
||||
this is iteration number 2
|
||||
\endcode
|
||||
|
||||
Optionally, you can pass a constant offset to \@shIterator. Example:
|
||||
|
||||
\code
|
||||
@shForeach(3)
|
||||
this is iteration number @shIterator(7)
|
||||
@shEndForeach
|
||||
|
||||
Gets replaced with:
|
||||
|
||||
this is iteration number 7
|
||||
this is iteration number 8
|
||||
this is iteration number 9
|
||||
\endcode
|
||||
|
||||
\subsection shCounter shCounter
|
||||
|
||||
Gets replaced after the preprocessing step with the number that equals the n-th occurence of counters of the same ID.
|
||||
|
||||
<B>Usage:</B> \@shCounter(ID)
|
||||
|
||||
<B>Example</B>:
|
||||
\code
|
||||
@shCounter(0)
|
||||
@shCounter(0)
|
||||
@shCounter(1)
|
||||
@shCounter(0)
|
||||
\endcode
|
||||
|
||||
Gets replaced with:
|
||||
|
||||
\code
|
||||
0
|
||||
1
|
||||
0
|
||||
2
|
||||
\endcode
|
||||
|
||||
*/
|
@ -0,0 +1,13 @@
|
||||
/*!
|
||||
|
||||
\mainpage
|
||||
|
||||
- \ref getting-started
|
||||
- \ref defining-materials-shaders
|
||||
- \ref macros
|
||||
- \ref configurations
|
||||
- \ref lod
|
||||
|
||||
- sh::Factory - the main interface class
|
||||
|
||||
*/
|
@ -0,0 +1,128 @@
|
||||
/*!
|
||||
|
||||
\page defining-materials-shaders Defining materials and shaders
|
||||
|
||||
\section first-material Your first material
|
||||
|
||||
Create a file called "myFirstMaterial.mat" and place it in the path you have used in your initialisation code (see \ref getting-started). Paste the following:
|
||||
|
||||
\code
|
||||
|
||||
material my_first_material
|
||||
{
|
||||
diffuse 1.0 1.0 1.0 1.0
|
||||
specular 0.4 0.4 0.4 32
|
||||
ambient 1.0 1.0 1.0
|
||||
emissive 0.0 0.0 0.0
|
||||
diffuseMap black.png
|
||||
|
||||
pass
|
||||
{
|
||||
diffuse $diffuse
|
||||
specular $specular
|
||||
ambient $ambient
|
||||
emissive $emissive
|
||||
|
||||
texture_unit diffuseMap
|
||||
{
|
||||
texture $diffuseMap
|
||||
create_in_ffp true // use this texture unit for fixed function pipeline
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
material material1
|
||||
{
|
||||
parent my_first_material
|
||||
diffuseMap texture1.png
|
||||
}
|
||||
|
||||
material material2
|
||||
{
|
||||
parent my_first_material
|
||||
diffuseMap texture2.png
|
||||
}
|
||||
|
||||
\endcode
|
||||
|
||||
\section first-shader The first shader
|
||||
|
||||
Change the 'pass' section to include some shaders:
|
||||
|
||||
\code
|
||||
pass
|
||||
{
|
||||
vertex_program my_first_shader_vertex
|
||||
fragment_program my_first_shader_fragment
|
||||
...
|
||||
}
|
||||
\endcode
|
||||
|
||||
\note This does \a not refer to a single shader with a fixed source code, but in fact will automatically create a new \a instance of this shader (if necessary), which can have its own uniform variables, compile-time macros and much more!
|
||||
|
||||
Next, we're going to define our shaders. Paste this in a new file called 'myfirstshader.shaderset'
|
||||
|
||||
\code
|
||||
shader_set my_first_shader_vertex
|
||||
{
|
||||
source example.shader
|
||||
type vertex
|
||||
profiles_cg vs_2_0 arbvp1
|
||||
profiles_hlsl vs_2_0
|
||||
}
|
||||
|
||||
shader_set my_first_shader_fragment
|
||||
{
|
||||
source example.shader
|
||||
type fragment
|
||||
profiles_cg ps_2_x ps_2_0 ps arbfp1
|
||||
profiles_hlsl ps_2_0
|
||||
}
|
||||
\endcode
|
||||
|
||||
Some notes:
|
||||
- There is no entry_point property because the entry point is always \a main.
|
||||
- Both profiles_cg and profiles_hlsl are a list of shader profiles. The first profile that is supported is automatically picked. GLSL does not have shader profiles.
|
||||
|
||||
Now, let's get into writing our shader! As you can guess from above, the filename should be 'example.shader'
|
||||
|
||||
\code
|
||||
#include "core.h"
|
||||
|
||||
#ifdef SH_VERTEX_SHADER
|
||||
|
||||
SH_BEGIN_PROGRAM
|
||||
shUniform(float4x4, wvp) @shAutoConstant(wvp, worldviewproj_matrix)
|
||||
shInput(float2, uv0)
|
||||
shOutput(float2, UV)
|
||||
SH_START_PROGRAM
|
||||
{
|
||||
shOutputPosition = shMatrixMult(wvp, shInputPosition);
|
||||
UV = uv0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
SH_BEGIN_PROGRAM
|
||||
// NOTE: It is important that the sampler name here (diffuseMap) matches
|
||||
// the name of the texture unit in the material. This is necessary because the system
|
||||
// skips texture units that are never "referenced" in the shader. This can be the case
|
||||
// when your base material has optional assets (for example a normal map) that are not
|
||||
// used by some derived materials.
|
||||
shSampler2D(diffuseMap)
|
||||
shInput(float2, UV)
|
||||
SH_START_PROGRAM
|
||||
{
|
||||
shOutputColour(0) = shSample(diffuseMap, UV);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
\endcode
|
||||
|
||||
There you have it! This shader will compile in several languages thanks to the porting defines in "core.h". If you need more defines, feel free to add them and don't forget to send them to me!
|
||||
|
||||
For a full list of macros available when writing your shaders, refer to the page \ref macros
|
||||
|
||||
In the future, some more in-depth shader examples might follow.
|
||||
*/
|
@ -0,0 +1,168 @@
|
||||
#if SH_HLSL == 1 || SH_CG == 1
|
||||
|
||||
#define shTexture2D sampler2D
|
||||
#define shSample(tex, coord) tex2D(tex, coord)
|
||||
#define shCubicSample(tex, coord) texCUBE(tex, coord)
|
||||
#define shLerp(a, b, t) lerp(a, b, t)
|
||||
#define shSaturate(a) saturate(a)
|
||||
|
||||
#define shSampler2D(name) , uniform sampler2D name : register(s@shCounter(0)) @shUseSampler(name)
|
||||
|
||||
#define shSamplerCube(name) , uniform samplerCUBE name : register(s@shCounter(0)) @shUseSampler(name)
|
||||
|
||||
#define shMatrixMult(m, v) mul(m, v)
|
||||
|
||||
#define shUniform(type, name) , uniform type name
|
||||
|
||||
#define shTangentInput(type) , in type tangent : TANGENT
|
||||
#define shVertexInput(type, name) , in type name : TEXCOORD@shCounter(1)
|
||||
#define shInput(type, name) , in type name : TEXCOORD@shCounter(1)
|
||||
#define shOutput(type, name) , out type name : TEXCOORD@shCounter(2)
|
||||
|
||||
#define shNormalInput(type) , in type normal : NORMAL
|
||||
|
||||
#define shColourInput(type) , in type colour : COLOR
|
||||
|
||||
#ifdef SH_VERTEX_SHADER
|
||||
|
||||
#define shOutputPosition oPosition
|
||||
#define shInputPosition iPosition
|
||||
|
||||
|
||||
#define SH_BEGIN_PROGRAM \
|
||||
void main( \
|
||||
float4 iPosition : POSITION \
|
||||
, out float4 oPosition : POSITION
|
||||
|
||||
#define SH_START_PROGRAM \
|
||||
) \
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef SH_FRAGMENT_SHADER
|
||||
|
||||
#define shOutputColour(num) oColor##num
|
||||
|
||||
#define shDeclareMrtOutput(num) , out float4 oColor##num : COLOR##num
|
||||
|
||||
#define SH_BEGIN_PROGRAM \
|
||||
void main( \
|
||||
out float4 oColor0 : COLOR
|
||||
|
||||
#define SH_START_PROGRAM \
|
||||
) \
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#if SH_GLSL == 1
|
||||
|
||||
@version 120
|
||||
|
||||
#define float2 vec2
|
||||
#define float3 vec3
|
||||
#define float4 vec4
|
||||
#define int2 ivec2
|
||||
#define int3 ivec3
|
||||
#define int4 ivec4
|
||||
#define shTexture2D sampler2D
|
||||
#define shSample(tex, coord) texture2D(tex, coord)
|
||||
#define shCubicSample(tex, coord) textureCube(tex, coord)
|
||||
#define shLerp(a, b, t) mix(a, b, t)
|
||||
#define shSaturate(a) clamp(a, 0.0, 1.0)
|
||||
|
||||
#define shUniform(type, name) uniform type name;
|
||||
|
||||
#define shSampler2D(name) uniform sampler2D name; @shUseSampler(name)
|
||||
|
||||
#define shSamplerCube(name) uniform samplerCube name; @shUseSampler(name)
|
||||
|
||||
#define shMatrixMult(m, v) (m * v)
|
||||
|
||||
#define shOutputPosition gl_Position
|
||||
|
||||
#define float4x4 mat4
|
||||
#define float3x3 mat3
|
||||
|
||||
// GLSL 1.3
|
||||
#if 0
|
||||
|
||||
// automatically recognized by ogre when the input name equals this
|
||||
#define shInputPosition vertex
|
||||
|
||||
#define shOutputColour(num) oColor##num
|
||||
|
||||
#define shTangentInput(type) in type tangent;
|
||||
#define shVertexInput(type, name) in type name;
|
||||
#define shInput(type, name) in type name;
|
||||
#define shOutput(type, name) out type name;
|
||||
|
||||
// automatically recognized by ogre when the input name equals this
|
||||
#define shNormalInput(type) in type normal;
|
||||
#define shColourInput(type) in type colour;
|
||||
|
||||
#ifdef SH_VERTEX_SHADER
|
||||
|
||||
#define SH_BEGIN_PROGRAM \
|
||||
in float4 vertex;
|
||||
#define SH_START_PROGRAM \
|
||||
void main(void)
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef SH_FRAGMENT_SHADER
|
||||
|
||||
#define shDeclareMrtOutput(num) out vec4 oColor##num;
|
||||
|
||||
#define SH_BEGIN_PROGRAM \
|
||||
out float4 oColor0;
|
||||
#define SH_START_PROGRAM \
|
||||
void main(void)
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
// GLSL 1.2
|
||||
|
||||
#if 1
|
||||
|
||||
// automatically recognized by ogre when the input name equals this
|
||||
#define shInputPosition vertex
|
||||
|
||||
#define shOutputColour(num) gl_FragData[num]
|
||||
|
||||
#define shTangentInput(type) attribute type tangent;
|
||||
#define shVertexInput(type, name) attribute type name;
|
||||
#define shInput(type, name) varying type name;
|
||||
#define shOutput(type, name) varying type name;
|
||||
|
||||
// automatically recognized by ogre when the input name equals this
|
||||
#define shNormalInput(type) attribute type normal;
|
||||
#define shColourInput(type) attribute type colour;
|
||||
|
||||
#ifdef SH_VERTEX_SHADER
|
||||
|
||||
#define SH_BEGIN_PROGRAM \
|
||||
attribute vec4 vertex;
|
||||
#define SH_START_PROGRAM \
|
||||
void main(void)
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef SH_FRAGMENT_SHADER
|
||||
|
||||
#define shDeclareMrtOutput(num)
|
||||
|
||||
#define SH_BEGIN_PROGRAM
|
||||
|
||||
#define SH_START_PROGRAM \
|
||||
void main(void)
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
@ -0,0 +1,9 @@
|
||||
Copyright (c) 2012 <scrawl@baseoftrash.de>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
@ -0,0 +1,583 @@
|
||||
#include "Factory.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include "Platform.hpp"
|
||||
#include "ScriptLoader.hpp"
|
||||
#include "ShaderSet.hpp"
|
||||
#include "MaterialInstanceTextureUnit.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
Factory* Factory::sThis = 0;
|
||||
|
||||
Factory& Factory::getInstance()
|
||||
{
|
||||
assert (sThis);
|
||||
return *sThis;
|
||||
}
|
||||
|
||||
Factory* Factory::getInstancePtr()
|
||||
{
|
||||
return sThis;
|
||||
}
|
||||
|
||||
Factory::Factory (Platform* platform)
|
||||
: mPlatform(platform)
|
||||
, mShadersEnabled(true)
|
||||
, mShaderDebugOutputEnabled(false)
|
||||
, mCurrentLanguage(Language_None)
|
||||
, mListener(NULL)
|
||||
, mCurrentConfiguration(NULL)
|
||||
, mCurrentLodConfiguration(NULL)
|
||||
, mReadMicrocodeCache(false)
|
||||
, mWriteMicrocodeCache(false)
|
||||
, mReadSourceCache(false)
|
||||
, mWriteSourceCache(false)
|
||||
{
|
||||
assert (!sThis);
|
||||
sThis = this;
|
||||
|
||||
mPlatform->setFactory(this);
|
||||
}
|
||||
|
||||
void Factory::loadAllFiles()
|
||||
{
|
||||
assert(mCurrentLanguage != Language_None);
|
||||
|
||||
bool anyShaderDirty = false;
|
||||
|
||||
if (boost::filesystem::exists (mPlatform->getCacheFolder () + "/lastModified.txt"))
|
||||
{
|
||||
std::ifstream file;
|
||||
file.open(std::string(mPlatform->getCacheFolder () + "/lastModified.txt").c_str());
|
||||
|
||||
std::string line;
|
||||
while (getline(file, line))
|
||||
{
|
||||
std::string sourceFile = line;
|
||||
|
||||
if (!getline(file, line))
|
||||
assert(0);
|
||||
|
||||
int modified = boost::lexical_cast<int>(line);
|
||||
|
||||
mShadersLastModified[sourceFile] = modified;
|
||||
}
|
||||
}
|
||||
|
||||
// load configurations
|
||||
{
|
||||
ScriptLoader shaderSetLoader(".configuration");
|
||||
ScriptLoader::loadAllFiles (&shaderSetLoader, mPlatform->getBasePath());
|
||||
std::map <std::string, ScriptNode*> nodes = shaderSetLoader.getAllConfigScripts();
|
||||
for (std::map <std::string, ScriptNode*>::const_iterator it = nodes.begin();
|
||||
it != nodes.end(); ++it)
|
||||
{
|
||||
if (!(it->second->getName() == "configuration"))
|
||||
{
|
||||
std::cerr << "sh::Factory: Warning: Unsupported root node type \"" << it->second->getName() << "\" for file type .configuration" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
PropertySetGet newConfiguration;
|
||||
newConfiguration.setParent(&mGlobalSettings);
|
||||
|
||||
std::vector<ScriptNode*> props = it->second->getChildren();
|
||||
for (std::vector<ScriptNode*>::const_iterator propIt = props.begin(); propIt != props.end(); ++propIt)
|
||||
{
|
||||
std::string name = (*propIt)->getName();
|
||||
std::string val = (*propIt)->getValue();
|
||||
|
||||
newConfiguration.setProperty (name, makeProperty(val));
|
||||
}
|
||||
|
||||
mConfigurations[it->first] = newConfiguration;
|
||||
}
|
||||
}
|
||||
|
||||
// load lod configurations
|
||||
{
|
||||
ScriptLoader lodLoader(".lod");
|
||||
ScriptLoader::loadAllFiles (&lodLoader, mPlatform->getBasePath());
|
||||
std::map <std::string, ScriptNode*> nodes = lodLoader.getAllConfigScripts();
|
||||
for (std::map <std::string, ScriptNode*>::const_iterator it = nodes.begin();
|
||||
it != nodes.end(); ++it)
|
||||
{
|
||||
if (!(it->second->getName() == "lod_configuration"))
|
||||
{
|
||||
std::cerr << "sh::Factory: Warning: Unsupported root node type \"" << it->second->getName() << "\" for file type .lod" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
if (it->first == "0")
|
||||
{
|
||||
throw std::runtime_error("lod level 0 (max lod) can't have a configuration");
|
||||
}
|
||||
|
||||
PropertySetGet newLod;
|
||||
|
||||
std::vector<ScriptNode*> props = it->second->getChildren();
|
||||
for (std::vector<ScriptNode*>::const_iterator propIt = props.begin(); propIt != props.end(); ++propIt)
|
||||
{
|
||||
std::string name = (*propIt)->getName();
|
||||
std::string val = (*propIt)->getValue();
|
||||
|
||||
newLod.setProperty (name, makeProperty(val));
|
||||
}
|
||||
|
||||
mLodConfigurations[boost::lexical_cast<int>(it->first)] = newLod;
|
||||
}
|
||||
}
|
||||
|
||||
// load shader sets
|
||||
{
|
||||
ScriptLoader shaderSetLoader(".shaderset");
|
||||
ScriptLoader::loadAllFiles (&shaderSetLoader, mPlatform->getBasePath());
|
||||
std::map <std::string, ScriptNode*> nodes = shaderSetLoader.getAllConfigScripts();
|
||||
for (std::map <std::string, ScriptNode*>::const_iterator it = nodes.begin();
|
||||
it != nodes.end(); ++it)
|
||||
{
|
||||
if (!(it->second->getName() == "shader_set"))
|
||||
{
|
||||
std::cerr << "sh::Factory: Warning: Unsupported root node type \"" << it->second->getName() << "\" for file type .shaderset" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!it->second->findChild("profiles_cg"))
|
||||
throw std::runtime_error ("missing \"profiles_cg\" field for \"" + it->first + "\"");
|
||||
if (!it->second->findChild("profiles_hlsl"))
|
||||
throw std::runtime_error ("missing \"profiles_hlsl\" field for \"" + it->first + "\"");
|
||||
if (!it->second->findChild("source"))
|
||||
throw std::runtime_error ("missing \"source\" field for \"" + it->first + "\"");
|
||||
if (!it->second->findChild("type"))
|
||||
throw std::runtime_error ("missing \"type\" field for \"" + it->first + "\"");
|
||||
|
||||
std::vector<std::string> profiles_cg;
|
||||
boost::split (profiles_cg, it->second->findChild("profiles_cg")->getValue(), boost::is_any_of(" "));
|
||||
std::string cg_profile;
|
||||
for (std::vector<std::string>::iterator it2 = profiles_cg.begin(); it2 != profiles_cg.end(); ++it2)
|
||||
{
|
||||
if (mPlatform->isProfileSupported(*it2))
|
||||
{
|
||||
cg_profile = *it2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> profiles_hlsl;
|
||||
boost::split (profiles_hlsl, it->second->findChild("profiles_hlsl")->getValue(), boost::is_any_of(" "));
|
||||
std::string hlsl_profile;
|
||||
for (std::vector<std::string>::iterator it2 = profiles_hlsl.begin(); it2 != profiles_hlsl.end(); ++it2)
|
||||
{
|
||||
if (mPlatform->isProfileSupported(*it2))
|
||||
{
|
||||
hlsl_profile = *it2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string sourceFile = mPlatform->getBasePath() + "/" + it->second->findChild("source")->getValue();
|
||||
|
||||
ShaderSet newSet (it->second->findChild("type")->getValue(), cg_profile, hlsl_profile,
|
||||
sourceFile,
|
||||
mPlatform->getBasePath(),
|
||||
it->first,
|
||||
&mGlobalSettings);
|
||||
|
||||
int lastModified = boost::filesystem::last_write_time (boost::filesystem::path(sourceFile));
|
||||
if (mShadersLastModified.find(sourceFile) != mShadersLastModified.end()
|
||||
&& mShadersLastModified[sourceFile] != lastModified)
|
||||
{
|
||||
newSet.markDirty ();
|
||||
anyShaderDirty = true;
|
||||
}
|
||||
|
||||
mShadersLastModified[sourceFile] = lastModified;
|
||||
|
||||
mShaderSets.insert(std::make_pair(it->first, newSet));
|
||||
}
|
||||
}
|
||||
|
||||
// load materials
|
||||
{
|
||||
ScriptLoader materialLoader(".mat");
|
||||
ScriptLoader::loadAllFiles (&materialLoader, mPlatform->getBasePath());
|
||||
|
||||
std::map <std::string, ScriptNode*> nodes = materialLoader.getAllConfigScripts();
|
||||
for (std::map <std::string, ScriptNode*>::const_iterator it = nodes.begin();
|
||||
it != nodes.end(); ++it)
|
||||
{
|
||||
if (!(it->second->getName() == "material"))
|
||||
{
|
||||
std::cerr << "sh::Factory: Warning: Unsupported root node type \"" << it->second->getName() << "\" for file type .mat" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
MaterialInstance newInstance(it->first, this);
|
||||
newInstance.create(mPlatform);
|
||||
if (!mShadersEnabled)
|
||||
newInstance.setShadersEnabled (false);
|
||||
|
||||
newInstance.setSourceFile (it->second->m_fileName);
|
||||
|
||||
std::vector<ScriptNode*> props = it->second->getChildren();
|
||||
for (std::vector<ScriptNode*>::const_iterator propIt = props.begin(); propIt != props.end(); ++propIt)
|
||||
{
|
||||
std::string name = (*propIt)->getName();
|
||||
|
||||
std::string val = (*propIt)->getValue();
|
||||
|
||||
if (name == "pass")
|
||||
{
|
||||
MaterialInstancePass* newPass = newInstance.createPass();
|
||||
std::vector<ScriptNode*> props2 = (*propIt)->getChildren();
|
||||
for (std::vector<ScriptNode*>::const_iterator propIt2 = props2.begin(); propIt2 != props2.end(); ++propIt2)
|
||||
{
|
||||
std::string name2 = (*propIt2)->getName();
|
||||
std::string val2 = (*propIt2)->getValue();
|
||||
|
||||
if (name2 == "shader_properties")
|
||||
{
|
||||
std::vector<ScriptNode*> shaderProps = (*propIt2)->getChildren();
|
||||
for (std::vector<ScriptNode*>::const_iterator shaderPropIt = shaderProps.begin(); shaderPropIt != shaderProps.end(); ++shaderPropIt)
|
||||
{
|
||||
std::string val = (*shaderPropIt)->getValue();
|
||||
newPass->mShaderProperties.setProperty((*shaderPropIt)->getName(), makeProperty(val));
|
||||
}
|
||||
}
|
||||
else if (name2 == "texture_unit")
|
||||
{
|
||||
MaterialInstanceTextureUnit* newTex = newPass->createTextureUnit(val2);
|
||||
std::vector<ScriptNode*> texProps = (*propIt2)->getChildren();
|
||||
for (std::vector<ScriptNode*>::const_iterator texPropIt = texProps.begin(); texPropIt != texProps.end(); ++texPropIt)
|
||||
{
|
||||
std::string val = (*texPropIt)->getValue();
|
||||
newTex->setProperty((*texPropIt)->getName(), makeProperty(val));
|
||||
}
|
||||
}
|
||||
else
|
||||
newPass->setProperty((*propIt2)->getName(), makeProperty(val2));
|
||||
}
|
||||
}
|
||||
else if (name == "parent")
|
||||
newInstance.setParentInstance(val);
|
||||
else
|
||||
newInstance.setProperty((*propIt)->getName(), makeProperty(val));
|
||||
}
|
||||
|
||||
if (newInstance.hasProperty("create_configuration"))
|
||||
{
|
||||
std::string config = retrieveValue<StringValue>(newInstance.getProperty("create_configuration"), NULL).get();
|
||||
newInstance.createForConfiguration (config, 0);
|
||||
}
|
||||
|
||||
mMaterials.insert (std::make_pair(it->first, newInstance));
|
||||
}
|
||||
|
||||
// now that all materials are loaded, replace the parent names with the actual pointers to parent
|
||||
for (MaterialMap::iterator it = mMaterials.begin(); it != mMaterials.end(); ++it)
|
||||
{
|
||||
std::string parent = it->second.getParentInstance();
|
||||
if (parent != "")
|
||||
{
|
||||
if (mMaterials.find (it->second.getParentInstance()) == mMaterials.end())
|
||||
throw std::runtime_error ("Unable to find parent for material instance \"" + it->first + "\"");
|
||||
it->second.setParent(&mMaterials.find(parent)->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (mPlatform->supportsShaderSerialization () && mReadMicrocodeCache && !anyShaderDirty)
|
||||
{
|
||||
std::string file = mPlatform->getCacheFolder () + "/shShaderCache.txt";
|
||||
if (boost::filesystem::exists(file))
|
||||
{
|
||||
mPlatform->deserializeShaders (file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Factory::~Factory ()
|
||||
{
|
||||
if (mPlatform->supportsShaderSerialization () && mWriteMicrocodeCache)
|
||||
{
|
||||
std::string file = mPlatform->getCacheFolder () + "/shShaderCache.txt";
|
||||
mPlatform->serializeShaders (file);
|
||||
}
|
||||
|
||||
if (mReadSourceCache)
|
||||
{
|
||||
// save the last modified time of shader sources
|
||||
std::ofstream file;
|
||||
file.open(std::string(mPlatform->getCacheFolder () + "/lastModified.txt").c_str());
|
||||
|
||||
for (LastModifiedMap::const_iterator it = mShadersLastModified.begin(); it != mShadersLastModified.end(); ++it)
|
||||
{
|
||||
file << it->first << "\n" << it->second << std::endl;
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
delete mPlatform;
|
||||
sThis = 0;
|
||||
}
|
||||
|
||||
MaterialInstance* Factory::searchInstance (const std::string& name)
|
||||
{
|
||||
if (mMaterials.find(name) != mMaterials.end())
|
||||
return &mMaterials.find(name)->second;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
MaterialInstance* Factory::findInstance (const std::string& name)
|
||||
{
|
||||
assert (mMaterials.find(name) != mMaterials.end());
|
||||
return &mMaterials.find(name)->second;
|
||||
}
|
||||
|
||||
MaterialInstance* Factory::requestMaterial (const std::string& name, const std::string& configuration, unsigned short lodIndex)
|
||||
{
|
||||
MaterialInstance* m = searchInstance (name);
|
||||
|
||||
if (configuration != "Default" && mConfigurations.find(configuration) == mConfigurations.end())
|
||||
return NULL;
|
||||
|
||||
if (m)
|
||||
{
|
||||
// make sure all lod techniques below (higher lod) exist
|
||||
int i = lodIndex;
|
||||
while (i>0)
|
||||
{
|
||||
--i;
|
||||
m->createForConfiguration (configuration, i);
|
||||
|
||||
if (mListener)
|
||||
mListener->materialCreated (m, configuration, i);
|
||||
}
|
||||
|
||||
m->createForConfiguration (configuration, lodIndex);
|
||||
if (mListener)
|
||||
mListener->materialCreated (m, configuration, lodIndex);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
MaterialInstance* Factory::createMaterialInstance (const std::string& name, const std::string& parentInstance)
|
||||
{
|
||||
if (parentInstance != "" && mMaterials.find(parentInstance) == mMaterials.end())
|
||||
throw std::runtime_error ("trying to clone material that does not exist");
|
||||
|
||||
MaterialInstance newInstance(name, this);
|
||||
|
||||
if (!mShadersEnabled)
|
||||
newInstance.setShadersEnabled(false);
|
||||
|
||||
if (parentInstance != "")
|
||||
newInstance.setParent (&mMaterials.find(parentInstance)->second);
|
||||
|
||||
newInstance.create(mPlatform);
|
||||
|
||||
mMaterials.insert (std::make_pair(name, newInstance));
|
||||
|
||||
return &mMaterials.find(name)->second;
|
||||
}
|
||||
|
||||
void Factory::destroyMaterialInstance (const std::string& name)
|
||||
{
|
||||
if (mMaterials.find(name) != mMaterials.end())
|
||||
mMaterials.erase(name);
|
||||
}
|
||||
|
||||
void Factory::setShadersEnabled (bool enabled)
|
||||
{
|
||||
mShadersEnabled = enabled;
|
||||
for (MaterialMap::iterator it = mMaterials.begin(); it != mMaterials.end(); ++it)
|
||||
{
|
||||
it->second.setShadersEnabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
void Factory::setGlobalSetting (const std::string& name, const std::string& value)
|
||||
{
|
||||
bool changed = true;
|
||||
if (mGlobalSettings.hasProperty(name))
|
||||
changed = (retrieveValue<StringValue>(mGlobalSettings.getProperty(name), NULL).get() != value);
|
||||
|
||||
mGlobalSettings.setProperty (name, makeProperty<StringValue>(new StringValue(value)));
|
||||
|
||||
if (changed)
|
||||
{
|
||||
for (MaterialMap::iterator it = mMaterials.begin(); it != mMaterials.end(); ++it)
|
||||
{
|
||||
it->second.destroyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Factory::setSharedParameter (const std::string& name, PropertyValuePtr value)
|
||||
{
|
||||
mPlatform->setSharedParameter(name, value);
|
||||
}
|
||||
|
||||
ShaderSet* Factory::getShaderSet (const std::string& name)
|
||||
{
|
||||
return &mShaderSets.find(name)->second;
|
||||
}
|
||||
|
||||
Platform* Factory::getPlatform ()
|
||||
{
|
||||
return mPlatform;
|
||||
}
|
||||
|
||||
Language Factory::getCurrentLanguage ()
|
||||
{
|
||||
return mCurrentLanguage;
|
||||
}
|
||||
|
||||
void Factory::setCurrentLanguage (Language lang)
|
||||
{
|
||||
bool changed = (mCurrentLanguage != lang);
|
||||
mCurrentLanguage = lang;
|
||||
|
||||
if (changed)
|
||||
{
|
||||
for (MaterialMap::iterator it = mMaterials.begin(); it != mMaterials.end(); ++it)
|
||||
{
|
||||
it->second.destroyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MaterialInstance* Factory::getMaterialInstance (const std::string& name)
|
||||
{
|
||||
return findInstance(name);
|
||||
}
|
||||
|
||||
void Factory::setTextureAlias (const std::string& alias, const std::string& realName)
|
||||
{
|
||||
mTextureAliases[alias] = realName;
|
||||
|
||||
// update the already existing texture units
|
||||
for (std::map<TextureUnitState*, std::string>::iterator it = mTextureAliasInstances.begin(); it != mTextureAliasInstances.end(); ++it)
|
||||
{
|
||||
if (it->second == alias)
|
||||
{
|
||||
it->first->setTextureName(realName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string Factory::retrieveTextureAlias (const std::string& name)
|
||||
{
|
||||
if (mTextureAliases.find(name) != mTextureAliases.end())
|
||||
return mTextureAliases[name];
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
PropertySetGet* Factory::getConfiguration (const std::string& name)
|
||||
{
|
||||
return &mConfigurations[name];
|
||||
}
|
||||
|
||||
void Factory::registerConfiguration (const std::string& name, PropertySetGet configuration)
|
||||
{
|
||||
mConfigurations[name] = configuration;
|
||||
mConfigurations[name].setParent (&mGlobalSettings);
|
||||
}
|
||||
|
||||
void Factory::registerLodConfiguration (int index, PropertySetGet configuration)
|
||||
{
|
||||
mLodConfigurations[index] = configuration;
|
||||
}
|
||||
|
||||
void Factory::setMaterialListener (MaterialListener* listener)
|
||||
{
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
void Factory::addTextureAliasInstance (const std::string& name, TextureUnitState* t)
|
||||
{
|
||||
mTextureAliasInstances[t] = name;
|
||||
}
|
||||
|
||||
void Factory::removeTextureAliasInstances (TextureUnitState* t)
|
||||
{
|
||||
mTextureAliasInstances.erase(t);
|
||||
}
|
||||
|
||||
void Factory::setActiveConfiguration (const std::string& configuration)
|
||||
{
|
||||
if (configuration == "Default")
|
||||
mCurrentConfiguration = 0;
|
||||
else
|
||||
{
|
||||
assert (mConfigurations.find(configuration) != mConfigurations.end());
|
||||
mCurrentConfiguration = &mConfigurations[configuration];
|
||||
}
|
||||
}
|
||||
|
||||
void Factory::setActiveLodLevel (int level)
|
||||
{
|
||||
if (level == 0)
|
||||
mCurrentLodConfiguration = 0;
|
||||
else
|
||||
{
|
||||
assert (mLodConfigurations.find(level) != mLodConfigurations.end());
|
||||
mCurrentLodConfiguration = &mLodConfigurations[level];
|
||||
}
|
||||
}
|
||||
|
||||
void Factory::setShaderDebugOutputEnabled (bool enabled)
|
||||
{
|
||||
mShaderDebugOutputEnabled = enabled;
|
||||
}
|
||||
|
||||
PropertySetGet* Factory::getCurrentGlobalSettings()
|
||||
{
|
||||
PropertySetGet* p = &mGlobalSettings;
|
||||
|
||||
// current global settings are affected by active configuration & active lod configuration
|
||||
|
||||
if (mCurrentConfiguration)
|
||||
{
|
||||
p = mCurrentConfiguration;
|
||||
}
|
||||
|
||||
if (mCurrentLodConfiguration)
|
||||
{
|
||||
mCurrentLodConfiguration->setParent(p);
|
||||
p = mCurrentLodConfiguration;
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void Factory::saveMaterials (const std::string& filename)
|
||||
{
|
||||
std::ofstream file;
|
||||
file.open (filename.c_str ());
|
||||
|
||||
for (MaterialMap::iterator it = mMaterials.begin(); it != mMaterials.end(); ++it)
|
||||
{
|
||||
it->second.save(file);
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
void Factory::_ensureMaterial(const std::string& name, const std::string& configuration)
|
||||
{
|
||||
MaterialInstance* m = searchInstance (name);
|
||||
assert(m);
|
||||
m->createForConfiguration (configuration, 0);
|
||||
}
|
||||
}
|
@ -0,0 +1,207 @@
|
||||
#ifndef SH_FACTORY_H
|
||||
#define SH_FACTORY_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "MaterialInstance.hpp"
|
||||
#include "ShaderSet.hpp"
|
||||
#include "Language.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
class Platform;
|
||||
|
||||
typedef std::map<std::string, MaterialInstance> MaterialMap;
|
||||
typedef std::map<std::string, ShaderSet> ShaderSetMap;
|
||||
typedef std::map<std::string, PropertySetGet> ConfigurationMap;
|
||||
typedef std::map<int, PropertySetGet> LodConfigurationMap;
|
||||
typedef std::map<std::string, int> LastModifiedMap;
|
||||
|
||||
typedef std::map<std::string, std::string> TextureAliasMap;
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* Allows you to be notified when a certain material was just created. Useful for changing material properties that you can't
|
||||
* do in a .mat script (for example a series of animated textures) \n
|
||||
* When receiving the event, you can get the platform material by calling m->getMaterial()
|
||||
* and casting that to the platform specific material (e.g. for Ogre, sh::OgreMaterial)
|
||||
*/
|
||||
class MaterialListener
|
||||
{
|
||||
public:
|
||||
virtual void materialCreated (MaterialInstance* m, const std::string& configuration, unsigned short lodIndex) = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* The main interface class
|
||||
*/
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
Factory(Platform* platform);
|
||||
///< @note Ownership of \a platform is transferred to this class, so you don't have to delete it.
|
||||
|
||||
~Factory();
|
||||
|
||||
/**
|
||||
* Create a MaterialInstance, optionally copying all properties from \a parentInstance
|
||||
* @param name name of the new instance
|
||||
* @param name of the parent (optional)
|
||||
* @return newly created instance
|
||||
*/
|
||||
MaterialInstance* createMaterialInstance (const std::string& name, const std::string& parentInstance = "");
|
||||
|
||||
/// @note It is safe to call this if the instance does not exist
|
||||
void destroyMaterialInstance (const std::string& name);
|
||||
|
||||
/// Use this to enable or disable shaders on-the-fly
|
||||
void setShadersEnabled (bool enabled);
|
||||
|
||||
/// write generated shaders to current directory, useful for debugging
|
||||
void setShaderDebugOutputEnabled (bool enabled);
|
||||
|
||||
/// Use this to manage user settings. \n
|
||||
/// Global settings can be retrieved in shaders through a macro. \n
|
||||
/// When a global setting is changed, the shaders that depend on them are recompiled automatically.
|
||||
void setGlobalSetting (const std::string& name, const std::string& value);
|
||||
|
||||
/// Adjusts the given shared parameter. \n
|
||||
/// Internally, this will change all uniform parameters of this name marked with the macro \@shSharedParameter \n
|
||||
/// @param name of the shared parameter
|
||||
/// @param value of the parameter, use sh::makeProperty to construct this value
|
||||
void setSharedParameter (const std::string& name, PropertyValuePtr value);
|
||||
|
||||
Language getCurrentLanguage ();
|
||||
|
||||
/// Switch between different shader languages (cg, glsl, hlsl)
|
||||
void setCurrentLanguage (Language lang);
|
||||
|
||||
/// Get a MaterialInstance by name
|
||||
MaterialInstance* getMaterialInstance (const std::string& name);
|
||||
|
||||
/// Register a configuration, which can then be used by switching the active material scheme
|
||||
void registerConfiguration (const std::string& name, PropertySetGet configuration);
|
||||
|
||||
/// Register a lod configuration, which can then be used by setting up lod distance values for the material \n
|
||||
/// 0 refers to highest lod, so use 1 or higher as index parameter
|
||||
void registerLodConfiguration (int index, PropertySetGet configuration);
|
||||
|
||||
/// Set an alias name for a texture, the real name can then be retrieved with the "texture_alias"
|
||||
/// property in a texture unit - this is useful if you don't know the name of your texture beforehand. \n
|
||||
/// Example: \n
|
||||
/// - In the material definition: texture_alias ReflectionMap \n
|
||||
/// - At runtime: factory->setTextureAlias("ReflectionMap", "rtt_654654"); \n
|
||||
/// You can call factory->setTextureAlias as many times as you want, and if the material was already created, its texture will be updated!
|
||||
void setTextureAlias (const std::string& alias, const std::string& realName);
|
||||
|
||||
/// Retrieve the real texture name for a texture alias (the real name is set by the user)
|
||||
std::string retrieveTextureAlias (const std::string& name);
|
||||
|
||||
/// Attach a listener for material created events
|
||||
void setMaterialListener (MaterialListener* listener);
|
||||
|
||||
/// Call this after you have set up basic stuff, like the shader language.
|
||||
void loadAllFiles ();
|
||||
|
||||
/// Controls writing of generated shader source code to the cache folder, so that the
|
||||
/// (rather expensive) preprocessing step can be skipped on the next run. See Factory::setReadSourceCache \n
|
||||
/// \note The default is off (no cache writing)
|
||||
void setWriteSourceCache(bool write) { mWriteSourceCache = write; }
|
||||
|
||||
/// Controls reading of generated shader sources from the cache folder
|
||||
/// \note The default is off (no cache reading)
|
||||
/// \note Even if microcode caching is enabled, generating (or caching) the source is still required due to the macros.
|
||||
void setReadSourceCache(bool read) { mReadSourceCache = read; }
|
||||
|
||||
/// Controls writing the microcode of the generated shaders to the cache folder. Microcode is machine independent
|
||||
/// and loads very fast compared to regular compilation. Note that the availability of this feature depends on the \a Platform.
|
||||
/// \note The default is off (no cache writing)
|
||||
void setWriteMicrocodeCache(bool write) { mWriteMicrocodeCache = write; }
|
||||
|
||||
/// Controls reading of shader microcode from the cache folder. Microcode is machine independent
|
||||
/// and loads very fast compared to regular compilation. Note that the availability of this feature depends on the \a Platform.
|
||||
/// \note The default is off (no cache reading)
|
||||
void setReadMicrocodeCache(bool read) { mReadMicrocodeCache = read; }
|
||||
|
||||
/// Saves all the materials that were initially loaded from the file with this name
|
||||
void saveMaterials (const std::string& filename);
|
||||
|
||||
static Factory& getInstance();
|
||||
///< Return instance of this class.
|
||||
|
||||
static Factory* getInstancePtr();
|
||||
|
||||
/// Make sure a material technique is loaded.\n
|
||||
/// You will probably never have to use this.
|
||||
void _ensureMaterial(const std::string& name, const std::string& configuration);
|
||||
|
||||
private:
|
||||
|
||||
MaterialInstance* requestMaterial (const std::string& name, const std::string& configuration, unsigned short lodIndex);
|
||||
ShaderSet* getShaderSet (const std::string& name);
|
||||
PropertySetGet* getConfiguration (const std::string& name);
|
||||
Platform* getPlatform ();
|
||||
|
||||
PropertySetGet* getCurrentGlobalSettings();
|
||||
|
||||
void addTextureAliasInstance (const std::string& name, TextureUnitState* t);
|
||||
void removeTextureAliasInstances (TextureUnitState* t);
|
||||
|
||||
std::string getCacheFolder () { return mPlatform->getCacheFolder (); }
|
||||
bool getReadSourceCache() { return mReadSourceCache; }
|
||||
bool getWriteSourceCache() { return mReadSourceCache; }
|
||||
public:
|
||||
bool getWriteMicrocodeCache() { return mWriteMicrocodeCache; } // Fixme
|
||||
|
||||
private:
|
||||
void setActiveConfiguration (const std::string& configuration);
|
||||
void setActiveLodLevel (int level);
|
||||
|
||||
bool getShaderDebugOutputEnabled() { return mShaderDebugOutputEnabled; }
|
||||
|
||||
std::map<TextureUnitState*, std::string> mTextureAliasInstances;
|
||||
|
||||
friend class Platform;
|
||||
friend class MaterialInstance;
|
||||
friend class ShaderInstance;
|
||||
friend class ShaderSet;
|
||||
friend class TextureUnitState;
|
||||
|
||||
private:
|
||||
static Factory* sThis;
|
||||
|
||||
bool mShadersEnabled;
|
||||
bool mShaderDebugOutputEnabled;
|
||||
|
||||
bool mReadMicrocodeCache;
|
||||
bool mWriteMicrocodeCache;
|
||||
bool mReadSourceCache;
|
||||
bool mWriteSourceCache;
|
||||
|
||||
MaterialMap mMaterials;
|
||||
ShaderSetMap mShaderSets;
|
||||
ConfigurationMap mConfigurations;
|
||||
LodConfigurationMap mLodConfigurations;
|
||||
LastModifiedMap mShadersLastModified;
|
||||
|
||||
PropertySetGet mGlobalSettings;
|
||||
|
||||
PropertySetGet* mCurrentConfiguration;
|
||||
PropertySetGet* mCurrentLodConfiguration;
|
||||
|
||||
TextureAliasMap mTextureAliases;
|
||||
|
||||
Language mCurrentLanguage;
|
||||
|
||||
MaterialListener* mListener;
|
||||
|
||||
Platform* mPlatform;
|
||||
|
||||
MaterialInstance* findInstance (const std::string& name);
|
||||
MaterialInstance* searchInstance (const std::string& name);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,16 @@
|
||||
#ifndef SH_LANGUAGE_H
|
||||
#define SH_LANGUAGE_H
|
||||
|
||||
namespace sh
|
||||
{
|
||||
enum Language
|
||||
{
|
||||
Language_CG,
|
||||
Language_HLSL,
|
||||
Language_GLSL,
|
||||
Language_Count,
|
||||
Language_None
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,218 @@
|
||||
#include "MaterialInstance.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "Factory.hpp"
|
||||
#include "ShaderSet.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
MaterialInstance::MaterialInstance (const std::string& name, Factory* f)
|
||||
: mName(name)
|
||||
, mShadersEnabled(true)
|
||||
, mFactory(f)
|
||||
, mListener(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
MaterialInstance::~MaterialInstance ()
|
||||
{
|
||||
}
|
||||
|
||||
void MaterialInstance::setParentInstance (const std::string& name)
|
||||
{
|
||||
mParentInstance = name;
|
||||
}
|
||||
|
||||
std::string MaterialInstance::getParentInstance ()
|
||||
{
|
||||
return mParentInstance;
|
||||
}
|
||||
|
||||
void MaterialInstance::create (Platform* platform)
|
||||
{
|
||||
mMaterial = platform->createMaterial(mName);
|
||||
|
||||
if (hasProperty ("shadow_caster_material"))
|
||||
mMaterial->setShadowCasterMaterial (retrieveValue<StringValue>(getProperty("shadow_caster_material"), NULL).get());
|
||||
|
||||
if (hasProperty ("lod_values"))
|
||||
mMaterial->setLodLevels (retrieveValue<StringValue>(getProperty("lod_values"), NULL).get());
|
||||
}
|
||||
|
||||
void MaterialInstance::destroyAll ()
|
||||
{
|
||||
if (hasProperty("create_configuration"))
|
||||
return;
|
||||
mMaterial->removeAll();
|
||||
mTexUnits.clear();
|
||||
}
|
||||
|
||||
void MaterialInstance::setProperty (const std::string& name, PropertyValuePtr value)
|
||||
{
|
||||
PropertySetGet::setProperty (name, value);
|
||||
destroyAll(); // trigger updates
|
||||
}
|
||||
|
||||
void MaterialInstance::createForConfiguration (const std::string& configuration, unsigned short lodIndex)
|
||||
{
|
||||
bool res = mMaterial->createConfiguration(configuration, lodIndex);
|
||||
if (!res)
|
||||
return; // listener was false positive
|
||||
|
||||
if (mListener)
|
||||
mListener->requestedConfiguration (this, configuration);
|
||||
|
||||
mFactory->setActiveConfiguration (configuration);
|
||||
mFactory->setActiveLodLevel (lodIndex);
|
||||
|
||||
bool allowFixedFunction = true;
|
||||
if (!mShadersEnabled && hasProperty("allow_fixed_function"))
|
||||
{
|
||||
allowFixedFunction = retrieveValue<BooleanValue>(getProperty("allow_fixed_function"), NULL).get();
|
||||
}
|
||||
|
||||
// get passes of the top-most parent
|
||||
PassVector passes = getPasses();
|
||||
if (passes.size() == 0)
|
||||
throw std::runtime_error ("material \"" + mName + "\" does not have any passes");
|
||||
|
||||
for (PassVector::iterator it = passes.begin(); it != passes.end(); ++it)
|
||||
{
|
||||
boost::shared_ptr<Pass> pass = mMaterial->createPass (configuration, lodIndex);
|
||||
it->copyAll (pass.get(), this);
|
||||
|
||||
// texture samplers used in the shaders
|
||||
std::vector<std::string> usedTextureSamplersVertex;
|
||||
std::vector<std::string> usedTextureSamplersFragment;
|
||||
|
||||
PropertySetGet* context = this;
|
||||
|
||||
// create or retrieve shaders
|
||||
bool hasVertex = it->hasProperty("vertex_program");
|
||||
bool hasFragment = it->hasProperty("fragment_program");
|
||||
if (mShadersEnabled || !allowFixedFunction)
|
||||
{
|
||||
it->setContext(context);
|
||||
it->mShaderProperties.setContext(context);
|
||||
if (hasVertex)
|
||||
{
|
||||
ShaderSet* vertex = mFactory->getShaderSet(retrieveValue<StringValue>(it->getProperty("vertex_program"), context).get());
|
||||
ShaderInstance* v = vertex->getInstance(&it->mShaderProperties);
|
||||
if (v)
|
||||
{
|
||||
pass->assignProgram (GPT_Vertex, v->getName());
|
||||
v->setUniformParameters (pass, &it->mShaderProperties);
|
||||
|
||||
std::vector<std::string> sharedParams = v->getSharedParameters ();
|
||||
for (std::vector<std::string>::iterator it = sharedParams.begin(); it != sharedParams.end(); ++it)
|
||||
{
|
||||
pass->addSharedParameter (GPT_Vertex, *it);
|
||||
}
|
||||
|
||||
std::vector<std::string> vector = v->getUsedSamplers ();
|
||||
usedTextureSamplersVertex.insert(usedTextureSamplersVertex.end(), vector.begin(), vector.end());
|
||||
}
|
||||
}
|
||||
if (hasFragment)
|
||||
{
|
||||
ShaderSet* fragment = mFactory->getShaderSet(retrieveValue<StringValue>(it->getProperty("fragment_program"), context).get());
|
||||
ShaderInstance* f = fragment->getInstance(&it->mShaderProperties);
|
||||
if (f)
|
||||
{
|
||||
pass->assignProgram (GPT_Fragment, f->getName());
|
||||
f->setUniformParameters (pass, &it->mShaderProperties);
|
||||
|
||||
std::vector<std::string> sharedParams = f->getSharedParameters ();
|
||||
for (std::vector<std::string>::iterator it = sharedParams.begin(); it != sharedParams.end(); ++it)
|
||||
{
|
||||
pass->addSharedParameter (GPT_Fragment, *it);
|
||||
}
|
||||
|
||||
std::vector<std::string> vector = f->getUsedSamplers ();
|
||||
usedTextureSamplersFragment.insert(usedTextureSamplersFragment.end(), vector.begin(), vector.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// create texture units
|
||||
std::vector<MaterialInstanceTextureUnit> texUnits = it->getTexUnits();
|
||||
int i=0;
|
||||
for (std::vector<MaterialInstanceTextureUnit>::iterator texIt = texUnits.begin(); texIt != texUnits.end(); ++texIt )
|
||||
{
|
||||
// only create those that are needed by the shader, OR those marked to be created in fixed function pipeline if shaders are disabled
|
||||
bool foundVertex = std::find(usedTextureSamplersVertex.begin(), usedTextureSamplersVertex.end(), texIt->getName()) != usedTextureSamplersVertex.end();
|
||||
bool foundFragment = std::find(usedTextureSamplersFragment.begin(), usedTextureSamplersFragment.end(), texIt->getName()) != usedTextureSamplersFragment.end();
|
||||
if ( (foundVertex || foundFragment)
|
||||
|| (((!mShadersEnabled || (!hasVertex || !hasFragment)) && allowFixedFunction) && texIt->hasProperty("create_in_ffp") && retrieveValue<BooleanValue>(texIt->getProperty("create_in_ffp"), this).get()))
|
||||
{
|
||||
boost::shared_ptr<TextureUnitState> texUnit = pass->createTextureUnitState ();
|
||||
texIt->copyAll (texUnit.get(), context);
|
||||
|
||||
mTexUnits.push_back(texUnit);
|
||||
|
||||
// set texture unit indices (required by GLSL)
|
||||
if (mShadersEnabled && ((hasVertex && foundVertex) || (hasFragment && foundFragment)) && mFactory->getCurrentLanguage () == Language_GLSL)
|
||||
{
|
||||
pass->setTextureUnitIndex (foundVertex ? GPT_Vertex : GPT_Fragment, texIt->getName(), i);
|
||||
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (mListener)
|
||||
mListener->createdConfiguration (this, configuration);
|
||||
}
|
||||
|
||||
Material* MaterialInstance::getMaterial ()
|
||||
{
|
||||
return mMaterial.get();
|
||||
}
|
||||
|
||||
MaterialInstancePass* MaterialInstance::createPass ()
|
||||
{
|
||||
mPasses.push_back (MaterialInstancePass());
|
||||
mPasses.back().setContext(this);
|
||||
return &mPasses.back();
|
||||
}
|
||||
|
||||
PassVector MaterialInstance::getPasses()
|
||||
{
|
||||
if (mParent)
|
||||
return static_cast<MaterialInstance*>(mParent)->getPasses();
|
||||
else
|
||||
return mPasses;
|
||||
}
|
||||
|
||||
void MaterialInstance::setShadersEnabled (bool enabled)
|
||||
{
|
||||
if (enabled == mShadersEnabled)
|
||||
return;
|
||||
mShadersEnabled = enabled;
|
||||
|
||||
// trigger updates
|
||||
if (mMaterial.get())
|
||||
destroyAll();
|
||||
}
|
||||
|
||||
void MaterialInstance::save (std::ofstream& stream)
|
||||
{
|
||||
stream << "material " << mName << "\n"
|
||||
<< "{\n";
|
||||
|
||||
if (mParent)
|
||||
{
|
||||
stream << "\t" << static_cast<MaterialInstance*>(mParent)->getName() << "\n";
|
||||
}
|
||||
|
||||
const PropertyMap& properties = listProperties ();
|
||||
for (PropertyMap::const_iterator it = properties.begin(); it != properties.end(); ++it)
|
||||
{
|
||||
stream << "\t" << it->first << " " << retrieveValue<StringValue>(getProperty(it->first), NULL).get() << "\n";
|
||||
}
|
||||
|
||||
stream << "}\n";
|
||||
}
|
||||
}
|
@ -0,0 +1,104 @@
|
||||
#ifndef SH_MATERIALINSTANCE_H
|
||||
#define SH_MATERIALINSTANCE_H
|
||||
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
|
||||
#include "PropertyBase.hpp"
|
||||
#include "Platform.hpp"
|
||||
#include "MaterialInstancePass.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
class Factory;
|
||||
|
||||
typedef std::vector<MaterialInstancePass> PassVector;
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* Allows you to be notified when a certain configuration for a material was just about to be created. \n
|
||||
* Useful for adjusting some properties prior to the material being created (Or you could also re-create
|
||||
* the whole material from scratch, i.e. use this as a method to create this material entirely in code)
|
||||
*/
|
||||
class MaterialInstanceListener
|
||||
{
|
||||
public:
|
||||
virtual void requestedConfiguration (MaterialInstance* m, const std::string& configuration) = 0; ///< called before creating
|
||||
virtual void createdConfiguration (MaterialInstance* m, const std::string& configuration) = 0; ///< called after creating
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* A specific material instance, which has all required properties set
|
||||
* (for example the diffuse & normal map, ambient/diffuse/specular values). \n
|
||||
* Depending on these properties, the system will automatically select a shader permutation
|
||||
* that suits these and create the backend materials / passes (provided by the \a Platform class).
|
||||
*/
|
||||
class MaterialInstance : public PropertySetGet
|
||||
{
|
||||
public:
|
||||
MaterialInstance (const std::string& name, Factory* f);
|
||||
virtual ~MaterialInstance ();
|
||||
|
||||
MaterialInstancePass* createPass ();
|
||||
PassVector getPasses(); ///< gets the passes of the top-most parent
|
||||
|
||||
/// @attention Because the backend material passes are created on demand, the returned material here might not contain anything yet!
|
||||
/// The only place where you should use this method, is for the MaterialInstance given by the MaterialListener::materialCreated event!
|
||||
Material* getMaterial();
|
||||
|
||||
/// attach a \a MaterialInstanceListener to this specific material (as opposed to \a MaterialListener, which listens to all materials)
|
||||
void setListener (MaterialInstanceListener* l) { mListener = l; }
|
||||
|
||||
std::string getName() { return mName; }
|
||||
|
||||
virtual void setProperty (const std::string& name, PropertyValuePtr value);
|
||||
|
||||
private:
|
||||
void setParentInstance (const std::string& name);
|
||||
std::string getParentInstance ();
|
||||
|
||||
void create (Platform* platform);
|
||||
void createForConfiguration (const std::string& configuration, unsigned short lodIndex);
|
||||
|
||||
void destroyAll ();
|
||||
|
||||
void setShadersEnabled (bool enabled);
|
||||
|
||||
void setSourceFile(const std::string& sourceFile) { mSourceFile = sourceFile; }
|
||||
|
||||
std::string getSourceFile() { return mSourceFile; }
|
||||
///< get the name of the file this material was read from, or empty if it was created dynamically by code
|
||||
|
||||
void save (std::ofstream& stream);
|
||||
///< this will only save the properties, not the passes and texture units, and as such
|
||||
/// is only intended to be used for derived materials
|
||||
|
||||
friend class Factory;
|
||||
|
||||
|
||||
private:
|
||||
std::string mParentInstance;
|
||||
///< this is only used during the file-loading phase. an instance could be loaded before its parent is loaded,
|
||||
/// so initially only the parent's name is written to this member.
|
||||
/// once all instances are loaded, the actual mParent pointer (from PropertySetGet class) can be set
|
||||
|
||||
std::vector< boost::shared_ptr<TextureUnitState> > mTexUnits;
|
||||
|
||||
MaterialInstanceListener* mListener;
|
||||
|
||||
PassVector mPasses;
|
||||
|
||||
std::string mName;
|
||||
|
||||
std::string mSourceFile;
|
||||
|
||||
boost::shared_ptr<Material> mMaterial;
|
||||
|
||||
bool mShadersEnabled;
|
||||
|
||||
Factory* mFactory;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,16 @@
|
||||
#include "MaterialInstancePass.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
|
||||
MaterialInstanceTextureUnit* MaterialInstancePass::createTextureUnit (const std::string& name)
|
||||
{
|
||||
mTexUnits.push_back(MaterialInstanceTextureUnit(name));
|
||||
return &mTexUnits.back();
|
||||
}
|
||||
|
||||
std::vector <MaterialInstanceTextureUnit> MaterialInstancePass::getTexUnits ()
|
||||
{
|
||||
return mTexUnits;
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
#ifndef SH_MATERIALINSTANCEPASS_H
|
||||
#define SH_MATERIALINSTANCEPASS_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "PropertyBase.hpp"
|
||||
#include "MaterialInstanceTextureUnit.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
/**
|
||||
* @brief
|
||||
* Holds properties of a single texture unit in a \a MaterialInstancePass. \n
|
||||
* No inheritance here for now.
|
||||
*/
|
||||
class MaterialInstancePass : public PropertySetGet
|
||||
{
|
||||
public:
|
||||
MaterialInstanceTextureUnit* createTextureUnit (const std::string& name);
|
||||
|
||||
PropertySetGet mShaderProperties;
|
||||
|
||||
std::vector <MaterialInstanceTextureUnit> getTexUnits ();
|
||||
private:
|
||||
std::vector <MaterialInstanceTextureUnit> mTexUnits;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,14 @@
|
||||
#include "MaterialInstanceTextureUnit.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
MaterialInstanceTextureUnit::MaterialInstanceTextureUnit (const std::string& name)
|
||||
: mName(name)
|
||||
{
|
||||
}
|
||||
|
||||
std::string MaterialInstanceTextureUnit::getName() const
|
||||
{
|
||||
return mName;
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
#ifndef SH_MATERIALINSTANCETEXTUREUNIT_H
|
||||
#define SH_MATERIALINSTANCETEXTUREUNIT_H
|
||||
|
||||
#include "PropertyBase.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
/**
|
||||
* @brief
|
||||
* A single texture unit state that belongs to a \a MaterialInstancePass \n
|
||||
* this is not the real "backend" \a TextureUnitState (provided by \a Platform),
|
||||
* it is merely a placeholder for properties. \n
|
||||
* @note The backend \a TextureUnitState will only be created if this texture unit is
|
||||
* actually used (i.e. referenced in the shader, or marked with property create_in_ffp = true).
|
||||
*/
|
||||
class MaterialInstanceTextureUnit : public PropertySetGet
|
||||
{
|
||||
public:
|
||||
MaterialInstanceTextureUnit (const std::string& name);
|
||||
std::string getName() const;
|
||||
private:
|
||||
std::string mName;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,94 @@
|
||||
#include "Platform.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "Factory.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
Platform::Platform (const std::string& basePath)
|
||||
: mBasePath(basePath)
|
||||
, mCacheFolder("./")
|
||||
, mShaderCachingEnabled(false)
|
||||
{
|
||||
}
|
||||
|
||||
Platform::~Platform ()
|
||||
{
|
||||
}
|
||||
|
||||
void Platform::setFactory (Factory* factory)
|
||||
{
|
||||
mFactory = factory;
|
||||
}
|
||||
|
||||
std::string Platform::getBasePath ()
|
||||
{
|
||||
return mBasePath;
|
||||
}
|
||||
|
||||
bool Platform::supportsMaterialQueuedListener ()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Platform::supportsShaderSerialization ()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
MaterialInstance* Platform::fireMaterialRequested (const std::string& name, const std::string& configuration, unsigned short lodIndex)
|
||||
{
|
||||
return mFactory->requestMaterial (name, configuration, lodIndex);
|
||||
}
|
||||
|
||||
void Platform::serializeShaders (const std::string& file)
|
||||
{
|
||||
throw std::runtime_error ("Shader serialization not supported by this platform");
|
||||
}
|
||||
|
||||
void Platform::deserializeShaders (const std::string& file)
|
||||
{
|
||||
throw std::runtime_error ("Shader serialization not supported by this platform");
|
||||
}
|
||||
|
||||
void Platform::setCacheFolder (const std::string& folder)
|
||||
{
|
||||
mCacheFolder = folder;
|
||||
}
|
||||
|
||||
void Platform::setShaderCachingEnabled (bool enabled)
|
||||
{
|
||||
mShaderCachingEnabled = enabled;
|
||||
}
|
||||
|
||||
std::string Platform::getCacheFolder() const
|
||||
{
|
||||
return mCacheFolder;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
bool TextureUnitState::setPropertyOverride (const std::string& name, PropertyValuePtr& value, PropertySetGet *context)
|
||||
{
|
||||
if (name == "texture_alias")
|
||||
{
|
||||
std::string aliasName = retrieveValue<StringValue>(value, context).get();
|
||||
|
||||
Factory::getInstance().addTextureAliasInstance (aliasName, this);
|
||||
|
||||
setTextureName (Factory::getInstance().retrieveTextureAlias (aliasName));
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
TextureUnitState::~TextureUnitState()
|
||||
{
|
||||
Factory* f = Factory::getInstancePtr ();
|
||||
if (f)
|
||||
f->removeTextureAliasInstances (this);
|
||||
}
|
||||
}
|
@ -0,0 +1,145 @@
|
||||
#ifndef SH_PLATFORM_H
|
||||
#define SH_PLATFORM_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#include "Language.hpp"
|
||||
#include "PropertyBase.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
class Factory;
|
||||
class MaterialInstance;
|
||||
|
||||
enum GpuProgramType
|
||||
{
|
||||
GPT_Vertex,
|
||||
GPT_Fragment
|
||||
// GPT_Geometry
|
||||
};
|
||||
|
||||
// These classes are supposed to be filled by the platform implementation
|
||||
class GpuProgram
|
||||
{
|
||||
public:
|
||||
virtual bool getSupported () = 0; ///< @return true if the compilation was successful
|
||||
|
||||
/// @param name name of the uniform in the shader
|
||||
/// @param autoConstantName name of the auto constant (for example world_viewproj_matrix)
|
||||
/// @param extraInfo if any extra info is needed (e.g. light index), put it here
|
||||
virtual void setAutoConstant (const std::string& name, const std::string& autoConstantName, const std::string& extraInfo = "") = 0;
|
||||
};
|
||||
|
||||
class TextureUnitState : public PropertySet
|
||||
{
|
||||
public:
|
||||
virtual ~TextureUnitState();
|
||||
|
||||
virtual void setTextureName (const std::string& textureName) = 0;
|
||||
|
||||
protected:
|
||||
virtual bool setPropertyOverride (const std::string& name, PropertyValuePtr& value, PropertySetGet *context);
|
||||
};
|
||||
|
||||
class Pass : public PropertySet
|
||||
{
|
||||
public:
|
||||
virtual boost::shared_ptr<TextureUnitState> createTextureUnitState () = 0;
|
||||
virtual void assignProgram (GpuProgramType type, const std::string& name) = 0;
|
||||
|
||||
/// @param type gpu program type
|
||||
/// @param name name of the uniform in the shader
|
||||
/// @param vt type of value, e.g. vector4
|
||||
/// @param value value to set
|
||||
/// @param context used for retrieving linked values
|
||||
virtual void setGpuConstant (int type, const std::string& name, ValueType vt, PropertyValuePtr value, PropertySetGet* context) = 0;
|
||||
|
||||
virtual void setTextureUnitIndex (int programType, const std::string& name, int index) = 0;
|
||||
|
||||
virtual void addSharedParameter (int type, const std::string& name) = 0;
|
||||
};
|
||||
|
||||
class Material : public PropertySet
|
||||
{
|
||||
public:
|
||||
virtual boost::shared_ptr<Pass> createPass (const std::string& configuration, unsigned short lodIndex) = 0;
|
||||
virtual bool createConfiguration (const std::string& name, unsigned short lodIndex) = 0; ///< @return false if already exists
|
||||
virtual void removeAll () = 0; ///< remove all configurations
|
||||
|
||||
virtual void setLodLevels (const std::string& lodLevels) = 0;
|
||||
|
||||
virtual void setShadowCasterMaterial (const std::string& name) = 0;
|
||||
};
|
||||
|
||||
class Platform
|
||||
{
|
||||
public:
|
||||
Platform (const std::string& basePath);
|
||||
virtual ~Platform ();
|
||||
|
||||
void setShaderCachingEnabled (bool enabled);
|
||||
|
||||
/// set the folder to use for shader caching
|
||||
void setCacheFolder (const std::string& folder);
|
||||
|
||||
private:
|
||||
virtual boost::shared_ptr<Material> createMaterial (const std::string& name) = 0;
|
||||
|
||||
virtual boost::shared_ptr<GpuProgram> createGpuProgram (
|
||||
GpuProgramType type,
|
||||
const std::string& compileArguments,
|
||||
const std::string& name, const std::string& profile,
|
||||
const std::string& source, Language lang) = 0;
|
||||
|
||||
virtual void setSharedParameter (const std::string& name, PropertyValuePtr value) = 0;
|
||||
|
||||
virtual bool isProfileSupported (const std::string& profile) = 0;
|
||||
|
||||
virtual void serializeShaders (const std::string& file);
|
||||
virtual void deserializeShaders (const std::string& file);
|
||||
|
||||
std::string getCacheFolder () const;
|
||||
|
||||
friend class Factory;
|
||||
friend class MaterialInstance;
|
||||
friend class ShaderInstance;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* this will be \a true if the platform supports serialization (writing shader microcode
|
||||
* to disk) and deserialization (create gpu program from saved microcode)
|
||||
*/
|
||||
virtual bool supportsShaderSerialization ();
|
||||
|
||||
/**
|
||||
* this will be \a true if the platform supports a listener that notifies the system
|
||||
* whenever a material is requested for rendering. if this is supported, shaders can be
|
||||
* compiled on-demand when needed (and not earlier)
|
||||
* @todo the Factory is not designed yet to handle the case where this method returns false
|
||||
*/
|
||||
virtual bool supportsMaterialQueuedListener ();
|
||||
|
||||
/**
|
||||
* fire event: material requested for rendering
|
||||
* @param name material name
|
||||
* @param configuration requested configuration
|
||||
*/
|
||||
MaterialInstance* fireMaterialRequested (const std::string& name, const std::string& configuration, unsigned short lodIndex);
|
||||
|
||||
std::string mCacheFolder;
|
||||
Factory* mFactory;
|
||||
|
||||
protected:
|
||||
bool mShaderCachingEnabled;
|
||||
|
||||
private:
|
||||
void setFactory (Factory* factory);
|
||||
|
||||
std::string mBasePath;
|
||||
std::string getBasePath();
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,99 @@
|
||||
#include "Preprocessor.hpp"
|
||||
|
||||
#include <boost/wave.hpp>
|
||||
#include <boost/wave/cpplexer/cpp_lex_token.hpp>
|
||||
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
|
||||
|
||||
namespace sh
|
||||
{
|
||||
std::string Preprocessor::preprocess (std::string source, const std::string& includePath, std::vector<std::string> definitions, const std::string& name)
|
||||
{
|
||||
std::stringstream returnString;
|
||||
|
||||
// current file position is saved for exception handling
|
||||
boost::wave::util::file_position_type current_position;
|
||||
|
||||
try
|
||||
{
|
||||
// This token type is one of the central types used throughout the library.
|
||||
// It is a template parameter to some of the public classes and instances
|
||||
// of this type are returned from the iterators.
|
||||
typedef boost::wave::cpplexer::lex_token<> token_type;
|
||||
|
||||
// The template boost::wave::cpplexer::lex_iterator<> is the lexer type to
|
||||
// to use as the token source for the preprocessing engine. It is
|
||||
// parametrized with the token type.
|
||||
typedef boost::wave::cpplexer::lex_iterator<token_type> lex_iterator_type;
|
||||
|
||||
// This is the resulting context type. The first template parameter should
|
||||
// match the iterator type used during construction of the context
|
||||
// instance (see below). It is the type of the underlying input stream.
|
||||
typedef boost::wave::context<std::string::iterator, lex_iterator_type
|
||||
, boost::wave::iteration_context_policies::load_file_to_string,
|
||||
emit_custom_line_directives_hooks>
|
||||
context_type;
|
||||
|
||||
// The preprocessor iterator shouldn't be constructed directly. It is
|
||||
// generated through a wave::context<> object. This wave:context<> object
|
||||
// is additionally used to initialize and define different parameters of
|
||||
// the actual preprocessing.
|
||||
//
|
||||
// The preprocessing of the input stream is done on the fly behind the
|
||||
// scenes during iteration over the range of context_type::iterator_type
|
||||
// instances.
|
||||
context_type ctx (source.begin(), source.end(), name.c_str());
|
||||
ctx.add_include_path(includePath.c_str());
|
||||
for (std::vector<std::string>::iterator it = definitions.begin(); it != definitions.end(); ++it)
|
||||
{
|
||||
ctx.add_macro_definition(*it);
|
||||
}
|
||||
|
||||
// Get the preprocessor iterators and use them to generate the token
|
||||
// sequence.
|
||||
context_type::iterator_type first = ctx.begin();
|
||||
context_type::iterator_type last = ctx.end();
|
||||
|
||||
// The input stream is preprocessed for you while iterating over the range
|
||||
// [first, last). The dereferenced iterator returns tokens holding
|
||||
// information about the preprocessed input stream, such as token type,
|
||||
// token value, and position.
|
||||
while (first != last)
|
||||
{
|
||||
current_position = (*first).get_position();
|
||||
returnString << (*first).get_value();
|
||||
++first;
|
||||
}
|
||||
}
|
||||
catch (boost::wave::cpp_exception const& e)
|
||||
{
|
||||
// some preprocessing error
|
||||
std::stringstream error;
|
||||
error
|
||||
<< e.file_name() << "(" << e.line_no() << "): "
|
||||
<< e.description();
|
||||
throw std::runtime_error(error.str());
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
// use last recognized token to retrieve the error position
|
||||
std::stringstream error;
|
||||
error
|
||||
<< current_position.get_file()
|
||||
<< "(" << current_position.get_line() << "): "
|
||||
<< "exception caught: " << e.what();
|
||||
throw std::runtime_error(error.str());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// use last recognized token to retrieve the error position
|
||||
std::stringstream error;
|
||||
error
|
||||
<< current_position.get_file()
|
||||
<< "(" << current_position.get_line() << "): "
|
||||
<< "unexpected exception caught.";
|
||||
throw std::runtime_error(error.str());
|
||||
}
|
||||
|
||||
return returnString.str();
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
#ifndef SH_PREPROCESSOR_H
|
||||
#define SH_PREPROCESSOR_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <cstdio>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include <boost/wave/cpp_throw.hpp>
|
||||
#include <boost/wave/cpp_exceptions.hpp>
|
||||
#include <boost/wave/token_ids.hpp>
|
||||
#include <boost/wave/util/macro_helpers.hpp>
|
||||
#include <boost/wave/preprocessing_hooks.hpp>
|
||||
|
||||
namespace sh
|
||||
{
|
||||
/**
|
||||
* @brief A simple interface for the boost::wave preprocessor
|
||||
*/
|
||||
class Preprocessor
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Run a shader source string through the preprocessor
|
||||
* @param source source string
|
||||
* @param includePath path to search for includes (that are included with #include)
|
||||
* @param definitions macros to predefine (vector of strings of the format MACRO=value, or just MACRO to define it as 1)
|
||||
* @param name name to use for error messages
|
||||
* @return processed string
|
||||
*/
|
||||
static std::string preprocess (std::string source, const std::string& includePath, std::vector<std::string> definitions, const std::string& name);
|
||||
};
|
||||
|
||||
|
||||
|
||||
class emit_custom_line_directives_hooks
|
||||
: public boost::wave::context_policies::default_preprocessing_hooks
|
||||
{
|
||||
public:
|
||||
|
||||
template <typename ContextT, typename ContainerT>
|
||||
bool
|
||||
emit_line_directive(ContextT const& ctx, ContainerT &pending,
|
||||
typename ContextT::token_type const& act_token)
|
||||
{
|
||||
// emit a #line directive showing the relative filename instead
|
||||
typename ContextT::position_type pos = act_token.get_position();
|
||||
unsigned int column = 1;
|
||||
|
||||
typedef typename ContextT::token_type result_type;
|
||||
|
||||
// no line directives for now
|
||||
pos.set_column(column);
|
||||
pending.push_back(result_type(boost::wave::T_GENERATEDNEWLINE, "\n", pos));
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,268 @@
|
||||
#include "PropertyBase.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
namespace sh
|
||||
{
|
||||
|
||||
IntValue::IntValue(int in)
|
||||
: mValue(in)
|
||||
{
|
||||
}
|
||||
|
||||
IntValue::IntValue(const std::string& in)
|
||||
{
|
||||
mValue = boost::lexical_cast<int>(in);
|
||||
}
|
||||
|
||||
std::string IntValue::serialize()
|
||||
{
|
||||
return boost::lexical_cast<std::string>(mValue);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
BooleanValue::BooleanValue (bool in)
|
||||
: mValue(in)
|
||||
{
|
||||
}
|
||||
|
||||
BooleanValue::BooleanValue (const std::string& in)
|
||||
{
|
||||
if (in == "true")
|
||||
mValue = true;
|
||||
else if (in == "false")
|
||||
mValue = false;
|
||||
else
|
||||
{
|
||||
std::cerr << "sh::BooleanValue: Warning: Unrecognized value \"" << in << "\" for property value of type BooleanValue" << std::endl;
|
||||
mValue = false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string BooleanValue::serialize ()
|
||||
{
|
||||
if (mValue)
|
||||
return "true";
|
||||
else
|
||||
return "false";
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
StringValue::StringValue (const std::string& in)
|
||||
{
|
||||
mStringValue = in;
|
||||
}
|
||||
|
||||
std::string StringValue::serialize()
|
||||
{
|
||||
return mStringValue;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
LinkedValue::LinkedValue (const std::string& in)
|
||||
{
|
||||
mStringValue = in;
|
||||
mStringValue.erase(0, 1);
|
||||
}
|
||||
|
||||
std::string LinkedValue::serialize()
|
||||
{
|
||||
throw std::runtime_error ("can't directly get a linked value");
|
||||
}
|
||||
|
||||
std::string LinkedValue::get(PropertySetGet* context) const
|
||||
{
|
||||
PropertyValuePtr p = context->getProperty(mStringValue);
|
||||
return retrieveValue<StringValue>(p, NULL).get();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
FloatValue::FloatValue (float in)
|
||||
{
|
||||
mValue = in;
|
||||
}
|
||||
|
||||
FloatValue::FloatValue (const std::string& in)
|
||||
{
|
||||
mValue = boost::lexical_cast<float>(in);
|
||||
}
|
||||
|
||||
std::string FloatValue::serialize ()
|
||||
{
|
||||
return boost::lexical_cast<std::string>(mValue);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
Vector2::Vector2 (float x, float y)
|
||||
: mX(x)
|
||||
, mY(y)
|
||||
{
|
||||
}
|
||||
|
||||
Vector2::Vector2 (const std::string& in)
|
||||
{
|
||||
std::vector<std::string> tokens;
|
||||
boost::split(tokens, in, boost::is_any_of(" "));
|
||||
assert ((tokens.size() == 2) && "Invalid Vector2 conversion");
|
||||
mX = boost::lexical_cast<float> (tokens[0]);
|
||||
mY = boost::lexical_cast<float> (tokens[1]);
|
||||
}
|
||||
|
||||
std::string Vector2::serialize ()
|
||||
{
|
||||
return boost::lexical_cast<std::string>(mX) + " "
|
||||
+ boost::lexical_cast<std::string>(mY);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
Vector3::Vector3 (float x, float y, float z)
|
||||
: mX(x)
|
||||
, mY(y)
|
||||
, mZ(z)
|
||||
{
|
||||
}
|
||||
|
||||
Vector3::Vector3 (const std::string& in)
|
||||
{
|
||||
std::vector<std::string> tokens;
|
||||
boost::split(tokens, in, boost::is_any_of(" "));
|
||||
assert ((tokens.size() == 3) && "Invalid Vector3 conversion");
|
||||
mX = boost::lexical_cast<float> (tokens[0]);
|
||||
mY = boost::lexical_cast<float> (tokens[1]);
|
||||
mZ = boost::lexical_cast<float> (tokens[2]);
|
||||
}
|
||||
|
||||
std::string Vector3::serialize ()
|
||||
{
|
||||
return boost::lexical_cast<std::string>(mX) + " "
|
||||
+ boost::lexical_cast<std::string>(mY) + " "
|
||||
+ boost::lexical_cast<std::string>(mZ);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
Vector4::Vector4 (float x, float y, float z, float w)
|
||||
: mX(x)
|
||||
, mY(y)
|
||||
, mZ(z)
|
||||
, mW(w)
|
||||
{
|
||||
}
|
||||
|
||||
Vector4::Vector4 (const std::string& in)
|
||||
{
|
||||
std::vector<std::string> tokens;
|
||||
boost::split(tokens, in, boost::is_any_of(" "));
|
||||
assert ((tokens.size() == 4) && "Invalid Vector4 conversion");
|
||||
mX = boost::lexical_cast<float> (tokens[0]);
|
||||
mY = boost::lexical_cast<float> (tokens[1]);
|
||||
mZ = boost::lexical_cast<float> (tokens[2]);
|
||||
mW = boost::lexical_cast<float> (tokens[3]);
|
||||
}
|
||||
|
||||
std::string Vector4::serialize ()
|
||||
{
|
||||
return boost::lexical_cast<std::string>(mX) + " "
|
||||
+ boost::lexical_cast<std::string>(mY) + " "
|
||||
+ boost::lexical_cast<std::string>(mZ) + " "
|
||||
+ boost::lexical_cast<std::string>(mW);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
void PropertySet::setProperty (const std::string& name, PropertyValuePtr &value, PropertySetGet* context)
|
||||
{
|
||||
if (!setPropertyOverride (name, value, context))
|
||||
std::cerr << "sh::PropertySet: Warning: No match for property with name '" << name << "'" << std::endl;
|
||||
}
|
||||
|
||||
bool PropertySet::setPropertyOverride (const std::string& name, PropertyValuePtr &value, PropertySetGet* context)
|
||||
{
|
||||
// if we got here, none of the sub-classes was able to make use of the property
|
||||
return false;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
PropertySetGet::PropertySetGet (PropertySetGet* parent)
|
||||
: mParent(parent)
|
||||
, mContext(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
PropertySetGet::PropertySetGet ()
|
||||
: mParent(NULL)
|
||||
, mContext(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
void PropertySetGet::setParent (PropertySetGet* parent)
|
||||
{
|
||||
mParent = parent;
|
||||
}
|
||||
|
||||
void PropertySetGet::setContext (PropertySetGet* context)
|
||||
{
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
PropertySetGet* PropertySetGet::getContext()
|
||||
{
|
||||
return mContext;
|
||||
}
|
||||
|
||||
void PropertySetGet::setProperty (const std::string& name, PropertyValuePtr value)
|
||||
{
|
||||
mProperties [name] = value;
|
||||
}
|
||||
|
||||
PropertyValuePtr& PropertySetGet::getProperty (const std::string& name)
|
||||
{
|
||||
bool found = (mProperties.find(name) != mProperties.end());
|
||||
|
||||
if (!found)
|
||||
{
|
||||
if (!mParent)
|
||||
throw std::runtime_error ("Trying to retrieve property \"" + name + "\" that does not exist");
|
||||
else
|
||||
return mParent->getProperty (name);
|
||||
}
|
||||
else
|
||||
return mProperties[name];
|
||||
}
|
||||
|
||||
bool PropertySetGet::hasProperty (const std::string& name)
|
||||
{
|
||||
bool found = (mProperties.find(name) != mProperties.end());
|
||||
|
||||
if (!found)
|
||||
{
|
||||
if (!mParent)
|
||||
return false;
|
||||
else
|
||||
return mParent->hasProperty (name);
|
||||
}
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
void PropertySetGet::copyAll (PropertySet* target, PropertySetGet* context)
|
||||
{
|
||||
if (mParent)
|
||||
mParent->copyAll (target, context);
|
||||
for (PropertyMap::iterator it = mProperties.begin(); it != mProperties.end(); ++it)
|
||||
{
|
||||
target->setProperty(it->first, it->second, context);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,235 @@
|
||||
#ifndef SH_PROPERTYBASE_H
|
||||
#define SH_PROPERTYBASE_H
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
namespace sh
|
||||
{
|
||||
class StringValue;
|
||||
class PropertySetGet;
|
||||
class LinkedValue;
|
||||
|
||||
enum ValueType
|
||||
{
|
||||
VT_String,
|
||||
VT_Int,
|
||||
VT_Float,
|
||||
VT_Vector2,
|
||||
VT_Vector3,
|
||||
VT_Vector4
|
||||
};
|
||||
|
||||
class PropertyValue
|
||||
{
|
||||
public:
|
||||
PropertyValue() {}
|
||||
|
||||
virtual ~PropertyValue() {}
|
||||
|
||||
std::string _getStringValue() { return mStringValue; }
|
||||
|
||||
virtual std::string serialize() = 0;
|
||||
|
||||
protected:
|
||||
std::string mStringValue; ///< this will possibly not contain anything in the specialised classes
|
||||
};
|
||||
typedef boost::shared_ptr<PropertyValue> PropertyValuePtr;
|
||||
|
||||
class StringValue : public PropertyValue
|
||||
{
|
||||
public:
|
||||
StringValue (const std::string& in);
|
||||
std::string get() const { return mStringValue; }
|
||||
|
||||
virtual std::string serialize();
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Used for retrieving a named property from a context
|
||||
*/
|
||||
class LinkedValue : public PropertyValue
|
||||
{
|
||||
public:
|
||||
LinkedValue (const std::string& in);
|
||||
|
||||
std::string get(PropertySetGet* context) const;
|
||||
|
||||
virtual std::string serialize();
|
||||
};
|
||||
|
||||
class FloatValue : public PropertyValue
|
||||
{
|
||||
public:
|
||||
FloatValue (float in);
|
||||
FloatValue (const std::string& in);
|
||||
float get() const { return mValue; }
|
||||
|
||||
virtual std::string serialize();
|
||||
private:
|
||||
float mValue;
|
||||
};
|
||||
|
||||
class IntValue : public PropertyValue
|
||||
{
|
||||
public:
|
||||
IntValue (int in);
|
||||
IntValue (const std::string& in);
|
||||
int get() const { return mValue; }
|
||||
|
||||
virtual std::string serialize();
|
||||
private:
|
||||
int mValue;
|
||||
};
|
||||
|
||||
class BooleanValue : public PropertyValue
|
||||
{
|
||||
public:
|
||||
BooleanValue (bool in);
|
||||
BooleanValue (const std::string& in);
|
||||
bool get() const { return mValue; }
|
||||
|
||||
virtual std::string serialize();
|
||||
private:
|
||||
bool mValue;
|
||||
};
|
||||
|
||||
class Vector2 : public PropertyValue
|
||||
{
|
||||
public:
|
||||
Vector2 (float x, float y);
|
||||
Vector2 (const std::string& in);
|
||||
|
||||
float mX, mY;
|
||||
|
||||
virtual std::string serialize();
|
||||
};
|
||||
|
||||
class Vector3 : public PropertyValue
|
||||
{
|
||||
public:
|
||||
Vector3 (float x, float y, float z);
|
||||
Vector3 (const std::string& in);
|
||||
|
||||
float mX, mY, mZ;
|
||||
|
||||
virtual std::string serialize();
|
||||
};
|
||||
|
||||
class Vector4 : public PropertyValue
|
||||
{
|
||||
public:
|
||||
Vector4 (float x, float y, float z, float w);
|
||||
Vector4 (const std::string& in);
|
||||
|
||||
float mX, mY, mZ, mW;
|
||||
|
||||
virtual std::string serialize();
|
||||
};
|
||||
|
||||
/// \brief base class that allows setting properties with any kind of value-type
|
||||
class PropertySet
|
||||
{
|
||||
public:
|
||||
void setProperty (const std::string& name, PropertyValuePtr& value, PropertySetGet* context);
|
||||
|
||||
protected:
|
||||
virtual bool setPropertyOverride (const std::string& name, PropertyValuePtr& value, PropertySetGet* context);
|
||||
///< @return \a true if the specified property was found, or false otherwise
|
||||
};
|
||||
|
||||
typedef std::map<std::string, PropertyValuePtr> PropertyMap;
|
||||
|
||||
/// \brief base class that allows setting properties with any kind of value-type and retrieving them
|
||||
class PropertySetGet
|
||||
{
|
||||
public:
|
||||
PropertySetGet (PropertySetGet* parent);
|
||||
PropertySetGet ();
|
||||
|
||||
virtual ~PropertySetGet() {}
|
||||
|
||||
void copyAll (PropertySet* target, PropertySetGet* context); ///< call setProperty for each property/value pair stored in \a this
|
||||
|
||||
void setParent (PropertySetGet* parent);
|
||||
void setContext (PropertySetGet* context);
|
||||
PropertySetGet* getContext();
|
||||
|
||||
virtual void setProperty (const std::string& name, PropertyValuePtr value);
|
||||
PropertyValuePtr& getProperty (const std::string& name);
|
||||
|
||||
const PropertyMap& listProperties() { return mProperties; }
|
||||
|
||||
bool hasProperty (const std::string& name);
|
||||
|
||||
private:
|
||||
PropertyMap mProperties;
|
||||
|
||||
protected:
|
||||
PropertySetGet* mParent;
|
||||
///< the parent can provide properties as well (when they are retrieved via getProperty) \n
|
||||
/// multiple levels of inheritance are also supported \n
|
||||
/// children can override properties of their parents
|
||||
|
||||
PropertySetGet* mContext;
|
||||
///< used to retrieve linked property values
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
static T retrieveValue (boost::shared_ptr<PropertyValue>& value, PropertySetGet* context)
|
||||
{
|
||||
if (typeid(*value).name() == typeid(LinkedValue).name())
|
||||
{
|
||||
std::string v = static_cast<LinkedValue*>(value.get())->get(context);
|
||||
PropertyValuePtr newVal = PropertyValuePtr (new StringValue(v));
|
||||
return retrieveValue<T>(newVal, NULL);
|
||||
}
|
||||
if (typeid(T).name() == typeid(*value).name())
|
||||
{
|
||||
// requested type is the same as source type, only have to cast it
|
||||
return *static_cast<T*>(value.get());
|
||||
}
|
||||
|
||||
if ((typeid(T).name() == typeid(StringValue).name())
|
||||
&& typeid(*value).name() != typeid(StringValue).name())
|
||||
{
|
||||
// if string type is requested and value is not string, use serialize method to convert to string
|
||||
T* ptr = new T (value->serialize()); // note that T is always StringValue here, but we can't use it here
|
||||
value = boost::shared_ptr<PropertyValue> (static_cast<PropertyValue*>(ptr));
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
{
|
||||
// remaining case: deserialization from string by passing the string to constructor of class T
|
||||
T* ptr = new T(value->_getStringValue());
|
||||
PropertyValuePtr newVal (static_cast<PropertyValue*>(ptr));
|
||||
value = newVal;
|
||||
return *ptr;
|
||||
}
|
||||
}
|
||||
///<
|
||||
/// @brief alternate version that supports linked values (use of $variables in parent material)
|
||||
/// @note \a value is changed in-place to the converted object
|
||||
/// @return converted object \n
|
||||
|
||||
/// Create a property from a string
|
||||
inline PropertyValuePtr makeProperty (const std::string& prop)
|
||||
{
|
||||
if (prop.size() > 1 && prop[0] == '$')
|
||||
return PropertyValuePtr (static_cast<PropertyValue*>(new LinkedValue(prop)));
|
||||
else
|
||||
return PropertyValuePtr (static_cast<PropertyValue*> (new StringValue(prop)));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
/// Create a property of any type
|
||||
/// Example: sh::makeProperty\<sh::Vector4\> (new sh::Vector4(1, 1, 1, 1))
|
||||
inline PropertyValuePtr makeProperty (T* p)
|
||||
{
|
||||
return PropertyValuePtr ( static_cast<PropertyValue*>(p) );
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,401 @@
|
||||
#include "ScriptLoader.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <exception>
|
||||
#include <fstream>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
namespace sh
|
||||
{
|
||||
void ScriptLoader::loadAllFiles(ScriptLoader* c, const std::string& path)
|
||||
{
|
||||
for ( boost::filesystem::recursive_directory_iterator end, dir(path); dir != end; ++dir )
|
||||
{
|
||||
boost::filesystem::path p(*dir);
|
||||
if(p.extension() == c->m_fileEnding)
|
||||
{
|
||||
c->m_currentFileName = (*dir).path().string();
|
||||
std::ifstream in((*dir).path().string().c_str(), std::ios::binary);
|
||||
c->parseScript(in);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ScriptLoader::ScriptLoader(const std::string& fileEnding)
|
||||
{
|
||||
m_fileEnding = fileEnding;
|
||||
}
|
||||
|
||||
ScriptLoader::~ScriptLoader()
|
||||
{
|
||||
clearScriptList();
|
||||
}
|
||||
|
||||
void ScriptLoader::clearScriptList()
|
||||
{
|
||||
std::map <std::string, ScriptNode *>::iterator i;
|
||||
for (i = m_scriptList.begin(); i != m_scriptList.end(); i++)
|
||||
{
|
||||
delete i->second;
|
||||
}
|
||||
m_scriptList.clear();
|
||||
}
|
||||
|
||||
ScriptNode *ScriptLoader::getConfigScript(const std::string &name)
|
||||
{
|
||||
std::map <std::string, ScriptNode*>::iterator i;
|
||||
|
||||
std::string key = name;
|
||||
i = m_scriptList.find(key);
|
||||
|
||||
//If found..
|
||||
if (i != m_scriptList.end())
|
||||
{
|
||||
return i->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
std::map <std::string, ScriptNode*> ScriptLoader::getAllConfigScripts ()
|
||||
{
|
||||
return m_scriptList;
|
||||
}
|
||||
|
||||
void ScriptLoader::parseScript(std::ifstream &stream)
|
||||
{
|
||||
//Get first token
|
||||
_nextToken(stream);
|
||||
if (tok == TOKEN_EOF)
|
||||
{
|
||||
stream.close();
|
||||
return;
|
||||
}
|
||||
|
||||
//Parse the script
|
||||
_parseNodes(stream, 0);
|
||||
|
||||
stream.close();
|
||||
}
|
||||
|
||||
void ScriptLoader::_nextToken(std::ifstream &stream)
|
||||
{
|
||||
//EOF token
|
||||
if (!stream.good())
|
||||
{
|
||||
tok = TOKEN_EOF;
|
||||
return;
|
||||
}
|
||||
|
||||
//(Get next character)
|
||||
int ch = stream.get();
|
||||
|
||||
while ((ch == ' ' || ch == 9) && !stream.eof())
|
||||
{ //Skip leading spaces / tabs
|
||||
ch = stream.get();
|
||||
}
|
||||
|
||||
if (!stream.good())
|
||||
{
|
||||
tok = TOKEN_EOF;
|
||||
return;
|
||||
}
|
||||
|
||||
//Newline token
|
||||
if (ch == '\r' || ch == '\n')
|
||||
{
|
||||
do
|
||||
{
|
||||
ch = stream.get();
|
||||
} while ((ch == '\r' || ch == '\n') && !stream.eof());
|
||||
|
||||
stream.unget();
|
||||
|
||||
tok = TOKEN_NewLine;
|
||||
return;
|
||||
}
|
||||
|
||||
//Open brace token
|
||||
else if (ch == '{')
|
||||
{
|
||||
tok = TOKEN_OpenBrace;
|
||||
return;
|
||||
}
|
||||
|
||||
//Close brace token
|
||||
else if (ch == '}')
|
||||
{
|
||||
tok = TOKEN_CloseBrace;
|
||||
return;
|
||||
}
|
||||
|
||||
//Text token
|
||||
if (ch < 32 || ch > 122) //Verify valid char
|
||||
{
|
||||
throw std::runtime_error("Parse Error: Invalid character, ConfigLoader::load()");
|
||||
}
|
||||
|
||||
tokVal = "";
|
||||
tok = TOKEN_Text;
|
||||
do
|
||||
{
|
||||
//Skip comments
|
||||
if (ch == '/')
|
||||
{
|
||||
int ch2 = stream.peek();
|
||||
|
||||
//C++ style comment (//)
|
||||
if (ch2 == '/')
|
||||
{
|
||||
stream.get();
|
||||
do
|
||||
{
|
||||
ch = stream.get();
|
||||
} while (ch != '\r' && ch != '\n' && !stream.eof());
|
||||
|
||||
tok = TOKEN_NewLine;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//Add valid char to tokVal
|
||||
tokVal += (char)ch;
|
||||
|
||||
//Next char
|
||||
ch = stream.get();
|
||||
|
||||
} while (ch > 32 && ch <= 122 && !stream.eof());
|
||||
|
||||
stream.unget();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ScriptLoader::_skipNewLines(std::ifstream &stream)
|
||||
{
|
||||
while (tok == TOKEN_NewLine)
|
||||
{
|
||||
_nextToken(stream);
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptLoader::_parseNodes(std::ifstream &stream, ScriptNode *parent)
|
||||
{
|
||||
typedef std::pair<std::string, ScriptNode*> ScriptItem;
|
||||
|
||||
while (true)
|
||||
{
|
||||
switch (tok)
|
||||
{
|
||||
//Node
|
||||
case TOKEN_Text:
|
||||
{
|
||||
//Add the new node
|
||||
ScriptNode *newNode;
|
||||
if (parent)
|
||||
{
|
||||
newNode = parent->addChild(tokVal);
|
||||
}
|
||||
else
|
||||
{
|
||||
newNode = new ScriptNode(0, tokVal);
|
||||
}
|
||||
|
||||
//Get values
|
||||
_nextToken(stream);
|
||||
std::string valueStr;
|
||||
int i=0;
|
||||
while (tok == TOKEN_Text)
|
||||
{
|
||||
if (i == 0)
|
||||
valueStr += tokVal;
|
||||
else
|
||||
valueStr += " " + tokVal;
|
||||
_nextToken(stream);
|
||||
++i;
|
||||
}
|
||||
newNode->setValue(valueStr);
|
||||
|
||||
//Add root nodes to scriptList
|
||||
if (!parent)
|
||||
{
|
||||
std::string key;
|
||||
|
||||
if (newNode->getValue() == "")
|
||||
throw std::runtime_error("Root node must have a name (\"" + newNode->getName() + "\")");
|
||||
key = newNode->getValue();
|
||||
|
||||
m_scriptList.insert(ScriptItem(key, newNode));
|
||||
}
|
||||
|
||||
_skipNewLines(stream);
|
||||
|
||||
//Add any sub-nodes
|
||||
if (tok == TOKEN_OpenBrace)
|
||||
{
|
||||
//Parse nodes
|
||||
_nextToken(stream);
|
||||
_parseNodes(stream, newNode);
|
||||
//Check for matching closing brace
|
||||
if (tok != TOKEN_CloseBrace)
|
||||
{
|
||||
throw std::runtime_error("Parse Error: Expecting closing brace");
|
||||
}
|
||||
_nextToken(stream);
|
||||
_skipNewLines(stream);
|
||||
}
|
||||
|
||||
newNode->m_fileName = m_currentFileName;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
//Out of place brace
|
||||
case TOKEN_OpenBrace:
|
||||
throw std::runtime_error("Parse Error: Opening brace out of plane");
|
||||
break;
|
||||
|
||||
//Return if end of nodes have been reached
|
||||
case TOKEN_CloseBrace:
|
||||
return;
|
||||
|
||||
//Return if reached end of file
|
||||
case TOKEN_EOF:
|
||||
return;
|
||||
|
||||
case TOKEN_NewLine:
|
||||
_nextToken(stream);
|
||||
break;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
ScriptNode::ScriptNode(ScriptNode *parent, const std::string &name)
|
||||
{
|
||||
m_name = name;
|
||||
m_parent = parent;
|
||||
_removeSelf = true; //For proper destruction
|
||||
m_lastChildFound = -1;
|
||||
|
||||
//Add self to parent's child list (unless this is the root node being created)
|
||||
if (parent != NULL)
|
||||
{
|
||||
m_parent->m_children.push_back(this);
|
||||
_iter = --(m_parent->m_children.end());
|
||||
}
|
||||
}
|
||||
|
||||
ScriptNode::~ScriptNode()
|
||||
{
|
||||
//Delete all children
|
||||
std::vector<ScriptNode*>::iterator i;
|
||||
for (i = m_children.begin(); i != m_children.end(); i++)
|
||||
{
|
||||
ScriptNode *node = *i;
|
||||
node->_removeSelf = false;
|
||||
delete node;
|
||||
}
|
||||
m_children.clear();
|
||||
|
||||
//Remove self from parent's child list
|
||||
if (_removeSelf && m_parent != NULL)
|
||||
{
|
||||
m_parent->m_children.erase(_iter);
|
||||
}
|
||||
}
|
||||
|
||||
ScriptNode *ScriptNode::addChild(const std::string &name, bool replaceExisting)
|
||||
{
|
||||
if (replaceExisting)
|
||||
{
|
||||
ScriptNode *node = findChild(name, false);
|
||||
if (node)
|
||||
{
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return new ScriptNode(this, name);
|
||||
}
|
||||
|
||||
ScriptNode *ScriptNode::findChild(const std::string &name, bool recursive)
|
||||
{
|
||||
int indx, prevC, nextC;
|
||||
int childCount = (int)m_children.size();
|
||||
|
||||
if (m_lastChildFound != -1)
|
||||
{
|
||||
//If possible, try checking the nodes neighboring the last successful search
|
||||
//(often nodes searched for in sequence, so this will boost search speeds).
|
||||
prevC = m_lastChildFound-1; if (prevC < 0) prevC = 0; else if (prevC >= childCount) prevC = childCount-1;
|
||||
nextC = m_lastChildFound+1; if (nextC < 0) nextC = 0; else if (nextC >= childCount) nextC = childCount-1;
|
||||
for (indx = prevC; indx <= nextC; ++indx)
|
||||
{
|
||||
ScriptNode *node = m_children[indx];
|
||||
if (node->m_name == name)
|
||||
{
|
||||
m_lastChildFound = indx;
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
//If not found that way, search for the node from start to finish, avoiding the
|
||||
//already searched area above.
|
||||
for (indx = nextC + 1; indx < childCount; ++indx)
|
||||
{
|
||||
ScriptNode *node = m_children[indx];
|
||||
if (node->m_name == name) {
|
||||
m_lastChildFound = indx;
|
||||
return node;
|
||||
}
|
||||
}
|
||||
for (indx = 0; indx < prevC; ++indx)
|
||||
{
|
||||
ScriptNode *node = m_children[indx];
|
||||
if (node->m_name == name) {
|
||||
m_lastChildFound = indx;
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//Search for the node from start to finish
|
||||
for (indx = 0; indx < childCount; ++indx){
|
||||
ScriptNode *node = m_children[indx];
|
||||
if (node->m_name == name) {
|
||||
m_lastChildFound = indx;
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//If not found, search child nodes (if recursive == true)
|
||||
if (recursive)
|
||||
{
|
||||
for (indx = 0; indx < childCount; ++indx)
|
||||
{
|
||||
m_children[indx]->findChild(name, recursive);
|
||||
}
|
||||
}
|
||||
|
||||
//Not found anywhere
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void ScriptNode::setParent(ScriptNode *newParent)
|
||||
{
|
||||
//Remove self from current parent
|
||||
m_parent->m_children.erase(_iter);
|
||||
|
||||
//Set new parent
|
||||
m_parent = newParent;
|
||||
|
||||
//Add self to new parent
|
||||
m_parent->m_children.push_back(this);
|
||||
_iter = --(m_parent->m_children.end());
|
||||
}
|
||||
}
|
@ -0,0 +1,134 @@
|
||||
#ifndef SH_CONFIG_LOADER_H__
|
||||
#define SH_CONFIG_LOADER_H__
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
|
||||
namespace sh
|
||||
{
|
||||
class ScriptNode;
|
||||
|
||||
/**
|
||||
* @brief The base class of loaders that read Ogre style script files to get configuration and settings.
|
||||
* Heavily inspired by: http://www.ogre3d.org/tikiwiki/All-purpose+script+parser
|
||||
* ( "Non-ogre version")
|
||||
*/
|
||||
class ScriptLoader
|
||||
{
|
||||
public:
|
||||
static void loadAllFiles(ScriptLoader* c, const std::string& path);
|
||||
|
||||
ScriptLoader(const std::string& fileEnding);
|
||||
virtual ~ScriptLoader();
|
||||
|
||||
std::string m_fileEnding;
|
||||
|
||||
// For a line like
|
||||
// entity animals/dog
|
||||
// {
|
||||
// ...
|
||||
// }
|
||||
// The type is "entity" and the name is "animals/dog"
|
||||
// Or if animal/dog was not there then name is ""
|
||||
ScriptNode *getConfigScript (const std::string &name);
|
||||
|
||||
std::map <std::string, ScriptNode*> getAllConfigScripts ();
|
||||
|
||||
void parseScript(std::ifstream &stream);
|
||||
|
||||
std::string m_currentFileName;
|
||||
|
||||
protected:
|
||||
|
||||
float m_LoadOrder;
|
||||
// like "*.object"
|
||||
|
||||
std::map <std::string, ScriptNode*> m_scriptList;
|
||||
|
||||
enum Token
|
||||
{
|
||||
TOKEN_Text,
|
||||
TOKEN_NewLine,
|
||||
TOKEN_OpenBrace,
|
||||
TOKEN_CloseBrace,
|
||||
TOKEN_EOF
|
||||
};
|
||||
|
||||
Token tok, lastTok;
|
||||
std::string tokVal;
|
||||
|
||||
void _parseNodes(std::ifstream &stream, ScriptNode *parent);
|
||||
void _nextToken(std::ifstream &stream);
|
||||
void _skipNewLines(std::ifstream &stream);
|
||||
|
||||
void clearScriptList();
|
||||
};
|
||||
|
||||
class ScriptNode
|
||||
{
|
||||
public:
|
||||
ScriptNode(ScriptNode *parent, const std::string &name = "untitled");
|
||||
~ScriptNode();
|
||||
|
||||
inline void setName(const std::string &name)
|
||||
{
|
||||
this->m_name = name;
|
||||
}
|
||||
|
||||
inline std::string &getName()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
inline void setValue(const std::string &value)
|
||||
{
|
||||
m_value = value;
|
||||
}
|
||||
|
||||
inline std::string &getValue()
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
ScriptNode *addChild(const std::string &name = "untitled", bool replaceExisting = false);
|
||||
ScriptNode *findChild(const std::string &name, bool recursive = false);
|
||||
|
||||
inline std::vector<ScriptNode*> &getChildren()
|
||||
{
|
||||
return m_children;
|
||||
}
|
||||
|
||||
inline ScriptNode *getChild(unsigned int index = 0)
|
||||
{
|
||||
assert(index < m_children.size());
|
||||
return m_children[index];
|
||||
}
|
||||
|
||||
void setParent(ScriptNode *newParent);
|
||||
|
||||
inline ScriptNode *getParent()
|
||||
{
|
||||
return m_parent;
|
||||
}
|
||||
|
||||
std::string m_fileName;
|
||||
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
std::string m_value;
|
||||
std::vector<ScriptNode*> m_children;
|
||||
ScriptNode *m_parent;
|
||||
|
||||
|
||||
int m_lastChildFound; //The last child node's index found with a call to findChild()
|
||||
|
||||
std::vector<ScriptNode*>::iterator _iter;
|
||||
bool _removeSelf;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,707 @@
|
||||
#include "ShaderInstance.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include "Preprocessor.hpp"
|
||||
#include "Factory.hpp"
|
||||
#include "ShaderSet.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
std::string convertLang (sh::Language lang)
|
||||
{
|
||||
if (lang == sh::Language_CG)
|
||||
return "SH_CG";
|
||||
else if (lang == sh::Language_HLSL)
|
||||
return "SH_HLSL";
|
||||
else //if (lang == sh::Language_GLSL)
|
||||
return "SH_GLSL";
|
||||
}
|
||||
|
||||
char getComponent(int num)
|
||||
{
|
||||
if (num == 0)
|
||||
return 'x';
|
||||
else if (num == 1)
|
||||
return 'y';
|
||||
else if (num == 2)
|
||||
return 'z';
|
||||
else if (num == 3)
|
||||
return 'w';
|
||||
else
|
||||
throw std::runtime_error("invalid component");
|
||||
}
|
||||
|
||||
std::string getFloat(sh::Language lang, int num_components)
|
||||
{
|
||||
if (lang == sh::Language_CG || lang == sh::Language_HLSL)
|
||||
return (num_components == 1) ? "float" : "float" + boost::lexical_cast<std::string>(num_components);
|
||||
else
|
||||
return (num_components == 1) ? "float" : "vec" + boost::lexical_cast<std::string>(num_components);
|
||||
}
|
||||
|
||||
bool isCmd (const std::string& source, size_t pos, const std::string& cmd)
|
||||
{
|
||||
return (source.size() >= pos + cmd.size() && source.substr(pos, cmd.size()) == cmd);
|
||||
}
|
||||
|
||||
void writeDebugFile (const std::string& content, const std::string& filename)
|
||||
{
|
||||
boost::filesystem::path full_path(boost::filesystem::current_path());
|
||||
std::ofstream of ((full_path / filename ).string().c_str() , std::ios_base::out);
|
||||
of.write(content.c_str(), content.size());
|
||||
of.close();
|
||||
}
|
||||
}
|
||||
|
||||
namespace sh
|
||||
{
|
||||
std::string Passthrough::expand_assign(std::string toAssign)
|
||||
{
|
||||
std::string res;
|
||||
|
||||
int i = 0;
|
||||
int current_passthrough = passthrough_number;
|
||||
int current_component_left = component_start;
|
||||
int current_component_right = 0;
|
||||
int components_left = num_components;
|
||||
int components_at_once;
|
||||
while (i < num_components)
|
||||
{
|
||||
if (components_left + current_component_left <= 4)
|
||||
components_at_once = components_left;
|
||||
else
|
||||
components_at_once = 4 - current_component_left;
|
||||
|
||||
std::string componentStr = ".";
|
||||
for (int j = 0; j < components_at_once; ++j)
|
||||
componentStr += getComponent(j + current_component_left);
|
||||
std::string componentStr2 = ".";
|
||||
for (int j = 0; j < components_at_once; ++j)
|
||||
componentStr2 += getComponent(j + current_component_right);
|
||||
if (num_components == 1)
|
||||
{
|
||||
componentStr2 = "";
|
||||
}
|
||||
res += "passthrough" + boost::lexical_cast<std::string>(current_passthrough) + componentStr + " = " + toAssign + componentStr2;
|
||||
|
||||
current_component_left += components_at_once;
|
||||
current_component_right += components_at_once;
|
||||
components_left -= components_at_once;
|
||||
|
||||
i += components_at_once;
|
||||
|
||||
if (components_left == 0)
|
||||
{
|
||||
// finished
|
||||
return res;
|
||||
}
|
||||
else
|
||||
{
|
||||
// add semicolon to every instruction but the last
|
||||
res += "; ";
|
||||
}
|
||||
|
||||
if (current_component_left == 4)
|
||||
{
|
||||
current_passthrough++;
|
||||
current_component_left = 0;
|
||||
}
|
||||
}
|
||||
throw std::runtime_error("expand_assign error"); // this should never happen, but gets us rid of the "control reaches end of non-void function" warning
|
||||
}
|
||||
|
||||
std::string Passthrough::expand_receive()
|
||||
{
|
||||
std::string res;
|
||||
|
||||
res += getFloat(lang, num_components) + "(";
|
||||
|
||||
int i = 0;
|
||||
int current_passthrough = passthrough_number;
|
||||
int current_component = component_start;
|
||||
int components_left = num_components;
|
||||
while (i < num_components)
|
||||
{
|
||||
int components_at_once = std::min(components_left, 4 - current_component);
|
||||
|
||||
std::string componentStr;
|
||||
for (int j = 0; j < components_at_once; ++j)
|
||||
componentStr += getComponent(j + current_component);
|
||||
|
||||
res += "passthrough" + boost::lexical_cast<std::string>(current_passthrough) + "." + componentStr;
|
||||
|
||||
current_component += components_at_once;
|
||||
|
||||
components_left -= components_at_once;
|
||||
|
||||
i += components_at_once;
|
||||
|
||||
if (components_left == 0)
|
||||
{
|
||||
// finished
|
||||
return res + ")";
|
||||
;
|
||||
}
|
||||
else
|
||||
{
|
||||
// add comma to every variable but the last
|
||||
res += ", ";
|
||||
}
|
||||
|
||||
if (current_component == 4)
|
||||
{
|
||||
current_passthrough++;
|
||||
current_component = 0;
|
||||
}
|
||||
}
|
||||
|
||||
throw std::runtime_error("expand_receive error"); // this should never happen, but gets us rid of the "control reaches end of non-void function" warning
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
void ShaderInstance::parse (std::string& source, PropertySetGet* properties)
|
||||
{
|
||||
size_t pos = 0;
|
||||
while (true)
|
||||
{
|
||||
pos = source.find("@", pos);
|
||||
if (pos == std::string::npos)
|
||||
break;
|
||||
|
||||
if (isCmd(source, pos, "@shProperty"))
|
||||
{
|
||||
std::vector<std::string> args = extractMacroArguments (pos, source);
|
||||
|
||||
size_t start = source.find("(", pos);
|
||||
size_t end = source.find(")", pos);
|
||||
std::string cmd = source.substr(pos+1, start-(pos+1));
|
||||
|
||||
std::string replaceValue;
|
||||
if (cmd == "shPropertyBool")
|
||||
{
|
||||
std::string propertyName = args[0];
|
||||
PropertyValuePtr value = properties->getProperty(propertyName);
|
||||
bool val = retrieveValue<BooleanValue>(value, properties->getContext()).get();
|
||||
replaceValue = val ? "1" : "0";
|
||||
}
|
||||
else if (cmd == "shPropertyNotBool") // same as above, but inverts the result
|
||||
{
|
||||
std::string propertyName = args[0];
|
||||
PropertyValuePtr value = properties->getProperty(propertyName);
|
||||
bool val = retrieveValue<BooleanValue>(value, properties->getContext()).get();
|
||||
replaceValue = val ? "0" : "1";
|
||||
}
|
||||
else if (cmd == "shPropertyString")
|
||||
{
|
||||
std::string propertyName = args[0];
|
||||
PropertyValuePtr value = properties->getProperty(propertyName);
|
||||
replaceValue = retrieveValue<StringValue>(value, properties->getContext()).get();
|
||||
}
|
||||
else if (cmd == "shPropertyEqual")
|
||||
{
|
||||
std::string propertyName = args[0];
|
||||
std::string comparedAgainst = args[1];
|
||||
std::string value = retrieveValue<StringValue>(properties->getProperty(propertyName), properties->getContext()).get();
|
||||
replaceValue = (value == comparedAgainst) ? "1" : "0";
|
||||
}
|
||||
else
|
||||
throw std::runtime_error ("unknown command \"" + cmd + "\"");
|
||||
source.replace(pos, (end+1)-pos, replaceValue);
|
||||
}
|
||||
else if (isCmd(source, pos, "@shGlobalSetting"))
|
||||
{
|
||||
std::vector<std::string> args = extractMacroArguments (pos, source);
|
||||
|
||||
std::string cmd = source.substr(pos+1, source.find("(", pos)-(pos+1));
|
||||
std::string replaceValue;
|
||||
if (cmd == "shGlobalSettingBool")
|
||||
{
|
||||
std::string settingName = args[0];
|
||||
std::string value = retrieveValue<StringValue>(mParent->getCurrentGlobalSettings()->getProperty(settingName), NULL).get();
|
||||
replaceValue = (value == "true" || value == "1") ? "1" : "0";
|
||||
}
|
||||
else if (cmd == "shGlobalSettingEqual")
|
||||
{
|
||||
std::string settingName = args[0];
|
||||
std::string comparedAgainst = args[1];
|
||||
std::string value = retrieveValue<StringValue>(mParent->getCurrentGlobalSettings()->getProperty(settingName), NULL).get();
|
||||
replaceValue = (value == comparedAgainst) ? "1" : "0";
|
||||
}
|
||||
else if (cmd == "shGlobalSettingString")
|
||||
{
|
||||
std::string settingName = args[0];
|
||||
replaceValue = retrieveValue<StringValue>(mParent->getCurrentGlobalSettings()->getProperty(settingName), NULL).get();
|
||||
}
|
||||
else
|
||||
throw std::runtime_error ("unknown command \"" + cmd + "\"");
|
||||
|
||||
source.replace(pos, (source.find(")", pos)+1)-pos, replaceValue);
|
||||
}
|
||||
else if (isCmd(source, pos, "@shForeach"))
|
||||
{
|
||||
|
||||
assert(source.find("@shEndForeach", pos) != std::string::npos);
|
||||
size_t block_end = source.find("@shEndForeach", pos);
|
||||
|
||||
// get the argument for parsing
|
||||
size_t start = source.find("(", pos);
|
||||
size_t end = start;
|
||||
int brace_depth = 1;
|
||||
while (brace_depth > 0)
|
||||
{
|
||||
++end;
|
||||
if (source[end] == '(')
|
||||
++brace_depth;
|
||||
else if (source[end] == ')')
|
||||
--brace_depth;
|
||||
}
|
||||
std::string arg = source.substr(start+1, end-(start+1));
|
||||
parse(arg, properties);
|
||||
|
||||
int num = boost::lexical_cast<int>(arg);
|
||||
|
||||
// get the content of the inner block
|
||||
std::string content = source.substr(end+1, block_end - (end+1));
|
||||
|
||||
// replace both outer and inner block with content of inner block num times
|
||||
std::string replaceStr;
|
||||
for (int i=0; i<num; ++i)
|
||||
{
|
||||
// replace @shIterator with the current iteration
|
||||
std::string addStr = content;
|
||||
|
||||
while (true)
|
||||
{
|
||||
size_t pos2 = addStr.find("@shIterator");
|
||||
if (pos2 == std::string::npos)
|
||||
break;
|
||||
|
||||
// optional offset parameter.
|
||||
size_t openBracePos = pos2 + std::string("@shIterator").length();
|
||||
if (addStr[openBracePos] == '(')
|
||||
{
|
||||
// get the argument for parsing
|
||||
size_t _start = openBracePos;
|
||||
size_t _end = _start;
|
||||
int _brace_depth = 1;
|
||||
while (_brace_depth > 0)
|
||||
{
|
||||
++_end;
|
||||
if (addStr[_end] == '(')
|
||||
++_brace_depth;
|
||||
else if (addStr[_end] == ')')
|
||||
--_brace_depth;
|
||||
}
|
||||
std::string arg = addStr.substr(_start+1, _end-(_start+1));
|
||||
parse(arg, properties);
|
||||
|
||||
int offset = boost::lexical_cast<int> (arg);
|
||||
addStr.replace(pos2, (_end+1)-pos2, boost::lexical_cast<std::string>(i+offset));
|
||||
}
|
||||
else
|
||||
{
|
||||
addStr.replace(pos2, std::string("@shIterator").length(), boost::lexical_cast<std::string>(i));
|
||||
}
|
||||
}
|
||||
|
||||
replaceStr += addStr;
|
||||
}
|
||||
source.replace(pos, (block_end+std::string("@shEndForeach").length())-pos, replaceStr);
|
||||
}
|
||||
else if (source.size() > pos+1)
|
||||
++pos; // skip
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ShaderInstance::ShaderInstance (ShaderSet* parent, const std::string& name, PropertySetGet* properties)
|
||||
: mName(name)
|
||||
, mParent(parent)
|
||||
, mSupported(true)
|
||||
, mCurrentPassthrough(0)
|
||||
, mCurrentComponent(0)
|
||||
{
|
||||
std::string source = mParent->getSource();
|
||||
int type = mParent->getType();
|
||||
std::string basePath = mParent->getBasePath();
|
||||
size_t pos;
|
||||
|
||||
bool readCache = Factory::getInstance ().getReadSourceCache () && boost::filesystem::exists(
|
||||
Factory::getInstance ().getCacheFolder () + "/" + mName)
|
||||
&& !mParent->isDirty ();
|
||||
bool writeCache = Factory::getInstance ().getWriteSourceCache ();
|
||||
|
||||
|
||||
if (readCache)
|
||||
{
|
||||
std::ifstream ifs( std::string(Factory::getInstance ().getCacheFolder () + "/" + mName).c_str() );
|
||||
std::stringstream ss;
|
||||
ss << ifs.rdbuf();
|
||||
source = ss.str();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::vector<std::string> definitions;
|
||||
|
||||
if (mParent->getType() == GPT_Vertex)
|
||||
definitions.push_back("SH_VERTEX_SHADER");
|
||||
else
|
||||
definitions.push_back("SH_FRAGMENT_SHADER");
|
||||
definitions.push_back(convertLang(Factory::getInstance().getCurrentLanguage()));
|
||||
|
||||
parse(source, properties);
|
||||
|
||||
if (Factory::getInstance ().getShaderDebugOutputEnabled ())
|
||||
writeDebugFile(source, name + ".pre");
|
||||
else
|
||||
{
|
||||
#ifdef SHINY_WRITE_SHADER_DEBUG
|
||||
writeDebugFile(source, name + ".pre");
|
||||
#endif
|
||||
}
|
||||
|
||||
// why do we need our own preprocessor? there are several custom commands available in the shader files
|
||||
// (for example for binding uniforms to properties or auto constants) - more below. it is important that these
|
||||
// commands are _only executed if the specific code path actually "survives" the compilation.
|
||||
// thus, we run the code through a preprocessor first to remove the parts that are unused because of
|
||||
// unmet #if conditions (or other preprocessor directives).
|
||||
source = Preprocessor::preprocess(source, basePath, definitions, name);
|
||||
|
||||
// parse counter
|
||||
std::map<int, int> counters;
|
||||
while (true)
|
||||
{
|
||||
pos = source.find("@shCounter");
|
||||
if (pos == std::string::npos)
|
||||
break;
|
||||
|
||||
size_t end = source.find(")", pos);
|
||||
|
||||
std::vector<std::string> args = extractMacroArguments (pos, source);
|
||||
assert(args.size());
|
||||
|
||||
int index = boost::lexical_cast<int>(args[0]);
|
||||
|
||||
if (counters.find(index) == counters.end())
|
||||
counters[index] = 0;
|
||||
|
||||
source.replace(pos, (end+1)-pos, boost::lexical_cast<std::string>(counters[index]++));
|
||||
}
|
||||
|
||||
// parse passthrough declarations
|
||||
while (true)
|
||||
{
|
||||
pos = source.find("@shAllocatePassthrough");
|
||||
if (pos == std::string::npos)
|
||||
break;
|
||||
|
||||
if (mCurrentPassthrough > 7)
|
||||
throw std::runtime_error ("too many passthrough's requested (max 8)");
|
||||
|
||||
std::vector<std::string> args = extractMacroArguments (pos, source);
|
||||
assert(args.size() == 2);
|
||||
|
||||
size_t end = source.find(")", pos);
|
||||
|
||||
Passthrough passthrough;
|
||||
|
||||
passthrough.num_components = boost::lexical_cast<int>(args[0]);
|
||||
assert (passthrough.num_components != 0);
|
||||
|
||||
std::string passthroughName = args[1];
|
||||
passthrough.lang = Factory::getInstance().getCurrentLanguage ();
|
||||
passthrough.component_start = mCurrentComponent;
|
||||
passthrough.passthrough_number = mCurrentPassthrough;
|
||||
|
||||
mPassthroughMap[passthroughName] = passthrough;
|
||||
|
||||
mCurrentComponent += passthrough.num_components;
|
||||
if (mCurrentComponent > 3)
|
||||
{
|
||||
mCurrentComponent -= 4;
|
||||
++mCurrentPassthrough;
|
||||
}
|
||||
|
||||
source.erase(pos, (end+1)-pos);
|
||||
}
|
||||
|
||||
// passthrough assign
|
||||
while (true)
|
||||
{
|
||||
pos = source.find("@shPassthroughAssign");
|
||||
if (pos == std::string::npos)
|
||||
break;
|
||||
|
||||
std::vector<std::string> args = extractMacroArguments (pos, source);
|
||||
assert(args.size() == 2);
|
||||
|
||||
size_t end = source.find(")", pos);
|
||||
|
||||
std::string passthroughName = args[0];
|
||||
std::string assignTo = args[1];
|
||||
|
||||
assert(mPassthroughMap.find(passthroughName) != mPassthroughMap.end());
|
||||
Passthrough& p = mPassthroughMap[passthroughName];
|
||||
|
||||
source.replace(pos, (end+1)-pos, p.expand_assign(assignTo));
|
||||
}
|
||||
|
||||
// passthrough receive
|
||||
while (true)
|
||||
{
|
||||
pos = source.find("@shPassthroughReceive");
|
||||
if (pos == std::string::npos)
|
||||
break;
|
||||
|
||||
std::vector<std::string> args = extractMacroArguments (pos, source);
|
||||
assert(args.size() == 1);
|
||||
|
||||
size_t end = source.find(")", pos);
|
||||
std::string passthroughName = args[0];
|
||||
|
||||
assert(mPassthroughMap.find(passthroughName) != mPassthroughMap.end());
|
||||
Passthrough& p = mPassthroughMap[passthroughName];
|
||||
|
||||
source.replace(pos, (end+1)-pos, p.expand_receive());
|
||||
}
|
||||
|
||||
// passthrough vertex outputs
|
||||
while (true)
|
||||
{
|
||||
pos = source.find("@shPassthroughVertexOutputs");
|
||||
if (pos == std::string::npos)
|
||||
break;
|
||||
|
||||
std::string result;
|
||||
for (int i = 0; i < mCurrentPassthrough+1; ++i)
|
||||
{
|
||||
// not using newlines here, otherwise the line numbers reported by compiler would be messed up..
|
||||
if (Factory::getInstance().getCurrentLanguage () == Language_CG || Factory::getInstance().getCurrentLanguage () == Language_HLSL)
|
||||
result += ", out float4 passthrough" + boost::lexical_cast<std::string>(i) + " : TEXCOORD" + boost::lexical_cast<std::string>(i);
|
||||
|
||||
/*
|
||||
else
|
||||
result += "out vec4 passthrough" + boost::lexical_cast<std::string>(i) + "; ";
|
||||
*/
|
||||
else
|
||||
result += "varying vec4 passthrough" + boost::lexical_cast<std::string>(i) + "; ";
|
||||
}
|
||||
|
||||
source.replace(pos, std::string("@shPassthroughVertexOutputs").length(), result);
|
||||
}
|
||||
|
||||
// passthrough fragment inputs
|
||||
while (true)
|
||||
{
|
||||
pos = source.find("@shPassthroughFragmentInputs");
|
||||
if (pos == std::string::npos)
|
||||
break;
|
||||
|
||||
std::string result;
|
||||
for (int i = 0; i < mCurrentPassthrough+1; ++i)
|
||||
{
|
||||
// not using newlines here, otherwise the line numbers reported by compiler would be messed up..
|
||||
if (Factory::getInstance().getCurrentLanguage () == Language_CG || Factory::getInstance().getCurrentLanguage () == Language_HLSL)
|
||||
result += ", in float4 passthrough" + boost::lexical_cast<std::string>(i) + " : TEXCOORD" + boost::lexical_cast<std::string>(i);
|
||||
/*
|
||||
else
|
||||
result += "in vec4 passthrough" + boost::lexical_cast<std::string>(i) + "; ";
|
||||
*/
|
||||
else
|
||||
result += "varying vec4 passthrough" + boost::lexical_cast<std::string>(i) + "; ";
|
||||
}
|
||||
|
||||
source.replace(pos, std::string("@shPassthroughFragmentInputs").length(), result);
|
||||
}
|
||||
}
|
||||
|
||||
// save to cache _here_ - we want to preserve some macros
|
||||
if (writeCache && !readCache)
|
||||
{
|
||||
std::ofstream of (std::string(Factory::getInstance ().getCacheFolder () + "/" + mName).c_str(), std::ios_base::out);
|
||||
of.write(source.c_str(), source.size());
|
||||
of.close();
|
||||
}
|
||||
|
||||
|
||||
// parse shared parameters
|
||||
while (true)
|
||||
{
|
||||
pos = source.find("@shSharedParameter");
|
||||
if (pos == std::string::npos)
|
||||
break;
|
||||
|
||||
std::vector<std::string> args = extractMacroArguments (pos, source);
|
||||
assert(args.size());
|
||||
|
||||
size_t end = source.find(")", pos);
|
||||
|
||||
mSharedParameters.push_back(args[0]);
|
||||
|
||||
source.erase(pos, (end+1)-pos);
|
||||
}
|
||||
|
||||
// parse auto constants
|
||||
typedef std::map< std::string, std::pair<std::string, std::string> > AutoConstantMap;
|
||||
AutoConstantMap autoConstants;
|
||||
while (true)
|
||||
{
|
||||
pos = source.find("@shAutoConstant");
|
||||
if (pos == std::string::npos)
|
||||
break;
|
||||
|
||||
std::vector<std::string> args = extractMacroArguments (pos, source);
|
||||
assert(args.size() >= 2);
|
||||
|
||||
size_t end = source.find(")", pos);
|
||||
|
||||
std::string autoConstantName, uniformName;
|
||||
std::string extraData;
|
||||
|
||||
uniformName = args[0];
|
||||
autoConstantName = args[1];
|
||||
if (args.size() > 2)
|
||||
extraData = args[2];
|
||||
|
||||
autoConstants[uniformName] = std::make_pair(autoConstantName, extraData);
|
||||
|
||||
source.erase(pos, (end+1)-pos);
|
||||
}
|
||||
|
||||
// parse uniform properties
|
||||
while (true)
|
||||
{
|
||||
pos = source.find("@shUniformProperty");
|
||||
if (pos == std::string::npos)
|
||||
break;
|
||||
|
||||
std::vector<std::string> args = extractMacroArguments (pos, source);
|
||||
assert(args.size() == 2);
|
||||
|
||||
size_t start = source.find("(", pos);
|
||||
size_t end = source.find(")", pos);
|
||||
std::string cmd = source.substr(pos, start-pos);
|
||||
|
||||
ValueType vt;
|
||||
if (cmd == "@shUniformProperty4f")
|
||||
vt = VT_Vector4;
|
||||
else if (cmd == "@shUniformProperty3f")
|
||||
vt = VT_Vector3;
|
||||
else if (cmd == "@shUniformProperty2f")
|
||||
vt = VT_Vector2;
|
||||
else if (cmd == "@shUniformProperty1f")
|
||||
vt = VT_Float;
|
||||
else if (cmd == "@shUniformPropertyInt")
|
||||
vt = VT_Int;
|
||||
else
|
||||
throw std::runtime_error ("unsupported command \"" + cmd + "\"");
|
||||
|
||||
|
||||
std::string propertyName, uniformName;
|
||||
uniformName = args[0];
|
||||
propertyName = args[1];
|
||||
mUniformProperties[uniformName] = std::make_pair(propertyName, vt);
|
||||
|
||||
source.erase(pos, (end+1)-pos);
|
||||
}
|
||||
|
||||
// parse texture samplers used
|
||||
while (true)
|
||||
{
|
||||
pos = source.find("@shUseSampler");
|
||||
if (pos == std::string::npos)
|
||||
break;
|
||||
|
||||
size_t end = source.find(")", pos);
|
||||
|
||||
mUsedSamplers.push_back(extractMacroArguments (pos, source)[0]);
|
||||
source.erase(pos, (end+1)-pos);
|
||||
}
|
||||
|
||||
// convert any left-over @'s to #
|
||||
boost::algorithm::replace_all(source, "@", "#");
|
||||
|
||||
Platform* platform = Factory::getInstance().getPlatform();
|
||||
|
||||
std::string profile;
|
||||
if (Factory::getInstance ().getCurrentLanguage () == Language_CG)
|
||||
profile = mParent->getCgProfile ();
|
||||
else if (Factory::getInstance ().getCurrentLanguage () == Language_HLSL)
|
||||
profile = mParent->getHlslProfile ();
|
||||
|
||||
|
||||
if (type == GPT_Vertex)
|
||||
mProgram = boost::shared_ptr<GpuProgram>(platform->createGpuProgram(GPT_Vertex, "", mName, profile, source, Factory::getInstance().getCurrentLanguage()));
|
||||
else if (type == GPT_Fragment)
|
||||
mProgram = boost::shared_ptr<GpuProgram>(platform->createGpuProgram(GPT_Fragment, "", mName, profile, source, Factory::getInstance().getCurrentLanguage()));
|
||||
|
||||
|
||||
if (Factory::getInstance ().getShaderDebugOutputEnabled ())
|
||||
writeDebugFile(source, name);
|
||||
else
|
||||
{
|
||||
#ifdef SHINY_WRITE_SHADER_DEBUG
|
||||
writeDebugFile(source, name);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!mProgram->getSupported())
|
||||
{
|
||||
std::cerr << " Full source code below: \n" << source << std::endl;
|
||||
mSupported = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// set auto constants
|
||||
for (AutoConstantMap::iterator it = autoConstants.begin(); it != autoConstants.end(); ++it)
|
||||
{
|
||||
mProgram->setAutoConstant(it->first, it->second.first, it->second.second);
|
||||
}
|
||||
}
|
||||
|
||||
std::string ShaderInstance::getName ()
|
||||
{
|
||||
return mName;
|
||||
}
|
||||
|
||||
bool ShaderInstance::getSupported () const
|
||||
{
|
||||
return mSupported;
|
||||
}
|
||||
|
||||
std::vector<std::string> ShaderInstance::getUsedSamplers()
|
||||
{
|
||||
return mUsedSamplers;
|
||||
}
|
||||
|
||||
void ShaderInstance::setUniformParameters (boost::shared_ptr<Pass> pass, PropertySetGet* properties)
|
||||
{
|
||||
for (UniformMap::iterator it = mUniformProperties.begin(); it != mUniformProperties.end(); ++it)
|
||||
{
|
||||
pass->setGpuConstant(mParent->getType(), it->first, it->second.second, properties->getProperty(it->second.first), properties->getContext());
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> ShaderInstance::extractMacroArguments (size_t pos, const std::string& source)
|
||||
{
|
||||
size_t start = source.find("(", pos);
|
||||
size_t end = source.find(")", pos);
|
||||
std::string args = source.substr(start+1, end-(start+1));
|
||||
std::vector<std::string> results;
|
||||
boost::algorithm::split(results, args, boost::is_any_of(","));
|
||||
std::for_each(results.begin(), results.end(),
|
||||
boost::bind(&boost::trim<std::string>,
|
||||
_1, std::locale() ));
|
||||
return results;
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
#ifndef SH_SHADERINSTANCE_H
|
||||
#define SH_SHADERINSTANCE_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "Platform.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
class ShaderSet;
|
||||
|
||||
typedef std::map< std::string, std::pair<std::string, ValueType > > UniformMap;
|
||||
|
||||
struct Passthrough
|
||||
{
|
||||
Language lang; ///< language to generate for
|
||||
|
||||
int num_components; ///< e.g. 4 for a float4
|
||||
|
||||
int passthrough_number;
|
||||
int component_start; ///< 0 = x
|
||||
|
||||
std::string expand_assign(std::string assignTo);
|
||||
std::string expand_receive();
|
||||
};
|
||||
typedef std::map<std::string, Passthrough> PassthroughMap;
|
||||
|
||||
/**
|
||||
* @brief A specific instance of a \a ShaderSet with a deterministic shader source
|
||||
*/
|
||||
class ShaderInstance
|
||||
{
|
||||
public:
|
||||
ShaderInstance (ShaderSet* parent, const std::string& name, PropertySetGet* properties);
|
||||
|
||||
std::string getName();
|
||||
|
||||
bool getSupported () const;
|
||||
|
||||
std::vector<std::string> getUsedSamplers();
|
||||
std::vector<std::string> getSharedParameters() { return mSharedParameters; }
|
||||
|
||||
void setUniformParameters (boost::shared_ptr<Pass> pass, PropertySetGet* properties);
|
||||
|
||||
private:
|
||||
boost::shared_ptr<GpuProgram> mProgram;
|
||||
std::string mName;
|
||||
ShaderSet* mParent;
|
||||
bool mSupported; ///< shader compilation was sucessful?
|
||||
|
||||
std::vector<std::string> mUsedSamplers;
|
||||
///< names of the texture samplers that are used by this shader
|
||||
|
||||
std::vector<std::string> mSharedParameters;
|
||||
|
||||
UniformMap mUniformProperties;
|
||||
///< uniforms that this depends on, and their property names / value-types
|
||||
/// @note this lists shared uniform parameters as well
|
||||
|
||||
int mCurrentPassthrough; ///< 0 - x
|
||||
int mCurrentComponent; ///< 0:x, 1:y, 2:z, 3:w
|
||||
|
||||
PassthroughMap mPassthroughMap;
|
||||
|
||||
std::vector<std::string> extractMacroArguments (size_t pos, const std::string& source); ///< take a macro invocation and return vector of arguments
|
||||
|
||||
void parse (std::string& source, PropertySetGet* properties);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,172 @@
|
||||
#include "ShaderSet.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/functional/hash.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include "Factory.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
ShaderSet::ShaderSet (const std::string& type, const std::string& cgProfile, const std::string& hlslProfile, const std::string& sourceFile, const std::string& basePath,
|
||||
const std::string& name, PropertySetGet* globalSettingsPtr)
|
||||
: mBasePath(basePath)
|
||||
, mName(name)
|
||||
, mCgProfile(cgProfile)
|
||||
, mHlslProfile(hlslProfile)
|
||||
, mIsDirty(false)
|
||||
{
|
||||
if (type == "vertex")
|
||||
mType = GPT_Vertex;
|
||||
else // if (type == "fragment")
|
||||
mType = GPT_Fragment;
|
||||
|
||||
std::ifstream stream(sourceFile.c_str(), std::ifstream::in);
|
||||
std::stringstream buffer;
|
||||
|
||||
buffer << stream.rdbuf();
|
||||
stream.close();
|
||||
mSource = buffer.str();
|
||||
parse();
|
||||
}
|
||||
|
||||
void ShaderSet::parse()
|
||||
{
|
||||
std::string currentToken;
|
||||
bool tokenIsRecognized = false;
|
||||
bool isInBraces = false;
|
||||
for (std::string::const_iterator it = mSource.begin(); it != mSource.end(); ++it)
|
||||
{
|
||||
char c = *it;
|
||||
if (((c == ' ') && !isInBraces) || (c == '\n') ||
|
||||
( ((c == '(') || (c == ')'))
|
||||
&& !tokenIsRecognized))
|
||||
{
|
||||
if (tokenIsRecognized)
|
||||
{
|
||||
if (boost::starts_with(currentToken, "@shGlobalSetting"))
|
||||
{
|
||||
assert ((currentToken.find('(') != std::string::npos) && (currentToken.find(')') != std::string::npos));
|
||||
size_t start = currentToken.find('(')+1;
|
||||
mGlobalSettings.push_back(currentToken.substr(start, currentToken.find(')')-start));
|
||||
}
|
||||
else if (boost::starts_with(currentToken, "@shPropertyEqual"))
|
||||
{
|
||||
assert ((currentToken.find('(') != std::string::npos) && (currentToken.find(')') != std::string::npos)
|
||||
&& (currentToken.find(',') != std::string::npos));
|
||||
size_t start = currentToken.find('(')+1;
|
||||
size_t end = currentToken.find(',');
|
||||
mProperties.push_back(currentToken.substr(start, end-start));
|
||||
}
|
||||
else if (boost::starts_with(currentToken, "@shProperty"))
|
||||
{
|
||||
assert ((currentToken.find('(') != std::string::npos) && (currentToken.find(')') != std::string::npos));
|
||||
size_t start = currentToken.find('(')+1;
|
||||
std::string propertyName = currentToken.substr(start, currentToken.find(')')-start);
|
||||
// if the property name is constructed dynamically (e.g. through an iterator) then there is nothing we can do
|
||||
if (propertyName.find("@") == std::string::npos)
|
||||
mProperties.push_back(propertyName);
|
||||
}
|
||||
}
|
||||
|
||||
currentToken = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (currentToken == "")
|
||||
{
|
||||
if (c == '@')
|
||||
tokenIsRecognized = true;
|
||||
else
|
||||
tokenIsRecognized = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (c == '@')
|
||||
{
|
||||
// ouch, there are nested macros
|
||||
// ( for example @shForeach(@shPropertyString(foobar)) )
|
||||
currentToken = "";
|
||||
}
|
||||
}
|
||||
|
||||
if (c == '(' && tokenIsRecognized)
|
||||
isInBraces = true;
|
||||
else if (c == ')' && tokenIsRecognized)
|
||||
isInBraces = false;
|
||||
|
||||
currentToken += c;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ShaderInstance* ShaderSet::getInstance (PropertySetGet* properties)
|
||||
{
|
||||
size_t h = buildHash (properties);
|
||||
if (std::find(mFailedToCompile.begin(), mFailedToCompile.end(), h) != mFailedToCompile.end())
|
||||
return NULL;
|
||||
if (mInstances.find(h) == mInstances.end())
|
||||
{
|
||||
ShaderInstance newInstance(this, mName + "_" + boost::lexical_cast<std::string>(h), properties);
|
||||
if (!newInstance.getSupported())
|
||||
{
|
||||
mFailedToCompile.push_back(h);
|
||||
return NULL;
|
||||
}
|
||||
mInstances.insert(std::make_pair(h, newInstance));
|
||||
}
|
||||
return &mInstances.find(h)->second;
|
||||
}
|
||||
|
||||
size_t ShaderSet::buildHash (PropertySetGet* properties)
|
||||
{
|
||||
size_t seed = 0;
|
||||
PropertySetGet* currentGlobalSettings = getCurrentGlobalSettings ();
|
||||
|
||||
for (std::vector<std::string>::iterator it = mProperties.begin(); it != mProperties.end(); ++it)
|
||||
{
|
||||
std::string v = retrieveValue<StringValue>(properties->getProperty(*it), properties->getContext()).get();
|
||||
boost::hash_combine(seed, v);
|
||||
}
|
||||
for (std::vector <std::string>::iterator it = mGlobalSettings.begin(); it != mGlobalSettings.end(); ++it)
|
||||
{
|
||||
boost::hash_combine(seed, retrieveValue<StringValue>(currentGlobalSettings->getProperty(*it), NULL).get());
|
||||
}
|
||||
boost::hash_combine(seed, static_cast<int>(Factory::getInstance().getCurrentLanguage()));
|
||||
return seed;
|
||||
}
|
||||
|
||||
PropertySetGet* ShaderSet::getCurrentGlobalSettings() const
|
||||
{
|
||||
return Factory::getInstance ().getCurrentGlobalSettings ();
|
||||
}
|
||||
|
||||
std::string ShaderSet::getBasePath() const
|
||||
{
|
||||
return mBasePath;
|
||||
}
|
||||
|
||||
std::string ShaderSet::getSource() const
|
||||
{
|
||||
return mSource;
|
||||
}
|
||||
|
||||
std::string ShaderSet::getCgProfile() const
|
||||
{
|
||||
return mCgProfile;
|
||||
}
|
||||
|
||||
std::string ShaderSet::getHlslProfile() const
|
||||
{
|
||||
return mHlslProfile;
|
||||
}
|
||||
|
||||
int ShaderSet::getType() const
|
||||
{
|
||||
return mType;
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
#ifndef SH_SHADERSET_H
|
||||
#define SH_SHADERSET_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include "ShaderInstance.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
class PropertySetGet;
|
||||
|
||||
typedef std::map<size_t, ShaderInstance> ShaderInstanceMap;
|
||||
|
||||
/**
|
||||
* @brief Contains possible shader permutations of a single uber-shader (represented by one source file)
|
||||
*/
|
||||
class ShaderSet
|
||||
{
|
||||
public:
|
||||
ShaderSet (const std::string& type, const std::string& cgProfile, const std::string& hlslProfile, const std::string& sourceFile, const std::string& basePath,
|
||||
const std::string& name, PropertySetGet* globalSettingsPtr);
|
||||
|
||||
/// Retrieve a shader instance for the given properties. \n
|
||||
/// If a \a ShaderInstance with the same properties exists already, simply returns this instance. \n
|
||||
/// Otherwise, creates a new \a ShaderInstance (i.e. compiles a new shader). \n
|
||||
/// Might also return NULL if the shader failed to compile. \n
|
||||
/// @note Only the properties that actually affect the shader source are taken into consideration here,
|
||||
/// so it does not matter if you pass any extra properties that the shader does not care about.
|
||||
ShaderInstance* getInstance (PropertySetGet* properties);
|
||||
|
||||
void markDirty() { mIsDirty = true; }
|
||||
///< Signals that the cache is out of date, and thus should not be used this time
|
||||
|
||||
private:
|
||||
PropertySetGet* getCurrentGlobalSettings() const;
|
||||
std::string getBasePath() const;
|
||||
std::string getSource() const;
|
||||
std::string getCgProfile() const;
|
||||
std::string getHlslProfile() const;
|
||||
int getType() const;
|
||||
|
||||
bool isDirty() { return mIsDirty; }
|
||||
|
||||
friend class ShaderInstance;
|
||||
|
||||
bool mIsDirty;
|
||||
|
||||
private:
|
||||
GpuProgramType mType;
|
||||
std::string mSource;
|
||||
std::string mBasePath;
|
||||
std::string mCgProfile;
|
||||
std::string mHlslProfile;
|
||||
std::string mName;
|
||||
|
||||
std::vector <size_t> mFailedToCompile;
|
||||
|
||||
std::vector <std::string> mGlobalSettings; ///< names of the global settings that affect the shader source
|
||||
std::vector <std::string> mProperties; ///< names of the per-material properties that affect the shader source
|
||||
|
||||
ShaderInstanceMap mInstances; ///< maps permutation ID (generated from the properties) to \a ShaderInstance
|
||||
|
||||
void parse(); ///< find out which properties and global settings affect the shader source
|
||||
|
||||
size_t buildHash (PropertySetGet* properties);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,70 @@
|
||||
#include <stdexcept>
|
||||
|
||||
#include "OgreGpuProgram.hpp"
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include <OgreHighLevelGpuProgramManager.h>
|
||||
#include <OgreGpuProgramManager.h>
|
||||
#include <OgreVector4.h>
|
||||
|
||||
namespace sh
|
||||
{
|
||||
OgreGpuProgram::OgreGpuProgram(
|
||||
GpuProgramType type,
|
||||
const std::string& compileArguments,
|
||||
const std::string& name, const std::string& profile,
|
||||
const std::string& source, const std::string& lang,
|
||||
const std::string& resourceGroup)
|
||||
: GpuProgram()
|
||||
{
|
||||
Ogre::HighLevelGpuProgramManager& mgr = Ogre::HighLevelGpuProgramManager::getSingleton();
|
||||
assert (mgr.getByName(name).isNull() && "Vertex program already exists");
|
||||
|
||||
Ogre::GpuProgramType t;
|
||||
if (type == GPT_Vertex)
|
||||
t = Ogre::GPT_VERTEX_PROGRAM;
|
||||
else
|
||||
t = Ogre::GPT_FRAGMENT_PROGRAM;
|
||||
|
||||
mProgram = mgr.createProgram(name, resourceGroup, lang, t);
|
||||
if (lang != "glsl")
|
||||
mProgram->setParameter("entry_point", "main");
|
||||
|
||||
if (lang == "hlsl")
|
||||
mProgram->setParameter("target", profile);
|
||||
else if (lang == "cg")
|
||||
mProgram->setParameter("profiles", profile);
|
||||
|
||||
mProgram->setSource(source);
|
||||
mProgram->load();
|
||||
|
||||
if (mProgram.isNull() || !mProgram->isSupported())
|
||||
std::cerr << "Failed to compile shader \"" << name << "\". Consider the OGRE log for more information." << std::endl;
|
||||
}
|
||||
|
||||
bool OgreGpuProgram::getSupported()
|
||||
{
|
||||
return (!mProgram.isNull() && mProgram->isSupported());
|
||||
}
|
||||
|
||||
void OgreGpuProgram::setAutoConstant (const std::string& name, const std::string& autoConstantName, const std::string& extraInfo)
|
||||
{
|
||||
assert (!mProgram.isNull() && mProgram->isSupported());
|
||||
const Ogre::GpuProgramParameters::AutoConstantDefinition* d = Ogre::GpuProgramParameters::getAutoConstantDefinition(autoConstantName);
|
||||
|
||||
if (!d)
|
||||
throw std::runtime_error ("can't find auto constant with name \"" + autoConstantName + "\"");
|
||||
Ogre::GpuProgramParameters::AutoConstantType t = d->acType;
|
||||
|
||||
// this simplifies debugging for CG a lot.
|
||||
mProgram->getDefaultParameters()->setIgnoreMissingParams(true);
|
||||
|
||||
if (d->dataType == Ogre::GpuProgramParameters::ACDT_NONE)
|
||||
mProgram->getDefaultParameters()->setNamedAutoConstant (name, t, 0);
|
||||
else if (d->dataType == Ogre::GpuProgramParameters::ACDT_INT)
|
||||
mProgram->getDefaultParameters()->setNamedAutoConstant (name, t, extraInfo == "" ? 0 : boost::lexical_cast<int>(extraInfo));
|
||||
else if (d->dataType == Ogre::GpuProgramParameters::ACDT_REAL)
|
||||
mProgram->getDefaultParameters()->setNamedAutoConstantReal (name, t, extraInfo == "" ? 0.f : boost::lexical_cast<float>(extraInfo));
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
#ifndef SH_OGREGPUPROGRAM_H
|
||||
#define SH_OGREGPUPROGRAM_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <OgreHighLevelGpuProgram.h>
|
||||
|
||||
#include "../../Main/Platform.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
class OgreGpuProgram : public GpuProgram
|
||||
{
|
||||
public:
|
||||
OgreGpuProgram (
|
||||
GpuProgramType type,
|
||||
const std::string& compileArguments,
|
||||
const std::string& name, const std::string& profile,
|
||||
const std::string& source, const std::string& lang,
|
||||
const std::string& resourceGroup);
|
||||
|
||||
virtual bool getSupported();
|
||||
|
||||
virtual void setAutoConstant (const std::string& name, const std::string& autoConstantName, const std::string& extraInfo = "");
|
||||
|
||||
private:
|
||||
Ogre::HighLevelGpuProgramPtr mProgram;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,99 @@
|
||||
#include "OgreMaterial.hpp"
|
||||
|
||||
#include <OgreMaterialManager.h>
|
||||
#include <OgreTechnique.h>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "OgrePass.hpp"
|
||||
#include "OgreMaterialSerializer.hpp"
|
||||
#include "OgrePlatform.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
static const std::string sDefaultTechniqueName = "SH_DefaultTechnique";
|
||||
|
||||
OgreMaterial::OgreMaterial (const std::string& name, const std::string& resourceGroup)
|
||||
: Material()
|
||||
{
|
||||
assert (Ogre::MaterialManager::getSingleton().getByName(name).isNull() && "Material already exists");
|
||||
mMaterial = Ogre::MaterialManager::getSingleton().create (name, resourceGroup);
|
||||
mMaterial->removeAllTechniques();
|
||||
mMaterial->createTechnique()->setSchemeName (sDefaultTechniqueName);
|
||||
mMaterial->compile();
|
||||
}
|
||||
|
||||
OgreMaterial::~OgreMaterial()
|
||||
{
|
||||
Ogre::MaterialManager::getSingleton().remove(mMaterial->getName());
|
||||
}
|
||||
|
||||
boost::shared_ptr<Pass> OgreMaterial::createPass (const std::string& configuration, unsigned short lodIndex)
|
||||
{
|
||||
return boost::shared_ptr<Pass> (new OgrePass (this, configuration, lodIndex));
|
||||
}
|
||||
|
||||
void OgreMaterial::removeAll ()
|
||||
{
|
||||
mMaterial->removeAllTechniques();
|
||||
mMaterial->createTechnique()->setSchemeName (sDefaultTechniqueName);
|
||||
mMaterial->compile();
|
||||
}
|
||||
|
||||
void OgreMaterial::setLodLevels (const std::string& lodLevels)
|
||||
{
|
||||
OgreMaterialSerializer& s = OgrePlatform::getSerializer();
|
||||
|
||||
s.setMaterialProperty ("lod_values", lodLevels, mMaterial);
|
||||
}
|
||||
|
||||
bool OgreMaterial::createConfiguration (const std::string& name, unsigned short lodIndex)
|
||||
{
|
||||
for (int i=0; i<mMaterial->getNumTechniques(); ++i)
|
||||
{
|
||||
if (mMaterial->getTechnique(i)->getSchemeName() == name && mMaterial->getTechnique(i)->getLodIndex() == lodIndex)
|
||||
return false;
|
||||
}
|
||||
|
||||
Ogre::Technique* t = mMaterial->createTechnique();
|
||||
t->setSchemeName (name);
|
||||
t->setLodIndex (lodIndex);
|
||||
if (mShadowCasterMaterial != "")
|
||||
t->setShadowCasterMaterial(mShadowCasterMaterial);
|
||||
|
||||
mMaterial->compile();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Ogre::MaterialPtr OgreMaterial::getOgreMaterial ()
|
||||
{
|
||||
return mMaterial;
|
||||
}
|
||||
|
||||
Ogre::Technique* OgreMaterial::getOgreTechniqueForConfiguration (const std::string& configurationName, unsigned short lodIndex)
|
||||
{
|
||||
for (int i=0; i<mMaterial->getNumTechniques(); ++i)
|
||||
{
|
||||
if (mMaterial->getTechnique(i)->getSchemeName() == configurationName && mMaterial->getTechnique(i)->getLodIndex() == lodIndex)
|
||||
{
|
||||
return mMaterial->getTechnique(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare and throw error message
|
||||
std::stringstream message;
|
||||
message << "Could not find configurationName '" << configurationName
|
||||
<< "' and lodIndex " << lodIndex;
|
||||
|
||||
throw std::runtime_error(message.str());
|
||||
}
|
||||
|
||||
void OgreMaterial::setShadowCasterMaterial (const std::string& name)
|
||||
{
|
||||
mShadowCasterMaterial = name;
|
||||
for (int i=0; i<mMaterial->getNumTechniques(); ++i)
|
||||
{
|
||||
mMaterial->getTechnique(i)->setShadowCasterMaterial(mShadowCasterMaterial);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
#ifndef SH_OGREMATERIAL_H
|
||||
#define SH_OGREMATERIAL_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <OgreMaterial.h>
|
||||
|
||||
#include "../../Main/Platform.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
class OgreMaterial : public Material
|
||||
{
|
||||
public:
|
||||
OgreMaterial (const std::string& name, const std::string& resourceGroup);
|
||||
virtual ~OgreMaterial();
|
||||
|
||||
virtual boost::shared_ptr<Pass> createPass (const std::string& configuration, unsigned short lodIndex);
|
||||
virtual bool createConfiguration (const std::string& name, unsigned short lodIndex);
|
||||
|
||||
virtual void removeAll ();
|
||||
|
||||
Ogre::MaterialPtr getOgreMaterial();
|
||||
|
||||
virtual void setLodLevels (const std::string& lodLevels);
|
||||
|
||||
Ogre::Technique* getOgreTechniqueForConfiguration (const std::string& configurationName, unsigned short lodIndex = 0);
|
||||
|
||||
virtual void setShadowCasterMaterial (const std::string& name);
|
||||
|
||||
private:
|
||||
Ogre::MaterialPtr mMaterial;
|
||||
|
||||
std::string mShadowCasterMaterial;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,67 @@
|
||||
#include "OgreMaterialSerializer.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
void OgreMaterialSerializer::reset()
|
||||
{
|
||||
mScriptContext.section = Ogre::MSS_NONE;
|
||||
mScriptContext.material.setNull();
|
||||
mScriptContext.technique = 0;
|
||||
mScriptContext.pass = 0;
|
||||
mScriptContext.textureUnit = 0;
|
||||
mScriptContext.program.setNull();
|
||||
mScriptContext.lineNo = 0;
|
||||
mScriptContext.filename.clear();
|
||||
mScriptContext.techLev = -1;
|
||||
mScriptContext.passLev = -1;
|
||||
mScriptContext.stateLev = -1;
|
||||
}
|
||||
|
||||
bool OgreMaterialSerializer::setPassProperty (const std::string& param, std::string value, Ogre::Pass* pass)
|
||||
{
|
||||
reset();
|
||||
|
||||
mScriptContext.section = Ogre::MSS_PASS;
|
||||
mScriptContext.pass = pass;
|
||||
|
||||
if (mPassAttribParsers.find (param) == mPassAttribParsers.end())
|
||||
return false;
|
||||
else
|
||||
{
|
||||
mPassAttribParsers.find(param)->second(value, mScriptContext);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool OgreMaterialSerializer::setTextureUnitProperty (const std::string& param, std::string value, Ogre::TextureUnitState* t)
|
||||
{
|
||||
reset();
|
||||
|
||||
mScriptContext.section = Ogre::MSS_TEXTUREUNIT;
|
||||
mScriptContext.textureUnit = t;
|
||||
|
||||
if (mTextureUnitAttribParsers.find (param) == mTextureUnitAttribParsers.end())
|
||||
return false;
|
||||
else
|
||||
{
|
||||
mTextureUnitAttribParsers.find(param)->second(value, mScriptContext);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool OgreMaterialSerializer::setMaterialProperty (const std::string& param, std::string value, Ogre::MaterialPtr m)
|
||||
{
|
||||
reset();
|
||||
|
||||
mScriptContext.section = Ogre::MSS_MATERIAL;
|
||||
mScriptContext.material = m;
|
||||
|
||||
if (mMaterialAttribParsers.find (param) == mMaterialAttribParsers.end())
|
||||
return false;
|
||||
else
|
||||
{
|
||||
mMaterialAttribParsers.find(param)->second(value, mScriptContext);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
#ifndef SH_OGREMATERIALSERIALIZER_H
|
||||
#define SH_OGREMATERIALSERIALIZER_H
|
||||
|
||||
#include <OgreMaterialSerializer.h>
|
||||
|
||||
namespace Ogre
|
||||
{
|
||||
class Pass;
|
||||
}
|
||||
|
||||
namespace sh
|
||||
{
|
||||
/**
|
||||
* @brief This class allows me to let Ogre handle the pass & texture unit properties
|
||||
*/
|
||||
class OgreMaterialSerializer : public Ogre::MaterialSerializer
|
||||
{
|
||||
public:
|
||||
bool setPassProperty (const std::string& param, std::string value, Ogre::Pass* pass);
|
||||
bool setTextureUnitProperty (const std::string& param, std::string value, Ogre::TextureUnitState* t);
|
||||
bool setMaterialProperty (const std::string& param, std::string value, Ogre::MaterialPtr m);
|
||||
|
||||
private:
|
||||
void reset();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,128 @@
|
||||
#include <stdexcept>
|
||||
|
||||
#include "OgrePass.hpp"
|
||||
|
||||
#include <OgrePass.h>
|
||||
#include <OgreTechnique.h>
|
||||
|
||||
#include "OgreTextureUnitState.hpp"
|
||||
#include "OgreGpuProgram.hpp"
|
||||
#include "OgreMaterial.hpp"
|
||||
#include "OgreMaterialSerializer.hpp"
|
||||
#include "OgrePlatform.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
OgrePass::OgrePass (OgreMaterial* parent, const std::string& configuration, unsigned short lodIndex)
|
||||
: Pass()
|
||||
{
|
||||
Ogre::Technique* t = parent->getOgreTechniqueForConfiguration(configuration, lodIndex);
|
||||
mPass = t->createPass();
|
||||
}
|
||||
|
||||
boost::shared_ptr<TextureUnitState> OgrePass::createTextureUnitState ()
|
||||
{
|
||||
return boost::shared_ptr<TextureUnitState> (new OgreTextureUnitState (this));
|
||||
}
|
||||
|
||||
void OgrePass::assignProgram (GpuProgramType type, const std::string& name)
|
||||
{
|
||||
if (type == GPT_Vertex)
|
||||
mPass->setVertexProgram (name);
|
||||
else if (type == GPT_Fragment)
|
||||
mPass->setFragmentProgram (name);
|
||||
else
|
||||
throw std::runtime_error("unsupported GpuProgramType");
|
||||
}
|
||||
|
||||
Ogre::Pass* OgrePass::getOgrePass ()
|
||||
{
|
||||
return mPass;
|
||||
}
|
||||
|
||||
bool OgrePass::setPropertyOverride (const std::string &name, PropertyValuePtr& value, PropertySetGet* context)
|
||||
{
|
||||
if (((typeid(*value) == typeid(StringValue)) || typeid(*value) == typeid(LinkedValue))
|
||||
&& retrieveValue<StringValue>(value, context).get() == "default")
|
||||
return true;
|
||||
|
||||
if (name == "vertex_program")
|
||||
return true; // handled already
|
||||
else if (name == "fragment_program")
|
||||
return true; // handled already
|
||||
else if (name == "ffp_vertex_colour_ambient")
|
||||
{
|
||||
bool enabled = retrieveValue<BooleanValue>(value, context).get();
|
||||
// fixed-function vertex colour tracking
|
||||
mPass->setVertexColourTracking(enabled ? Ogre::TVC_AMBIENT : Ogre::TVC_NONE);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
OgreMaterialSerializer& s = OgrePlatform::getSerializer();
|
||||
|
||||
return s.setPassProperty (name, retrieveValue<StringValue>(value, context).get(), mPass);
|
||||
}
|
||||
}
|
||||
|
||||
void OgrePass::setGpuConstant (int type, const std::string& name, ValueType vt, PropertyValuePtr value, PropertySetGet* context)
|
||||
{
|
||||
Ogre::GpuProgramParametersSharedPtr params;
|
||||
if (type == GPT_Vertex)
|
||||
{
|
||||
if (!mPass->hasVertexProgram ())
|
||||
return;
|
||||
params = mPass->getVertexProgramParameters();
|
||||
}
|
||||
else if (type == GPT_Fragment)
|
||||
{
|
||||
if (!mPass->hasFragmentProgram ())
|
||||
return;
|
||||
params = mPass->getFragmentProgramParameters();
|
||||
}
|
||||
|
||||
if (vt == VT_Float)
|
||||
params->setNamedConstant (name, retrieveValue<FloatValue>(value, context).get());
|
||||
else if (vt == VT_Int)
|
||||
params->setNamedConstant (name, retrieveValue<IntValue>(value, context).get());
|
||||
else if (vt == VT_Vector4)
|
||||
{
|
||||
Vector4 v = retrieveValue<Vector4>(value, context);
|
||||
params->setNamedConstant (name, Ogre::Vector4(v.mX, v.mY, v.mZ, v.mW));
|
||||
}
|
||||
else if (vt == VT_Vector3)
|
||||
{
|
||||
Vector3 v = retrieveValue<Vector3>(value, context);
|
||||
params->setNamedConstant (name, Ogre::Vector4(v.mX, v.mY, v.mZ, 1.0));
|
||||
}
|
||||
else if (vt == VT_Vector2)
|
||||
{
|
||||
Vector2 v = retrieveValue<Vector2>(value, context);
|
||||
params->setNamedConstant (name, Ogre::Vector4(v.mX, v.mY, 1.0, 1.0));
|
||||
}
|
||||
else
|
||||
throw std::runtime_error ("unsupported constant type");
|
||||
}
|
||||
|
||||
void OgrePass::addSharedParameter (int type, const std::string& name)
|
||||
{
|
||||
Ogre::GpuProgramParametersSharedPtr params;
|
||||
if (type == GPT_Vertex)
|
||||
params = mPass->getVertexProgramParameters();
|
||||
else if (type == GPT_Fragment)
|
||||
params = mPass->getFragmentProgramParameters();
|
||||
|
||||
params->addSharedParameters (name);
|
||||
}
|
||||
|
||||
void OgrePass::setTextureUnitIndex (int programType, const std::string& name, int index)
|
||||
{
|
||||
Ogre::GpuProgramParametersSharedPtr params;
|
||||
if (programType == GPT_Vertex)
|
||||
params = mPass->getVertexProgramParameters();
|
||||
else if (programType == GPT_Fragment)
|
||||
params = mPass->getFragmentProgramParameters();
|
||||
|
||||
params->setNamedConstant(name, index);
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
#ifndef SH_OGREPASS_H
|
||||
#define SH_OGREPASS_H
|
||||
|
||||
#include <OgrePass.h>
|
||||
|
||||
#include "../../Main/Platform.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
class OgreMaterial;
|
||||
|
||||
class OgrePass : public Pass
|
||||
{
|
||||
public:
|
||||
OgrePass (OgreMaterial* parent, const std::string& configuration, unsigned short lodIndex);
|
||||
|
||||
virtual boost::shared_ptr<TextureUnitState> createTextureUnitState ();
|
||||
virtual void assignProgram (GpuProgramType type, const std::string& name);
|
||||
|
||||
Ogre::Pass* getOgrePass();
|
||||
|
||||
virtual void setGpuConstant (int type, const std::string& name, ValueType vt, PropertyValuePtr value, PropertySetGet* context);
|
||||
|
||||
virtual void addSharedParameter (int type, const std::string& name);
|
||||
virtual void setTextureUnitIndex (int programType, const std::string& name, int index);
|
||||
|
||||
private:
|
||||
Ogre::Pass* mPass;
|
||||
|
||||
protected:
|
||||
virtual bool setPropertyOverride (const std::string &name, PropertyValuePtr& value, PropertySetGet* context);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,174 @@
|
||||
#include <stdexcept>
|
||||
|
||||
#include "OgrePlatform.hpp"
|
||||
|
||||
#include <OgreDataStream.h>
|
||||
#include <OgreGpuProgramManager.h>
|
||||
#include <OgreRoot.h>
|
||||
|
||||
#include "OgreMaterial.hpp"
|
||||
#include "OgreGpuProgram.hpp"
|
||||
#include "OgreMaterialSerializer.hpp"
|
||||
|
||||
#include "../../Main/MaterialInstance.hpp"
|
||||
#include "../../Main/Factory.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
std::string convertLang (sh::Language lang)
|
||||
{
|
||||
if (lang == sh::Language_CG)
|
||||
return "cg";
|
||||
else if (lang == sh::Language_HLSL)
|
||||
return "hlsl";
|
||||
else if (lang == sh::Language_GLSL)
|
||||
return "glsl";
|
||||
throw std::runtime_error ("invalid language, valid are: cg, hlsl, glsl");
|
||||
}
|
||||
}
|
||||
|
||||
namespace sh
|
||||
{
|
||||
OgreMaterialSerializer* OgrePlatform::sSerializer = 0;
|
||||
|
||||
OgrePlatform::OgrePlatform(const std::string& resourceGroupName, const std::string& basePath)
|
||||
: Platform(basePath)
|
||||
, mResourceGroup(resourceGroupName)
|
||||
{
|
||||
Ogre::MaterialManager::getSingleton().addListener(this);
|
||||
|
||||
if (supportsShaderSerialization())
|
||||
Ogre::GpuProgramManager::getSingletonPtr()->setSaveMicrocodesToCache(true);
|
||||
|
||||
sSerializer = new OgreMaterialSerializer();
|
||||
}
|
||||
|
||||
OgreMaterialSerializer& OgrePlatform::getSerializer()
|
||||
{
|
||||
assert(sSerializer);
|
||||
return *sSerializer;
|
||||
}
|
||||
|
||||
OgrePlatform::~OgrePlatform ()
|
||||
{
|
||||
delete sSerializer;
|
||||
}
|
||||
|
||||
bool OgrePlatform::isProfileSupported (const std::string& profile)
|
||||
{
|
||||
return Ogre::GpuProgramManager::getSingleton().isSyntaxSupported(profile);
|
||||
}
|
||||
|
||||
bool OgrePlatform::supportsShaderSerialization ()
|
||||
{
|
||||
// Not very reliable in OpenGL mode (requires extension), and somehow doesn't work on linux even if the extension is present
|
||||
return Ogre::Root::getSingleton ().getRenderSystem ()->getName ().find("OpenGL") == std::string::npos;
|
||||
}
|
||||
|
||||
bool OgrePlatform::supportsMaterialQueuedListener ()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
boost::shared_ptr<Material> OgrePlatform::createMaterial (const std::string& name)
|
||||
{
|
||||
OgreMaterial* material = new OgreMaterial(name, mResourceGroup);
|
||||
return boost::shared_ptr<Material> (material);
|
||||
}
|
||||
|
||||
boost::shared_ptr<GpuProgram> OgrePlatform::createGpuProgram (
|
||||
GpuProgramType type,
|
||||
const std::string& compileArguments,
|
||||
const std::string& name, const std::string& profile,
|
||||
const std::string& source, Language lang)
|
||||
{
|
||||
OgreGpuProgram* prog = new OgreGpuProgram (type, compileArguments, name, profile, source, convertLang(lang), mResourceGroup);
|
||||
return boost::shared_ptr<GpuProgram> (static_cast<GpuProgram*>(prog));
|
||||
}
|
||||
|
||||
Ogre::Technique* OgrePlatform::handleSchemeNotFound (
|
||||
unsigned short schemeIndex, const Ogre::String &schemeName, Ogre::Material *originalMaterial,
|
||||
unsigned short lodIndex, const Ogre::Renderable *rend)
|
||||
{
|
||||
MaterialInstance* m = fireMaterialRequested(originalMaterial->getName(), schemeName, lodIndex);
|
||||
if (m)
|
||||
{
|
||||
OgreMaterial* _m = static_cast<OgreMaterial*>(m->getMaterial());
|
||||
return _m->getOgreTechniqueForConfiguration (schemeName, lodIndex);
|
||||
}
|
||||
else
|
||||
return 0; // material does not belong to us
|
||||
}
|
||||
|
||||
void OgrePlatform::serializeShaders (const std::string& file)
|
||||
{
|
||||
std::fstream output;
|
||||
output.open(file.c_str(), std::ios::out | std::ios::binary);
|
||||
Ogre::DataStreamPtr shaderCache (OGRE_NEW Ogre::FileStreamDataStream(file, &output, false));
|
||||
Ogre::GpuProgramManager::getSingleton().saveMicrocodeCache(shaderCache);
|
||||
}
|
||||
|
||||
void OgrePlatform::deserializeShaders (const std::string& file)
|
||||
{
|
||||
std::ifstream inp;
|
||||
inp.open(file.c_str(), std::ios::in | std::ios::binary);
|
||||
Ogre::DataStreamPtr shaderCache(OGRE_NEW Ogre::FileStreamDataStream(file, &inp, false));
|
||||
Ogre::GpuProgramManager::getSingleton().loadMicrocodeCache(shaderCache);
|
||||
}
|
||||
|
||||
void OgrePlatform::setSharedParameter (const std::string& name, PropertyValuePtr value)
|
||||
{
|
||||
Ogre::GpuSharedParametersPtr params;
|
||||
if (mSharedParameters.find(name) == mSharedParameters.end())
|
||||
{
|
||||
params = Ogre::GpuProgramManager::getSingleton().createSharedParameters(name);
|
||||
Ogre::GpuConstantType type;
|
||||
if (typeid(*value) == typeid(Vector4))
|
||||
type = Ogre::GCT_FLOAT4;
|
||||
else if (typeid(*value) == typeid(Vector3))
|
||||
type = Ogre::GCT_FLOAT3;
|
||||
else if (typeid(*value) == typeid(Vector2))
|
||||
type = Ogre::GCT_FLOAT2;
|
||||
else if (typeid(*value) == typeid(FloatValue))
|
||||
type = Ogre::GCT_FLOAT1;
|
||||
else if (typeid(*value) == typeid(IntValue))
|
||||
type = Ogre::GCT_INT1;
|
||||
else
|
||||
assert(0);
|
||||
params->addConstantDefinition(name, type);
|
||||
mSharedParameters[name] = params;
|
||||
}
|
||||
else
|
||||
params = mSharedParameters.find(name)->second;
|
||||
|
||||
Ogre::Vector4 v (1.0, 1.0, 1.0, 1.0);
|
||||
if (typeid(*value) == typeid(Vector4))
|
||||
{
|
||||
Vector4 vec = retrieveValue<Vector4>(value, NULL);
|
||||
v.x = vec.mX;
|
||||
v.y = vec.mY;
|
||||
v.z = vec.mZ;
|
||||
v.w = vec.mW;
|
||||
}
|
||||
else if (typeid(*value) == typeid(Vector3))
|
||||
{
|
||||
Vector3 vec = retrieveValue<Vector3>(value, NULL);
|
||||
v.x = vec.mX;
|
||||
v.y = vec.mY;
|
||||
v.z = vec.mZ;
|
||||
}
|
||||
else if (typeid(*value) == typeid(Vector2))
|
||||
{
|
||||
Vector2 vec = retrieveValue<Vector2>(value, NULL);
|
||||
v.x = vec.mX;
|
||||
v.y = vec.mY;
|
||||
}
|
||||
else if (typeid(*value) == typeid(FloatValue))
|
||||
v.x = retrieveValue<FloatValue>(value, NULL).get();
|
||||
else if (typeid(*value) == typeid(IntValue))
|
||||
v.x = static_cast<float>(retrieveValue<IntValue>(value, NULL).get());
|
||||
else
|
||||
throw std::runtime_error ("unsupported property type for shared parameter \"" + name + "\"");
|
||||
params->setNamedConstant(name, v);
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
#ifndef SH_OGREPLATFORM_H
|
||||
#define SH_OGREPLATFORM_H
|
||||
|
||||
/**
|
||||
* @addtogroup Platforms
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup Ogre
|
||||
* A set of classes to interact with Ogre's material system
|
||||
* @{
|
||||
*/
|
||||
|
||||
#include "../../Main/Platform.hpp"
|
||||
|
||||
#include <OgreMaterialManager.h>
|
||||
#include <OgreGpuProgramParams.h>
|
||||
|
||||
namespace sh
|
||||
{
|
||||
class OgreMaterialSerializer;
|
||||
|
||||
class OgrePlatform : public Platform, public Ogre::MaterialManager::Listener
|
||||
{
|
||||
public:
|
||||
OgrePlatform (const std::string& resourceGroupName, const std::string& basePath);
|
||||
virtual ~OgrePlatform ();
|
||||
|
||||
virtual Ogre::Technique* handleSchemeNotFound (
|
||||
unsigned short schemeIndex, const Ogre::String &schemeName, Ogre::Material *originalMaterial,
|
||||
unsigned short lodIndex, const Ogre::Renderable *rend);
|
||||
|
||||
static OgreMaterialSerializer& getSerializer();
|
||||
|
||||
private:
|
||||
virtual bool isProfileSupported (const std::string& profile);
|
||||
|
||||
virtual void serializeShaders (const std::string& file);
|
||||
virtual void deserializeShaders (const std::string& file);
|
||||
|
||||
virtual boost::shared_ptr<Material> createMaterial (const std::string& name);
|
||||
|
||||
virtual boost::shared_ptr<GpuProgram> createGpuProgram (
|
||||
GpuProgramType type,
|
||||
const std::string& compileArguments,
|
||||
const std::string& name, const std::string& profile,
|
||||
const std::string& source, Language lang);
|
||||
|
||||
virtual void setSharedParameter (const std::string& name, PropertyValuePtr value);
|
||||
|
||||
friend class ShaderInstance;
|
||||
friend class Factory;
|
||||
|
||||
protected:
|
||||
virtual bool supportsShaderSerialization ();
|
||||
virtual bool supportsMaterialQueuedListener ();
|
||||
|
||||
std::string mResourceGroup;
|
||||
|
||||
static OgreMaterialSerializer* sSerializer;
|
||||
|
||||
std::map <std::string, Ogre::GpuSharedParametersPtr> mSharedParameters;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
*/
|
||||
|
||||
#endif
|
@ -0,0 +1,40 @@
|
||||
#include "OgreTextureUnitState.hpp"
|
||||
|
||||
#include "OgrePass.hpp"
|
||||
#include "OgrePlatform.hpp"
|
||||
#include "OgreMaterialSerializer.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
OgreTextureUnitState::OgreTextureUnitState (OgrePass* parent)
|
||||
: TextureUnitState()
|
||||
{
|
||||
mTextureUnitState = parent->getOgrePass()->createTextureUnitState("");
|
||||
}
|
||||
|
||||
bool OgreTextureUnitState::setPropertyOverride (const std::string &name, PropertyValuePtr& value, PropertySetGet* context)
|
||||
{
|
||||
OgreMaterialSerializer& s = OgrePlatform::getSerializer();
|
||||
|
||||
if (name == "texture_alias")
|
||||
{
|
||||
// texture alias in this library refers to something else than in ogre
|
||||
// delegate up
|
||||
return TextureUnitState::setPropertyOverride (name, value, context);
|
||||
}
|
||||
else if (name == "direct_texture")
|
||||
{
|
||||
setTextureName (retrieveValue<StringValue>(value, context).get());
|
||||
return true;
|
||||
}
|
||||
else if (name == "create_in_ffp")
|
||||
return true; // handled elsewhere
|
||||
|
||||
return s.setTextureUnitProperty (name, retrieveValue<StringValue>(value, context).get(), mTextureUnitState);
|
||||
}
|
||||
|
||||
void OgreTextureUnitState::setTextureName (const std::string& textureName)
|
||||
{
|
||||
mTextureUnitState->setTextureName(textureName);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
#ifndef SH_OGRETEXTUREUNITSTATE_H
|
||||
#define SH_OGRETEXTUREUNITSTATE_H
|
||||
|
||||
#include <OgreTextureUnitState.h>
|
||||
|
||||
#include "../../Main/Platform.hpp"
|
||||
|
||||
namespace sh
|
||||
{
|
||||
class OgrePass;
|
||||
|
||||
class OgreTextureUnitState : public TextureUnitState
|
||||
{
|
||||
public:
|
||||
OgreTextureUnitState (OgrePass* parent);
|
||||
|
||||
virtual void setTextureName (const std::string& textureName);
|
||||
|
||||
private:
|
||||
Ogre::TextureUnitState* mTextureUnitState;
|
||||
|
||||
protected:
|
||||
virtual bool setPropertyOverride (const std::string &name, PropertyValuePtr& value, PropertySetGet* context);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,236 @@
|
||||
/*=============================================================================
|
||||
Boost.Wave: A Standard compliant C++ preprocessor library
|
||||
http://www.boost.org/
|
||||
|
||||
Copyright (c) 2001 Daniel C. Nuffer.
|
||||
Copyright (c) 2001-2011 Hartmut Kaiser.
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
|
||||
#define BOOST_WAVE_SOURCE 1
|
||||
|
||||
// disable stupid compiler warnings
|
||||
#include <boost/config/warning_disable.hpp>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include <boost/wave/wave_config.hpp> // configuration data
|
||||
#include <boost/wave/cpplexer/re2clex/aq.hpp>
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
// this must occur after all of the includes and before any code appears
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace boost {
|
||||
namespace wave {
|
||||
namespace cpplexer {
|
||||
namespace re2clex {
|
||||
|
||||
int aq_grow(aq_queue q)
|
||||
{
|
||||
using namespace std; // some systems have memcpy/realloc in std
|
||||
std::size_t new_size = q->max_size << 1;
|
||||
aq_stdelement* new_queue = (aq_stdelement*)realloc(q->queue,
|
||||
new_size * sizeof(aq_stdelement));
|
||||
|
||||
BOOST_ASSERT(NULL != q);
|
||||
BOOST_ASSERT(q->max_size < 100000);
|
||||
BOOST_ASSERT(q->size <= q->max_size);
|
||||
|
||||
#define ASSERT_SIZE BOOST_ASSERT( \
|
||||
((q->tail + q->max_size + 1) - q->head) % q->max_size == \
|
||||
q->size % q->max_size)
|
||||
|
||||
ASSERT_SIZE;
|
||||
BOOST_ASSERT(q->head <= q->max_size);
|
||||
BOOST_ASSERT(q->tail <= q->max_size);
|
||||
|
||||
if (!new_queue)
|
||||
{
|
||||
BOOST_ASSERT(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
q->queue = new_queue;
|
||||
if (q->tail <= q->head) /* tail has wrapped around */
|
||||
{
|
||||
/* move the tail from the beginning to the end */
|
||||
memcpy(q->queue + q->max_size, q->queue,
|
||||
(q->tail + 1) * sizeof(aq_stdelement));
|
||||
q->tail += q->max_size;
|
||||
}
|
||||
q->max_size = new_size;
|
||||
|
||||
BOOST_ASSERT(q->size <= q->max_size);
|
||||
ASSERT_SIZE;
|
||||
BOOST_ASSERT(q->head <= q->max_size);
|
||||
BOOST_ASSERT(q->tail <= q->max_size);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int aq_enqueue(aq_queue q, aq_stdelement e)
|
||||
{
|
||||
BOOST_ASSERT(NULL != q);
|
||||
BOOST_ASSERT(q->size <= q->max_size);
|
||||
ASSERT_SIZE;
|
||||
BOOST_ASSERT(q->head <= q->max_size);
|
||||
BOOST_ASSERT(q->tail <= q->max_size);
|
||||
|
||||
|
||||
if (AQ_FULL(q))
|
||||
if (!aq_grow(q))
|
||||
return 0;
|
||||
|
||||
++q->tail;
|
||||
if (q->tail == q->max_size)
|
||||
q->tail = 0;
|
||||
|
||||
q->queue[q->tail] = e;
|
||||
++q->size;
|
||||
|
||||
BOOST_ASSERT(q->size <= q->max_size);
|
||||
ASSERT_SIZE;
|
||||
BOOST_ASSERT(q->head <= q->max_size);
|
||||
BOOST_ASSERT(q->tail <= q->max_size);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int aq_enqueue_front(aq_queue q, aq_stdelement e)
|
||||
{
|
||||
BOOST_ASSERT(NULL != q);
|
||||
|
||||
BOOST_ASSERT(q->size <= q->max_size);
|
||||
ASSERT_SIZE;
|
||||
BOOST_ASSERT(q->head <= q->max_size);
|
||||
BOOST_ASSERT(q->tail <= q->max_size);
|
||||
|
||||
|
||||
if (AQ_FULL(q))
|
||||
if (!aq_grow(q))
|
||||
return 0;
|
||||
|
||||
if (q->head == 0)
|
||||
q->head = q->max_size - 1;
|
||||
else
|
||||
--q->head;
|
||||
|
||||
q->queue[q->head] = e;
|
||||
++q->size;
|
||||
|
||||
BOOST_ASSERT(q->size <= q->max_size);
|
||||
ASSERT_SIZE;
|
||||
BOOST_ASSERT(q->head <= q->max_size);
|
||||
BOOST_ASSERT(q->tail <= q->max_size);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int aq_serve(aq_queue q, aq_stdelement *e)
|
||||
{
|
||||
|
||||
BOOST_ASSERT(NULL != q);
|
||||
BOOST_ASSERT(q->size <= q->max_size);
|
||||
ASSERT_SIZE;
|
||||
BOOST_ASSERT(q->head <= q->max_size);
|
||||
BOOST_ASSERT(q->tail <= q->max_size);
|
||||
|
||||
|
||||
if (AQ_EMPTY(q))
|
||||
return 0;
|
||||
|
||||
*e = q->queue[q->head];
|
||||
return aq_pop(q);
|
||||
}
|
||||
|
||||
int aq_pop(aq_queue q)
|
||||
{
|
||||
|
||||
BOOST_ASSERT(NULL != q);
|
||||
BOOST_ASSERT(q->size <= q->max_size);
|
||||
ASSERT_SIZE;
|
||||
BOOST_ASSERT(q->head <= q->max_size);
|
||||
BOOST_ASSERT(q->tail <= q->max_size);
|
||||
|
||||
|
||||
if (AQ_EMPTY(q))
|
||||
return 0;
|
||||
|
||||
++q->head;
|
||||
if (q->head == q->max_size)
|
||||
q->head = 0;
|
||||
--q->size;
|
||||
|
||||
BOOST_ASSERT(q->size <= q->max_size);
|
||||
ASSERT_SIZE;
|
||||
BOOST_ASSERT(q->head <= q->max_size);
|
||||
BOOST_ASSERT(q->tail <= q->max_size);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
aq_queue aq_create(void)
|
||||
{
|
||||
aq_queue q;
|
||||
|
||||
using namespace std; // some systems have malloc in std
|
||||
q = (aq_queue)malloc(sizeof(aq_queuetype));
|
||||
if (!q)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
q->max_size = 8; /* initial size */
|
||||
q->queue = (aq_stdelement*)malloc(
|
||||
sizeof(aq_stdelement) * q->max_size);
|
||||
if (!q->queue)
|
||||
{
|
||||
free(q);
|
||||
return 0;
|
||||
}
|
||||
|
||||
q->head = 0;
|
||||
q->tail = q->max_size - 1;
|
||||
q->size = 0;
|
||||
|
||||
|
||||
BOOST_ASSERT(q->size <= q->max_size);
|
||||
ASSERT_SIZE;
|
||||
BOOST_ASSERT(q->head <= q->max_size);
|
||||
BOOST_ASSERT(q->tail <= q->max_size);
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
void aq_terminate(aq_queue q)
|
||||
{
|
||||
using namespace std; // some systems have free in std
|
||||
|
||||
BOOST_ASSERT(NULL != q);
|
||||
BOOST_ASSERT(q->size <= q->max_size);
|
||||
ASSERT_SIZE;
|
||||
BOOST_ASSERT(q->head <= q->max_size);
|
||||
BOOST_ASSERT(q->tail <= q->max_size);
|
||||
|
||||
free(q->queue);
|
||||
free(q);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
} // namespace re2clex
|
||||
} // namespace cpplexer
|
||||
} // namespace wave
|
||||
} // namespace boost
|
||||
|
||||
// the suffix header occurs after all of the code
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
@ -0,0 +1,442 @@
|
||||
/*=============================================================================
|
||||
Boost.Wave: A Standard compliant C++ preprocessor library
|
||||
|
||||
Copyright (c) 2001 Daniel C. Nuffer
|
||||
Copyright (c) 2001-2011 Hartmut Kaiser.
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
TODO:
|
||||
It also may be necessary to add $ to identifiers, for asm.
|
||||
handle errors better.
|
||||
have some easier way to parse strings instead of files (done)
|
||||
=============================================================================*/
|
||||
|
||||
#define BOOST_WAVE_SOURCE 1
|
||||
|
||||
// disable stupid compiler warnings
|
||||
#include <boost/config/warning_disable.hpp>
|
||||
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <boost/wave/wave_config.hpp> // configuration data
|
||||
|
||||
#if defined(BOOST_HAS_UNISTD_H)
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#include <io.h>
|
||||
#endif
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
|
||||
#include <boost/wave/token_ids.hpp>
|
||||
#include <boost/wave/cpplexer/re2clex/aq.hpp>
|
||||
#include <boost/wave/cpplexer/re2clex/scanner.hpp>
|
||||
#include <boost/wave/cpplexer/re2clex/cpp_re.hpp>
|
||||
#include <boost/wave/cpplexer/cpplexer_exceptions.hpp>
|
||||
|
||||
// this must occur after all of the includes and before any code appears
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
#if defined(BOOST_MSVC)
|
||||
#pragma warning (disable: 4101) // 'foo' : unreferenced local variable
|
||||
#pragma warning (disable: 4102) // 'foo' : unreferenced label
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
#define BOOST_WAVE_BSIZE 196608
|
||||
|
||||
#define YYCTYPE uchar
|
||||
#define YYCURSOR cursor
|
||||
#define YYLIMIT limit
|
||||
#define YYMARKER marker
|
||||
#define YYFILL(n) \
|
||||
{ \
|
||||
cursor = uchar_wrapper(fill(s, cursor), cursor.column); \
|
||||
limit = uchar_wrapper (s->lim); \
|
||||
} \
|
||||
/**/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
#define BOOST_WAVE_UPDATE_CURSOR() \
|
||||
{ \
|
||||
s->line += count_backslash_newlines(s, cursor); \
|
||||
s->curr_column = cursor.column; \
|
||||
s->cur = cursor; \
|
||||
s->lim = limit; \
|
||||
s->ptr = marker; \
|
||||
} \
|
||||
/**/
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
#define BOOST_WAVE_RET(i) \
|
||||
{ \
|
||||
BOOST_WAVE_UPDATE_CURSOR() \
|
||||
if (s->cur > s->lim) \
|
||||
return T_EOF; /* may happen for empty files */ \
|
||||
return (i); \
|
||||
} \
|
||||
/**/
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace boost {
|
||||
namespace wave {
|
||||
namespace cpplexer {
|
||||
namespace re2clex {
|
||||
|
||||
#define RE2C_ASSERT BOOST_ASSERT
|
||||
|
||||
int get_one_char(Scanner *s)
|
||||
{
|
||||
if (0 != s->act) {
|
||||
RE2C_ASSERT(s->first != 0 && s->last != 0);
|
||||
RE2C_ASSERT(s->first <= s->act && s->act <= s->last);
|
||||
if (s->act < s->last)
|
||||
return *(s->act)++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::ptrdiff_t rewind_stream (Scanner *s, int cnt)
|
||||
{
|
||||
if (0 != s->act) {
|
||||
RE2C_ASSERT(s->first != 0 && s->last != 0);
|
||||
s->act += cnt;
|
||||
RE2C_ASSERT(s->first <= s->act && s->act <= s->last);
|
||||
return s->act - s->first;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::size_t get_first_eol_offset(Scanner* s)
|
||||
{
|
||||
if (!AQ_EMPTY(s->eol_offsets))
|
||||
{
|
||||
return s->eol_offsets->queue[s->eol_offsets->head];
|
||||
}
|
||||
else
|
||||
{
|
||||
return (unsigned int)-1;
|
||||
}
|
||||
}
|
||||
|
||||
void adjust_eol_offsets(Scanner* s, std::size_t adjustment)
|
||||
{
|
||||
aq_queue q;
|
||||
std::size_t i;
|
||||
|
||||
if (!s->eol_offsets)
|
||||
s->eol_offsets = aq_create();
|
||||
|
||||
q = s->eol_offsets;
|
||||
|
||||
if (AQ_EMPTY(q))
|
||||
return;
|
||||
|
||||
i = q->head;
|
||||
while (i != q->tail)
|
||||
{
|
||||
if (adjustment > q->queue[i])
|
||||
q->queue[i] = 0;
|
||||
else
|
||||
q->queue[i] -= adjustment;
|
||||
++i;
|
||||
if (i == q->max_size)
|
||||
i = 0;
|
||||
}
|
||||
if (adjustment > q->queue[i])
|
||||
q->queue[i] = 0;
|
||||
else
|
||||
q->queue[i] -= adjustment;
|
||||
}
|
||||
|
||||
int count_backslash_newlines(Scanner *s, uchar *cursor)
|
||||
{
|
||||
std::size_t diff, offset;
|
||||
int skipped = 0;
|
||||
|
||||
/* figure out how many backslash-newlines skipped over unknowingly. */
|
||||
diff = cursor - s->bot;
|
||||
offset = get_first_eol_offset(s);
|
||||
while (offset <= diff && offset != (unsigned int)-1)
|
||||
{
|
||||
skipped++;
|
||||
aq_pop(s->eol_offsets);
|
||||
offset = get_first_eol_offset(s);
|
||||
}
|
||||
return skipped;
|
||||
}
|
||||
|
||||
bool is_backslash(uchar *p, uchar *end, int &len)
|
||||
{
|
||||
if (*p == '\\') {
|
||||
len = 1;
|
||||
return true;
|
||||
}
|
||||
else if (*p == '?' && *(p+1) == '?' && (p+2 < end && *(p+2) == '/')) {
|
||||
len = 3;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
uchar *fill(Scanner *s, uchar *cursor)
|
||||
{
|
||||
using namespace std; // some systems have memcpy etc. in namespace std
|
||||
if(!s->eof)
|
||||
{
|
||||
uchar* p;
|
||||
std::ptrdiff_t cnt = s->tok - s->bot;
|
||||
if(cnt)
|
||||
{
|
||||
if (NULL == s->lim)
|
||||
s->lim = s->top;
|
||||
memmove(s->bot, s->tok, s->lim - s->tok);
|
||||
s->tok = s->cur = s->bot;
|
||||
s->ptr -= cnt;
|
||||
cursor -= cnt;
|
||||
s->lim -= cnt;
|
||||
adjust_eol_offsets(s, cnt);
|
||||
}
|
||||
|
||||
if((s->top - s->lim) < BOOST_WAVE_BSIZE)
|
||||
{
|
||||
uchar *buf = (uchar*) malloc(((s->lim - s->bot) + BOOST_WAVE_BSIZE)*sizeof(uchar));
|
||||
if (buf == 0)
|
||||
{
|
||||
using namespace std; // some systems have printf in std
|
||||
if (0 != s->error_proc) {
|
||||
(*s->error_proc)(s, lexing_exception::unexpected_error,
|
||||
"Out of memory!");
|
||||
}
|
||||
else
|
||||
printf("Out of memory!\n");
|
||||
|
||||
/* get the scanner to stop */
|
||||
*cursor = 0;
|
||||
return cursor;
|
||||
}
|
||||
|
||||
memmove(buf, s->tok, s->lim - s->tok);
|
||||
s->tok = s->cur = buf;
|
||||
s->ptr = &buf[s->ptr - s->bot];
|
||||
cursor = &buf[cursor - s->bot];
|
||||
s->lim = &buf[s->lim - s->bot];
|
||||
s->top = &s->lim[BOOST_WAVE_BSIZE];
|
||||
free(s->bot);
|
||||
s->bot = buf;
|
||||
}
|
||||
|
||||
if (s->act != 0) {
|
||||
cnt = s->last - s->act;
|
||||
if (cnt > BOOST_WAVE_BSIZE)
|
||||
cnt = BOOST_WAVE_BSIZE;
|
||||
memmove(s->lim, s->act, cnt);
|
||||
s->act += cnt;
|
||||
if (cnt != BOOST_WAVE_BSIZE)
|
||||
{
|
||||
s->eof = &s->lim[cnt]; *(s->eof)++ = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
/* backslash-newline erasing time */
|
||||
|
||||
/* first scan for backslash-newline and erase them */
|
||||
for (p = s->lim; p < s->lim + cnt - 2; ++p)
|
||||
{
|
||||
int len = 0;
|
||||
if (is_backslash(p, s->lim + cnt, len))
|
||||
{
|
||||
if (*(p+len) == '\n')
|
||||
{
|
||||
int offset = len + 1;
|
||||
memmove(p, p + offset, s->lim + cnt - p - offset);
|
||||
cnt -= offset;
|
||||
--p;
|
||||
aq_enqueue(s->eol_offsets, p - s->bot + 1);
|
||||
}
|
||||
else if (*(p+len) == '\r')
|
||||
{
|
||||
if (*(p+len+1) == '\n')
|
||||
{
|
||||
int offset = len + 2;
|
||||
memmove(p, p + offset, s->lim + cnt - p - offset);
|
||||
cnt -= offset;
|
||||
--p;
|
||||
}
|
||||
else
|
||||
{
|
||||
int offset = len + 1;
|
||||
memmove(p, p + offset, s->lim + cnt - p - offset);
|
||||
cnt -= offset;
|
||||
--p;
|
||||
}
|
||||
aq_enqueue(s->eol_offsets, p - s->bot + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* FIXME: the following code should be fixed to recognize correctly the
|
||||
trigraph backslash token */
|
||||
|
||||
/* check to see if what we just read ends in a backslash */
|
||||
if (cnt >= 2)
|
||||
{
|
||||
uchar last = s->lim[cnt-1];
|
||||
uchar last2 = s->lim[cnt-2];
|
||||
/* check \ EOB */
|
||||
if (last == '\\')
|
||||
{
|
||||
int next = get_one_char(s);
|
||||
/* check for \ \n or \ \r or \ \r \n straddling the border */
|
||||
if (next == '\n')
|
||||
{
|
||||
--cnt; /* chop the final \, we've already read the \n. */
|
||||
aq_enqueue(s->eol_offsets, cnt + (s->lim - s->bot));
|
||||
}
|
||||
else if (next == '\r')
|
||||
{
|
||||
int next2 = get_one_char(s);
|
||||
if (next2 == '\n')
|
||||
{
|
||||
--cnt; /* skip the backslash */
|
||||
}
|
||||
else
|
||||
{
|
||||
/* rewind one, and skip one char */
|
||||
rewind_stream(s, -1);
|
||||
--cnt;
|
||||
}
|
||||
aq_enqueue(s->eol_offsets, cnt + (s->lim - s->bot));
|
||||
}
|
||||
else if (next != -1) /* -1 means end of file */
|
||||
{
|
||||
/* next was something else, so rewind the stream */
|
||||
rewind_stream(s, -1);
|
||||
}
|
||||
}
|
||||
/* check \ \r EOB */
|
||||
else if (last == '\r' && last2 == '\\')
|
||||
{
|
||||
int next = get_one_char(s);
|
||||
if (next == '\n')
|
||||
{
|
||||
cnt -= 2; /* skip the \ \r */
|
||||
}
|
||||
else
|
||||
{
|
||||
/* rewind one, and skip two chars */
|
||||
rewind_stream(s, -1);
|
||||
cnt -= 2;
|
||||
}
|
||||
aq_enqueue(s->eol_offsets, cnt + (s->lim - s->bot));
|
||||
}
|
||||
/* check \ \n EOB */
|
||||
else if (last == '\n' && last2 == '\\')
|
||||
{
|
||||
cnt -= 2;
|
||||
aq_enqueue(s->eol_offsets, cnt + (s->lim - s->bot));
|
||||
}
|
||||
}
|
||||
|
||||
s->lim += cnt;
|
||||
if (s->eof) /* eof needs adjusting if we erased backslash-newlines */
|
||||
{
|
||||
s->eof = s->lim;
|
||||
*(s->eof)++ = '\0';
|
||||
}
|
||||
}
|
||||
return cursor;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Special wrapper class holding the current cursor position
|
||||
struct uchar_wrapper
|
||||
{
|
||||
uchar_wrapper (uchar *base_cursor, unsigned int column = 1)
|
||||
: base_cursor(base_cursor), column(column)
|
||||
{}
|
||||
|
||||
uchar_wrapper& operator++()
|
||||
{
|
||||
++base_cursor;
|
||||
++column;
|
||||
return *this;
|
||||
}
|
||||
|
||||
uchar_wrapper& operator--()
|
||||
{
|
||||
--base_cursor;
|
||||
--column;
|
||||
return *this;
|
||||
}
|
||||
|
||||
uchar operator* () const
|
||||
{
|
||||
return *base_cursor;
|
||||
}
|
||||
|
||||
operator uchar *() const
|
||||
{
|
||||
return base_cursor;
|
||||
}
|
||||
|
||||
friend std::ptrdiff_t
|
||||
operator- (uchar_wrapper const& lhs, uchar_wrapper const& rhs)
|
||||
{
|
||||
return lhs.base_cursor - rhs.base_cursor;
|
||||
}
|
||||
|
||||
uchar *base_cursor;
|
||||
unsigned int column;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
boost::wave::token_id scan(Scanner *s)
|
||||
{
|
||||
BOOST_ASSERT(0 != s->error_proc); // error handler must be given
|
||||
|
||||
uchar_wrapper cursor (s->tok = s->cur, s->column = s->curr_column);
|
||||
uchar_wrapper marker (s->ptr);
|
||||
uchar_wrapper limit (s->lim);
|
||||
|
||||
// include the correct Re2C token definition rules
|
||||
#if BOOST_WAVE_USE_STRICT_LEXER != 0
|
||||
#include "strict_cpp_re.inc"
|
||||
#else
|
||||
#include "cpp_re.inc"
|
||||
#endif
|
||||
|
||||
} /* end of scan */
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
} // namespace re2clex
|
||||
} // namespace cpplexer
|
||||
} // namespace wave
|
||||
} // namespace boost
|
||||
|
||||
#undef BOOST_WAVE_RET
|
||||
#undef BOOST_WAVE_BSIZE
|
||||
#undef YYCTYPE
|
||||
#undef YYCURSOR
|
||||
#undef YYLIMIT
|
||||
#undef YYMARKER
|
||||
#undef YYFILL
|
||||
|
||||
// the suffix header occurs after all of the code
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,52 @@
|
||||
/*=============================================================================
|
||||
Boost.Wave: A Standard compliant C++ preprocessor library
|
||||
http://www.boost.org/
|
||||
|
||||
Copyright (c) 2001-2011 Hartmut Kaiser. Distributed under the Boost
|
||||
Software License, Version 1.0. (See accompanying file
|
||||
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
|
||||
#define BOOST_WAVE_SOURCE 1
|
||||
|
||||
// disable stupid compiler warnings
|
||||
#include <boost/config/warning_disable.hpp>
|
||||
#include <boost/wave/wave_config.hpp>
|
||||
|
||||
#if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <boost/wave/cpplexer/cpp_lex_token.hpp>
|
||||
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
|
||||
|
||||
#include <boost/wave/grammars/cpp_expression_grammar.hpp>
|
||||
|
||||
// this must occur after all of the includes and before any code appears
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Explicit instantiation of the expression_grammar_gen template with the
|
||||
// correct lexer iterator type. This instantiates the corresponding parse
|
||||
// function, which in turn instantiates the expression_grammar object (see
|
||||
// wave/grammars/cpp_expression_grammar.hpp)
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// if you want to use your own token type the following line must be adjusted
|
||||
typedef boost::wave::cpplexer::lex_token<> token_type;
|
||||
|
||||
// no need to change anything below
|
||||
template struct boost::wave::grammars::expression_grammar_gen<token_type>;
|
||||
|
||||
// the suffix header occurs after all of the code
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // #if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
|
||||
|
@ -0,0 +1,56 @@
|
||||
/*=============================================================================
|
||||
Boost.Wave: A Standard compliant C++ preprocessor library
|
||||
http://www.boost.org/
|
||||
|
||||
Copyright (c) 2001-2011 Hartmut Kaiser. Distributed under the Boost
|
||||
Software License, Version 1.0. (See accompanying file
|
||||
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
|
||||
#define BOOST_WAVE_SOURCE 1
|
||||
|
||||
// disable stupid compiler warnings
|
||||
#include <boost/config/warning_disable.hpp>
|
||||
#include <boost/wave/wave_config.hpp>
|
||||
|
||||
#if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
|
||||
#include <boost/wave/cpplexer/cpp_lex_token.hpp>
|
||||
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
|
||||
|
||||
#include <boost/wave/grammars/cpp_grammar.hpp>
|
||||
|
||||
// this must occur after all of the includes and before any code appears
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Explicit instantiation of the cpp_grammar_gen template with the correct
|
||||
// token type. This instantiates the corresponding pt_parse function, which
|
||||
// in turn instantiates the cpp_grammar object
|
||||
// (see wave/grammars/cpp_grammar.hpp)
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// if you want to use your own token type the following line must be adjusted
|
||||
typedef boost::wave::cpplexer::lex_token<> token_type;
|
||||
|
||||
// no need to change anything below
|
||||
typedef boost::wave::cpplexer::lex_iterator<token_type> lexer_type;
|
||||
typedef std::list<token_type, boost::fast_pool_allocator<token_type> >
|
||||
token_sequence_type;
|
||||
|
||||
template struct boost::wave::grammars::cpp_grammar_gen<lexer_type, token_sequence_type>;
|
||||
|
||||
// the suffix header occurs after all of the code
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // #if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
|
||||
|
@ -0,0 +1,56 @@
|
||||
/*=============================================================================
|
||||
Boost.Wave: A Standard compliant C++ preprocessor library
|
||||
http://www.boost.org/
|
||||
|
||||
Copyright (c) 2001-2011 Hartmut Kaiser. Distributed under the Boost
|
||||
Software License, Version 1.0. (See accompanying file
|
||||
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
|
||||
#define BOOST_WAVE_SOURCE 1
|
||||
|
||||
// disable stupid compiler warnings
|
||||
#include <boost/config/warning_disable.hpp>
|
||||
#include <boost/wave/wave_config.hpp>
|
||||
|
||||
#if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <boost/wave/cpplexer/cpp_lex_token.hpp>
|
||||
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
|
||||
|
||||
#include <boost/wave/grammars/cpp_literal_grammar_gen.hpp>
|
||||
#include <boost/wave/grammars/cpp_intlit_grammar.hpp>
|
||||
#include <boost/wave/grammars/cpp_chlit_grammar.hpp>
|
||||
|
||||
// this must occur after all of the includes and before any code appears
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Explicit instantiation of the intlit_grammar_gen and chlit_grammar_gen
|
||||
// templates with the correct token type. This instantiates the corresponding
|
||||
// parse function, which in turn instantiates the corresponding parser object.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
typedef boost::wave::cpplexer::lex_token<> token_type;
|
||||
|
||||
// no need to change anything below
|
||||
template struct boost::wave::grammars::intlit_grammar_gen<token_type>;
|
||||
#if BOOST_WAVE_WCHAR_T_SIGNEDNESS == BOOST_WAVE_WCHAR_T_AUTOSELECT || \
|
||||
BOOST_WAVE_WCHAR_T_SIGNEDNESS == BOOST_WAVE_WCHAR_T_FORCE_SIGNED
|
||||
template struct boost::wave::grammars::chlit_grammar_gen<int, token_type>;
|
||||
#endif
|
||||
template struct boost::wave::grammars::chlit_grammar_gen<unsigned int, token_type>;
|
||||
|
||||
// the suffix header occurs after all of the code
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // #if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
|
||||
|
@ -0,0 +1,52 @@
|
||||
/*=============================================================================
|
||||
Boost.Wave: A Standard compliant C++ preprocessor library
|
||||
http://www.boost.org/
|
||||
|
||||
Copyright (c) 2001-2011 Hartmut Kaiser. Distributed under the Boost
|
||||
Software License, Version 1.0. (See accompanying file
|
||||
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
|
||||
#define BOOST_WAVE_SOURCE 1
|
||||
|
||||
// disable stupid compiler warnings
|
||||
#include <boost/config/warning_disable.hpp>
|
||||
#include <boost/wave/wave_config.hpp>
|
||||
|
||||
#if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <boost/wave/cpplexer/cpp_lex_token.hpp>
|
||||
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
|
||||
|
||||
#include <boost/wave/grammars/cpp_defined_grammar.hpp>
|
||||
|
||||
// this must occur after all of the includes and before any code appears
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Explicit instantiation of the defined_grammar_gen template
|
||||
// with the correct token type. This instantiates the corresponding parse
|
||||
// function, which in turn instantiates the defined_grammar
|
||||
// object (see wave/grammars/cpp_defined_grammar.hpp)
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// if you want to use your own token type the following line must be adjusted
|
||||
typedef boost::wave::cpplexer::lex_token<> token_type;
|
||||
|
||||
// no need to change anything below
|
||||
typedef boost::wave::cpplexer::lex_iterator<token_type> lexer_type;
|
||||
template struct boost::wave::grammars::defined_grammar_gen<lexer_type>;
|
||||
|
||||
// the suffix header occurs after all of the code
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // #if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
|
||||
|
@ -0,0 +1,52 @@
|
||||
/*=============================================================================
|
||||
Boost.Wave: A Standard compliant C++ preprocessor library
|
||||
http://www.boost.org/
|
||||
|
||||
Copyright (c) 2001-2011 Hartmut Kaiser. Distributed under the Boost
|
||||
Software License, Version 1.0. (See accompanying file
|
||||
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
|
||||
#define BOOST_WAVE_SOURCE 1
|
||||
|
||||
// disable stupid compiler warnings
|
||||
#include <boost/config/warning_disable.hpp>
|
||||
#include <boost/wave/wave_config.hpp>
|
||||
|
||||
#if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <boost/wave/cpplexer/cpp_lex_token.hpp>
|
||||
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
|
||||
|
||||
#include <boost/wave/grammars/cpp_predef_macros_grammar.hpp>
|
||||
|
||||
// this must occur after all of the includes and before any code appears
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Explicit instantiation of the predefined_macros_grammar_gen template
|
||||
// with the correct token type. This instantiates the corresponding pt_parse
|
||||
// function, which in turn instantiates the cpp_predefined_macros_grammar
|
||||
// object (see wave/grammars/cpp_predef_macros_grammar.hpp)
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// if you want to use your own token type the following line must be adjusted
|
||||
typedef boost::wave::cpplexer::lex_token<> token_type;
|
||||
|
||||
// no need to change anything below
|
||||
typedef boost::wave::cpplexer::lex_iterator<token_type> lexer_type;
|
||||
template struct boost::wave::grammars::predefined_macros_grammar_gen<lexer_type>;
|
||||
|
||||
// the suffix header occurs after all of the code
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // #if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
|
||||
|
@ -0,0 +1,65 @@
|
||||
/*=============================================================================
|
||||
Boost.Wave: A Standard compliant C++ preprocessor library
|
||||
Explicit instantiation of the lex_functor generation function
|
||||
|
||||
http://www.boost.org/
|
||||
|
||||
Copyright (c) 2001-2011 Hartmut Kaiser. Distributed under the Boost
|
||||
Software License, Version 1.0. (See accompanying file
|
||||
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
|
||||
#define BOOST_WAVE_SOURCE 1
|
||||
|
||||
// disable stupid compiler warnings
|
||||
#include <boost/config/warning_disable.hpp>
|
||||
#include <boost/wave/wave_config.hpp> // configuration data
|
||||
|
||||
#if BOOST_WAVE_SEPARATE_LEXER_INSTANTIATION != 0
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <boost/wave/token_ids.hpp>
|
||||
#include <boost/wave/cpplexer/cpp_lex_token.hpp>
|
||||
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// The following file needs to be included only once throughout the whole
|
||||
// program.
|
||||
#include <boost/wave/cpplexer/re2clex/cpp_re2c_lexer.hpp>
|
||||
|
||||
// this must occur after all of the includes and before any code appears
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// This instantiates the correct 'new_lexer' function, which generates the
|
||||
// C++ lexer used in this sample. You will have to instantiate the
|
||||
// new_lexer_gen<> template with the same iterator type, as you have used for
|
||||
// instantiating the boost::wave::context<> object.
|
||||
//
|
||||
// This is moved into a separate compilation unit to decouple the compilation
|
||||
// of the C++ lexer from the compilation of the other modules, which helps to
|
||||
// reduce compilation time.
|
||||
//
|
||||
// The template parameter(s) supplied should be identical to the first
|
||||
// parameter supplied while instantiating the boost::wave::context<> template
|
||||
// (see the file cpp.cpp).
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// if you want to use another iterator type for the underlying input stream
|
||||
// a corresponding explicit template instantiation needs to be added below
|
||||
template struct boost::wave::cpplexer::new_lexer_gen<
|
||||
BOOST_WAVE_STRINGTYPE::iterator>;
|
||||
template struct boost::wave::cpplexer::new_lexer_gen<
|
||||
BOOST_WAVE_STRINGTYPE::const_iterator>;
|
||||
|
||||
// the suffix header occurs after all of the code
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // BOOST_WAVE_SEPARATE_LEXER_INSTANTIATION != 0
|
@ -0,0 +1,64 @@
|
||||
/*=============================================================================
|
||||
Boost.Wave: A Standard compliant C++ preprocessor library
|
||||
Explicit instantiation of the lex_functor generation function
|
||||
|
||||
http://www.boost.org/
|
||||
|
||||
Copyright (c) 2001-2011 Hartmut Kaiser. Distributed under the Boost
|
||||
Software License, Version 1.0. (See accompanying file
|
||||
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
|
||||
#define BOOST_WAVE_SOURCE 1
|
||||
|
||||
// disable stupid compiler warnings
|
||||
#include <boost/config/warning_disable.hpp>
|
||||
#include <boost/wave/wave_config.hpp> // configuration data
|
||||
|
||||
#if BOOST_WAVE_SEPARATE_LEXER_INSTANTIATION != 0
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <boost/wave/token_ids.hpp>
|
||||
#include <boost/wave/cpplexer/cpp_lex_token.hpp>
|
||||
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// The following file needs to be included only once throughout the whole
|
||||
// program.
|
||||
#include <boost/wave/cpplexer/re2clex/cpp_re2c_lexer.hpp>
|
||||
|
||||
// this must occur after all of the includes and before any code appears
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// If you've used another iterator type as std::string::iterator, you have to
|
||||
// instantiate the new_lexer_gen<> template for this iterator type too.
|
||||
// The reason is, that the library internally uses the new_lexer_gen<>
|
||||
// template with a std::string::iterator. (You just have to undefine the
|
||||
// following line.)
|
||||
//
|
||||
// This is moved into a separate compilation unit to decouple the compilation
|
||||
// of the C++ lexer from the compilation of the other modules, which helps to
|
||||
// reduce compilation time.
|
||||
//
|
||||
// The template parameter(s) supplied should be identical to the first
|
||||
// parameter supplied while instantiating the boost::wave::context<> template
|
||||
// (see the file cpp.cpp).
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if !defined(BOOST_WAVE_STRINGTYPE_USE_STDSTRING)
|
||||
template struct boost::wave::cpplexer::new_lexer_gen<std::string::iterator>;
|
||||
template struct boost::wave::cpplexer::new_lexer_gen<std::string::const_iterator>;
|
||||
#endif
|
||||
|
||||
// the suffix header occurs after all of the code
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // BOOST_WAVE_SEPARATE_LEXER_INSTANTIATION != 0
|
@ -0,0 +1,447 @@
|
||||
/*=============================================================================
|
||||
Boost.Wave: A Standard compliant C++ preprocessor library
|
||||
The definition of a default set of token identifiers and related
|
||||
functions.
|
||||
|
||||
http://www.boost.org/
|
||||
|
||||
Copyright (c) 2001-2011 Hartmut Kaiser. Distributed under the Boost
|
||||
Software License, Version 1.0. (See accompanying file
|
||||
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
|
||||
#define BOOST_WAVE_SOURCE 1
|
||||
|
||||
// disable stupid compiler warnings
|
||||
#include <boost/config/warning_disable.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
#include <boost/wave/wave_config.hpp>
|
||||
#include <boost/wave/token_ids.hpp>
|
||||
|
||||
// this must occur after all of the includes and before any code appears
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace boost {
|
||||
namespace wave {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// return a token name
|
||||
BOOST_WAVE_STRINGTYPE
|
||||
get_token_name(token_id tokid)
|
||||
{
|
||||
// Table of token names
|
||||
//
|
||||
// Please note that the sequence of token names must match the sequence of
|
||||
// token id's defined in then enum token_id above.
|
||||
static char const *tok_names[] = {
|
||||
/* 256 */ "AND",
|
||||
/* 257 */ "ANDAND",
|
||||
/* 258 */ "ASSIGN",
|
||||
/* 259 */ "ANDASSIGN",
|
||||
/* 260 */ "OR",
|
||||
/* 261 */ "ORASSIGN",
|
||||
/* 262 */ "XOR",
|
||||
/* 263 */ "XORASSIGN",
|
||||
/* 264 */ "COMMA",
|
||||
/* 265 */ "COLON",
|
||||
/* 266 */ "DIVIDE",
|
||||
/* 267 */ "DIVIDEASSIGN",
|
||||
/* 268 */ "DOT",
|
||||
/* 269 */ "DOTSTAR",
|
||||
/* 270 */ "ELLIPSIS",
|
||||
/* 271 */ "EQUAL",
|
||||
/* 272 */ "GREATER",
|
||||
/* 273 */ "GREATEREQUAL",
|
||||
/* 274 */ "LEFTBRACE",
|
||||
/* 275 */ "LESS",
|
||||
/* 276 */ "LESSEQUAL",
|
||||
/* 277 */ "LEFTPAREN",
|
||||
/* 278 */ "LEFTBRACKET",
|
||||
/* 279 */ "MINUS",
|
||||
/* 280 */ "MINUSASSIGN",
|
||||
/* 281 */ "MINUSMINUS",
|
||||
/* 282 */ "PERCENT",
|
||||
/* 283 */ "PERCENTASSIGN",
|
||||
/* 284 */ "NOT",
|
||||
/* 285 */ "NOTEQUAL",
|
||||
/* 286 */ "OROR",
|
||||
/* 287 */ "PLUS",
|
||||
/* 288 */ "PLUSASSIGN",
|
||||
/* 289 */ "PLUSPLUS",
|
||||
/* 290 */ "ARROW",
|
||||
/* 291 */ "ARROWSTAR",
|
||||
/* 292 */ "QUESTION_MARK",
|
||||
/* 293 */ "RIGHTBRACE",
|
||||
/* 294 */ "RIGHTPAREN",
|
||||
/* 295 */ "RIGHTBRACKET",
|
||||
/* 296 */ "COLON_COLON",
|
||||
/* 297 */ "SEMICOLON",
|
||||
/* 298 */ "SHIFTLEFT",
|
||||
/* 299 */ "SHIFTLEFTASSIGN",
|
||||
/* 300 */ "SHIFTRIGHT",
|
||||
/* 301 */ "SHIFTRIGHTASSIGN",
|
||||
/* 302 */ "STAR",
|
||||
/* 303 */ "COMPL",
|
||||
/* 304 */ "STARASSIGN",
|
||||
/* 305 */ "ASM",
|
||||
/* 306 */ "AUTO",
|
||||
/* 307 */ "BOOL",
|
||||
/* 308 */ "FALSE",
|
||||
/* 309 */ "TRUE",
|
||||
/* 310 */ "BREAK",
|
||||
/* 311 */ "CASE",
|
||||
/* 312 */ "CATCH",
|
||||
/* 313 */ "CHAR",
|
||||
/* 314 */ "CLASS",
|
||||
/* 315 */ "CONST",
|
||||
/* 316 */ "CONSTCAST",
|
||||
/* 317 */ "CONTINUE",
|
||||
/* 318 */ "DEFAULT",
|
||||
/* 319 */ "DELETE",
|
||||
/* 320 */ "DO",
|
||||
/* 321 */ "DOUBLE",
|
||||
/* 322 */ "DYNAMICCAST",
|
||||
/* 323 */ "ELSE",
|
||||
/* 324 */ "ENUM",
|
||||
/* 325 */ "EXPLICIT",
|
||||
/* 326 */ "EXPORT",
|
||||
/* 327 */ "EXTERN",
|
||||
/* 328 */ "FLOAT",
|
||||
/* 329 */ "FOR",
|
||||
/* 330 */ "FRIEND",
|
||||
/* 331 */ "GOTO",
|
||||
/* 332 */ "IF",
|
||||
/* 333 */ "INLINE",
|
||||
/* 334 */ "INT",
|
||||
/* 335 */ "LONG",
|
||||
/* 336 */ "MUTABLE",
|
||||
/* 337 */ "NAMESPACE",
|
||||
/* 338 */ "NEW",
|
||||
/* 339 */ "OPERATOR",
|
||||
/* 340 */ "PRIVATE",
|
||||
/* 341 */ "PROTECTED",
|
||||
/* 342 */ "PUBLIC",
|
||||
/* 343 */ "REGISTER",
|
||||
/* 344 */ "REINTERPRETCAST",
|
||||
/* 345 */ "RETURN",
|
||||
/* 346 */ "SHORT",
|
||||
/* 347 */ "SIGNED",
|
||||
/* 348 */ "SIZEOF",
|
||||
/* 349 */ "STATIC",
|
||||
/* 350 */ "STATICCAST",
|
||||
/* 351 */ "STRUCT",
|
||||
/* 352 */ "SWITCH",
|
||||
/* 353 */ "TEMPLATE",
|
||||
/* 354 */ "THIS",
|
||||
/* 355 */ "THROW",
|
||||
/* 356 */ "TRY",
|
||||
/* 357 */ "TYPEDEF",
|
||||
/* 358 */ "TYPEID",
|
||||
/* 359 */ "TYPENAME",
|
||||
/* 360 */ "UNION",
|
||||
/* 361 */ "UNSIGNED",
|
||||
/* 362 */ "USING",
|
||||
/* 363 */ "VIRTUAL",
|
||||
/* 364 */ "VOID",
|
||||
/* 365 */ "VOLATILE",
|
||||
/* 366 */ "WCHART",
|
||||
/* 367 */ "WHILE",
|
||||
/* 368 */ "PP_DEFINE",
|
||||
/* 369 */ "PP_IF",
|
||||
/* 370 */ "PP_IFDEF",
|
||||
/* 371 */ "PP_IFNDEF",
|
||||
/* 372 */ "PP_ELSE",
|
||||
/* 373 */ "PP_ELIF",
|
||||
/* 374 */ "PP_ENDIF",
|
||||
/* 375 */ "PP_ERROR",
|
||||
/* 376 */ "PP_LINE",
|
||||
/* 377 */ "PP_PRAGMA",
|
||||
/* 378 */ "PP_UNDEF",
|
||||
/* 379 */ "PP_WARNING",
|
||||
/* 380 */ "IDENTIFIER",
|
||||
/* 381 */ "OCTALINT",
|
||||
/* 382 */ "DECIMALINT",
|
||||
/* 383 */ "HEXAINT",
|
||||
/* 384 */ "INTLIT",
|
||||
/* 385 */ "LONGINTLIT",
|
||||
/* 386 */ "FLOATLIT",
|
||||
/* 387 */ "CCOMMENT",
|
||||
/* 388 */ "CPPCOMMENT",
|
||||
/* 389 */ "CHARLIT",
|
||||
/* 390 */ "STRINGLIT",
|
||||
/* 391 */ "CONTLINE",
|
||||
/* 392 */ "SPACE",
|
||||
/* 393 */ "SPACE2",
|
||||
/* 394 */ "NEWLINE",
|
||||
/* 395 */ "POUND_POUND",
|
||||
/* 396 */ "POUND",
|
||||
/* 397 */ "ANY",
|
||||
/* 398 */ "PP_INCLUDE",
|
||||
/* 399 */ "PP_QHEADER",
|
||||
/* 400 */ "PP_HHEADER",
|
||||
/* 401 */ "EOF",
|
||||
/* 402 */ "EOI",
|
||||
/* 403 */ "PP_NUMBER",
|
||||
|
||||
// MS extensions
|
||||
/* 404 */ "MSEXT_INT8",
|
||||
/* 405 */ "MSEXT_INT16",
|
||||
/* 406 */ "MSEXT_INT32",
|
||||
/* 407 */ "MSEXT_INT64",
|
||||
/* 408 */ "MSEXT_BASED",
|
||||
/* 409 */ "MSEXT_DECLSPEC",
|
||||
/* 410 */ "MSEXT_CDECL",
|
||||
/* 411 */ "MSEXT_FASTCALL",
|
||||
/* 412 */ "MSEXT_STDCALL",
|
||||
/* 413 */ "MSEXT_TRY",
|
||||
/* 414 */ "MSEXT_EXCEPT",
|
||||
/* 415 */ "MSEXT_FINALLY",
|
||||
/* 416 */ "MSEXT_LEAVE",
|
||||
/* 417 */ "MSEXT_INLINE",
|
||||
/* 418 */ "MSEXT_ASM",
|
||||
/* 419 */ "MSEXT_REGION",
|
||||
/* 420 */ "MSEXT_ENDREGION",
|
||||
|
||||
/* 421 */ "IMPORT",
|
||||
|
||||
/* 422 */ "ALIGNAS",
|
||||
/* 423 */ "ALIGNOF",
|
||||
/* 424 */ "CHAR16_T",
|
||||
/* 425 */ "CHAR32_T",
|
||||
/* 426 */ "CONSTEXPR",
|
||||
/* 427 */ "DECLTYPE",
|
||||
/* 428 */ "NOEXCEPT",
|
||||
/* 429 */ "NULLPTR",
|
||||
/* 430 */ "STATIC_ASSERT",
|
||||
/* 431 */ "THREADLOCAL",
|
||||
/* 432 */ "RAWSTRINGLIT",
|
||||
};
|
||||
|
||||
// make sure, I have not forgotten any commas (as I did more than once)
|
||||
BOOST_STATIC_ASSERT(
|
||||
sizeof(tok_names)/sizeof(tok_names[0]) == T_LAST_TOKEN-T_FIRST_TOKEN
|
||||
);
|
||||
|
||||
unsigned int id = BASEID_FROM_TOKEN(tokid)-T_FIRST_TOKEN;
|
||||
return (id < T_LAST_TOKEN-T_FIRST_TOKEN) ? tok_names[id] : "<UnknownToken>";
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// return a token name
|
||||
char const *
|
||||
get_token_value(token_id tokid)
|
||||
{
|
||||
// Table of token values
|
||||
//
|
||||
// Please note that the sequence of token names must match the sequence of
|
||||
// token id's defined in then enum token_id above.
|
||||
static char const *tok_values[] = {
|
||||
/* 256 */ "&",
|
||||
/* 257 */ "&&",
|
||||
/* 258 */ "=",
|
||||
/* 259 */ "&=",
|
||||
/* 260 */ "|",
|
||||
/* 261 */ "|=",
|
||||
/* 262 */ "^",
|
||||
/* 263 */ "^=",
|
||||
/* 264 */ ",",
|
||||
/* 265 */ ":",
|
||||
/* 266 */ "/",
|
||||
/* 267 */ "/=",
|
||||
/* 268 */ ".",
|
||||
/* 269 */ ".*",
|
||||
/* 270 */ "...",
|
||||
/* 271 */ "==",
|
||||
/* 272 */ ">",
|
||||
/* 273 */ ">=",
|
||||
/* 274 */ "{",
|
||||
/* 275 */ "<",
|
||||
/* 276 */ "<=",
|
||||
/* 277 */ "(",
|
||||
/* 278 */ "[",
|
||||
/* 279 */ "-",
|
||||
/* 280 */ "-=",
|
||||
/* 281 */ "--",
|
||||
/* 282 */ "%",
|
||||
/* 283 */ "%=",
|
||||
/* 284 */ "!",
|
||||
/* 285 */ "!=",
|
||||
/* 286 */ "||",
|
||||
/* 287 */ "+",
|
||||
/* 288 */ "+=",
|
||||
/* 289 */ "++",
|
||||
/* 290 */ "->",
|
||||
/* 291 */ "->*",
|
||||
/* 292 */ "?",
|
||||
/* 293 */ "}",
|
||||
/* 294 */ ")",
|
||||
/* 295 */ "]",
|
||||
/* 296 */ "::",
|
||||
/* 297 */ ";",
|
||||
/* 298 */ "<<",
|
||||
/* 299 */ "<<=",
|
||||
/* 300 */ ">>",
|
||||
/* 301 */ ">>=",
|
||||
/* 302 */ "*",
|
||||
/* 303 */ "~",
|
||||
/* 304 */ "*=",
|
||||
/* 305 */ "asm",
|
||||
/* 306 */ "auto",
|
||||
/* 307 */ "bool",
|
||||
/* 308 */ "false",
|
||||
/* 309 */ "true",
|
||||
/* 310 */ "break",
|
||||
/* 311 */ "case",
|
||||
/* 312 */ "catch",
|
||||
/* 313 */ "char",
|
||||
/* 314 */ "class",
|
||||
/* 315 */ "const",
|
||||
/* 316 */ "const_cast",
|
||||
/* 317 */ "continue",
|
||||
/* 318 */ "default",
|
||||
/* 319 */ "delete",
|
||||
/* 320 */ "do",
|
||||
/* 321 */ "double",
|
||||
/* 322 */ "dynamic_cast",
|
||||
/* 323 */ "else",
|
||||
/* 324 */ "enum",
|
||||
/* 325 */ "explicit",
|
||||
/* 326 */ "export",
|
||||
/* 327 */ "extern",
|
||||
/* 328 */ "float",
|
||||
/* 329 */ "for",
|
||||
/* 330 */ "friend",
|
||||
/* 331 */ "goto",
|
||||
/* 332 */ "if",
|
||||
/* 333 */ "inline",
|
||||
/* 334 */ "int",
|
||||
/* 335 */ "long",
|
||||
/* 336 */ "mutable",
|
||||
/* 337 */ "namespace",
|
||||
/* 338 */ "new",
|
||||
/* 339 */ "operator",
|
||||
/* 340 */ "private",
|
||||
/* 341 */ "protected",
|
||||
/* 342 */ "public",
|
||||
/* 343 */ "register",
|
||||
/* 344 */ "reinterpret_cast",
|
||||
/* 345 */ "return",
|
||||
/* 346 */ "short",
|
||||
/* 347 */ "signed",
|
||||
/* 348 */ "sizeof",
|
||||
/* 349 */ "static",
|
||||
/* 350 */ "static_cast",
|
||||
/* 351 */ "struct",
|
||||
/* 352 */ "switch",
|
||||
/* 353 */ "template",
|
||||
/* 354 */ "this",
|
||||
/* 355 */ "throw",
|
||||
/* 356 */ "try",
|
||||
/* 357 */ "typedef",
|
||||
/* 358 */ "typeid",
|
||||
/* 359 */ "typename",
|
||||
/* 360 */ "union",
|
||||
/* 361 */ "unsigned",
|
||||
/* 362 */ "using",
|
||||
/* 363 */ "virtual",
|
||||
/* 364 */ "void",
|
||||
/* 365 */ "volatile",
|
||||
/* 366 */ "wchar_t",
|
||||
/* 367 */ "while",
|
||||
/* 368 */ "#define",
|
||||
/* 369 */ "#if",
|
||||
/* 370 */ "#ifdef",
|
||||
/* 371 */ "#ifndef",
|
||||
/* 372 */ "#else",
|
||||
/* 373 */ "#elif",
|
||||
/* 374 */ "#endif",
|
||||
/* 375 */ "#error",
|
||||
/* 376 */ "#line",
|
||||
/* 377 */ "#pragma",
|
||||
/* 378 */ "#undef",
|
||||
/* 379 */ "#warning",
|
||||
/* 380 */ "", // identifier
|
||||
/* 381 */ "", // octalint
|
||||
/* 382 */ "", // decimalint
|
||||
/* 383 */ "", // hexlit
|
||||
/* 384 */ "", // intlit
|
||||
/* 385 */ "", // longintlit
|
||||
/* 386 */ "", // floatlit
|
||||
/* 387 */ "", // ccomment
|
||||
/* 388 */ "", // cppcomment
|
||||
/* 389 */ "", // charlit
|
||||
/* 390 */ "", // stringlit
|
||||
/* 391 */ "", // contline
|
||||
/* 392 */ "", // space
|
||||
/* 393 */ "", // space2
|
||||
/* 394 */ "\n",
|
||||
/* 395 */ "##",
|
||||
/* 396 */ "#",
|
||||
/* 397 */ "", // any
|
||||
/* 398 */ "#include",
|
||||
/* 399 */ "#include",
|
||||
/* 400 */ "#include",
|
||||
/* 401 */ "", // eof
|
||||
/* 402 */ "", // eoi
|
||||
/* 403 */ "", // pp-number
|
||||
|
||||
// MS extensions
|
||||
/* 404 */ "__int8",
|
||||
/* 405 */ "__int16",
|
||||
/* 406 */ "__int32",
|
||||
/* 407 */ "__int64",
|
||||
/* 408 */ "__based",
|
||||
/* 409 */ "__declspec",
|
||||
/* 410 */ "__cdecl",
|
||||
/* 411 */ "__fastcall",
|
||||
/* 412 */ "__stdcall",
|
||||
/* 413 */ "__try",
|
||||
/* 414 */ "__except",
|
||||
/* 415 */ "__finally",
|
||||
/* 416 */ "__leave",
|
||||
/* 417 */ "__inline",
|
||||
/* 418 */ "__asm",
|
||||
/* 419 */ "#region",
|
||||
/* 420 */ "#endregion",
|
||||
|
||||
/* 421 */ "import",
|
||||
|
||||
/* 422 */ "alignas",
|
||||
/* 423 */ "alignof",
|
||||
/* 424 */ "char16_t",
|
||||
/* 425 */ "char32_t",
|
||||
/* 426 */ "constexpr",
|
||||
/* 427 */ "decltype",
|
||||
/* 428 */ "noexcept",
|
||||
/* 429 */ "nullptr",
|
||||
/* 430 */ "static_assert",
|
||||
/* 431 */ "threadlocal",
|
||||
/* 432 */ "", // extrawstringlit
|
||||
};
|
||||
|
||||
// make sure, I have not forgotten any commas (as I did more than once)
|
||||
BOOST_STATIC_ASSERT(
|
||||
sizeof(tok_values)/sizeof(tok_values[0]) == T_LAST_TOKEN-T_FIRST_TOKEN
|
||||
);
|
||||
|
||||
unsigned int id = BASEID_FROM_TOKEN(tokid)-T_FIRST_TOKEN;
|
||||
return (id < T_LAST_TOKEN-T_FIRST_TOKEN) ? tok_values[id] : "<UnknownToken>";
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
} // namespace wave
|
||||
} // namespace boost
|
||||
|
||||
// the suffix header occurs after all of the code
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
|
@ -0,0 +1,33 @@
|
||||
shiny - a shader and material management library for OGRE
|
||||
|
||||
FEATURES
|
||||
|
||||
- High-level layer on top of OGRE's material system. It allows you to generate multiple techniques for all your materials from a set of high-level per-material properties.
|
||||
|
||||
- Several available Macros in shader source files. Just a few examples of the possibilities: binding OGRE auto constants, binding uniforms to material properties, foreach loops (repeat shader source a given number of times), retrieving per-material properties in an #if condition, automatic packing for vertex to fragment passthroughs. These macros allow you to generate even very complex shaders (for example the Ogre::Terrain shader) without assembling them in C++ code.
|
||||
|
||||
- Integrated preprocessor (no, I didn't reinvent the wheel, I used boost::wave which turned out to be an excellent choice) that allows me to blend out macros that shouldn't be in use because e.g. the shader permutation doesn't need this specific feature.
|
||||
|
||||
- User settings integration. They can be set by a C++ interface and retrieved through a macro in shader files.
|
||||
|
||||
- Automatic handling of shader permutations, i.e. shaders are shared between materials in a smart way.
|
||||
|
||||
- An optional "meta-language" (well, actually it's just a small header with some conditional defines) that you may use to compile the same shader source for different target languages. If you don't like it, you can still code in GLSL / CG etc separately. You can also switch between the languages at runtime.
|
||||
|
||||
- On-demand material and shader creation. It uses Ogre's material listener to compile the shaders as soon as they are needed for rendering, and not earlier.
|
||||
|
||||
- Shader changes are fully dynamic and real-time. Changing a user setting will recompile all shaders affected by this setting when they are next needed.
|
||||
|
||||
- Serialization system that extends Ogre's material script system, it uses Ogre's script parser, but also adds some additional properties that are not available in Ogre's material system.
|
||||
|
||||
- A concept called "Configuration" allowing you to create a different set of your shaders, doing the same thing except for some minor differences: the properties that are overridden by the active configuration. Possible uses for this are using simpler shaders (no shadows, no fog etc) when rendering for example realtime reflections or a minimap. You can easily switch between configurations by changing the active Ogre material scheme (for example on a viewport level).
|
||||
|
||||
- Fixed function support. You can globally enable or disable shaders at any time, and for texture units you can specify if they're only needed for the shader-based path (e.g. normal maps) or if they should also be created in the fixed function path.
|
||||
|
||||
LICENSE
|
||||
|
||||
see License.txt
|
||||
|
||||
AUTHOR
|
||||
|
||||
scrawl <scrawl@baseoftrash.de>
|
Loading…
Reference in New Issue