Skip to content

Commit

Permalink
add code for substitute-tokens to automatically insert incoming textu…
Browse files Browse the repository at this point in the history
…res to default shader
  • Loading branch information
Gargaj committed Feb 17, 2015
1 parent 5bbd67d commit e1a2ca9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 41 deletions.
43 changes: 42 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@
#include "external/scintilla/src/UniConversion.h"
#include "external/jsonxx/jsonxx.h"

void ReplaceTokens( std::string &sDefShader, const char * sTokenBegin, const char * sTokenName, const char * sTokenEnd, std::vector<std::string> &tokens )
{
if (sDefShader.find(sTokenBegin) != std::string::npos
&& sDefShader.find(sTokenName) != std::string::npos
&& sDefShader.find(sTokenEnd) != std::string::npos
&& sDefShader.find(sTokenBegin) < sDefShader.find(sTokenName)
&& sDefShader.find(sTokenName) < sDefShader.find(sTokenEnd))
{
int nTokenStart = sDefShader.find(sTokenBegin) + strlen(sTokenBegin);
std::string sTextureToken = sDefShader.substr( nTokenStart, sDefShader.find(sTokenEnd) - nTokenStart );

std::string sFinalShader;
sFinalShader = sDefShader.substr( 0, sDefShader.find(sTokenBegin) );

//for (std::map<std::string, Renderer::Texture*>::iterator it = tokens.begin(); it != tokens.end(); it++)
for (int i=0; i < tokens.size(); i++)
{
std::string s = sTextureToken;
while (s.find(sTokenName) != std::string::npos)
{
s.replace( s.find(sTokenName), strlen(sTokenName), tokens[i], 0, std::string::npos );
}
sFinalShader += s;
}
sFinalShader += sDefShader.substr( sDefShader.find(sTokenEnd) + strlen(sTokenEnd), std::string::npos );
sDefShader = sFinalShader;
}
}

int main()
{
RENDERER_SETTINGS settings;
Expand Down Expand Up @@ -116,7 +145,19 @@ int main()
}
if (!shaderInitSuccessful)
{
strncpy( szShader, Renderer::defaultShader, 65535 );
std::string sDefShader = Renderer::defaultShader;

std::vector<std::string> tokens;
for (std::map<std::string, Renderer::Texture*>::iterator it = textures.begin(); it != textures.end(); it++)
tokens.push_back(it->first);
ReplaceTokens(sDefShader, "{%textures:begin%}", "{%textures:name%}", "{%textures:end%}", tokens);

tokens.clear();
for (std::map<int,std::string>::iterator it = midiRoutes.begin(); it != midiRoutes.end(); it++)
tokens.push_back(it->second);
ReplaceTokens(sDefShader, "{%midi:begin%}", "{%midi:name%}", "{%midi:end%}", tokens);

strncpy( szShader, sDefShader.c_str(), 65535 );
if (!Renderer::ReloadShader( szShader, strlen(szShader), szError, 4096 ))
{
puts(szError);
Expand Down
32 changes: 10 additions & 22 deletions platform_sdl/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,34 +107,22 @@ namespace Renderer
char defaultShader[65536] =
"#version 430 core\n"
"\n"
"///////////////////////////////////////////////////////////////////////////////\n"
"// shader inputs/outputs\n"
"uniform float fGlobalTime; // in seconds\n"
"uniform vec2 v2Resolution; // viewport resolution (in pixels) (1080p or 720p)\n"
// "uniform mat4 iMidiPad; // 16 buttons of midi controller\n"
// "uniform float iMidiPadValue; // sum of all elements in iMidiPad/16\n"
"uniform vec2 v2Resolution; // viewport resolution (in pixels)\n"
"\n"
"// all samplers have linear filtering applied, wrapping set to repeat\n"
"uniform sampler1D texFFT; // 1024\n"
// "uniform float iFFT[8]; // latest frame\n"
// "uniform float iFFTs[8]; // smoothed latest frame\n"
// "uniform sampler2D iFFTsHistory; // smoothed fft history, 8x1024, x coord = bin, y coord n-frames earlier, y=0 is latest frame\n"
"uniform sampler1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq\n"
"{%textures:begin%}" // leave off \n here
"uniform sampler2D {%textures:name%};\n"
"{%textures:end%}" // leave off \n here
"{%midi:begin%}" // leave off \n here
"float {%midi:name%};\n"
"{%midi:end%}" // leave off \n here
"\n"
"// predefined textures\n"
"uniform sampler2D texTex1;\n"
"uniform sampler2D texTex2;\n"
"uniform sampler2D texTex3;\n"
"uniform sampler2D texTex4;\n"
"uniform sampler2D texNoise;\n"
"uniform sampler2D texChecker;\n"
"\n"
"// out_color must be written in order to see anything\n"
"layout(location = 0) out vec4 out_color;\n"
"///////////////////////////////////////////////////////////////////////////////\n"
"layout(location = 0) out vec4 out_color; // out_color must be written in order to see anything\n"
"\n"
"void main(void)\n"
"{\n"
" vec2 uv = vec2( gl_FragCoord.xy ) / v2Resolution;\n"
" vec2 uv = gl_TexCoord[0].xy;\n";
" uv -= 0.5;\n"
" uv /= vec2(v2Resolution.y / v2Resolution.x, 1);\n"
"\n"
Expand Down
21 changes: 11 additions & 10 deletions platform_w32_dx11/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ namespace Renderer
{
char * defaultShaderFilename = "shader.dx11.hlsl";
char defaultShader[65536] =
"Texture2D texFFT;\n"
"Texture2D texNoise;\n"
"Texture2D texChecker;\n"
"Texture2D texTex1;\n"
"Texture2D texTex2;\n"
"Texture2D texTex3;\n"
"Texture2D texTex4;\n"
"{%textures:begin%}" // leave off \n here
"Texture2D {%textures:name%};\n"
"{%textures:end%}" // leave off \n here
"Texture1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq\n"
"SamplerState smp;\n"
"\n"
"cbuffer constants\n"
"{\n"
" float fGlobalTime;\n"
" float2 v2Resolution;\n"
" float fGlobalTime; // in seconds\n"
" float2 v2Resolution; // viewport resolution (in pixels)\n"
"{%midi:begin%}"
" float {%midi:name%};\n"
"{%midi:end%}"
"}\n"
"\n"
"float4 main( float4 position : SV_POSITION, float2 TexCoord : TEXCOORD ) : SV_TARGET\n"
Expand Down Expand Up @@ -385,7 +385,8 @@ namespace Renderer
ID3D11InputLayout * pFullscreenQuadLayout = NULL;
ID3D11SamplerState * pFullscreenQuadSamplerState = NULL;
ID3D11Buffer * pFullscreenQuadConstantBuffer = NULL;
unsigned char pFullscreenQuadConstants[128];
#define FULLSCREENQUADCONSTANTS_SIZE 512
unsigned char pFullscreenQuadConstants[FULLSCREENQUADCONSTANTS_SIZE];
ID3D11BlendState* pFullscreenQuadBlendState = NULL;
ID3D11RasterizerState * pFullscreenQuadRasterizerState = NULL;

Expand Down
18 changes: 10 additions & 8 deletions platform_w32_dx9/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ namespace Renderer
char * defaultShaderFilename = "shader.dx9.hlsl";
char defaultShader[65536] =
"texture texTFFT; sampler1D texFFT = sampler_state { Texture = <texTFFT>; }; \n"
"texture texTNoise; sampler2D texNoise = sampler_state { Texture = <texTNoise>; };\n"
"texture texTChecker; sampler2D texChecker = sampler_state { Texture = <texTChecker>; };\n"
"texture texTTex1; sampler2D texTex1 = sampler_state { Texture = <texTTex1>; };\n"
"texture texTTex2; sampler2D texTex2 = sampler_state { Texture = <texTTex2>; };\n"
"texture texTTex3; sampler2D texTex3 = sampler_state { Texture = <texTTex3>; };\n"
"texture texTTex4; sampler2D texTex4 = sampler_state { Texture = <texTTex4>; };\n"
"// towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq\n"
"\n"
"float fGlobalTime;\n"
"float2 v2Resolution;\n"
"{%textures:begin%}" // leave off \n here
"texture raw{%textures:name%}; sampler2D {%textures:name%} = sampler_state { Texture = <raw{%textures:name%}>; };\n"
"{%textures:end%}"
"\n"
"{%midi:begin%}" // leave off \n here
"float {%midi:name%};\n"
"{%midi:end%}"
"float fGlobalTime; // in seconds\n"
"float2 v2Resolution; // viewport resolution (in pixels)\n"
"\n"
"float4 main( float2 TexCoord : TEXCOORD0 ) : COLOR0\n"
"{\n"
Expand Down

0 comments on commit e1a2ca9

Please sign in to comment.