private void RenderCubeMapFace(OpsContext context, CubeTexture newTexture, Surface rt, CubeMapFace face, int mip, int size, bool SRGB) { context.Device.SetRenderTarget(0, rt); ShadeVertex[] vb = new ShadeVertex[] { ShadeVertex.ForCube(-1.0f, -1.0f, size, face), ShadeVertex.ForCube(1.0f, -1.0f, size, face), ShadeVertex.ForCube(-1.0f, 1.0f, size, face), ShadeVertex.ForCube(1.0f, 1.0f, size, face), }; context.Device.BeginScene(); context.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vb); context.Device.EndScene(); context.Device.SetRenderTarget(0, context.Device.GetBackBuffer(0, 0, BackBufferType.Mono)); SurfaceLoader.FromSurface(newTexture.GetCubeMapSurface(face, mip), rt, Filter.None | (SRGB?Filter.SrgbOut:0), 0); }
public static ShadeVertex ForTexture( float ptX, float ptY, int width, int height)//texel size { ShadeVertex v = new ShadeVertex(); float tzX = 1.0f / (float)width; float tzY = 1.0f / (float)height; v.PtX = ptX - tzX * 0.5f; v.PtY = ptY + tzY * 0.5f; v.TcX = (1.0f + ptX) * 0.5f; v.TcY = (1.0f + ptY) * 0.5f; v.TcZ = 0; v.TzX = tzX; v.TzY = tzY; v.TzZ = 0; return(v); }
public static ShadeVertex ForVolume( float ptX, float ptY, int slice, int width, int height, int depth)//texel size { ShadeVertex v = new ShadeVertex(); float tzX = 1.0f / (float)width; float tzY = 1.0f / (float)height; float tzZ = 1.0f / (float)depth; v.PtX = ptX - tzX * 0.5f; v.PtY = ptY + tzY * 0.5f; v.TcX = (1.0f + ptX) * 0.5f; v.TcY = (1.0f + ptY) * 0.5f; v.TcZ = (tzZ * slice) + (tzZ + 0.5f); v.TzX = tzX; v.TzY = tzY; v.TzZ = tzZ; return(v); }
public void Run(OpsContext context, OpsStatement statement) { ShadeArgs args = statement.Arguments as ShadeArgs; ConstantTable constantTable = SetupDevice(context, args); EffectHandle hTarget = constantTable.GetConstant(null, "Target"); ArrayList containers = statement.GetContent(context); OpsConsole.WriteLine("Shading textures with \"{0}\"", args.File); foreach (OpsTexture container in containers) { if (hTarget != null) { context.Device.SetTexture( constantTable.GetSamplerIndex(hTarget), container.Texture); } if (container.Texture is Texture) { Texture oldTexture = container.Texture as Texture; Texture newTexture = OpsTextureHelper.CloneTexture(oldTexture, Usage.None, Pool.Managed); for (int mip = 0; mip < oldTexture.LevelCount; mip++) { SurfaceDescription sd = oldTexture.GetLevelDescription(mip); CheckFormatValid(sd.Format); Surface rt = context.Device.CreateRenderTarget(sd.Width, sd.Height, sd.Format, MultiSampleType.None, 0, true); context.Device.SetRenderTarget(0, rt); ShadeVertex[] vb = new ShadeVertex[] { ShadeVertex.ForTexture(-1.0f, -1.0f, sd.Width, sd.Height), ShadeVertex.ForTexture(1.0f, -1.0f, sd.Width, sd.Height), ShadeVertex.ForTexture(-1.0f, 1.0f, sd.Width, sd.Height), ShadeVertex.ForTexture(1.0f, 1.0f, sd.Width, sd.Height), }; context.Device.BeginScene(); context.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vb); context.Device.EndScene(); context.Device.SetRenderTarget(0, context.Device.GetBackBuffer(0, 0, BackBufferType.Mono)); SurfaceLoader.FromSurface(newTexture.GetSurfaceLevel(mip), rt, Filter.None | (container.SRGB?Filter.SrgbOut:0), 0); } oldTexture.Dispose(); container.Texture = newTexture; } else if (container.Texture is VolumeTexture) { VolumeTexture oldTexture = container.Texture as VolumeTexture; VolumeTexture newTexture = OpsTextureHelper.CloneVolume(oldTexture, Usage.None, Pool.Managed); for (int mip = 0; mip < oldTexture.LevelCount; mip++) { VolumeDescription vd = oldTexture.GetLevelDescription(mip); CheckFormatValid(vd.Format); Surface sliceRT = context.Device.CreateRenderTarget(vd.Width, vd.Height, vd.Format, MultiSampleType.None, 0, true); for (int slice = 0; slice < vd.Depth; slice++) { context.Device.SetRenderTarget(0, sliceRT); ShadeVertex[] vb = new ShadeVertex[] { ShadeVertex.ForVolume(-1.0f, -1.0f, slice, vd.Width, vd.Height, vd.Depth), ShadeVertex.ForVolume(1.0f, -1.0f, slice, vd.Width, vd.Height, vd.Depth), ShadeVertex.ForVolume(-1.0f, 1.0f, slice, vd.Width, vd.Height, vd.Depth), ShadeVertex.ForVolume(1.0f, 1.0f, slice, vd.Width, vd.Height, vd.Depth), }; context.Device.BeginScene(); context.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vb); context.Device.EndScene(); context.Device.SetRenderTarget(0, context.Device.GetBackBuffer(0, 0, BackBufferType.Mono)); OpsTextureHelper.LoadVolumeSliceFromSurface(newTexture, mip, slice, Filter.None | (container.SRGB?Filter.SrgbOut:0), sliceRT); } sliceRT.Dispose(); } oldTexture.Dispose(); container.Texture = newTexture; } else if (container.Texture is CubeTexture) { CubeTexture oldTexture = container.Texture as CubeTexture; CubeTexture newTexture = OpsTextureHelper.CloneCube(oldTexture, Usage.None, Pool.Managed); for (int mip = 0; mip < oldTexture.LevelCount; mip++) { SurfaceDescription sd = oldTexture.GetLevelDescription(mip); CheckFormatValid(sd.Format); Surface rt = context.Device.CreateRenderTarget(sd.Width, sd.Height, sd.Format, MultiSampleType.None, 0, true); RenderCubeMapFace(context, newTexture, rt, CubeMapFace.PositiveX, mip, sd.Width, container.SRGB); RenderCubeMapFace(context, newTexture, rt, CubeMapFace.PositiveY, mip, sd.Width, container.SRGB); RenderCubeMapFace(context, newTexture, rt, CubeMapFace.PositiveZ, mip, sd.Width, container.SRGB); RenderCubeMapFace(context, newTexture, rt, CubeMapFace.NegativeX, mip, sd.Width, container.SRGB); RenderCubeMapFace(context, newTexture, rt, CubeMapFace.NegativeY, mip, sd.Width, container.SRGB); RenderCubeMapFace(context, newTexture, rt, CubeMapFace.NegativeZ, mip, sd.Width, container.SRGB); } oldTexture.Dispose(); container.Texture = newTexture; } } }
public static ShadeVertex ForCube( float ptX, float ptY, int surfaceSize, CubeMapFace face) { ShadeVertex v = new ShadeVertex(); float pixelSize = 2.0f / (float)surfaceSize; v.PtX = ptX - pixelSize * 0.5f; v.PtY = ptY + pixelSize * 0.5f; float nX = ptX; float nY = ptY; switch (face) { case CubeMapFace.PositiveX: v.TzX = 0; v.TzY = pixelSize; v.TzZ = pixelSize; v.TcX = 1.0f; v.TcY = nY; v.TcZ = -nX; break; case CubeMapFace.NegativeX: v.TzX = 0; v.TzY = pixelSize; v.TzZ = pixelSize; v.TcX = -1.0f; v.TcY = nY; v.TcZ = nX; break; case CubeMapFace.PositiveY: v.TzX = pixelSize; v.TzY = 0; v.TzZ = pixelSize; v.TcX = nX; v.TcY = 1.0f; v.TcZ = -nY; break; case CubeMapFace.NegativeY: v.TzX = pixelSize; v.TzY = 0; v.TzZ = pixelSize; v.TcX = nX; v.TcY = -1.0f; v.TcZ = nY; break; case CubeMapFace.PositiveZ: v.TzX = pixelSize; v.TzY = pixelSize; v.TzZ = 0; v.TcX = nX; v.TcY = nY; v.TcZ = 1.0f; break; case CubeMapFace.NegativeZ: v.TzX = pixelSize; v.TzY = pixelSize; v.TzZ = 0; v.TcX = -nX; v.TcY = nY; v.TcZ = -1.0f; break; default: throw new OpsException("Unknown Face Type"); } return(v); }
public void Run(OpsContext context, OpsStatement statement) { ShadeArgs args = statement.Arguments as ShadeArgs; ConstantTable constantTable = SetupDevice(context, args); EffectHandle hTarget = constantTable.GetConstant(null, "Target"); ArrayList containers = statement.GetContent(context); OpsConsole.WriteLine( "Shading textures with \"{0}\"", args.File); foreach(OpsTexture container in containers) { if(hTarget != null) { context.Device.SetTexture( constantTable.GetSamplerIndex(hTarget), container.Texture); } if(container.Texture is Texture) { Texture oldTexture = container.Texture as Texture; Texture newTexture = OpsTextureHelper.CloneTexture(oldTexture, Usage.None, Pool.Managed); for( int mip = 0; mip < oldTexture.LevelCount; mip++) { SurfaceDescription sd = oldTexture.GetLevelDescription(mip); CheckFormatValid(sd.Format); Surface rt = context.Device.CreateRenderTarget(sd.Width, sd.Height, sd.Format, MultiSampleType.None, 0, true); context.Device.SetRenderTarget(0, rt); ShadeVertex[] vb = new ShadeVertex[] { ShadeVertex.ForTexture(-1.0f, -1.0f, sd.Width, sd.Height), ShadeVertex.ForTexture( 1.0f, -1.0f, sd.Width, sd.Height), ShadeVertex.ForTexture(-1.0f, 1.0f, sd.Width, sd.Height), ShadeVertex.ForTexture( 1.0f, 1.0f, sd.Width, sd.Height), }; context.Device.BeginScene(); context.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vb); context.Device.EndScene(); context.Device.SetRenderTarget(0, context.Device.GetBackBuffer(0, 0, BackBufferType.Mono) ); SurfaceLoader.FromSurface(newTexture.GetSurfaceLevel(mip), rt, Filter.None| (container.SRGB?Filter.SrgbOut:0), 0); } oldTexture.Dispose(); container.Texture = newTexture; } else if(container.Texture is VolumeTexture) { VolumeTexture oldTexture = container.Texture as VolumeTexture; VolumeTexture newTexture = OpsTextureHelper.CloneVolume(oldTexture, Usage.None, Pool.Managed); for( int mip = 0; mip < oldTexture.LevelCount; mip++) { VolumeDescription vd = oldTexture.GetLevelDescription(mip); CheckFormatValid(vd.Format); Surface sliceRT = context.Device.CreateRenderTarget(vd.Width, vd.Height, vd.Format, MultiSampleType.None, 0, true); for(int slice = 0; slice < vd.Depth; slice++) { context.Device.SetRenderTarget(0, sliceRT); ShadeVertex[] vb = new ShadeVertex[] { ShadeVertex.ForVolume(-1.0f, -1.0f, slice, vd.Width, vd.Height, vd.Depth), ShadeVertex.ForVolume( 1.0f, -1.0f, slice, vd.Width, vd.Height, vd.Depth), ShadeVertex.ForVolume(-1.0f, 1.0f, slice, vd.Width, vd.Height, vd.Depth), ShadeVertex.ForVolume( 1.0f, 1.0f, slice, vd.Width, vd.Height, vd.Depth), }; context.Device.BeginScene(); context.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vb); context.Device.EndScene(); context.Device.SetRenderTarget(0, context.Device.GetBackBuffer(0, 0, BackBufferType.Mono) ); OpsTextureHelper.LoadVolumeSliceFromSurface(newTexture, mip, slice, Filter.None| (container.SRGB?Filter.SrgbOut:0), sliceRT); } sliceRT.Dispose(); } oldTexture.Dispose(); container.Texture = newTexture; } else if(container.Texture is CubeTexture) { CubeTexture oldTexture = container.Texture as CubeTexture; CubeTexture newTexture = OpsTextureHelper.CloneCube(oldTexture, Usage.None, Pool.Managed); for( int mip = 0; mip < oldTexture.LevelCount; mip++) { SurfaceDescription sd = oldTexture.GetLevelDescription(mip); CheckFormatValid(sd.Format); Surface rt = context.Device.CreateRenderTarget( sd.Width, sd.Height, sd.Format, MultiSampleType.None, 0, true ); RenderCubeMapFace( context, newTexture, rt, CubeMapFace.PositiveX, mip, sd.Width, container.SRGB ); RenderCubeMapFace( context, newTexture, rt, CubeMapFace.PositiveY, mip, sd.Width, container.SRGB); RenderCubeMapFace( context, newTexture, rt, CubeMapFace.PositiveZ, mip, sd.Width, container.SRGB); RenderCubeMapFace( context, newTexture, rt, CubeMapFace.NegativeX, mip, sd.Width, container.SRGB); RenderCubeMapFace( context, newTexture, rt, CubeMapFace.NegativeY, mip, sd.Width, container.SRGB); RenderCubeMapFace( context, newTexture, rt, CubeMapFace.NegativeZ, mip, sd.Width, container.SRGB); } oldTexture.Dispose(); container.Texture = newTexture; } } }
public static ShadeVertex ForCube( float ptX, float ptY, int surfaceSize, CubeMapFace face) { ShadeVertex v = new ShadeVertex(); float pixelSize = 2.0f / (float)surfaceSize; v.PtX=ptX - pixelSize*0.5f; v.PtY=ptY + pixelSize*0.5f; float nX = ptX; float nY = ptY; switch( face ) { case CubeMapFace.PositiveX: v.TzX = 0; v.TzY = pixelSize; v.TzZ = pixelSize; v.TcX = 1.0f; v.TcY = nY; v.TcZ = -nX; break; case CubeMapFace.NegativeX: v.TzX = 0; v.TzY = pixelSize; v.TzZ = pixelSize; v.TcX = -1.0f; v.TcY = nY; v.TcZ = nX; break; case CubeMapFace.PositiveY: v.TzX = pixelSize; v.TzY = 0; v.TzZ = pixelSize; v.TcX = nX; v.TcY = 1.0f; v.TcZ = -nY; break; case CubeMapFace.NegativeY: v.TzX = pixelSize; v.TzY = 0; v.TzZ = pixelSize; v.TcX = nX; v.TcY = -1.0f; v.TcZ = nY; break; case CubeMapFace.PositiveZ: v.TzX = pixelSize; v.TzY = pixelSize; v.TzZ = 0; v.TcX = nX; v.TcY = nY; v.TcZ = 1.0f; break; case CubeMapFace.NegativeZ: v.TzX = pixelSize; v.TzY = pixelSize; v.TzZ = 0; v.TcX = -nX; v.TcY = nY; v.TcZ = -1.0f; break; default: throw new OpsException("Unknown Face Type"); } return v; }
public static ShadeVertex ForVolume( float ptX, float ptY, int slice, int width, int height, int depth)//texel size { ShadeVertex v = new ShadeVertex(); float tzX = 1.0f/(float)width; float tzY = 1.0f/(float)height; float tzZ = 1.0f/(float)depth; v.PtX=ptX - tzX*0.5f; v.PtY=ptY + tzY*0.5f; v.TcX=(1.0f+ptX)*0.5f; v.TcY=(1.0f+ptY)*0.5f; v.TcZ= (tzZ * slice) + (tzZ + 0.5f); v.TzX=tzX; v.TzY=tzY; v.TzZ=tzZ; return v; }
public static ShadeVertex ForTexture( float ptX, float ptY, int width, int height)//texel size { ShadeVertex v = new ShadeVertex(); float tzX = 1.0f/(float)width; float tzY = 1.0f/(float)height; v.PtX=ptX - tzX*0.5f; v.PtY=ptY + tzY*0.5f; v.TcX=(1.0f+ptX)*0.5f; v.TcY=(1.0f+ptY)*0.5f; v.TcZ=0; v.TzX=tzX; v.TzY=tzY; v.TzZ=0; return v; }
private void RenderCubeMapFace(OpsContext context, CubeTexture newTexture, Surface rt, CubeMapFace face, int mip, int size, bool SRGB) { context.Device.SetRenderTarget(0, rt); ShadeVertex[] vb = new ShadeVertex[] { ShadeVertex.ForCube(-1.0f, -1.0f, size, face), ShadeVertex.ForCube( 1.0f, -1.0f, size, face), ShadeVertex.ForCube(-1.0f, 1.0f, size, face), ShadeVertex.ForCube( 1.0f, 1.0f, size, face), }; context.Device.BeginScene(); context.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vb); context.Device.EndScene(); context.Device.SetRenderTarget(0, context.Device.GetBackBuffer(0, 0, BackBufferType.Mono) ); SurfaceLoader.FromSurface(newTexture.GetCubeMapSurface(face, mip), rt, Filter.None| (SRGB?Filter.SrgbOut:0), 0); }