This commit is contained in:
Bob
2010-02-16 17:11:17 +13:00
parent 3198f3419d
commit 20e586b9ad
8 changed files with 73 additions and 36 deletions

View File

@@ -48,11 +48,19 @@ namespace OpenRa.Graphics
device.Begin();
device.Clear(Color.Black);
SpriteShader.SetValue("Palette", PaletteTexture);
SpriteShader.SetValue("Scroll", scroll.X, scroll.Y);
SpriteShader.SetValue("r1", r1.X, r1.Y);
SpriteShader.SetValue("r2", r2.X, r2.Y);
SpriteShader.Commit();
SetShaderParams( SpriteShader, r1, r2, scroll );
SetShaderParams( LineShader, r1, r2, scroll );
SetShaderParams( RgbaSpriteShader, r1, r2, scroll );
SetShaderParams( WorldSpriteShader, r1, r2, scroll );
}
private void SetShaderParams( Shader s, float2 r1, float2 r2, float2 scroll )
{
s.SetValue( "Palette", PaletteTexture );
s.SetValue( "Scroll", scroll.X, scroll.Y );
s.SetValue( "r1", r1.X, r1.Y );
s.SetValue( "r2", r2.X, r2.Y );
s.Commit();
}
public void EndFrame()

View File

@@ -37,6 +37,7 @@ namespace OpenRa.Graphics
if (sprites > 0)
{
shader.Quality = quality;
shader.SetValue( "DiffuseTexture", currentSheet.Texture );
shader.Render(() =>
{
vertexBuffer.SetData(vertices);

View File

@@ -79,6 +79,7 @@ namespace OpenRa.Graphics
}
renderer.SpriteShader.Quality = ShaderQuality.Low;
renderer.SpriteShader.SetValue( "DiffuseTexture", terrainSheet.Texture );
renderer.SpriteShader.Render(() =>
renderer.DrawBatch(vertexBuffer, indexBuffer,
new Range<int>(verticesPerRow * firstRow, verticesPerRow * lastRow),

View File

@@ -50,7 +50,7 @@ namespace OpenRa.GlRenderer
Wgl.wglMakeCurrent(dc, rc);
cgContext = Cg.cgCreateContext();
//Cg.cgSetErrorCallback(CgErrorCallback);
Cg.cgSetErrorCallback(CgErrorCallback);
CgGl.cgGLRegisterStates(cgContext);
CgGl.cgGLSetManageTextureParameters(cgContext, true);
@@ -59,20 +59,22 @@ namespace OpenRa.GlRenderer
Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
CheckGlError();
Gl.glEnableClientState(Gl.GL_INDEX_ARRAY);
Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
CheckGlError();
}
void CgErrorCallback()
static Cg.CGerrorCallbackFuncDelegate CgErrorCallback = () =>
{
var err = Cg.cgGetError();
var str = Cg.cgGetErrorString(err);
var str = Cg.cgGetErrorString( err );
throw new InvalidOperationException(
string.Format("CG Error: {0}: {1}", err, str));
}
string.Format( "CG Error: {0}: {1}", err, str ) );
};
public void EnableScissor(int left, int top, int width, int height)
{
if( width < 0 ) width = 0;
if( height < 0 ) height = 0;
Gl.glScissor(left, top, width, height);
CheckGlError();
Gl.glEnable(Gl.GL_SCISSOR_TEST);
@@ -90,7 +92,7 @@ namespace OpenRa.GlRenderer
public void Clear(Color c)
{
Gl.glClearColor(1, 1, 1, 1);
Gl.glClearColor(0, 0, 0, 0);
CheckGlError();
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
CheckGlError();
@@ -110,9 +112,20 @@ namespace OpenRa.GlRenderer
public void DrawIndexedPrimitives(PrimitiveType pt, int numVerts, int numPrimitives)
{
Gl.glDrawElements((int)pt, numPrimitives, Gl.GL_UNSIGNED_SHORT, IntPtr.Zero);
Gl.glDrawElements((int)pt, numPrimitives * IndicesPerPrimitive( pt ), Gl.GL_UNSIGNED_SHORT, IntPtr.Zero);
CheckGlError();
}
static int IndicesPerPrimitive( PrimitiveType pt )
{
switch( pt )
{
case PrimitiveType.PointList: return 1;
case PrimitiveType.LineList: return 2;
case PrimitiveType.TriangleList: return 3;
}
throw new NotImplementedException();
}
}
public struct Range<T>
@@ -135,7 +148,7 @@ namespace OpenRa.GlRenderer
{
Bind();
Gl.glBufferData(Gl.GL_ARRAY_BUFFER,
new IntPtr(Marshal.SizeOf(typeof(T))), data, Gl.GL_DYNAMIC_DRAW);
new IntPtr(Marshal.SizeOf(typeof(T))*data.Length), data, Gl.GL_DYNAMIC_DRAW);
GraphicsDevice.CheckGlError();
}
@@ -209,7 +222,9 @@ namespace OpenRa.GlRenderer
public Shader(GraphicsDevice dev, Stream s)
{
this.dev = dev;
var code = new StreamReader(s).ReadToEnd();
string code;
using (var file = new StreamReader(s))
code = file.ReadToEnd();
effect = Cg.cgCreateEffect(dev.cgContext, code, null);
if (effect == IntPtr.Zero)
@@ -230,6 +245,11 @@ namespace OpenRa.GlRenderer
if (highTechnique == IntPtr.Zero && lowTechnique == IntPtr.Zero)
throw new InvalidOperationException("No valid techniques");
if( highTechnique == IntPtr.Zero )
highTechnique = lowTechnique;
if( lowTechnique == IntPtr.Zero )
lowTechnique = highTechnique;
}
public ShaderQuality Quality { get; set; }
@@ -255,13 +275,15 @@ namespace OpenRa.GlRenderer
public void SetValue(string name, Texture texture)
{
var param = Cg.cgGetNamedEffectParameter(effect, name);
CgGl.cgGLSetupSampler(param, texture.texture);
var param = Cg.cgGetNamedEffectParameter( effect, name );
if( param != IntPtr.Zero && texture != null )
CgGl.cgGLSetupSampler( param, texture.texture );
}
public void SetValue(string name, float x, float y)
{
var param = Cg.cgGetNamedEffectParameter(effect, name);
if( param != IntPtr.Zero )
CgGl.cgGLSetParameter2f(param, x, y);
}
@@ -281,6 +303,9 @@ namespace OpenRa.GlRenderer
public void SetData(Bitmap bitmap)
{
Gl.glBindTexture( Gl.GL_TEXTURE_2D, texture );
GraphicsDevice.CheckGlError();
var bits = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly,
@@ -291,7 +316,7 @@ namespace OpenRa.GlRenderer
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAX_LEVEL, 0);
GraphicsDevice.CheckGlError();
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, bits.Width, bits.Height,
0, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, bits.Scan0); // todo: weird strides
0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, bits.Scan0); // todo: weird strides
GraphicsDevice.CheckGlError();
bitmap.UnlockBits(bits);

View File

@@ -32,9 +32,8 @@ VertexOut Simple_vp(VertexIn v) {
}
float4 Simple_fp(FragmentIn f) : COLOR0 {
return float4( 1,0,0,1 );
// float4 r = tex2D(DiffuseTexture, f.Tex0);
// return r;
float4 r = tex2D(DiffuseTexture, f.Tex0);
return r;
}
technique high_quality {
@@ -44,8 +43,8 @@ technique high_quality {
// CullFace = NONE;
// CullMode = None;
// FillMode = Solid;
VertexProgram = compile arbvp1 Simple_vp();
FragmentProgram = compile arbfp1 Simple_fp();
VertexProgram = compile latest Simple_vp();
FragmentProgram = compile latest Simple_fp();
//SrcBlend = SrcAlpha;
//DestBlend = InvSrcAlpha;

View File

@@ -60,7 +60,7 @@ technique low_quality {
BlendEnable = false;
DepthTestEnable = false;
CullFaceEnable = false;
VertexProgram = compile arbvp1 Simple_vp();
FragmentProgram = compile arbfp1 Palette_fp();
VertexProgram = compile latest Simple_vp();
FragmentProgram = compile latest Palette_fp();
}
}

View File

@@ -34,8 +34,8 @@ technique high_quality {
DepthTestEnable = false;
//CullMode = None;
//FillMode = Wireframe;
VertexProgram = compile arbvp1 Simple_vp();
FragmentProgram = compile arbfp1 Simple_fp();
VertexProgram = compile latest Simple_vp();
FragmentProgram = compile latest Simple_fp();
//SrcBlend = SrcAlpha;
//DestBlend = InvSrcAlpha;

View File

@@ -57,10 +57,13 @@ float4 Palette_fp(VertexOut f) : COLOR0 {
technique low_quality {
pass p0 {
BlendEnable = false;
BlendEnable = true;
DepthTestEnable = false;
CullFaceEnable = false;
VertexProgram = compile arbvp1 Simple_vp();
FragmentProgram = compile arbfp1 Palette_fp();
VertexProgram = compile latest Simple_vp();
FragmentProgram = compile latest Palette_fp();
BlendEquation = FuncAdd;
BlendFunc = int2( SrcAlpha, OneMinusSrcAlpha );
}
}