From 9566385aac8bdc298f5afb9e0809a8e98483f89d Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 2 Mar 2013 10:36:58 +1300 Subject: [PATCH] Add renderer support for additional vec* uniforms. --- .../Graphics/IGraphicsDevice.cs | 2 ++ OpenRA.Renderer.Cg/Shader.cs | 23 +++++++++++++++++ OpenRA.Renderer.Gl/Shader.cs | 25 +++++++++++++++++++ OpenRA.Renderer.Null/NullGraphicsDevice.cs | 2 ++ 4 files changed, 52 insertions(+) diff --git a/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs b/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs index 620f633011..bb57b64c1c 100755 --- a/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs +++ b/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs @@ -60,7 +60,9 @@ namespace OpenRA.FileFormats.Graphics public interface IShader { + void SetVec(string name, float x); void SetVec(string name, float x, float y); + void SetVec(string name, float[] vec, int length); void SetTexture(string param, ITexture texture); void SetMatrix(string param, float[] mtx); void Render(Action a); diff --git a/OpenRA.Renderer.Cg/Shader.cs b/OpenRA.Renderer.Cg/Shader.cs index 88b341adce..9ef2c1d3c3 100644 --- a/OpenRA.Renderer.Cg/Shader.cs +++ b/OpenRA.Renderer.Cg/Shader.cs @@ -75,6 +75,13 @@ namespace OpenRA.Renderer.Cg Tao.Cg.CgGl.cgGLSetupSampler(param, texture.texture); } + public void SetVec(string name, float x) + { + var param = Tao.Cg.Cg.cgGetNamedEffectParameter(effect, name); + if (param != IntPtr.Zero) + Tao.Cg.CgGl.cgGLSetParameter1f(param, x); + } + public void SetVec(string name, float x, float y) { var param = Tao.Cg.Cg.cgGetNamedEffectParameter(effect, name); @@ -82,6 +89,22 @@ namespace OpenRA.Renderer.Cg Tao.Cg.CgGl.cgGLSetParameter2f(param, x, y); } + public void SetVec(string name, float[] vec, int length) + { + var param = Tao.Cg.Cg.cgGetNamedEffectParameter(effect, name); + if (param == IntPtr.Zero) + return; + + switch(length) + { + case 1: Tao.Cg.CgGl.cgGLSetParameter1fv(param, vec); break; + case 2: Tao.Cg.CgGl.cgGLSetParameter2fv(param, vec); break; + case 3: Tao.Cg.CgGl.cgGLSetParameter3fv(param, vec); break; + case 4: Tao.Cg.CgGl.cgGLSetParameter4fv(param, vec); break; + default: throw new InvalidDataException("Invalid vector length"); + } + } + public void SetMatrix(string name, float[] mtx) { if (mtx.Length != 16) diff --git a/OpenRA.Renderer.Gl/Shader.cs b/OpenRA.Renderer.Gl/Shader.cs index 9df56e1504..1bc9427f1d 100644 --- a/OpenRA.Renderer.Gl/Shader.cs +++ b/OpenRA.Renderer.Gl/Shader.cs @@ -141,6 +141,16 @@ namespace OpenRA.Renderer.Glsl textures[texUnit] = t; } + public void SetVec(string name, float x) + { + Gl.glUseProgramObjectARB(program); + ErrorHandler.CheckGlError(); + int param = Gl.glGetUniformLocationARB(program, name); + ErrorHandler.CheckGlError(); + Gl.glUniform1fARB(param,x); + ErrorHandler.CheckGlError(); + } + public void SetVec(string name, float x, float y) { Gl.glUseProgramObjectARB(program); @@ -151,6 +161,21 @@ namespace OpenRA.Renderer.Glsl ErrorHandler.CheckGlError(); } + public void SetVec(string name, float[] vec, int length) + { + int param = Gl.glGetUniformLocationARB(program, name); + ErrorHandler.CheckGlError(); + switch(length) + { + case 1: Gl.glUniform1fv(param, 1, vec); break; + case 2: Gl.glUniform2fv(param, 1, vec); break; + case 3: Gl.glUniform3fv(param, 1, vec); break; + case 4: Gl.glUniform4fv(param, 1, vec); break; + default: throw new InvalidDataException("Invalid vector length"); + } + ErrorHandler.CheckGlError(); + } + public void SetMatrix(string name, float[] mtx) { if (mtx.Length != 16) diff --git a/OpenRA.Renderer.Null/NullGraphicsDevice.cs b/OpenRA.Renderer.Null/NullGraphicsDevice.cs index 79497ead37..a98c6db52c 100644 --- a/OpenRA.Renderer.Null/NullGraphicsDevice.cs +++ b/OpenRA.Renderer.Null/NullGraphicsDevice.cs @@ -58,7 +58,9 @@ namespace OpenRA.Renderer.Null public class NullShader : IShader { + public void SetVec(string name, float x) { } public void SetVec(string name, float x, float y) { } + public void SetVec(string name, float[] vec, int length) { } public void SetTexture(string param, ITexture texture) { } public void SetMatrix(string param, float[] mtx) { } public void Commit() { }