private void DestroyGpuProgram(GpuProgram gpuProgram) { string programName = gpuProgram.Name; Resource res = HighLevelGpuProgramManager.Instance.GetByName(programName); if (res != null) { HighLevelGpuProgramManager.Instance.Remove(programName); } }
public override GpuProgram _getBindingDelegate() { global::System.IntPtr cPtr = OgrePINVOKE.HighLevelGpuProgram__getBindingDelegate(swigCPtr); GpuProgram ret = (cPtr == global::System.IntPtr.Zero) ? null : new GpuProgram(cPtr, false); if (OgrePINVOKE.SWIGPendingException.Pending) { throw OgrePINVOKE.SWIGPendingException.Retrieve(); } return(ret); }
public GpuProgram __deref__() { global::System.IntPtr cPtr = OgrePINVOKE.GpuProgramPtr___deref__(swigCPtr); GpuProgram ret = (cPtr == global::System.IntPtr.Zero) ? null : new GpuProgram(cPtr, false); if (OgrePINVOKE.SWIGPendingException.Pending) { throw OgrePINVOKE.SWIGPendingException.Retrieve(); } return(ret); }
/// <summary> /// Bind the auto parameters for a given CPU and GPU program set. /// </summary> /// <param name="pCpuProgram"> </param> /// <param name="pGpuProgram"> </param> internal void BindAutoParameters(Program pCpuProgram, GpuProgram pGpuProgram) { var gpuParams = pGpuProgram.DefaultParameters; var progParams = pCpuProgram.Parameters; for (int itParams = 0; itParams < progParams.Count; ++itParams) { UniformParameter curParam = progParams[itParams]; var gpuConstDef = gpuParams.FindNamedConstantDefinition(curParam.Name); if (gpuConstDef != null) { if (curParam.IsAutoConstantParameter) { if (curParam.IsAutoConstantRealParameter) { gpuParams.SetNamedAutoConstantReal(curParam.Name, curParam.AutoConstantType, curParam.AutoConstantRealData); } else if (curParam.IsAutoConstantIntParameter) { gpuParams.SetNamedAutoConstant(curParam.Name, curParam.AutoConstantType, curParam.AutoConstantIntData); } } } //Case this is not auto constant - we have to update its variablity ourself. else { gpuConstDef.Variability |= (GpuProgramParameters.GpuParamVariability)curParam.Variablity; //update variability in the float map. if (gpuConstDef.IsSampler == false) { var floatLogical = gpuParams.FloatLogicalBufferStruct; if (floatLogical != null) { for (int i = 0; i < floatLogical.Map.Count; i++) { if (floatLogical.Map[i].PhysicalIndex == gpuConstDef.PhysicalIndex) { floatLogical.Map[i].Variability |= gpuConstDef.Variability; break; } } } } } } }
private void BindTextureSampler(Program cpuProgram, GpuProgram gpuProgram) { var gpuParams = gpuProgram.DefaultParameters; var progParams = cpuProgram.Parameters; //Bind the samplers foreach (var curParam in progParams) { if (curParam.IsSampler) { gpuParams.SetNamedConstant(curParam.Name, curParam.Index); } } }
private void BindTextureSamplers(Program cpuProgram, GpuProgram gpuProgram) { var gpuParams = gpuProgram.DefaultParameters; var progParams = cpuProgram.Parameters; //bind the samplers foreach (var curParam in progParams) { if (curParam.IsSampler) { // The optimizer may remove some unnecessary parameters, so we should ignore them gpuParams.IgnoreMissingParameters = true; gpuParams.SetNamedConstant(curParam.Name, curParam.Index); } } }
public void Dispose() { if (this.vsCpuProgram != null) { ProgramManager.Instance.DestroyCpuProgram(this.vsCpuProgram); this.vsCpuProgram = null; } if (this.psCpuProgram != null) { ProgramManager.Instance.DestroyCpuProgram(this.psCpuProgram); this.psCpuProgram = null; } this.vsGpuProgram = null; this.psGpuProgram = null; }
internal override bool PostCreateGpuPrograms(ProgramSet programSet) { Program vsCpuProgram = programSet.CpuVertexProgram; GpuProgram vsGpuProgram = programSet.GpuVertexProgram; Program fsCpuProgram = programSet.CpuVertexProgram; GpuProgram fsGpuProgram = programSet.GpuFragmentProgram; BindAutoParameters(programSet.CpuVertexProgram, programSet.GpuVertexProgram); BindAutoParameters(programSet.CpuFragmentProgram, programSet.GpuFragmentProgram); BindTextureSamplers(vsCpuProgram, vsGpuProgram); BindTextureSamplers(fsCpuProgram, fsGpuProgram); return(true); }
public RenderView() { InitializeComponent(); timer = new Timer(); timer.Interval = 10; timer.Tick += Tick; timer.Start(); device = new WindowsFormsDevice(this); GpuProgram.Initialize(device, DeviceWidth, DeviceHeight); CenterToScreen(); RefreshMaterialNameLabel(); }
void Render() { GpuProgram.Clear(device, new Color(49 / 255.0f, 77 / 255.0f, 121 / 255.0f)); GpuProgram.SetDirectionLight(light); GpuProgram.SetCamera(camera); //NormalizedDevice.DrawLine(device, new Vertex(new Vector3(0, 0), Color.red, new Vector2(0, 0)), // new Vertex(new Vector3(0.5f, 0.5f), Color.green, new Vector2(1, 1))); //NormalizedDevice.DrawTriangle(device, new Vertex(new Vector3(0, 0.25f), Color.red, new Vector2(0, 0)), // new Vertex(new Vector3(-0.25f, -0.25f), Color.green, new Vector2(0, 0)), // new Vertex(new Vector3(0.25f, -0.45f), Color.yellow, new Vector2(0, 0))); Matrix4x4 M, V, P; float x, y, z; float pitch, yaw, roll; float curTime = TimeHelper.GetTimeSinceStartup(); camera.orthographic = orthographic.Checked; V = camera.WorldToCameraMatrix(); P = camera.ProjectionMatrix(); x = -1f; y = Mathf.Repeat(curTime * 1, 6) - 3f; z = 4.5f; pitch = -90; yaw = curTime * 45; roll = 0; M = Matrix4x4.TRS(new Vector3(x, y, z), Quaternion.Euler(pitch, yaw, roll), Vector3.one); GpuProgram.DrawCall(device, meshPanel, material, M, V, P); x = 0; y = 0; z = 2.5f; pitch = -60 + curTime * 25; yaw = curTime * 25; roll = 0; if (manualRotation.Checked) { pitch = pitchBar.Value; yaw = yawBar.Value; } M = Matrix4x4.TRS(new Vector3(x, y, z), Quaternion.Euler(pitch, yaw, roll), Vector3.one); GpuProgram.DrawCall(device, meshCube, material, M, V, P); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(GpuProgram obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
bool CreateDefaultTechnique() { string sourceFile = "Base\\Shaders\\SimpleExample.cg_hlsl"; string vertexSyntax; string fragmentSyntax; { if (RenderSystem.Instance.IsDirect3D()) { vertexSyntax = "vs_3_0"; fragmentSyntax = "ps_3_0"; } else if (RenderSystem.Instance.IsOpenGLES()) { vertexSyntax = "hlsl2glsl"; fragmentSyntax = "hlsl2glsl"; } else { vertexSyntax = "arbvp1"; fragmentSyntax = "arbfp1"; } } //technique is supported? if (!GpuProgramManager.Instance.IsSyntaxSupported(fragmentSyntax)) { return(false); } if (!GpuProgramManager.Instance.IsSyntaxSupported(vertexSyntax)) { return(false); } BaseMaterial.ReceiveShadows = false; //create techniques foreach (MaterialSchemes materialScheme in Enum.GetValues(typeof(MaterialSchemes))) { Technique technique = BaseMaterial.CreateTechnique(); technique.SchemeName = materialScheme.ToString(); //pass 0: ambient pass //pass 1: directional light //pass 2: point light //pass 3: spot light for (int nPass = 0; nPass < 4; nPass++) { //create pass Pass pass = technique.CreatePass(); bool ambientPass = nPass <= 1; bool lightPass = nPass >= 1; RenderLightType lightType = RenderLightType.Directional; ambientPass = nPass == 0; lightPass = nPass != 0; switch (nPass) { case 1: lightType = RenderLightType.Directional; break; case 2: lightType = RenderLightType.Point; break; case 3: lightType = RenderLightType.Spot; break; } if (lightPass) { pass.SpecialRendering = true; pass.SpecialRenderingIteratePerLight = true; pass.SpecialRenderingLightType = lightType; } int lightCount = lightPass ? 1 : 0; ///////////////////////////////////// //configure general pass settings { //disable Direct3D standard fog features pass.SetFogOverride(FogMode.None, new ColorValue(0, 0, 0), 0, 0, 0); //Light pass if (!ambientPass) { pass.DepthWrite = false; pass.SourceBlendFactor = SceneBlendFactor.One; pass.DestBlendFactor = SceneBlendFactor.One; } } ///////////////////////////////////// //generate general compile arguments and create texture unit states StringBuilder generalArguments = new StringBuilder(256); { if (RenderSystem.Instance.IsDirect3D()) { generalArguments.Append(" -DDIRECT3D"); } if (RenderSystem.Instance.IsOpenGL()) { generalArguments.Append(" -DOPENGL"); } if (RenderSystem.Instance.IsOpenGLES()) { generalArguments.Append(" -DOPENGL_ES"); } if (ambientPass) { generalArguments.Append(" -DAMBIENT_PASS"); } generalArguments.AppendFormat(" -DLIGHT_COUNT={0}", lightCount); generalArguments.Append(" -DLIGHTING"); //DiffuseMap if (!string.IsNullOrEmpty(DiffuseMap)) { generalArguments.Append(" -DDIFFUSE_MAP"); pass.CreateTextureUnitState(ConvertToFullPath(DiffuseMap)); } } ///////////////////////////////////// //generate programs //generate program for only ambient pass if (ambientPass && !lightPass) { string error; //vertex program GpuProgram vertexProgram = GpuProgramCacheManager.Instance.AddProgram( "SimpleExample_Vertex_", GpuProgramType.Vertex, sourceFile, "main_vp", vertexSyntax, generalArguments.ToString(), out error); if (vertexProgram == null) { Log.Fatal(error); return(false); } SetProgramAutoConstants(vertexProgram.DefaultParameters, 0); pass.VertexProgramName = vertexProgram.Name; //fragment program GpuProgram fragmentProgram = GpuProgramCacheManager.Instance.AddProgram( "SimpleExample_Fragment_", GpuProgramType.Fragment, sourceFile, "main_fp", fragmentSyntax, generalArguments.ToString(), out error); if (fragmentProgram == null) { Log.Fatal(error); return(false); } SetProgramAutoConstants(fragmentProgram.DefaultParameters, 0); pass.FragmentProgramName = fragmentProgram.Name; } //generate program for light passes if (lightPass) { string error; StringBuilder arguments = new StringBuilder(generalArguments.Length + 100); arguments.Append(generalArguments.ToString()); arguments.AppendFormat(" -DLIGHTTYPE_{0}", lightType.ToString().ToUpper()); //vertex program GpuProgram vertexProgram = GpuProgramCacheManager.Instance.AddProgram( "SimpleExample_Vertex_", GpuProgramType.Vertex, sourceFile, "main_vp", vertexSyntax, arguments.ToString(), out error); if (vertexProgram == null) { Log.Fatal(error); return(false); } SetProgramAutoConstants(vertexProgram.DefaultParameters, lightCount); pass.VertexProgramName = vertexProgram.Name; //fragment program GpuProgram fragmentProgram = GpuProgramCacheManager.Instance.AddProgram( "SimpleExample_Fragment_", GpuProgramType.Fragment, sourceFile, "main_fp", fragmentSyntax, arguments.ToString(), out error); if (fragmentProgram == null) { Log.Fatal(error); return(false); } SetProgramAutoConstants(fragmentProgram.DefaultParameters, lightCount); pass.FragmentProgramName = fragmentProgram.Name; } } } return(true); }
private static void DrawLineInteger(IDevice device, ref Vertex v1, ref Vertex v2) { int x1 = (int)v1.pos.x; int x2 = (int)v2.pos.x; int y1 = (int)v1.pos.y; int y2 = (int)v2.pos.y; int dx = x2 - x1; int dy = y2 - y1; int stepX = 1; int stepY = 1; if (dx < 0) { stepX = -1; dx = -dx; } if (dy < 0) { stepY = -1; dy = -dy; } int dy2 = dy << 1; int dx2 = dx << 1; if (dy > dx) { Mathf.Swap(ref x1, ref y1); Mathf.Swap(ref x2, ref y2); Mathf.Swap(ref dx, ref dy); Mathf.Swap(ref dx2, ref dy2); Mathf.Swap(ref stepX, ref stepY); } int x = x1; int y = y1; int errorValue = dy2 - dx; float delta = 1 / (dx == 0 ? 1.0f : dx); float t = 0; float z = 0; Vertex fragV; Color finallyColor; bool zTest = GpuProgram.IsZTestOn(); bool zWrite = GpuProgram.IsZWriteOn(); for (int i = 0; i <= dx; i++, x += stepX) { t = i * delta; z = Mathf.Lerp(v1.pos.z, v2.pos.z, t); if (!zTest || GpuProgram.ZTest(x, y, z)) { fragV = Vertex.Lerp(v1, v2, t); fragV.uv /= fragV.invertRealZ; // real uv mapping by division 1/z if (GpuProgram.CallFragmentStage(ref fragV, x, y, out finallyColor)) { device.DrawPixel(x, y, finallyColor); if (zWrite) { GpuProgram.SetBufferZ(x, y, z); } } } errorValue += dy2; if (errorValue >= 0) { errorValue -= dx2; y += stepY; } } }
/// <summary> /// /// </summary> /// <param name="gpup"></param> /// <param name="name"></param> /// <param name="value"></param> public void SetGpuProgramParameter(GpuProgram gpup, string name, Vector3 value) { SetGpuProgramParameter(gpup, name, value, true); }
protected override bool OnInitBaseMaterial() { if (!base.OnInitBaseMaterial()) { return(false); } string sourceFile = "Base\\Shaders\\DefaultShadowCaster.cg_hlsl"; string vertexSyntax; string fragmentSyntax; { if (RenderSystem.Instance.IsDirect3D()) { vertexSyntax = "vs_3_0"; fragmentSyntax = "ps_3_0"; } else if (RenderSystem.Instance.IsOpenGLES()) { vertexSyntax = "hlsl2glsl"; fragmentSyntax = "hlsl2glsl"; } else { vertexSyntax = "arbvp1"; fragmentSyntax = "arbfp1"; } } Technique technique = BaseMaterial.CreateTechnique(); Pass pass = technique.CreatePass(); pass.SetFogOverride(FogMode.None, new ColorValue(0, 0, 0), 0, 0, 0); //generate general compile arguments StringBuilder arguments = new StringBuilder(256); { if (RenderSystem.Instance.IsDirect3D()) { arguments.Append(" -DDIRECT3D"); } if (RenderSystem.Instance.IsOpenGL()) { arguments.Append(" -DOPENGL"); } if (RenderSystem.Instance.IsOpenGLES()) { arguments.Append(" -DOPENGL_ES"); } arguments.AppendFormat(" -DLIGHTTYPE_{0}", lightType.ToString().ToUpper()); if (lightType == RenderLightType.Directional || lightType == RenderLightType.Spot) { if (atiHardwareShadows) { arguments.Append(" -DATI_HARDWARE_SHADOWS"); } if (nvidiaHardwareShadows) { arguments.Append(" -DNVIDIA_HARDWARE_SHADOWS"); } } //hardware instancing if (RenderSystem.Instance.HasShaderModel3() && RenderSystem.Instance.Capabilities.HardwareInstancing) { pass.SupportHardwareInstancing = true; arguments.Append(" -DINSTANCING"); } } //generate programs { string error; //vertex program GpuProgram vertexProgram = GpuProgramCacheManager.Instance.AddProgram( "DefaultShadowCaster_Vertex_", GpuProgramType.Vertex, sourceFile, "main_vp", vertexSyntax, arguments.ToString(), out error); if (vertexProgram == null) { string err = "File:" + sourceFile; //Incin err += " Type Error: DefaultShadowCaster_Vertex_: Arguments: " + arguments.ToString(); err += " Error : " + error; Log.Fatal(err); return(false); } SetProgramAutoConstants(vertexProgram.DefaultParameters); pass.VertexProgramName = vertexProgram.Name; //fragment program GpuProgram fragmentProgram = GpuProgramCacheManager.Instance.AddProgram( "DefaultShadowCaster_Fragment_", GpuProgramType.Fragment, sourceFile, "main_fp", fragmentSyntax, arguments.ToString(), out error); if (fragmentProgram == null) { string err = "File:" + sourceFile; //Incin err += " Type Error: DefaultShadowCaster_Fragment_ Arguments: " + arguments.ToString(); err += " Error : " + error; Log.Fatal(err); return(false); } SetProgramAutoConstants(fragmentProgram.DefaultParameters); pass.FragmentProgramName = fragmentProgram.Name; } return(true); }
/// <summary> /// /// </summary> /// <param name="pageNum"></param> protected void ChangePage(int pageNum) { if (this.materialControlsContainer.Count == 0) { return; } this.currentPage = (pageNum == -1) ? (this.currentPage + 1) % this.numPages : pageNum; string pageText = string.Format("Parameters {0} / {1}", this.currentPage + 1, this.numPages); ((Button)TrayManager.GetWidget("PageButtonControl")).Caption = pageText; if (this.activeMaterial != null && this.activeMaterial.SupportedTechniques.Count > 0) { Technique currentTechnique = this.activeMaterial.SupportedTechniques[0]; if (currentTechnique != null) { this.activePass = currentTechnique.GetPass(0); if (this.activePass != null) { if (this.activePass.HasFragmentProgram) { this.activeFragmentProgram = this.activePass.FragmentProgram; this.activeFragmentParameters = this.activePass.FragmentProgramParameters; } if (this.activePass.HasVertexProgram) { this.activeVertexProgram = this.activePass.VertexProgram; this.activeVertexParameters = this.activePass.VertexProgramParameters; } int activeControlCount = this.materialControlsContainer[this.currentMaterial].ShaderControlsCount; int startControlIndex = this.currentPage * ControlsPerPage; int numControls = activeControlCount - startControlIndex; if (numControls <= 0) { this.currentPage = 0; startControlIndex = 0; numControls = activeControlCount; } for (int i = 0; i < ControlsPerPage; i++) { Slider shaderControlSlider = this.shaderControls[i]; if (i < numControls) { shaderControlSlider.Show(); int controlIndex = startControlIndex + i; ShaderControl activeShaderDef = this.materialControlsContainer[this.currentMaterial].GetShaderControl(controlIndex); shaderControlSlider.SetRange(activeShaderDef.MinVal, activeShaderDef.MaxVal, 50, false); shaderControlSlider.Caption = activeShaderDef.Name; shaderControlSlider.SliderMoved += new SliderMovedHandler(shaderControlSlider_SliderMoved); float uniformVal = 0.0f; switch (activeShaderDef.Type) { case ShaderType.GpuVertex: case ShaderType.GpuFragment: { GpuProgramParameters activeParameters = (activeShaderDef.Type == ShaderType.GpuVertex) ? this.activeVertexParameters : this.activeFragmentParameters; if (activeParameters != null) { throw new NotImplementedException("Fix this"); //int idx = activeParameters.GetParamIndex( activeShaderDef.ParamName ); //activeShaderDef.PhysicalIndex = idx; //uniformVal = activeParameters.GetNamedFloatConstant( activeShaderDef.ParamName ).val[ activeShaderDef.ElementIndex ]; } } break; case ShaderType.MatSpecular: { // get the specular values from the material pass ColorEx oldSpec = this.activePass.Specular; int x = activeShaderDef.ElementIndex; uniformVal = x == 0 ? oldSpec.r : x == 1 ? oldSpec.g : x == 2 ? oldSpec.b : x == 3 ? oldSpec.a : 0; } break; case ShaderType.MatDiffuse: { // get the specular values from the material pass ColorEx oldSpec = this.activePass.Diffuse; int x = activeShaderDef.ElementIndex; uniformVal = x == 0 ? oldSpec.r : x == 1 ? oldSpec.g : x == 2 ? oldSpec.b : x == 3 ? oldSpec.a : 0; } break; case ShaderType.MatAmbient: { // get the specular values from the material pass ColorEx oldSpec = this.activePass.Ambient; int x = activeShaderDef.ElementIndex; uniformVal = x == 0 ? oldSpec.r : x == 1 ? oldSpec.g : x == 2 ? oldSpec.b : x == 3 ? oldSpec.a : 0; } break; case ShaderType.MatShininess: { // get the specular values from the material pass uniformVal = this.activePass.Shininess; } break; } shaderControlSlider.Value = uniformVal; } } } } } }
/// <summary> /// /// </summary> /// <param name="gpup"></param> /// <param name="name"></param> /// <param name="value"></param> /// <param name="updateGroundPasses"></param> public void SetGpuProgramParameter(GpuProgram gpup, string name, Vector3 value, bool updateGroundPasses) { if (!this.SkyX.MeshManager.IsCreated) { return; } GpuProgramParameters parameters = null; switch (gpup) { case GpuProgram.Vertex: { using (MaterialPtr mat = (MaterialPtr)MaterialManager.Singleton.GetByName(this.SkyX.MeshManager.MaterialName)) { //parameters = mat.GetTechnique(0).GetPass(0).VertexProgramParameters; parameters = mat.GetTechnique(0).GetPass(0).GetVertexProgramParameters(); } } break; case GpuProgram.Fragment: { using (MaterialPtr mat = (MaterialPtr)MaterialManager.Singleton.GetByName(this.SkyX.MeshManager.MaterialName)) { //parameters = mat.GetTechnique(0).GetPass(0).FragmentProgramParameters; parameters = mat.GetTechnique(0).GetPass(0).GetFragmentProgramParameters(); } } break; default: { throw new NotSupportedException("this type is not supported"); } } parameters.SetNamedConstant(name, value); if (!updateGroundPasses) { return; } foreach (Pass iter in _groundPasses) { switch (gpup) { case GpuProgram.Vertex: { //parameters = iter.VertexProgramParameters; parameters = iter.GetVertexProgramParameters(); } break; case GpuProgram.Fragment: { //parameters = iter.FragmentProgramParameters; parameters = iter.GetFragmentProgramParameters(); } break; } parameters.SetNamedConstant(name, value); } }
//------------------------------------------------------------------------- public void SetupTerrainMaterial() { if (string.IsNullOrEmpty(this.mCustomMaterialName)) { // define our own material Options.terrainMaterial = (Material)MaterialManager.Instance.GetByName(TERRAIN_MATERIAL_NAME); // Make unique terrain material name string s = mName + "/Terrain"; Options.terrainMaterial = (Material)MaterialManager.Instance.GetByName(s); if (null == Options.terrainMaterial) { Options.terrainMaterial = (Material)MaterialManager.Instance.Create(s, ResourceGroupManager.Instance.WorldResourceGroupName); } else { Options.terrainMaterial.GetTechnique(0).GetPass(0).RemoveAllTextureUnitStates(); } Pass pass = Options.terrainMaterial.GetTechnique(0).GetPass(0); if (this.mWorldTextureName != "") { pass.CreateTextureUnitState(this.mWorldTextureName, 0); } if (this.mDetailTextureName != "") { pass.CreateTextureUnitState(this.mDetailTextureName, 1); } Options.terrainMaterial.Lighting = Options.lit; if (Options.lodMorph && mPCZSM.TargetRenderSystem.Capabilities.HasCapability(Capabilities.VertexPrograms) && GpuProgramManager.Instance.GetByName("Terrain/VertexMorph") == null) { // Create & assign LOD morphing vertex program String syntax; if (GpuProgramManager.Instance.IsSyntaxSupported("arbvp1")) { syntax = "arbvp1"; } else { syntax = "vs_1_1"; } // Get source, and take into account current fog mode FogMode fm = mPCZSM.FogMode; string source = new TerrainVertexProgram().getProgramSource(fm, syntax, false); GpuProgram prog = GpuProgramManager.Instance.CreateProgramFromString("Terrain/VertexMorph", ResourceGroupManager.Instance. WorldResourceGroupName, source, GpuProgramType.Vertex, syntax); // Attach pass.SetVertexProgram("Terrain/VertexMorph"); // Get params GpuProgramParameters paras = pass.VertexProgramParameters; // worldviewproj paras.SetAutoConstant(0, GpuProgramParameters.AutoConstantType.WorldViewProjMatrix); // morph factor paras.SetAutoConstant(4, GpuProgramParameters.AutoConstantType.Custom, TerrainZoneRenderable.MORPH_CUSTOM_PARAM_ID); // fog exp density(if relevant) if (fm == FogMode.Exp || fm == FogMode.Exp2) { paras.SetConstant(5, new Vector3(mPCZSM.FogDensity, 0, 0)); // Override scene fog since otherwise it's applied twice // Set to linear and we derive [0,1] fog value in the shader pass.SetFog(true, FogMode.Linear, mPCZSM.FogColor, 0, 1, 0); } // Also set shadow receiver program string source2 = new TerrainVertexProgram().getProgramSource(fm, syntax, true); prog = GpuProgramManager.Instance.CreateProgramFromString("Terrain/VertexMorphShadowReceive", ResourceGroupManager.Instance.WorldResourceGroupName, source2, GpuProgramType.Vertex, syntax); pass.SetShadowReceiverVertexProgram("Terrain/VertexMorphShadowReceive"); paras = pass.ShadowReceiverVertexProgramParameters; // worldviewproj paras.SetAutoConstant(0, GpuProgramParameters.AutoConstantType.WorldViewProjMatrix); // world paras.SetAutoConstant(4, GpuProgramParameters.AutoConstantType.WorldMatrix); // texture view / proj paras.SetAutoConstant(8, GpuProgramParameters.AutoConstantType.TextureViewProjMatrix); // morph factor paras.SetAutoConstant(12, GpuProgramParameters.AutoConstantType.Custom, TerrainZoneRenderable.MORPH_CUSTOM_PARAM_ID); // Set param index this.mLodMorphParamName = ""; this.mLodMorphParamIndex = 4; } Options.terrainMaterial.Load(); } else { // Custom material Options.terrainMaterial = (Material)MaterialManager.Instance.GetByName(this.mCustomMaterialName); Options.terrainMaterial.Load(); } // now set up the linkage between vertex program and LOD morph param if (Options.lodMorph) { Technique t = Options.terrainMaterial.GetBestTechnique(); for (ushort i = 0; i < t.PassCount; ++i) { Pass p = t.GetPass(i); if (p.HasVertexProgram) { // we have to assume vertex program includes LOD morph capability GpuProgramParameters paras = p.VertexProgramParameters; // Check to see if custom param is already there //GpuProgramParameters::AutoConstantIterator aci = params->getAutoConstantIterator(); bool found = false; foreach (GpuProgramParameters.AutoConstantEntry ace in paras.AutoConstantList) { if (ace.Type == GpuProgramParameters.AutoConstantType.Custom && ace.Data == TerrainZoneRenderable.MORPH_CUSTOM_PARAM_ID) { found = true; break; } } if (!found) { if (this.mLodMorphParamName != "") { paras.SetNamedAutoConstant(this.mLodMorphParamName, GpuProgramParameters.AutoConstantType.Custom, TerrainZoneRenderable.MORPH_CUSTOM_PARAM_ID); } else { paras.SetAutoConstant(this.mLodMorphParamIndex, GpuProgramParameters.AutoConstantType.Custom, TerrainZoneRenderable.MORPH_CUSTOM_PARAM_ID); } } } } } }
/// <summary> /// Creates a GPU program. /// </summary> /// <param name="gpuProg">The GpuProgram.</param> public abstract void CreateGpuProgram(GpuProgram gpuProg);
protected void _translateGpuProgram(ScriptCompiler compiler, ObjectAbstractNode obj) { var customParameters = new NameValuePairList(); string syntax = string.Empty, source = string.Empty; AbstractNode parameters = null; foreach (var i in obj.Children) { if (i is PropertyAbstractNode) { var prop = (PropertyAbstractNode)i; if (prop.Id == (uint)Keywords.ID_SOURCE) { if (prop.Values.Count != 0) { if (prop.Values[0] is AtomAbstractNode) { source = ((AtomAbstractNode)prop.Values[0]).Value; } else { compiler.AddError(CompileErrorCode.InvalidParameters, prop.File, prop.Line, "source file expected"); } } else { compiler.AddError(CompileErrorCode.StringExpected, prop.File, prop.Line, "source file expected"); } } else if (prop.Id == (uint)Keywords.ID_SYNTAX) { if (prop.Values.Count != 0) { if (prop.Values[0] is AtomAbstractNode) { syntax = ((AtomAbstractNode)prop.Values[0]).Value; } else { compiler.AddError(CompileErrorCode.InvalidParameters, prop.File, prop.Line, "syntax string expected"); } } else { compiler.AddError(CompileErrorCode.StringExpected, prop.File, prop.Line, "syntax string expected"); } } else { string name = prop.Name, value = string.Empty; var first = true; foreach (var it in prop.Values) { if (it is AtomAbstractNode) { if (!first) { value += " "; } else { first = false; } value += ((AtomAbstractNode)it).Value; } } customParameters.Add(name, value); } } else if (i is ObjectAbstractNode) { if (((ObjectAbstractNode)i).Id == (uint)Keywords.ID_DEFAULT_PARAMS) { parameters = i; } else { processNode(compiler, i); } } } if (!GpuProgramManager.Instance.IsSyntaxSupported(syntax)) { compiler.AddError(CompileErrorCode.UnsupportedByRenderSystem, obj.File, obj.Line); //Register the unsupported program so that materials that use it know that //it exists but is unsupported var unsupportedProg = GpuProgramManager.Instance.Create(obj.Name, compiler.ResourceGroup, _translateIDToGpuProgramType(obj.Id), syntax); return; } // Allocate the program object progObj; GpuProgram prog = null; ScriptCompilerEvent evt = new CreateGpuProgramScriptCompilerEvent(obj.File, obj.Name, compiler.ResourceGroup, source, syntax, _translateIDToGpuProgramType(obj.Id)); var processed = compiler._fireEvent(ref evt, out progObj); if (!processed) { prog = (GpuProgram) GpuProgramManager.Instance.CreateProgram(obj.Name, compiler.ResourceGroup, source, _translateIDToGpuProgramType(obj.Id), syntax); } else { prog = (GpuProgram)progObj; } // Check that allocation worked if (prog == null) { compiler.AddError(CompileErrorCode.ObjectAllocationError, obj.File, obj.Line, "gpu program \"" + obj.Name + "\" could not be created"); return; } obj.Context = prog; prog.IsMorphAnimationIncluded = false; prog.PoseAnimationCount = 0; prog.IsSkeletalAnimationIncluded = false; prog.IsVertexTextureFetchRequired = false; prog.Origin = obj.File; // Set the custom parameters prog.SetParameters(customParameters); // Set up default parameters if (prog.IsSupported && parameters != null) { var ptr = prog.DefaultParameters; GpuProgramTranslator.TranslateProgramParameters(compiler, ptr, (ObjectAbstractNode)parameters); } }
private void BindSubShaders(Program program, GpuProgram gpuProgram) { if (program.DependencyCount > 0) { // Get all attached shaders so we do not attach shaders twice. // maybe GLSLProgram should take care of that ( prevent add duplicate shaders ) string attachedShaders = string.Empty; //TODO: gpuProgram.GetParameter("attach"); string subSharedDef = string.Empty; for (int i = 0; i < program.DependencyCount; i++) { // Here we append _VS and _FS to the library shaders (so max each lib shader // is compiled twice once as vertex and once as fragment shader) string subShaderName = program.GetDependency(i); if (program.Type == GpuProgramType.Vertex) { subShaderName += "_VS"; } else { subShaderName += "_FS"; } //Check if the library shader already compiled if (!HighLevelGpuProgramManager.Instance.ResourceExists(subShaderName)) { //Create the library shader HighLevelGpuProgram subGpuProgram = HighLevelGpuProgramManager.Instance.CreateProgram(subShaderName, ResourceGroupManager. DefaultResourceGroupName, TargetLanguage, program.Type); //Set the source name string sourceName = program.GetDependency(i) + "." + TargetLanguage; subGpuProgram.SourceFile = sourceName; //If we have compiler errors than stop processing if (subGpuProgram.HasCompileError) { throw new AxiomException("Could not compile shader library from the source file: " + sourceName); } this.libraryPrograms.Add(subShaderName); } //Check if the lib shader already attached to this shader if (attachedShaders.Contains(subShaderName)) { subSharedDef += subShaderName + " "; } } //Check if we have something to attach if (subSharedDef.Length > 0) { var nvpl = new Axiom.Collections.NameValuePairList(); nvpl.Add("attach", subSharedDef); gpuProgram.SetParameters(nvpl); } } }