public void Update(IPluginIO pin, DX11RenderContext context) { Device device = context.Device; DeviceContext ctx = context.CurrentDeviceContext; if (!this.deviceshaderdata.ContainsKey(context)) { this.deviceshaderdata.Add(context, new DX11ShaderData(context)); this.deviceshaderdata[context].SetEffect(this.FShader); } DX11ShaderData shaderdata = this.deviceshaderdata[context]; if (this.shaderupdated) { shaderdata.SetEffect(this.FShader); this.shaderupdated = false; } context.RenderStateStack.Push(new DX11RenderState()); this.OnBeginQuery(context); //Clear shader stages shaderdata.ResetShaderStages(ctx); context.Primitives.ApplyFullTriVS(); foreach (DX11ResourcePoolEntry<DX11RenderTarget2D> rt in this.lastframetargets) { rt.UnLock(); } this.lastframetargets.Clear(); DX11ObjectRenderSettings or = new DX11ObjectRenderSettings(); int wi, he; bool preserve = false; DX11ResourcePoolEntry<DX11RenderTarget2D> preservedtarget = null; for (int i = 0; i < this.spmax; i++) { int passcounter = 0; if (this.FInEnabled[i]) { List<DX11ResourcePoolEntry<DX11RenderTarget2D>> locktargets = new List<DX11ResourcePoolEntry<DX11RenderTarget2D>>(); #region Manage size DX11Texture2D initial; if (this.FIn.PluginIO.IsConnected) { if (this.FInUseDefaultSize[0]) { initial = context.DefaultTextures.WhiteTexture; wi = (int)this.FInSize[0].X; he = (int)this.FInSize[0].Y; } else { initial = this.FIn[i][context]; if (initial != null) { wi = initial.Width; he = initial.Height; } else { initial = context.DefaultTextures.WhiteTexture; wi = (int)this.FInSize[i].X; he = (int)this.FInSize[i].Y; } } } else { initial = context.DefaultTextures.WhiteTexture; wi = (int)this.FInSize[i].X; he = (int)this.FInSize[i].Y; } #endregion DX11RenderSettings r = new DX11RenderSettings(); r.RenderWidth = wi; r.RenderHeight = he; if (this.FInSemantics.PluginIO.IsConnected) { r.CustomSemantics.AddRange(this.FInSemantics.ToArray()); } if (this.FInResSemantics.PluginIO.IsConnected) { r.ResourceSemantics.AddRange(this.FInResSemantics.ToArray()); } this.varmanager.SetGlobalSettings(shaderdata.ShaderInstance, r); this.varmanager.ApplyGlobal(shaderdata.ShaderInstance); DX11Texture2D lastrt = initial; DX11ResourcePoolEntry<DX11RenderTarget2D> lasttmp = null; List<DX11Texture2D> rtlist = new List<DX11Texture2D>(); //Bind Initial (once only is ok) this.BindTextureSemantic(shaderdata.ShaderInstance.Effect, "INITIAL", initial); //Go trough all passes EffectTechnique tech = shaderdata.ShaderInstance.Effect.GetTechniqueByIndex(tid); for (int j = 0; j < tech.Description.PassCount; j++) { ImageShaderPass pi = this.varmanager.passes[j]; EffectPass pass = tech.GetPassByIndex(j); bool isLastPass = j == tech.Description.PassCount - 1; for (int kiter = 0; kiter < pi.IterationCount; kiter++) { if (passcounter > 0) { for (int pid = 0; pid < passcounter; pid++) { string pname = "PASSRESULT" + pid; this.BindTextureSemantic(shaderdata.ShaderInstance.Effect, pname, rtlist[pid]); } } Format fmt = initial.Format; if (pi.CustomFormat) { fmt = pi.Format; } bool mips = pi.Mips || (isLastPass && FInMipLastPass[i]); int w, h; if (j == 0) { h = he; w = wi; } else { h = pi.Reference == ImageShaderPass.eImageScaleReference.Initial ? he : lastrt.Height; w = pi.Reference == ImageShaderPass.eImageScaleReference.Initial ? wi : lastrt.Width; } if (pi.DoScale) { if (pi.Absolute) { w = Convert.ToInt32(pi.ScaleVector.X); h = Convert.ToInt32(pi.ScaleVector.Y); } else { w = Convert.ToInt32((float)w * pi.ScaleVector.X); h = Convert.ToInt32((float)h * pi.ScaleVector.Y); } w = Math.Max(w, 1); h = Math.Max(h, 1); } //Check format support for render target, and default to rgb8 if not if (!context.IsSupported(FormatSupport.RenderTarget, fmt)) { fmt = Format.R8G8B8A8_UNorm; } //Since device is not capable of telling us BGR not supported if (fmt == Format.B8G8R8A8_UNorm) { fmt = Format.R8G8B8A8_UNorm; } DX11ResourcePoolEntry<DX11RenderTarget2D> elem; if (preservedtarget != null) { elem = preservedtarget; } else { elem = context.ResourcePool.LockRenderTarget(w, h, fmt, new SampleDescription(1, 0), mips, 0); locktargets.Add(elem); } DX11RenderTarget2D rt = elem.Element; if (this.FDepthIn.PluginIO.IsConnected && pi.UseDepth) { context.RenderTargetStack.Push(this.FDepthIn[0][context], true, elem.Element); } else { context.RenderTargetStack.Push(elem.Element); } if (pi.Clear) { elem.Element.Clear(new Color4(0, 0, 0, 0)); } #region Check for depth/blend preset bool validdepth = false; bool validblend = false; DepthStencilStateDescription ds = new DepthStencilStateDescription(); BlendStateDescription bs = new BlendStateDescription(); if (pi.DepthPreset != "") { try { ds = DX11DepthStencilStates.Instance.GetState(pi.DepthPreset); validdepth = true; } catch { } } if (pi.BlendPreset != "") { try { bs = DX11BlendStates.Instance.GetState(pi.BlendPreset); validblend = true; } catch { } } #endregion if (validdepth || validblend) { DX11RenderState state = new DX11RenderState(); if (validdepth) { state.DepthStencil = ds; } if (validblend) { state.Blend = bs; } context.RenderStateStack.Push(state); } r.RenderWidth = w; r.RenderHeight = h; r.BackBuffer = elem.Element; this.varmanager.ApplyGlobal(shaderdata.ShaderInstance); //Apply settings (note that textures swap is handled later) this.varmanager.ApplyPerObject(context, shaderdata.ShaderInstance, or, i); //Bind last render target this.BindTextureSemantic(shaderdata.ShaderInstance.Effect, "PREVIOUS", lastrt); this.BindPassIndexSemantic(shaderdata.ShaderInstance.Effect, j); this.BindPassIterIndexSemantic(shaderdata.ShaderInstance.Effect, kiter); if (this.FDepthIn.PluginIO.IsConnected) { if (this.FDepthIn[0].Contains(context)) { this.BindTextureSemantic(shaderdata.ShaderInstance.Effect, "DEPTHTEXTURE", this.FDepthIn[0][context]); } } //Apply pass and draw quad pass.Apply(ctx); if (pi.ComputeData.Enabled) { pi.ComputeData.Dispatch(context, w, h); context.CleanUpCS(); } else { ctx.ComputeShader.Set(null); context.Primitives.FullScreenTriangle.Draw(); ctx.OutputMerger.SetTargets(this.nullrtvs); } //Generate mips if applicable if (mips) { ctx.GenerateMips(rt.SRV); } if (!pi.KeepTarget) { preserve = false; rtlist.Add(rt); lastrt = rt; lasttmp = elem; preservedtarget = null; passcounter++; } else { preserve = true; preservedtarget = elem; } context.RenderTargetStack.Pop(); if (validblend || validdepth) { context.RenderStateStack.Pop(); } if (pi.HasState) { context.RenderStateStack.Apply(); } } } //Set last render target this.FOut[i][context] = lastrt; //Unlock all resources foreach (DX11ResourcePoolEntry<DX11RenderTarget2D> lt in locktargets) { lt.UnLock(); } //Keep lock on last rt, since don't want it overidden lasttmp.Lock(); this.lastframetargets.Add(lasttmp); } else { this.FOut[i][context] = this.FIn[i][context]; } } context.RenderStateStack.Pop(); this.OnEndQuery(context); //UnLock previous frame in applicable //if (previoustarget != null) { context.ResourcePool.Unlock(previoustarget); } }
public void Update(IPluginIO pin, DX11RenderContext context) { Device device = context.Device; DeviceContext ctx = context.CurrentDeviceContext; if (!this.FOutLinkBuffer[0].Contains(context) || !this.FOutOffsetBuffer[0].Contains(context) || FInGridcellCount.IsChanged) { this.FOutLinkBuffer[0].Dispose(context); this.FOutOffsetBuffer[0].Dispose(context); DX11RWStructuredBuffer lb = new DX11RWStructuredBuffer(device, FInEleCount[0], 8, eDX11BufferMode.Counter); DX11RWStructuredBuffer ob = new DX11RWStructuredBuffer(device, FInGridcellCount[0] * FInGridcellCount[0] * FInGridcellCount[0], 4, eDX11BufferMode.Counter); this.FOutLinkBuffer[0][context] = lb; this.FOutOffsetBuffer[0][context] = ob; } // clear offsetbuffer int[] mask = new int[4] { -1, -1, -1, -1 }; ctx.ClearUnorderedAccessView(FOutOffsetBuffer[0][context].UAV,mask); // load shader if (this.shader == null) { string basepath = "LinkedList.effects.LinkedList.fx"; DX11Effect effect = DX11Effect.FromResource(Assembly.GetExecutingAssembly(), basepath); this.shader = new DX11ShaderInstance(context, effect); } if (this.FInPcBuffer.PluginIO.IsConnected) { shader.SelectTechnique("BuildHash"); shader.SetBySemantic("POINTCLOUDBUFFER", FInPcBuffer[0][context].SRV); shader.SetBySemantic("POINTTRANSFORM", FInTransform[0]); shader.SetBySemantic("RWLINKBUFFER", FOutLinkBuffer[0][context].UAV, 0); shader.SetBySemantic("RWOFFSETBUFFER", FOutOffsetBuffer[0][context].UAV); shader.SetBySemantic("GRIDCELLSIZE", FInGridcellCount[0]); shader.ApplyPass(0); ctx.Dispatch((FInEleCount[0] + 63) / 64, 1, 1); context.CleanUp(); context.CleanUpCS(); } }
public void Update(IPluginIO pin, DX11RenderContext context) { Device device = context.Device; DeviceContext ctx = context.CurrentDeviceContext; if (!this.deviceshaderdata.ContainsKey(context)) { this.deviceshaderdata.Add(context, new DX11ShaderData(context)); this.deviceshaderdata[context].SetEffect(this.FShader); } DX11ShaderData shaderdata = this.deviceshaderdata[context]; if (this.shaderupdated) { shaderdata.SetEffect(this.FShader); this.shaderupdated = false; } context.RenderStateStack.Push(new DX11RenderState()); this.OnBeginQuery(context); //Clear shader stages shaderdata.ResetShaderStages(ctx); context.Primitives.ApplyFullTriVS(); foreach (DX11ResourcePoolEntry<DX11RenderTarget2D> rt in this.lastframetargets) { rt.UnLock(); } this.lastframetargets.Clear(); DX11ObjectRenderSettings or = new DX11ObjectRenderSettings(); int wi, he; for (int i = 0; i < this.spmax; i++) { if (this.FInEnabled[i]) { List<DX11ResourcePoolEntry<DX11RenderTarget2D>> locktargets = new List<DX11ResourcePoolEntry<DX11RenderTarget2D>>(); DX11Texture2D initial; if (this.FIn.PluginIO.IsConnected) { if (this.FInUseDefaultSize[0]) { initial = context.DefaultTextures.WhiteTexture; wi = (int)this.FInSize[0].X; he = (int)this.FInSize[0].Y; } else { initial = this.FIn[i][context]; if (initial != null) { wi = initial.Width; he = initial.Height; } else { initial = context.DefaultTextures.WhiteTexture; wi = (int)this.FInSize[0].X; he = (int)this.FInSize[0].Y; } } } else { initial = context.DefaultTextures.WhiteTexture; wi = (int)this.FInSize[0].X; he = (int)this.FInSize[0].Y; } DX11RenderSettings r = new DX11RenderSettings(); r.RenderWidth = wi; r.RenderHeight = he; if (this.FInSemantics.PluginIO.IsConnected) { r.CustomSemantics.AddRange(this.FInSemantics.ToArray()); } if (this.FInResSemantics.PluginIO.IsConnected) { r.ResourceSemantics.AddRange(this.FInResSemantics.ToArray()); } this.varmanager.SetGlobalSettings(shaderdata.ShaderInstance, r); this.varmanager.ApplyGlobal(shaderdata.ShaderInstance); DX11Texture2D lastrt = initial; DX11ResourcePoolEntry<DX11RenderTarget2D> lasttmp = null; List<DX11Texture2D> rtlist = new List<DX11Texture2D>(); //Bind Initial (once only is ok) this.BindTextureSemantic(shaderdata.ShaderInstance.Effect, "INITIAL", initial); if (this.persistedframe != null) { this.BindSemanticSRV(shaderdata.ShaderInstance.Effect, "LASTFRAME", persistedframe.SRV); } //Go trough all passes EffectTechnique tech = shaderdata.ShaderInstance.Effect.GetTechniqueByIndex(tid); for (int j = 0; j < tech.Description.PassCount; j++) { ImageShaderPass pi = this.varmanager.passes[j]; EffectPass pass = tech.GetPassByIndex(j); if (j > 0) { int pid = j - 1; string pname = "PASSRESULT" + pid; this.BindTextureSemantic(shaderdata.ShaderInstance.Effect, pname, rtlist[pid]); } Format fmt = initial.Format; if (pi.CustomFormat) { fmt = pi.Format; } bool mips = pi.Mips; int w, h; if (j == 0) { h = he; w = wi; } else { h = pi.Reference == ImageShaderPass.eImageScaleReference.Initial ? he : lastrt.Height; w = pi.Reference == ImageShaderPass.eImageScaleReference.Initial ? wi : lastrt.Width; } if (pi.DoScale) { h = Convert.ToInt32((float)h * pi.Scale); w = Convert.ToInt32((float)w * pi.Scale); h = Math.Max(h, 1); w = Math.Max(w, 1); } //Check format support for render target, and default to rgb8 if not if (!context.IsSupported(FormatSupport.RenderTarget, fmt)) { fmt = Format.R8G8B8A8_UNorm; } //Since device is not capable of telling us BGR not supported if (fmt == Format.B8G8R8A8_UNorm) { fmt = Format.R8G8B8A8_UNorm; } DX11ResourcePoolEntry<DX11RenderTarget2D> elem = context.ResourcePool.LockRenderTarget(w, h, fmt, new SampleDescription(1, 0), mips, 0); locktargets.Add(elem); DX11RenderTarget2D rt = elem.Element; ctx.OutputMerger.SetTargets(rt.RTV); r.RenderWidth = w; r.RenderHeight = h; r.BackBuffer = rt; this.varmanager.ApplyGlobal(shaderdata.ShaderInstance); //Apply settings (note that textures swap is handled later) this.varmanager.ApplyPerObject(context, shaderdata.ShaderInstance, or, i); Viewport vp = new Viewport(); vp.Width = rt.Width; vp.Height = rt.Height; ctx.Rasterizer.SetViewports(vp); //Bind last render target this.BindTextureSemantic(shaderdata.ShaderInstance.Effect, "PREVIOUS", lastrt); //Apply pass and draw quad pass.Apply(ctx); if (pi.ComputeData.Enabled) { pi.ComputeData.Dispatch(context, w, h); context.CleanUpCS(); } else { ctx.ComputeShader.Set(null); context.Primitives.FullScreenTriangle.Draw(); ctx.OutputMerger.SetTargets(this.nullrtvs); } //Generate mips if applicable if (pi.Mips) { ctx.GenerateMips(rt.SRV); } rtlist.Add(rt); lastrt = rt; lasttmp = elem; } //Set last render target this.FOut[i][context] = lastrt; //Unlock all resources foreach (DX11ResourcePoolEntry<DX11RenderTarget2D> lt in locktargets) { lt.UnLock(); } //Keep lock on last rt, since don't want it overidden lasttmp.Lock(); //this.lastframetargets. //this.lasttarget = lasttmp; this.lastframetargets.Add(lasttmp); //previousrts[context] = lasttmp.Element; } else { this.FOut[i][context] = this.FIn[i][context]; } } context.RenderStateStack.Pop(); this.OnEndQuery(context); //UnLock previous frame in applicable //if (previoustarget != null) { context.ResourcePool.Unlock(previoustarget); } }
public void Update(IPluginIO pin, DX11RenderContext context) { Device device = context.Device; DeviceContext ctx = context.CurrentDeviceContext; if ( !this.FOutPointcloudRingBuffer[0].Contains(context) || !this.FOutUpdatedBuffer[0].Contains(context) || !this.bCounter.Contains(context) || !this.bOffset.Contains(context) || this.FInPointcloudRingBufferSize.IsChanged || this.FInEleCount.IsChanged) { this.FOutPointcloudRingBuffer[0].Dispose(context); DX11RWStructuredBuffer brPointcloud = new DX11RWStructuredBuffer(device, FInPointcloudRingBufferSize[0], FInStride[0], eDX11BufferMode.Counter); this.FOutPointcloudRingBuffer[0][context] = brPointcloud; this.FOutUpdatedBuffer[0].Dispose(context); DX11RWStructuredBuffer brUpdated = new DX11RWStructuredBuffer(device, FInPointcloudRingBufferSize[0], 4, eDX11BufferMode.Counter); this.FOutUpdatedBuffer[0][context] = brUpdated; this.bOffset.Dispose(context); this.bOffset[context] = new DX11RawBuffer(device, 16); this.bCounter.Dispose(context); this.bCounter[context] = new DX11RWStructuredBuffer(device, FInEleCount[0], 4, eDX11BufferMode.Counter); } // load shader if (this.shader == null) { string basepath = "RingBuffer.effects.RingBuffer.fx"; DX11Effect effect = DX11Effect.FromResource(Assembly.GetExecutingAssembly(), basepath); this.shader = new DX11ShaderInstance(context, effect); } if (this.FInPointcloudBuffer.PluginIO.IsConnected && FInSet[0] && currentFrame != FHDEHost.FrameTime) { currentFrame = FHDEHost.FrameTime; // prevents to execute this a second time int[] mask = new int[4] { 0, 0, 0, 0 }; ctx.ClearUnorderedAccessView(FOutUpdatedBuffer[0][context].UAV, mask); shader.SelectTechnique("AddPoints"); shader.SetBySemantic("POINTCLOUDBUFFER", FInPointcloudBuffer[0][context].SRV); shader.SetBySemantic("POINTCLOUDCOUNTBUFFER", FInCountBuffer[0][context].SRV); shader.SetBySemantic("POINTCLOUDRINGBUFFER", FOutPointcloudRingBuffer[0][context].UAV, FInPointcloudRingBufferSize[0]); shader.SetBySemantic("UPDATEDRINGBUFFER", FOutUpdatedBuffer[0][context].UAV, FInPointcloudRingBufferSize[0]); shader.SetBySemantic("POINTCLOUDRINGBUFFERSIZE", FInPointcloudRingBufferSize[0]); shader.SetBySemantic("OFFSETBUFFER", this.bOffset[context].SRV); shader.SetBySemantic("COUNTERBUFFER", this.bCounter[context].UAV, 0); shader.ApplyPass(0); ctx.Dispatch((FInEleCount[0] + 63) / 64, 1, 1); ctx.CopyStructureCount(this.bCounter[context].UAV, this.bOffset[context].Buffer, 0); shader.SelectTechnique("CalcOffset"); shader.ApplyPass(0); ctx.Dispatch(1, 1, 1); context.CleanUp(); context.CleanUpCS(); } }
public void Update(IPluginIO pin, DX11RenderContext context) { Device device = context.Device; DeviceContext ctx = context.CurrentDeviceContext; if (!this.FOutPointcloudBuffer[0].Contains(context) || !this.FOutIndexBuffer[0].Contains(context) || this.FInEleCount.IsChanged ) { this.FOutPointcloudBuffer[0].Dispose(context); this.FOutIndexBuffer[0].Dispose(context); DX11RWStructuredBuffer pcBuffer = new DX11RWStructuredBuffer(device, FInEleCount[0], FInStride[0], eDX11BufferMode.Counter); DX11RWStructuredBuffer idBuffer = new DX11RWStructuredBuffer(device, FInEleCount[0], 4, eDX11BufferMode.Counter); this.FOutPointcloudBuffer[0][context] = pcBuffer; this.FOutIndexBuffer[0][context] = idBuffer; } // clear offsetbuffer int[] mask = new int[4] { -1, -1, -1, -1 }; ctx.ClearUnorderedAccessView(FOutIndexBuffer[0][context].UAV,mask); // load shader if (this.shader == null) { string basepath = "RingBufferIndexing.effects.RingBufferIndexing.fx"; DX11Effect effect = DX11Effect.FromResource(Assembly.GetExecutingAssembly(), basepath); this.shader = new DX11ShaderInstance(context, effect); } if (this.FInPointcloudBuffer.PluginIO.IsConnected /* && currentFrame != FHDEHost.FrameTime*/) { currentFrame = FHDEHost.FrameTime; // prevents to execute this a second time shader.SelectTechnique("BuildHash"); shader.SetBySemantic("POINTCLOUDBUFFERIN", FInPointcloudBuffer[0][context].SRV); shader.SetBySemantic("POINTCLOUDBUFFEROUT", FOutPointcloudBuffer[0][context].UAV, 0); shader.SetBySemantic("INDEXBUFFER", FOutIndexBuffer[0][context].UAV); shader.ApplyPass(0); ctx.Dispatch((FInEleCount[0] + 63) / 64, 1, 1); context.CleanUp(); context.CleanUpCS(); } }