Commit e1a2ca90 authored by Gargaj's avatar Gargaj
Browse files

add code for substitute-tokens to automatically insert incoming textures to default shader

parent 5bbd67d0
...@@ -10,6 +10,35 @@ ...@@ -10,6 +10,35 @@
#include "external/scintilla/src/UniConversion.h" #include "external/scintilla/src/UniConversion.h"
#include "external/jsonxx/jsonxx.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() int main()
{ {
RENDERER_SETTINGS settings; RENDERER_SETTINGS settings;
...@@ -116,7 +145,19 @@ int main() ...@@ -116,7 +145,19 @@ int main()
} }
if (!shaderInitSuccessful) 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 )) if (!Renderer::ReloadShader( szShader, strlen(szShader), szError, 4096 ))
{ {
puts(szError); puts(szError);
......
...@@ -107,34 +107,22 @@ namespace Renderer ...@@ -107,34 +107,22 @@ namespace Renderer
char defaultShader[65536] = char defaultShader[65536] =
"#version 430 core\n" "#version 430 core\n"
"\n" "\n"
"///////////////////////////////////////////////////////////////////////////////\n"
"// shader inputs/outputs\n"
"uniform float fGlobalTime; // in seconds\n" "uniform float fGlobalTime; // in seconds\n"
"uniform vec2 v2Resolution; // viewport resolution (in pixels) (1080p or 720p)\n" "uniform vec2 v2Resolution; // viewport resolution (in pixels)\n"
// "uniform mat4 iMidiPad; // 16 buttons of midi controller\n"
// "uniform float iMidiPadValue; // sum of all elements in iMidiPad/16\n"
"\n" "\n"
"// all samplers have linear filtering applied, wrapping set to repeat\n" "uniform sampler1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq\n"
"uniform sampler1D texFFT; // 1024\n" "{%textures:begin%}" // leave off \n here
// "uniform float iFFT[8]; // latest frame\n" "uniform sampler2D {%textures:name%};\n"
// "uniform float iFFTs[8]; // smoothed latest frame\n" "{%textures:end%}" // leave off \n here
// "uniform sampler2D iFFTsHistory; // smoothed fft history, 8x1024, x coord = bin, y coord n-frames earlier, y=0 is latest frame\n" "{%midi:begin%}" // leave off \n here
"float {%midi:name%};\n"
"{%midi:end%}" // leave off \n here
"\n" "\n"
"// predefined textures\n" "layout(location = 0) out vec4 out_color; // out_color must be written in order to see anything\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"
"\n" "\n"
"void main(void)\n" "void main(void)\n"
"{\n" "{\n"
" vec2 uv = vec2( gl_FragCoord.xy ) / v2Resolution;\n" " vec2 uv = gl_TexCoord[0].xy;\n";
" uv -= 0.5;\n" " uv -= 0.5;\n"
" uv /= vec2(v2Resolution.y / v2Resolution.x, 1);\n" " uv /= vec2(v2Resolution.y / v2Resolution.x, 1);\n"
"\n" "\n"
......
...@@ -62,19 +62,19 @@ namespace Renderer ...@@ -62,19 +62,19 @@ namespace Renderer
{ {
char * defaultShaderFilename = "shader.dx11.hlsl"; char * defaultShaderFilename = "shader.dx11.hlsl";
char defaultShader[65536] = char defaultShader[65536] =
"Texture2D texFFT;\n" "{%textures:begin%}" // leave off \n here
"Texture2D texNoise;\n" "Texture2D {%textures:name%};\n"
"Texture2D texChecker;\n" "{%textures:end%}" // leave off \n here
"Texture2D texTex1;\n" "Texture1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq\n"
"Texture2D texTex2;\n"
"Texture2D texTex3;\n"
"Texture2D texTex4;\n"
"SamplerState smp;\n" "SamplerState smp;\n"
"\n" "\n"
"cbuffer constants\n" "cbuffer constants\n"
"{\n" "{\n"
" float fGlobalTime;\n" " float fGlobalTime; // in seconds\n"
" float2 v2Resolution;\n" " float2 v2Resolution; // viewport resolution (in pixels)\n"
"{%midi:begin%}"
" float {%midi:name%};\n"
"{%midi:end%}"
"}\n" "}\n"
"\n" "\n"
"float4 main( float4 position : SV_POSITION, float2 TexCoord : TEXCOORD ) : SV_TARGET\n" "float4 main( float4 position : SV_POSITION, float2 TexCoord : TEXCOORD ) : SV_TARGET\n"
...@@ -385,7 +385,8 @@ namespace Renderer ...@@ -385,7 +385,8 @@ namespace Renderer
ID3D11InputLayout * pFullscreenQuadLayout = NULL; ID3D11InputLayout * pFullscreenQuadLayout = NULL;
ID3D11SamplerState * pFullscreenQuadSamplerState = NULL; ID3D11SamplerState * pFullscreenQuadSamplerState = NULL;
ID3D11Buffer * pFullscreenQuadConstantBuffer = NULL; ID3D11Buffer * pFullscreenQuadConstantBuffer = NULL;
unsigned char pFullscreenQuadConstants[128]; #define FULLSCREENQUADCONSTANTS_SIZE 512
unsigned char pFullscreenQuadConstants[FULLSCREENQUADCONSTANTS_SIZE];
ID3D11BlendState* pFullscreenQuadBlendState = NULL; ID3D11BlendState* pFullscreenQuadBlendState = NULL;
ID3D11RasterizerState * pFullscreenQuadRasterizerState = NULL; ID3D11RasterizerState * pFullscreenQuadRasterizerState = NULL;
......
...@@ -60,15 +60,17 @@ namespace Renderer ...@@ -60,15 +60,17 @@ namespace Renderer
char * defaultShaderFilename = "shader.dx9.hlsl"; char * defaultShaderFilename = "shader.dx9.hlsl";
char defaultShader[65536] = char defaultShader[65536] =
"texture texTFFT; sampler1D texFFT = sampler_state { Texture = <texTFFT>; }; \n" "texture texTFFT; sampler1D texFFT = sampler_state { Texture = <texTFFT>; }; \n"
"texture texTNoise; sampler2D texNoise = sampler_state { Texture = <texTNoise>; };\n" "// towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq\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"
"\n" "\n"
"float fGlobalTime;\n" "{%textures:begin%}" // leave off \n here
"float2 v2Resolution;\n" "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" "\n"
"float4 main( float2 TexCoord : TEXCOORD0 ) : COLOR0\n" "float4 main( float2 TexCoord : TEXCOORD0 ) : COLOR0\n"
"{\n" "{\n"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment