private ConstantTable SetupDevice(OpsContext context, ShadeArgs args) { string errStr = null; ConstantTable constantTable; GraphicsStream pshader; GraphicsStream vshader; try { ConstantTable dummyTable; errStr = null; vshader = ShaderLoader.CompileShader(ShadeVertex.VertexShader, "VertexShader", null, null, "vs_3_0", ShaderFlags.None, out errStr, out dummyTable); } finally { if (errStr != null && errStr.Length > 0) { OpsConsole.WriteLine("Vertex Shader Compiler messages: " + errStr); OpsConsole.WriteLine("If this message is regarding your entry point, it may be something other than the default 'main'. Please use the argument 'Shader' to specify it."); } } try { Macro dxopsMacro = new Macro(); dxopsMacro.Name = "__DXOPS"; dxopsMacro.Definition = "1"; errStr = null; pshader = ShaderLoader.CompileShaderFromFile(args.File, args.Shader, new Macro[] { dxopsMacro }, null, "ps_3_0", ShaderFlags.None, out errStr, out constantTable); } finally { if (errStr != null && errStr.Length > 0) { OpsConsole.WriteLine("Pixel Shader Compiler messages: " + errStr); OpsConsole.WriteLine("If this message is regarding your entry point, it may be something other than the default 'main'. Please use the argument 'Shader' to specify it."); } } context.Device.SetRenderState(RenderStates.CullMode, (int)Cull.None); context.Device.SetRenderState(RenderStates.FillMode, (int)FillMode.Solid); context.Device.SetRenderState(RenderStates.AlphaTestEnable, false); context.Device.SetRenderState(RenderStates.AlphaBlendEnable, false); context.Device.SetRenderState(RenderStates.StencilEnable, false); context.Device.SetRenderState(RenderStates.ZEnable, false); context.Device.SetRenderState(RenderStates.ZBufferWriteEnable, false); context.Device.DepthStencilSurface = null; VertexDeclaration decl = new VertexDeclaration(context.Device, ShadeVertex.VertexDeclaration); context.Device.VertexDeclaration = decl; VertexShader vs = new VertexShader(context.Device, vshader); context.Device.VertexShader = vs; PixelShader ps = new PixelShader(context.Device, pshader); context.Device.PixelShader = ps; constantTable.SetDefaults(context.Device); foreach (OpsParsedArgument constant in args.Constants) { EffectHandle h = constantTable.GetConstant(null, constant.Name); if (h == null) { OpsConsole.WriteLine("WARNING: Parameter '{0}' was not found in shader.", constant.Name); continue; } ConstantDescription[] cds = constantTable.GetConstantDescription(h, 1); ConstantDescription cd = cds[0]; switch (cd.Class) { case ParameterClass.Object: { //texture switch (cd.ParameterType) { case ParameterType.Texture: case ParameterType.Texture1D: case ParameterType.Texture2D: case ParameterType.Texture3D: case ParameterType.TextureCube: { OpsTexture container = context.GetTexture(constant.Value); int sampler = constantTable.GetSamplerIndex(h); context.Device.SetTexture(sampler, container.Texture); } break; } break; } case ParameterClass.Scalar: case ParameterClass.Vector: case ParameterClass.MatrixColumns: case ParameterClass.MatrixRows: { ArrayList floatList = new ArrayList(); string[] floatStrings = constant.Value.Split(new char[] { ' ', ',' }); foreach (string floatStr in floatStrings) { if (floatStr.Length > 0) { floatList.Add(float.Parse(floatStr)); } } float[] floats = (float[])floatList.ToArray(typeof(float)); constantTable.SetValue(context.Device, h, floats); } break; } } return(constantTable); }
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; } } }
private ConstantTable SetupDevice(OpsContext context, ShadeArgs args) { string errStr = null; ConstantTable constantTable; GraphicsStream pshader; GraphicsStream vshader; try { ConstantTable dummyTable; errStr = null; vshader = ShaderLoader.CompileShader(ShadeVertex.VertexShader, "VertexShader", null, null, "vs_3_0", ShaderFlags.None, out errStr, out dummyTable); } finally { if (errStr != null && errStr.Length > 0) { OpsConsole.WriteLine("Vertex Shader Compiler messages: " + errStr); OpsConsole.WriteLine("If this message is regarding your entry point, it may be something other than the default 'main'. Please use the argument 'Shader' to specify it."); } } try { Macro dxopsMacro = new Macro(); dxopsMacro.Name = "__DXOPS"; dxopsMacro.Definition = "1"; errStr = null; pshader = ShaderLoader.CompileShaderFromFile(args.File, args.Shader, new Macro[]{ dxopsMacro } , null, "ps_3_0", ShaderFlags.None, out errStr, out constantTable); } finally { if (errStr != null && errStr.Length > 0) { OpsConsole.WriteLine("Pixel Shader Compiler messages: " + errStr); OpsConsole.WriteLine("If this message is regarding your entry point, it may be something other than the default 'main'. Please use the argument 'Shader' to specify it." ); } } context.Device.SetRenderState(RenderStates.CullMode, (int)Cull.None); context.Device.SetRenderState(RenderStates.FillMode, (int)FillMode.Solid); context.Device.SetRenderState(RenderStates.AlphaTestEnable, false); context.Device.SetRenderState(RenderStates.AlphaBlendEnable, false); context.Device.SetRenderState(RenderStates.StencilEnable, false); context.Device.SetRenderState(RenderStates.ZEnable, false); context.Device.SetRenderState(RenderStates.ZBufferWriteEnable, false); context.Device.DepthStencilSurface = null; VertexDeclaration decl = new VertexDeclaration(context.Device, ShadeVertex.VertexDeclaration); context.Device.VertexDeclaration = decl; VertexShader vs = new VertexShader(context.Device, vshader); context.Device.VertexShader = vs; PixelShader ps = new PixelShader(context.Device, pshader); context.Device.PixelShader = ps; constantTable.SetDefaults(context.Device); foreach(OpsParsedArgument constant in args.Constants) { EffectHandle h = constantTable.GetConstant(null, constant.Name); if (h == null) { OpsConsole.WriteLine( "WARNING: Parameter '{0}' was not found in shader.", constant.Name); continue; } ConstantDescription[] cds = constantTable.GetConstantDescription(h, 1); ConstantDescription cd = cds[0]; switch (cd.Class) { case ParameterClass.Object: {//texture switch (cd.ParameterType) { case ParameterType.Texture: case ParameterType.Texture1D: case ParameterType.Texture2D: case ParameterType.Texture3D: case ParameterType.TextureCube: { OpsTexture container = context.GetTexture(constant.Value); int sampler = constantTable.GetSamplerIndex(h); context.Device.SetTexture(sampler, container.Texture); } break; } break; } case ParameterClass.Scalar: case ParameterClass.Vector: case ParameterClass.MatrixColumns: case ParameterClass.MatrixRows: { ArrayList floatList = new ArrayList(); string[] floatStrings = constant.Value.Split(new char[] { ' ', ',' }); foreach (string floatStr in floatStrings) { if (floatStr.Length > 0) { floatList.Add(float.Parse(floatStr)); } } float[] floats = (float[])floatList.ToArray(typeof(float)); constantTable.SetValue(context.Device, h, floats); } break; } } return constantTable; }