diff --git a/src/projectM-engine/Renderer.cpp b/src/projectM-engine/Renderer.cpp index 7bf8cddca..4de7c6b2a 100644 --- a/src/projectM-engine/Renderer.cpp +++ b/src/projectM-engine/Renderer.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "omptl/omptl" #include "omptl/omptl_algorithm" @@ -20,6 +21,10 @@ Renderer::Renderer(int width, int height, int gx, int gy, int texsize, BeatDetec { int x; int y; +#ifdef USE_CG + this->compositeShadersEnabled = false; + this->warpShadersEnabled = false; +#endif this->totalframes = 1; this->noSwitch = false; this->showfps = false; @@ -106,17 +111,100 @@ SetupCg(); #endif } +void Renderer::SetPipeline(Pipeline &pipeline) +{ #ifdef USE_CG + + + + if(warpShadersEnabled) cgDestroyProgram(myCgWarpProgram); + if(compositeShadersEnabled) cgDestroyProgram(myCgCompositeProgram); + + warpShadersEnabled = LoadCgProgram(pipeline.shader.warp, myCgWarpProgram); + compositeShadersEnabled = LoadCgProgram(pipeline.shader.composite, myCgCompositeProgram); + + pipeline.shader.warp.clear(); + pipeline.shader.composite.clear(); + +#endif +} + +#ifdef USE_CG + + +bool Renderer::LoadCgProgram(std::string program, CGprogram &p) +{ + //if (p != NULL) cgDestroyProgram(p); + //p = NULL; + + if (program.length() > 0) + { + + std::string temp; + + temp.append(cgTemplate); + temp.append(program.substr(11)); + temp.append("OUT.color.xyz = ret;return OUT;}"); + + std::cout<<"Cg: Compilation Results:"<texsize, renderTarget->texsize, 1/(float)renderTarget->texsize,1/(float)renderTarget->texsize); cgGLSetParameter4f(cgGetNamedParameter(program, "aspect"), aspect,1,1/aspect,1); - - } #endif @@ -269,9 +341,10 @@ void Renderer::FinishPass1() glPopMatrix(); renderTarget->unlock(); + } -void Renderer::Pass2(const Pipeline *pipeline) +void Renderer::Pass2(const Pipeline *pipeline, const PipelineContext &pipelineContext) { //BEGIN PASS 2 // @@ -305,7 +378,7 @@ void Renderer::Pass2(const Pipeline *pipeline) glLineWidth( this->renderTarget->texsize < 512 ? 1 : this->renderTarget->texsize/512.0); - CompositeOutput(pipeline); + CompositeOutput(pipeline, pipelineContext); glMatrixMode(GL_MODELVIEW); @@ -333,47 +406,66 @@ void Renderer::RenderFrame(const Pipeline* pipeline, const PipelineContext &pipe SetupPass1(pipeline, pipelineContext); #ifdef USE_CG - cgGLBindProgram(myCgWarpProgram); - checkForCgError("binding warp program"); + if (warpShadersEnabled) + { cgGLEnableProfile(myCgProfile); checkForCgError("enabling warp profile"); + cgGLBindProgram(myCgWarpProgram); + checkForCgError("binding warp program"); + + + + SetupCgVariables(myCgWarpProgram, pipelineContext); + } + + #endif Interpolation(pipeline); #ifdef USE_CG + if (warpShadersEnabled) + { cgGLUnbindProgram(myCgProfile); checkForCgError("disabling fragment profile"); cgGLDisableProfile(myCgProfile); checkForCgError("disabling fragment profile"); + } #endif RenderItems(pipeline,pipelineContext); FinishPass1(); - Pass2(pipeline); + Pass2(pipeline, pipelineContext); } void Renderer::RenderFrame(PresetOutputs *presetOutputs, PresetInputs *presetInputs) { SetupPass1(presetOutputs, *presetInputs); #ifdef USE_CG + if (warpShadersEnabled) + { + cgGLEnableProfile(myCgProfile); + checkForCgError("enabling warp profile"); cgGLBindProgram(myCgWarpProgram); checkForCgError("binding warp program"); - cgGLEnableProfile(myCgProfile); - checkForCgError("enabling warp profile"); + SetupCgVariables(myCgWarpProgram, *presetInputs); + } #endif Interpolation(presetOutputs, presetInputs); #ifdef USE_CG + if (warpShadersEnabled) + { cgGLUnbindProgram(myCgProfile); checkForCgError("disabling fragment profile"); cgGLDisableProfile(myCgProfile); checkForCgError("disabling fragment profile"); + } #endif RenderItems(presetOutputs, *presetInputs); FinishPass1(); - Pass2(presetOutputs); + Pass2(presetOutputs, *presetInputs); } @@ -928,26 +1020,38 @@ void Renderer::draw_stats() glRasterPos2f(0, -.09+offset); - other_font->FaceSize((unsigned)(18*(this->vh/512.0))); + other_font->FaceSize((unsigned)(18*(vh/512.0))); - sprintf( buffer, " texsize: %d", this->renderTarget->texsize); + sprintf( buffer, " texsize: %d", renderTarget->texsize); other_font->Render(buffer); glRasterPos2f(0, -.13+offset); - sprintf( buffer, "viewport: %d x %d", this->vw, this->vh); + sprintf( buffer, " viewport: %d x %d", vw, vh); other_font->Render(buffer); glRasterPos2f(0, -.17+offset); - other_font->Render((this->renderTarget->useFBO ? " FBO: on" : " FBO: off")); + other_font->Render((renderTarget->useFBO ? " FBO: on" : " FBO: off")); glRasterPos2f(0, -.21+offset); - sprintf( buffer, " mesh: %d x %d", mesh.width, mesh.height); + sprintf( buffer, " mesh: %d x %d", mesh.width, mesh.height); other_font->Render(buffer); glRasterPos2f(0, -.25+offset); - sprintf( buffer, "textures: %.1fkB", textureManager->getTextureMemorySize() /1000.0f); + sprintf( buffer, " textures: %.1fkB", textureManager->getTextureMemorySize() /1000.0f); + other_font->Render(buffer); +#ifdef USE_CG + glRasterPos2f(0, -.29+offset); + sprintf( buffer, "shader profile: %s", cgGetProfileString(myCgProfile)); other_font->Render(buffer); + glRasterPos2f(0, -.33+offset); + sprintf( buffer, " warp shader: %s", warpShadersEnabled ? "on" : "off"); + other_font->Render(buffer); + + glRasterPos2f(0, -.37+offset); + sprintf( buffer, " comp shader: %s", compositeShadersEnabled ? "on" : "off"); + other_font->Render(buffer); +#endif glPopMatrix(); // glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); @@ -977,7 +1081,7 @@ void Renderer::draw_fps( float realfps ) -void Renderer::CompositeOutput(const Pipeline* pipeline) +void Renderer::CompositeOutput(const Pipeline* pipeline, const PipelineContext &pipelineContext) { @@ -994,11 +1098,17 @@ void Renderer::CompositeOutput(const Pipeline* pipeline) glEnable(GL_TEXTURE_2D); #ifdef USE_CG - cgGLBindProgram(myCgCompositeProgram); - checkForCgError("binding warp program"); + if (compositeShadersEnabled) + { cgGLEnableProfile(myCgProfile); checkForCgError("enabling warp profile"); + + cgGLBindProgram(myCgCompositeProgram); + checkForCgError("binding warp program"); + + SetupCgVariables(myCgCompositeProgram, pipelineContext); + } #endif @@ -1027,10 +1137,13 @@ void Renderer::CompositeOutput(const Pipeline* pipeline) glDisableClientState(GL_TEXTURE_COORD_ARRAY); #ifdef USE_CG + if (compositeShadersEnabled) + { cgGLUnbindProgram(myCgProfile); checkForCgError("disabling fragment profile"); cgGLDisableProfile(myCgProfile); checkForCgError("disabling fragment profile"); + } #endif for (std::vector::const_iterator pos = pipeline->compositeDrawables.begin(); pos != pipeline->compositeDrawables.end(); ++pos) diff --git a/src/projectM-engine/Renderer.hpp b/src/projectM-engine/Renderer.hpp index bc82f8c66..1356830f5 100644 --- a/src/projectM-engine/Renderer.hpp +++ b/src/projectM-engine/Renderer.hpp @@ -77,6 +77,7 @@ public: GLuint initRenderToTexture(); void PerPixelMath(PresetOutputs *presetOutputs, PresetInputs *presetInputs); + void SetPipeline(Pipeline &pipeline); void setPresetName(const std::string& theValue) { @@ -118,21 +119,21 @@ private: #ifdef USE_CG - CGcontext myCgContext; - CGprofile myCgProfile; + std::string cgTemplate; - CGprogram myCgWarpProgram, + bool warpShadersEnabled; + bool compositeShadersEnabled; + + CGcontext myCgContext; + CGprofile myCgProfile; + CGprogram myCgWarpProgram, myCgCompositeProgram; - - std::string warpProgram; - std::string compositeProgram; - std::string shaderFile; - - + bool LoadCgProgram(std::string program, CGprogram &p); + bool checkForCgCompileError(const char *situation); void checkForCgError(const char *situation); void SetupCg(); - void SetupCgVariables(CGprogram program, const PipelineContext &context); + void SetupCgVariables(CGprogram program, const PipelineContext &pipelineContext); #endif #ifdef USE_FTGL @@ -144,11 +145,12 @@ private: void SetupPass1(const Pipeline* pipeline, const PipelineContext &pipelineContext); + void SetupShaders(const Pipeline* pipeline, const PipelineContext &pipelineContext); void Interpolation(const Pipeline* pipeline); void RenderItems(const Pipeline* pipeline, const PipelineContext &pipelineContext); - void CompositeOutput(const Pipeline* pipeline); + void CompositeOutput(const Pipeline* pipeline, const PipelineContext &pipelineContext); void FinishPass1(); - void Pass2 (const Pipeline* pipeline); + void Pass2 (const Pipeline* pipeline, const PipelineContext &pipelineContext); inline static Point PerPixel(Point p, PerPixelContext &context) { diff --git a/src/projectM-engine/projectM.cg b/src/projectM-engine/projectM.cg new file mode 100644 index 000000000..bf3039123 --- /dev/null +++ b/src/projectM-engine/projectM.cg @@ -0,0 +1,85 @@ +#define M_PI 3.14159265359 +#define M_PI_2 6.28318530718 +#define M_INV_PI_2 0.159154943091895 + +#define lum(x) (dot(x,float3(0.32,0.49,0.29))) +#define tex2d tex2D +#define tex3d tex3D + + +#define getrad sqrt((uv.x-0.5)*2*(uv.x-0.5)*2+(uv.y-0.5)*2*(uv.y-0.5)*2)*.7071067 +#define getang atan2(((uv.y-0.5)*2),((uv.x-0.5)*2)) + +sampler2D sampler_main; + +#define GetBlur2 GetBlur1 +#define GetBlur3 GetBlur1 + +#define GetMain(uv) (tex2D(sampler_main,uv).xyz) +#define GetPixel(uv) (tex2D(sampler_main,uv).xyz) + +#define uv_orig uv + +#define sampler_pw_main sampler_main +#define sampler_pc_main sampler_main +#define sampler_fw_main sampler_main +#define sampler_fc_main sampler_main + +#define sampler_noise_lq sampler_main +#define sampler_noise_lq_lite sampler_main +#define sampler_noise_mq sampler_main +#define sampler_noise_hq sampler_main +#define sampler_noisevol_lq sampler_main +#define sampler_noisevol_hq sampler_main +#define texsize_noise_lq texsize +#define texsize_noise_lq_lite texsize +#define texsize_noise_mq texsize +#define texsize_noise_hq texsize +#define texsize_noisevol_lq texsize +#define texsize_noisevol_hq texsize + + +struct outtype {float4 color : COLOR;}; + + +//float3 lum(float3 a){return dot(a, float3(0.32,0.49,0.29));} + +float3 GetBlur1(float2 uv) +{ +float3 ret = tex2D(sampler_main, uv).xyz; +ret += tex2D(sampler_main, float2(uv.x-.001, uv.y)).xyz; +ret += tex2D(sampler_main, float2(uv.x+.001, uv.y)).xyz; +ret += tex2D(sampler_main, float2(uv.x, uv.y + .001)).xyz; +ret += tex2D(sampler_main, float2(uv.x, uv.y - .001)).xyz; + +return ret * 0.2; +} + +outtype projectm (float2 uv : TEXCOORD0, + uniform sampler2D sampler_arg : TEX0, + uniform float time, + uniform float4 rand_preset, + uniform float4 rand_frame, + uniform float progress, + uniform float frame, + uniform float fps, + uniform float bass, + uniform float mid, + uniform float treb, + uniform float vol, + uniform float bass_att, + uniform float mid_att, + uniform float treb_att, + uniform float vol_att, + uniform float4 texsize, + uniform float4 aspect + ) +{ + outtype OUT; + OUT.color.w=1; + float3 ret; + + sampler_main = sampler_arg; + + float rad = getrad; + float ang = getang;