public void Evaluate(int SpreadMax) { if (this.FPointer.IsChanged) { this.FInvalidate = true; } else return; SpreadMax = FPointer.SliceCount; var oldCount = FTextureOutput.SliceCount; if (FTextureOutput[0] == null) oldCount = 0; for (int i = SpreadMax; i < oldCount;i++ ) { FTextureOutput[i].Dispose(); } this.FValid.SliceCount = SpreadMax; this.FTextureOutput.SliceCount = SpreadMax; for (int i = oldCount; i < SpreadMax; i++) { FTextureOutput[i] = new DX11Resource<DX11Texture2D>(); } }
public void Evaluate(int SpreadMax) { if ( this.FOutPointcloudRingBuffer[0] == null || this.FOutUpdatedBuffer[0] == null || this.bCounter == null || this.bOffset == null) { this.FOutPointcloudRingBuffer[0] = new DX11Resource<IDX11RWStructureBuffer>(); this.FOutUpdatedBuffer[0] = new DX11Resource<IDX11RWStructureBuffer>(); this.bOffset = new DX11Resource<DX11RawBuffer>(); this.bCounter = new DX11Resource<IDX11RWStructureBuffer>(); } }
public void Evaluate(int SpreadMax) { if (FProcessor == null) FProcessor = new ProcessDestination<AsTextureDX11Instance>(FPinInImage); bool needsInit = FProcessor.CheckInputSize(); if (needsInit) { foreach (var textureOut in FTextureOutput) { if (textureOut != null) textureOut.Dispose(); } FTextureOutput.SliceCount = FProcessor.SliceCount; for (int i = 0; i < FProcessor.SliceCount; i++) { var textureSlice = new DX11Resource<DX11DynamicTexture2D>(); FProcessor[i].OutputSlice = textureSlice; FTextureOutput[i] = textureSlice; } } }
public void WriteImage(DX11Resource<DX11Texture2D> resource, FeralTic.DX11.DX11RenderContext context, string filename, SlimDX.Direct3D11.ImageFileFormat format) { var saver = GetAvailableSaver(); saver.Save(resource[context], context, filename, format); }
public void Render(DX11RenderContext context, DX11RenderSettings settings) { Device device = context.Device; DeviceContext ctx = context.CurrentDeviceContext; bool popstate = false; bool multistate = this.FInState.IsConnected && this.FInState.SliceCount > 1; bool stateConnected = this.FInState.IsConnected; if (this.FInEnabled[0]) { //In that case we do not care about geometry, but only apply pass for globals if (settings.RenderHint == eRenderHint.ApplyOnly) { this.ApplyOnly(context, settings); return; } if (settings.RenderHint == eRenderHint.Collector) { this.Collect(context, settings); return; } DX11ShaderData shaderdata = this.deviceshaderdata[context]; if ((shaderdata.IsValid && (this.geomconnected || settings.Geometry != null) && this.spmax > 0 && this.varmanager.SetGlobalSettings(shaderdata.ShaderInstance, settings)) || this.FInApplyOnly[0]) { this.OnBeginQuery(context); //Select preferred technique if available if (settings.PreferredTechniques.Count == 0 && this.techniqueindex != this.FInTechnique[0].Index) { this.techniqueindex = this.FInTechnique[0].Index; this.techniquechanged = true; } else if (settings.PreferredTechniques.Count > 0) { int i = settings.GetPreferredTechnique(this.FShader); if (i == -1) { i = this.FInTechnique[0].Index; } if (i != this.techniqueindex) { this.techniqueindex = i; this.techniquechanged = true; } } //Need to build input layout if (this.FGeometry.IsChanged || this.techniquechanged || shaderdata.LayoutValid.Count == 0) { shaderdata.Update(this.techniqueindex, 0, this.FGeometry); this.FOutLayoutValid.AssignFrom(shaderdata.LayoutValid); this.FOutLayoutMsg.AssignFrom(shaderdata.LayoutMsg); int errorCount = 0; StringBuilder sbMsg = new StringBuilder(); sbMsg.Append("Invalid layout detected for slices:"); for (int i = 0; i < shaderdata.LayoutValid.Count; i++) { if (shaderdata.LayoutValid[i] == false) { errorCount++; sbMsg.Append(i + ","); } } if (errorCount > 0) { this.FHost.Log(TLogType.Warning, sbMsg.ToString()); } this.techniquechanged = false; } if (this.stateconnected && !multistate) { context.RenderStateStack.Push(this.FInState[0]); popstate = true; } ShaderPipelineState pipelineState = null; if (!settings.PreserveShaderStages) { shaderdata.ResetShaderStages(ctx); } else { pipelineState = new ShaderPipelineState(context); } settings.DrawCallCount = spmax; //Set number of draw calls var objectsettings = this.objectSettings[context]; #pragma warning disable 0618 objectsettings.GeometryFromLayer = false; #pragma warning restore 0618 var orderedobjectsettings = this.orderedObjectSettings[context]; var variableCache = this.shaderVariableCache[context]; variableCache.ApplyGlobals(settings); //IDX11Geometry drawgeom = null; objectsettings.Geometry = null; DX11Resource <IDX11Geometry> pg = null; bool doOrder = false; List <int> orderedSlices = null; if (settings.LayerOrder != null && settings.LayerOrder.Enabled) { orderedobjectsettings.Clear(); for (int i = 0; i < this.spmax; i++) { DX11ObjectRenderSettings objSettings = new DX11ObjectRenderSettings(); objSettings.DrawCallIndex = i; objSettings.Geometry = null; objSettings.IterationCount = 1; objSettings.IterationIndex = 0; objSettings.WorldTransform = this.mworld[i % this.mworldcount]; objSettings.RenderStateTag = stateConnected ? this.FInState[i].Tag : null; orderedobjectsettings.Add(objSettings); } orderedSlices = settings.LayerOrder.Reorder(settings, orderedobjectsettings); doOrder = true; } int drawCount = doOrder ? orderedSlices.Count : this.spmax; if (this.spmax == 0) { drawCount = 0; } bool singleGeometry = this.FGeometry.SliceCount == 1 || settings.Geometry != null; if (settings.Geometry != null && this.FGeometry.IsConnected == false) { #pragma warning disable 0618 objectsettings.GeometryFromLayer = true; #pragma warning restore 0618 objectsettings.Geometry = settings.Geometry; singleGeometry = true; if (!shaderdata.SetInputAssemblerFromLayer(ctx, objectsettings.Geometry, 0)) { return; } } else if (singleGeometry) { pg = this.FGeometry[0]; objectsettings.Geometry = pg[context]; if (objectsettings.Geometry == null) { objectsettings.Geometry = new DX11InvalidGeometry(); } shaderdata.SetInputAssembler(ctx, objectsettings.Geometry, 0); } if (!multistate) { objectsettings.RenderStateTag = this.FInState[0] != null ? this.FInState[0].Tag : null; } for (int i = 0; i < drawCount; i++) { int idx = doOrder ? orderedSlices[i] : i; if (multistate) { context.RenderStateStack.Push(this.FInState[idx]); objectsettings.RenderStateTag = this.FInState[idx] != null ? this.FInState[idx].Tag : null; } if (shaderdata.IsLayoutValid(idx) || settings.Geometry != null) { objectsettings.IterationCount = this.FIter[idx]; for (int k = 0; k < objectsettings.IterationCount; k++) { objectsettings.IterationIndex = k; if (!singleGeometry) { if (settings.Geometry == null) { if (this.FGeometry[idx] != pg) { pg = this.FGeometry[idx]; objectsettings.Geometry = pg[context]; if (objectsettings.Geometry == null) { objectsettings.Geometry = new DX11InvalidGeometry(); } shaderdata.SetInputAssembler(ctx, objectsettings.Geometry, idx); } } else { objectsettings.Geometry = settings.Geometry; shaderdata.SetInputAssembler(ctx, objectsettings.Geometry, idx); } } //Prepare settings objectsettings.DrawCallIndex = idx; objectsettings.WorldTransform = this.mworld[idx % this.mworldcount]; if (settings.ValidateObject(objectsettings)) { variableCache.ApplySlice(objectsettings, idx); for (int ip = 0; ip < shaderdata.PassCount; ip++) { shaderdata.ApplyPass(ctx, ip); if (settings.DepthOnly) { ctx.PixelShader.Set(null); } objectsettings.Geometry.Draw(); } } } } if (multistate) { context.RenderStateStack.Pop(); } } shaderdata.ShaderInstance.CleanUp(); if (pipelineState != null) { pipelineState.Restore(context); } this.OnEndQuery(context); } //this.query.End(); } if (popstate) { context.RenderStateStack.Pop(); } else { //Since shaders can define their own states, reapply top of the stack context.RenderStateStack.Apply(); } if (this.FInLayer.IsConnected && this.FInEnabled[0]) { this.FInLayer.RenderAll(context, settings); } }
unsafe public void Update(IPluginIO pin, DX11RenderContext context) { FOutLog[0] = _message; //DX11RenderContext sharedContext = this.AssignedContext; try { if (FInvalidate == true & this.FTextureIn.PluginIO.IsConnected) { var dummyTex = new DX11Resource <DX11Texture2D>(); interop = new D3DAngleInterop(); angleSurface = interop.CreateOffscreenSurface(600, 600); //D3DShareHandle = interop.GetD3DSharedHandleForSurface(angleSurface, 500, 500); // angleSurface = interop.CreateSharedSurfaceFromTexture(v4SharedHandle, 500, 500); IntPtr share = interop.GetD3DSharedHandleForSurface(angleSurface, 600, 600); Texture2D tex = context.Device.OpenSharedResource <Texture2D>(share); ShaderResourceView srv = new ShaderResourceView(context.Device, tex); DX11Texture2D resource = DX11Texture2D.FromTextureAndSRV(context, tex, srv); SlimDX.DXGI.Resource angleRes = new SlimDX.DXGI.Resource(tex); interop.EnsureContext(); //Texture2D tex; SlimDX.DXGI.Resource sharedRes; IntPtr dxHandle = new IntPtr(); IntPtr shared; try { //Texture2DDescription desc = tex.Description; //desc.Height = 256; //desc.Width = 256; Texture2D inTex = this.FTextureIn[0][context].Resource; if (inTex == null) { _message = "thiss isn't real"; } Texture2DDescription desc, testDesc; try { testDesc = inTex.Description; desc = inTex.Description; } catch (Exception e) { testDesc = tex.Description; desc = tex.Description; _message = e.ToString(); } // = inTex.Description; desc.Height = 256; desc.Width = 256; desc.BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget; desc.OptionFlags = ResourceOptionFlags.Shared; desc.MipLevels = 1; if (desc.MipLevels != testDesc.MipLevels) { _message = "MIPS"; } if (desc.ArraySize != testDesc.ArraySize) { _message = "Array"; } if (desc.CpuAccessFlags != testDesc.CpuAccessFlags) { _message = "CPU"; } if (desc.SampleDescription != testDesc.SampleDescription) { _message = "SAMPLE"; } if (desc.Usage != testDesc.Usage) { _message = "Usage"; } t = new Texture2D(context.Device, desc); sharedRes = new SlimDX.DXGI.Resource(t); dxHandle = sharedRes.SharedHandle; //_message = FTexturePointer[0].ToString(); ////FTexturePointer ////int p = unchecked((int)this.FTexturePointer[0]); ////IntPtr shared = new IntPtr(FTexturePointer[0]); //if (shared != null) //{ shared = interop.CreateSharedSurfaceFromTexture(dxHandle, desc.Width, desc.Height); //shared = interop.CreateSharedSurfaceFromTexture(share, 600, 600); //} FTextureOutput[0] = new DX11Resource <DX11Texture2D>(); //FTextureOutput[0][context] = t; ShaderResourceView tSRV = new ShaderResourceView(context.Device, t); DX11Texture2D tRes = DX11Texture2D.FromTextureAndSRV(context, t, tSRV); FTextureOutput[0][context] = tRes; GL.GenTextures(1, glTex); GL.BindTexture(TextureTarget.Texture2D, glTex[0]); interop.BindSurfaceToTexture(shared); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMinFilter.Nearest); //GL.ActiveTexture(TextureUnit.Texture0); //GL.BindTexture(); } catch (Exception e) { //_message = e.ToString(); } var vert = @"//#version 300 es attribute vec4 vPosition; attribute vec2 aTexCoord; varying vec2 vTexCoord; void main() { gl_Position = vPosition; vTexCoord = aTexCoord; }"; var frag = @"//#version 300 es precision mediump float; varying vec2 vTexCoord; uniform vec4 color; uniform sampler2D iChannel0; void main() { vec2 uv = vTexCoord; vec3 col = texture2D(iChannel0, uv).xyz; gl_FragColor = vec4(col, 1.0); }"; var status = new int[1]; const int GL_TRUE = 1; _program = GL.CreateProgram(); var vs = GL.CreateShader(ShaderType.VertexShader); GL.ShaderSource(vs, vert); GL.CompileShader(vs); GL.GetShader(vs, ShaderParameter.CompileStatus, status); if (status[0] != GL_TRUE) { var error = GL.GetShaderInfoLog(vs); throw new Exception(error); } var fs = GL.CreateShader(ShaderType.FragmentShader); GL.ShaderSource(fs, frag); GL.CompileShader(fs); GL.GetShader(fs, ShaderParameter.CompileStatus, status); if (status[0] != GL_TRUE) { var error = GL.GetShaderInfoLog(fs); throw new Exception(error); } GL.AttachShader(_program, vs); GL.AttachShader(_program, fs); GL.BindAttribLocation(_program, 0, "vPosition"); GL.LinkProgram(_program); //GL.DeleteShader(vs); //GL.DeleteShader(fs); GL.UseProgram(_program); GL.GetProgram(_program, GetProgramParameterName.LinkStatus, status); if (status[0] != GL_TRUE) { var error = GL.GetProgramInfoLog(_program); throw new Exception(error); } uColor = GL.GetUniformLocation(_program, "color"); mPositionLoc = GL.GetAttribLocation(_program, "vPosition"); mTexCoordLoc = GL.GetAttribLocation(_program, "aTexCoord"); FPointer[0] = share.ToInt64(); FValid[0] = true; FInvalidate = false; //int[] glTex = new int[4]; //GL.GenTextures(1, glTex); //GL.TexParameter(GL) //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMagFilter.Nearest); } //this.AssignedContext.CurrentDeviceContext.CopyResource(this.FTextureIn[0][context].Resource, t); context.CurrentDeviceContext.CopyResource(FTextureIn[0][context].Resource, t); interop.MakeCurrent(angleSurface); float[] vVertices = new float[] { 0.0f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f }; float[] vertices = new float[] { -0.5f, 0.5f, 0.0f, // Position 0 0.0f, 0.0f, // TexCoord 0 -0.5f, -0.5f, 0.0f, // Position 1 0.0f, 1.0f, // TexCoord 1 0.5f, -0.5f, 0.0f, // Position 2 1.0f, 1.0f, // TexCoord 2 0.5f, 0.5f, 0.0f, // Position 3 1.0f, 0.0f // TexCoord 3 }; short[] indices = { 0, 1, 2, 0, 2, 3 }; GL.Viewport(0, 0, 500, 500); GL.ClearColor((float)ColorIn[0].R, (float)ColorIn[0].G, (float)ColorIn[0].B, 0.0f); GL.Clear(ClearBufferMask.ColorBufferBit); GL.UseProgram(_program); Color4 color = new Color4((float)ColorIn[0].R, (float)ColorIn[0].G, (float)ColorIn[0].B, (float)ColorIn[0].A); GL.Uniform4(uColor, color); //Vertex Attributes GL.VertexAttribPointer(mPositionLoc, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), vertices); // Load the texture coordinate GL.VertexAttribPointer(mTexCoordLoc, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), vertices.Skip(3).ToArray()); //GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, vVertices); //GL.EnableVertexAttribArray(0); GL.EnableVertexAttribArray(mPositionLoc); GL.EnableVertexAttribArray(mTexCoordLoc); //Texture Bind GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture2D, glTex[0]); int iChannel0 = GL.GetUniformLocation(_program, "iChannel0"); GL.Uniform1(iChannel0, 0); GL.DrawArrays(PrimitiveType.Triangles, 0, 3); GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedShort, indices); interop.SwapBuffers(angleSurface); GL.Finish(); //_message = "running update"; } catch (Exception e) { //_message = e.ToString(); //_message = e.ToString(); } }
public void Render(IPluginIO pin, DX11RenderContext context, DX11RenderSettings settings) { Device device = context.Device; DeviceContext ctx = context.CurrentDeviceContext; bool popstate = false; bool multistate = this.FInState.PluginIO.IsConnected && this.FInState.SliceCount > 1; if (this.FInEnabled[0]) { if (settings.RenderHint == eRenderHint.Collector) { if (this.FGeometry.PluginIO.IsConnected) { DX11ObjectGroup group = new DX11ObjectGroup(); group.ShaderName = this.Source.Name; group.Semantics.AddRange(settings.CustomSemantics); if (this.FGeometry.SliceCount == 1) { IDX11Geometry g = this.FGeometry[0][context]; if (g.Tag != null) { DX11RenderObject o = new DX11RenderObject(); o.ObjectType = g.PrimitiveType; o.Descriptor = g.Tag; o.Transforms = new Matrix[spmax]; for (int i = 0; i < this.spmax; i++) { o.Transforms[i] = this.mworld[i % this.mworldcount]; } group.RenderObjects.Add(o); settings.SceneDescriptor.Groups.Add(group); } } else { for (int i = 0; i < this.spmax; i++) { IDX11Geometry g = this.FGeometry[i][context]; if (g.Tag != null) { DX11RenderObject o = new DX11RenderObject(); o.ObjectType = g.PrimitiveType; o.Descriptor = g.Tag; o.Transforms = new Matrix[1]; o.Transforms[0] = this.mworld[i % this.mworldcount]; group.RenderObjects.Add(o); } } settings.SceneDescriptor.Groups.Add(group); } } return; } DX11ShaderData shaderdata = this.deviceshaderdata[context]; if ((shaderdata.IsValid && (this.geomconnected || settings.Geometry != null) && this.spmax > 0 && this.varmanager.SetGlobalSettings(shaderdata.ShaderInstance, settings)) || this.FInApplyOnly[0]) { this.OnBeginQuery(context); //Need to build input layout if (this.FGeometry.IsChanged || this.FInTechnique.IsChanged || shaderdata.LayoutValid.Count == 0) { shaderdata.Update(this.FInTechnique[0].Index, 0, this.FGeometry); this.FOutLayoutValid.AssignFrom(shaderdata.LayoutValid); this.FOutLayoutMsg.AssignFrom(shaderdata.LayoutMsg); } if (this.stateconnected && !multistate) { context.RenderStateStack.Push(this.FInState[0]); popstate = true; } if (!settings.PreserveShaderStages) { shaderdata.ResetShaderStages(ctx); } settings.DrawCallCount = spmax; //Set number of draw calls this.varmanager.ApplyGlobal(shaderdata.ShaderInstance); //IDX11Geometry drawgeom = null; objectsettings.Geometry = null; DX11Resource <IDX11Geometry> pg = null; for (int i = 0; i < this.spmax; i++) { if (multistate) { context.RenderStateStack.Push(this.FInState[i]); } if (shaderdata.IsLayoutValid(i) || settings.Geometry != null) { objectsettings.IterationCount = this.FIter[i]; for (int k = 0; k < objectsettings.IterationCount; k++) { objectsettings.IterationIndex = k; if (settings.Geometry == null) { if (this.FGeometry[i] != pg) { pg = this.FGeometry[i]; objectsettings.Geometry = pg[context]; shaderdata.SetInputAssembler(ctx, objectsettings.Geometry, i); } } else { objectsettings.Geometry = settings.Geometry; shaderdata.SetInputAssembler(ctx, objectsettings.Geometry, i); } //Prepare settings objectsettings.DrawCallIndex = i; objectsettings.WorldTransform = this.mworld[i % this.mworldcount]; if (settings.ValidateObject(objectsettings)) { this.varmanager.ApplyPerObject(context, shaderdata.ShaderInstance, this.objectsettings, i); shaderdata.ApplyPass(ctx); if (settings.DepthOnly) { ctx.PixelShader.Set(null); } objectsettings.Geometry.Draw(); shaderdata.ShaderInstance.CleanUp(); } } } if (multistate) { context.RenderStateStack.Pop(); } } this.OnEndQuery(context); } //this.query.End(); } if (popstate) { context.RenderStateStack.Pop(); } else { //Since shaders can define their own states, reapply top of the stack context.RenderStateStack.Apply(); } }
public void Process(DX11Resource <DX11Texture2D> input, FeralTic.DX11.DX11RenderContext context) { DX11Texture2D t = input[context]; var height = t.Height; var width = t.Width; var imageLink = Output; var imageAttributes = imageLink.ImageAttributes; var desiredImageFormat = ToOpenCVFormat(t.Format); if (desiredImageFormat == TColorFormat.UnInitialised) { throw (new Exception("No suitible image type available for this texture type " + t.Format.ToString())); } //-- //check attributes and reinitialise the image and offscreen buffer if we haven't got the right description ready // if (imageAttributes == null || FOffscreenBuffer == null || !imageAttributes.Initialised || FOffscreenBuffer.Description.Format != t.Description.Format || imageAttributes.Width != t.Width || imageAttributes.Height != t.Height || imageAttributes.ColorFormat != desiredImageFormat) { if (FOffscreenBuffer != null) { FOffscreenBuffer.Dispose(); } var description = new Texture2DDescription() { Width = width, Height = height, Format = t.Format, MipLevels = 1, Usage = ResourceUsage.Staging, BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, SampleDescription = new SampleDescription(1, 0), ArraySize = 1 }; FOffscreenBuffer = new Texture2D(context.Device, description); imageLink.Initialise(new CVImageAttributes(desiredImageFormat, t.Width, t.Height)); } // //-- //-- //copy the texture to offscreen buffer // context.CurrentDeviceContext.CopyResource(t.Resource, FOffscreenBuffer); // //-- //-- //copy the data out of the offscreen buffer // var surface = FOffscreenBuffer.AsSurface(); var bytesPerRow = imageAttributes.Stride; var data = MapForRead(context.CurrentDeviceContext); lock (imageLink.BackLock) { var image = imageLink.BackImage; try { var source = data.Data.DataPointer; image.SetPixels(source); var destination = image.Data; for (int row = 0; row < t.Height; row++) { CopyMemory(destination, source, bytesPerRow); source += data.RowPitch; destination += bytesPerRow; } } finally { UnMap(context.CurrentDeviceContext); } } // //-- imageLink.Swap(); }
public void Evaluate(int SpreadMax) { if (FEnabled[0] && FController.IsConnected && FController.SliceCount > 0 && FController.TryGetSlice(0) != null) { if (fcr == 0) { FImgTexOutL.SliceCount = FDistMapL.SliceCount = FImgTexOutR.SliceCount = FDistMapR.SliceCount = FController.SliceCount; //Connection.GetConnection(). FController[0].FrameReady += (sender, args) => { if (!ImageReady) { return; } FFrameID[0] = args.frame.Id; ImageReady = false; }; FController[0].ImageReady += (sender, args) => { ValidImage = args.image; imagedataL = args.image.Data(Image.CameraType.LEFT); imagedataR = args.image.Data(Image.CameraType.RIGHT); FInvalidate = true; ImageReady = true; //FImageFailed[0] = false; }; } for (int i = 0; i < FImgTexOutL.SliceCount; i++) { if (FImgTexOutL[i] == null) { FImgTexOutL[i] = new DX11Resource <DX11DynamicTexture2D>(); } if (FDistMapL[i] == null) { FDistMapL[i] = new DX11Resource <DX11DynamicTexture2D>(); } if (FImgTexOutR[i] == null) { FImgTexOutR[i] = new DX11Resource <DX11DynamicTexture2D>(); } if (FDistMapR[i] == null) { FDistMapR[i] = new DX11Resource <DX11DynamicTexture2D>(); } } if (FImgTexOutL.SliceCount > FController.SliceCount) { for (int i = FController.SliceCount; i < (FImgTexOutL.SliceCount - FController.SliceCount); i++) { FImgTexOutL[i]?.Dispose(); FDistMapL[i]?.Dispose(); FImgTexOutR[i]?.Dispose(); FDistMapR[i]?.Dispose(); } } FImgTexOutL.SliceCount = FDistMapL.SliceCount = FImgTexOutR.SliceCount = FDistMapR.SliceCount = FController.SliceCount; fcr++; } else { if (FImgTexOutL.SliceCount > 0) { for (int i = 0; i < FImgTexOutL.SliceCount; i++) { FImgTexOutL[i]?.Dispose(); FDistMapL[i]?.Dispose(); FImgTexOutR[i]?.Dispose(); FDistMapR[i]?.Dispose(); } FImgTexOutL.SliceCount = 0; FDistMapL.SliceCount = 0; FImgTexOutR.SliceCount = 0; FDistMapR.SliceCount = 0; } fcr = 0; } }
public void Update(DX11Resource<DX11DynamicTexture2D> textureSlice, DX11RenderContext context) { if (!this.Running || !FDataNewForTexture) { return; } FDataNewForTexture = false; DX11DynamicTexture2D tex; //create texture if necessary //should also check if properties (width,height) changed if (!textureSlice.Contains(context)) { tex = new DX11DynamicTexture2D(context, FFrameWidth, FFrameHeight, SlimDX.DXGI.Format.R8_UNorm); textureSlice[context] = tex; } else if (textureSlice[context].Width != this.FFrameWidth || textureSlice[context].Height != this.FFrameHeight) { textureSlice[context].Dispose(); tex = new DX11DynamicTexture2D(context, FFrameWidth, FFrameHeight, SlimDX.DXGI.Format.R8_UNorm); textureSlice[context] = tex; } else { tex = textureSlice[context]; } FDoubleBuffer.LockFront.AcquireReaderLock(100); try { //write data to surface if (FFrameWidth == tex.GetRowPitch()) { tex.WriteData(FDoubleBuffer.Front); } else { GCHandle pinnedArray = GCHandle.Alloc(FDoubleBuffer.Front, GCHandleType.Pinned); tex.WriteDataPitch(pinnedArray.AddrOfPinnedObject(), FDoubleBuffer.Front.Length); pinnedArray.Free(); } } catch { } finally { FDoubleBuffer.LockFront.ReleaseReaderLock(); } }
public void Evaluate(int SpreadMax) { this.FInvalidate = false; if (this.FOutput[0] == null) { this.FOutput[0] = new DX11Resource <DX11IndexedGeometry>(); } if (this.FInApply[0] || FFirst) { this.FFirst = false; this.FInput.Sync(); this.FInVerticesCount.Sync(); this.FInIndices.Sync(); this.FIndicesCount.Sync(); this.FInLayout.Sync(); this.FInTopology.Sync(); this.FInvalidate = true; DX11Resource <DX11IndexedGeometry> instance = this.FOutput[0]; this.FOutput[0] = instance; //Get Input Layout this.inputlayout = new InputElement[this.FInLayout.SliceCount]; this.vertexsize = 0; for (int i = 0; i < this.FInLayout.SliceCount; i++) { if (this.FInLayout.PluginIO.IsConnected && this.FInLayout[i] != null) { this.inputlayout[i] = this.FInLayout[i]; } else { //Set deault, can do better here this.inputlayout[i] = new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0); } this.vertexsize += FormatHelper.Instance.GetSize(this.inputlayout[i].Format); } InputLayoutFactory.AutoIndex(this.inputlayout); //Load Vertex Stream if (this.FVertexStream != null) { this.FVertexStream.Dispose(); } this.FVertexStream = new DataStream(this.FInVerticesCount[0] * this.vertexsize, true, true); for (int i = 0; i < this.FInVerticesCount[0] * (this.vertexsize / 4); i++) { this.FVertexStream.Write(this.FInput[i]); } this.FVertexStream.Position = 0; //Load index stream if (this.FIndexStream != null) { this.FIndexStream.Dispose(); } this.FIndexStream = new DataStream(this.FIndicesCount[0] * 4, true, true); for (int i = 0; i < this.FIndicesCount[0]; i++) { this.FIndexStream.Write(this.FInIndices[i]); } this.FIndexStream.Position = 0; } }
public void Render(IPluginIO pin, DX11RenderContext context, DX11RenderSettings settings) { Device device = context.Device; DeviceContext ctx = context.CurrentDeviceContext; bool popstate = false; if (this.FInEnabled[0]) { DX11ShaderData shaderdata = this.deviceshaderdata[context]; if ((shaderdata.IsValid && (this.geomconnected || settings.Geometry != null) && this.spmax > 0 && this.varmanager.SetGlobalSettings(shaderdata.ShaderInstance, settings)) || this.FInApplyOnly[0]) { this.OnBeginQuery(context); //Need to build input layout if (this.FGeometry.IsChanged || this.FInTechnique.IsChanged || shaderdata.LayoutValid.Count == 0) { shaderdata.Update(this.FInTechnique[0].Index, 0, this.FGeometry); this.FOutLayoutValid.AssignFrom(shaderdata.LayoutValid); this.FOutLayoutMsg.AssignFrom(shaderdata.LayoutMsg); } if (this.stateconnected) { context.RenderStateStack.Push(this.FInState[0]); popstate = true; } if (!settings.PreserveShaderStages) { shaderdata.ResetShaderStages(ctx); } settings.DrawCallCount = spmax; //Set number of draw calls this.varmanager.ApplyGlobal(shaderdata.ShaderInstance); //IDX11Geometry drawgeom = null; objectsettings.Geometry = null; DX11Resource <IDX11Geometry> pg = null; for (int i = 0; i < this.spmax; i++) { if (shaderdata.IsLayoutValid(i) || settings.Geometry != null) { objectsettings.IterationCount = this.FIter[i]; for (int k = 0; k < objectsettings.IterationCount; k++) { objectsettings.IterationIndex = k; if (settings.Geometry == null) { if (this.FGeometry[i] != pg) { pg = this.FGeometry[i]; objectsettings.Geometry = pg[context]; shaderdata.SetInputAssembler(ctx, objectsettings.Geometry, i); } } else { objectsettings.Geometry = settings.Geometry; shaderdata.SetInputAssembler(ctx, objectsettings.Geometry, i); } //Prepare settings objectsettings.DrawCallIndex = i; objectsettings.WorldTransform = this.mworld[i % this.mworldcount]; if (settings.ValidateObject(objectsettings)) { this.varmanager.ApplyPerObject(context, shaderdata.ShaderInstance, this.objectsettings, i); shaderdata.ApplyPass(ctx); if (settings.DepthOnly) { ctx.PixelShader.Set(null); } objectsettings.Geometry.Draw(); shaderdata.ShaderInstance.CleanUp(); } } } } this.OnEndQuery(context); } //this.query.End(); } if (popstate) { context.RenderStateStack.Pop(); } else { //Since shaders can define their own states, reapply top of the stack context.RenderStateStack.Apply(); } }
public void Process(DX11Resource<DX11Texture2D> input, FeralTic.DX11.DX11RenderContext context) { DX11Texture2D t = input[context]; var height = t.Height; var width = t.Width; var imageLink = Output; var imageAttributes = imageLink.ImageAttributes; var desiredImageFormat = ToOpenCVFormat(t.Format); if (desiredImageFormat == TColorFormat.UnInitialised) throw (new Exception("No suitible image type available for this texture type " + t.Format.ToString())); //-- //check attributes and reinitialise the image and offscreen buffer if we haven't got the right description ready // if (imageAttributes == null || FOffscreenBuffer == null || !imageAttributes.Initialised || FOffscreenBuffer.Description.Format != t.Description.Format || imageAttributes.Width != t.Width || imageAttributes.Height != t.Height || imageAttributes.ColorFormat != desiredImageFormat) { if (FOffscreenBuffer != null) FOffscreenBuffer.Dispose(); var description = new Texture2DDescription() { Width = width, Height = height, Format = t.Format, MipLevels = 1, Usage = ResourceUsage.Staging, BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, SampleDescription = new SampleDescription(1, 0), ArraySize = 1 }; FOffscreenBuffer = new Texture2D(context.Device, description); imageLink.Initialise(new CVImageAttributes(desiredImageFormat, t.Width, t.Height)); } // //-- //-- //copy the texture to offscreen buffer // context.CurrentDeviceContext.CopyResource(t.Resource, FOffscreenBuffer); // //-- //-- //copy the data out of the offscreen buffer // var surface = FOffscreenBuffer.AsSurface(); var bytesPerRow = imageAttributes.Stride; var data = MapForRead(context.CurrentDeviceContext); lock (imageLink.BackLock) { var image = imageLink.BackImage; try { var source = data.Data.DataPointer; image.SetPixels(source); var destination = image.Data; for (int row = 0; row < t.Height; row++) { CopyMemory(destination, source, bytesPerRow); source += data.RowPitch; destination += bytesPerRow; } } finally { UnMap(context.CurrentDeviceContext); } } // //-- imageLink.Swap(); }
public void Evaluate(int SpreadMax) { FInvalidate = false; if (FApply[0] || FFirst) { elstrides = 0; elcount = FElCount[0] < 0 ? Pd.InputSpreadMax : FElCount[0]; foreach (var pin in Pd.InputPins.Values) { pin.Spread.Sync(); elstrides += ((TypeDesc)pin.CustomData).Size; } FOutput.SliceCount = 1; FValid.SliceCount = 1; if (FOutput[0] == null) { FOutput[0] = new DX11Resource <IDX11ReadableStructureBuffer>(); } FInData.SetLength(0); for (int i = 0; i < elcount; i++) { foreach (var pin in Pd.InputPins.Values) { switch (((TypeDesc)pin.CustomData).TypeSig) { case TypeSignature.Float: var valf = (float)pin.Spread[i]; FInData.Write(BitConverter.GetBytes(valf), 0, 4); break; case TypeSignature.Uint: var valu = (uint)pin.Spread[i]; FInData.Write(BitConverter.GetBytes(valu), 0, 4); break; case TypeSignature.Float2: var valf2 = (Vector2D)pin.Spread[i]; FInData.Write(BitConverter.GetBytes((float)valf2.x), 0, 4); FInData.Write(BitConverter.GetBytes((float)valf2.y), 0, 4); break; case TypeSignature.Float3: var valf3 = (Vector3D)pin.Spread[i]; FInData.Write(BitConverter.GetBytes((float)valf3.x), 0, 4); FInData.Write(BitConverter.GetBytes((float)valf3.y), 0, 4); FInData.Write(BitConverter.GetBytes((float)valf3.z), 0, 4); break; case TypeSignature.Float4: var valf4 = (Vector4D)pin.Spread[i]; FInData.Write(BitConverter.GetBytes((float)valf4.x), 0, 4); FInData.Write(BitConverter.GetBytes((float)valf4.y), 0, 4); FInData.Write(BitConverter.GetBytes((float)valf4.z), 0, 4); FInData.Write(BitConverter.GetBytes((float)valf4.w), 0, 4); break; case TypeSignature.Uint2: var valu2 = (Vector2D)pin.Spread[i]; FInData.Write(BitConverter.GetBytes((uint)Math.Floor(valu2.x)), 0, 4); FInData.Write(BitConverter.GetBytes((uint)Math.Floor(valu2.y)), 0, 4); break; case TypeSignature.Uint3: var valu3 = (Vector3D)pin.Spread[i]; FInData.Write(BitConverter.GetBytes((uint)Math.Floor(valu3.x)), 0, 4); FInData.Write(BitConverter.GetBytes((uint)Math.Floor(valu3.y)), 0, 4); FInData.Write(BitConverter.GetBytes((uint)Math.Floor(valu3.z)), 0, 4); break; case TypeSignature.Uint4: var valu4 = (Vector4D)pin.Spread[i]; FInData.Write(BitConverter.GetBytes((uint)Math.Floor(valu4.x)), 0, 4); FInData.Write(BitConverter.GetBytes((uint)Math.Floor(valu4.y)), 0, 4); FInData.Write(BitConverter.GetBytes((uint)Math.Floor(valu4.z)), 0, 4); FInData.Write(BitConverter.GetBytes((uint)Math.Floor(valu4.w)), 0, 4); break; case TypeSignature.Float4x4: var valm = (Matrix4x4)pin.Spread[i]; valm = valm.Transpose(); foreach (var c in valm.Values) { FInData.Write(BitConverter.GetBytes((float)c), 0, 4); } break; default: throw new ArgumentOutOfRangeException(); } } } FInvalidate = true; FFirst = false; FOutput.Stream.IsChanged = true; } }
public void Render(IPluginIO pin, DX11RenderContext context, DX11RenderSettings settings) { Device device = context.Device; DeviceContext ctx = context.CurrentDeviceContext; bool popstate = false; bool multistate = this.FInState.IsConnected && this.FInState.SliceCount > 1; if (this.FInEnabled[0]) { //In that case we do not care about geometry, but only apply pass for globals if (settings.RenderHint == eRenderHint.ApplyOnly) { DX11ShaderData sdata = this.deviceshaderdata[context]; this.varmanager.SetGlobalSettings(sdata.ShaderInstance, settings); this.varmanager.ApplyGlobal(sdata.ShaderInstance); DX11ObjectRenderSettings oset = new DX11ObjectRenderSettings(); oset.DrawCallIndex = 0; oset.Geometry = null; oset.IterationCount = 1; oset.IterationIndex = 0; oset.WorldTransform = this.mworld[0 % this.mworldcount]; this.varmanager.ApplyPerObject(context, sdata.ShaderInstance, oset, 0); sdata.ApplyPass(ctx); if (this.FInLayer.IsConnected) { this.FInLayer[0][context].Render(this.FInLayer.PluginIO, context, settings); } return; } if (settings.RenderHint == eRenderHint.Collector) { if (this.FGeometry.PluginIO.IsConnected) { DX11ObjectGroup group = new DX11ObjectGroup(); group.ShaderName = this.Source.Name; group.Semantics.AddRange(settings.CustomSemantics); if (this.FGeometry.SliceCount == 1) { IDX11Geometry g = this.FGeometry[0][context]; if (g.Tag != null) { DX11RenderObject o = new DX11RenderObject(); o.ObjectType = g.PrimitiveType; o.Descriptor = g.Tag; o.Transforms = new Matrix[spmax]; for (int i = 0; i < this.spmax; i++) { o.Transforms[i] = this.mworld[i % this.mworldcount]; } group.RenderObjects.Add(o); settings.SceneDescriptor.Groups.Add(group); } } else { for (int i = 0; i < this.spmax; i++) { IDX11Geometry g = this.FGeometry[i][context]; if (g.Tag != null) { DX11RenderObject o = new DX11RenderObject(); o.ObjectType = g.PrimitiveType; o.Descriptor = g.Tag; o.Transforms = new Matrix[1]; o.Transforms[0] = this.mworld[i % this.mworldcount]; group.RenderObjects.Add(o); } } settings.SceneDescriptor.Groups.Add(group); } } return; } DX11ShaderData shaderdata = this.deviceshaderdata[context]; if ((shaderdata.IsValid && (this.geomconnected || settings.Geometry != null) && this.spmax > 0 && this.varmanager.SetGlobalSettings(shaderdata.ShaderInstance, settings)) || this.FInApplyOnly[0]) { this.OnBeginQuery(context); //Select preferred technique if available if (settings.PreferredTechniques.Count == 0 && this.techniqueindex != this.FInTechnique[0].Index) { this.techniqueindex = this.FInTechnique[0].Index; this.techniquechanged = true; } else if (settings.PreferredTechniques.Count > 0) { int i = settings.GetPreferredTechnique(this.FShader); if (i == -1) { i = this.FInTechnique[0].Index; } if (i != this.techniqueindex) { this.techniqueindex = i; this.techniquechanged = true; } } //Need to build input layout if (this.FGeometry.IsChanged || this.techniquechanged || shaderdata.LayoutValid.Count == 0) { shaderdata.Update(this.techniqueindex, 0, this.FGeometry); this.FOutLayoutValid.AssignFrom(shaderdata.LayoutValid); this.FOutLayoutMsg.AssignFrom(shaderdata.LayoutMsg); int errorCount = 0; StringBuilder sbMsg = new StringBuilder(); sbMsg.Append("Invalid layout detected for slices:"); for (int i = 0; i < shaderdata.LayoutValid.Count; i++) { if (shaderdata.LayoutValid[i] == false) { errorCount++; sbMsg.Append(i + ","); } } if (errorCount > 0) { this.FHost.Log(TLogType.Warning, sbMsg.ToString()); } this.techniquechanged = false; } if (this.stateconnected && !multistate) { context.RenderStateStack.Push(this.FInState[0]); popstate = true; } ShaderPipelineState pipelineState = null; if (!settings.PreserveShaderStages) { shaderdata.ResetShaderStages(ctx); } else { pipelineState = new ShaderPipelineState(context); } settings.DrawCallCount = spmax; //Set number of draw calls this.varmanager.ApplyGlobal(shaderdata.ShaderInstance); //IDX11Geometry drawgeom = null; objectsettings.Geometry = null; DX11Resource <IDX11Geometry> pg = null; bool doOrder = false; List <int> orderedSlices = null; if (settings.LayerOrder != null && settings.LayerOrder.Enabled) { this.orderedObjectSettings.Clear(); for (int i = 0; i < this.spmax; i++) { DX11ObjectRenderSettings objSettings = new DX11ObjectRenderSettings(); objSettings.DrawCallIndex = i; objSettings.Geometry = null; objSettings.IterationCount = 1; objSettings.IterationIndex = 0; objSettings.WorldTransform = this.mworld[i % this.mworldcount]; this.orderedObjectSettings.Add(objSettings); } orderedSlices = settings.LayerOrder.Reorder(settings, orderedObjectSettings); doOrder = true; } int drawCount = doOrder ? orderedSlices.Count : this.spmax; if (this.spmax == 0) { drawCount = 0; } for (int i = 0; i < drawCount; i++) { int idx = doOrder ? orderedSlices[i] : i; if (multistate) { context.RenderStateStack.Push(this.FInState[idx]); } if (shaderdata.IsLayoutValid(idx) || settings.Geometry != null) { objectsettings.IterationCount = this.FIter[idx]; for (int k = 0; k < objectsettings.IterationCount; k++) { objectsettings.IterationIndex = k; if (settings.Geometry == null) { if (this.FGeometry[idx] != pg) { pg = this.FGeometry[idx]; objectsettings.Geometry = pg[context]; shaderdata.SetInputAssembler(ctx, objectsettings.Geometry, idx); } } else { objectsettings.Geometry = settings.Geometry; shaderdata.SetInputAssembler(ctx, objectsettings.Geometry, idx); } //Prepare settings objectsettings.DrawCallIndex = idx; objectsettings.WorldTransform = this.mworld[idx % this.mworldcount]; if (settings.ValidateObject(objectsettings)) { this.varmanager.ApplyPerObject(context, shaderdata.ShaderInstance, this.objectsettings, idx); shaderdata.ApplyPass(ctx); if (settings.DepthOnly) { ctx.PixelShader.Set(null); } if (settings.PostPassAction != null) { settings.PostPassAction(context); } objectsettings.Geometry.Draw(); shaderdata.ShaderInstance.CleanUp(); } } } if (multistate) { context.RenderStateStack.Pop(); } if (settings.PostShaderAction != null) { settings.PostShaderAction(context); } } if (pipelineState != null) { pipelineState.Restore(context); } this.OnEndQuery(context); } //this.query.End(); } if (popstate) { context.RenderStateStack.Pop(); } else { //Since shaders can define their own states, reapply top of the stack context.RenderStateStack.Apply(); } if (this.FInLayer.IsConnected && this.FInEnabled[0]) { this.FInLayer[0][context].Render(this.FInLayer.PluginIO, context, settings); } }
private void Pinmode_Changed(IDiffSpread <eDepthBufferMode> spread) { if (this.currentmode != spread[0]) { if (this.currentmode == eDepthBufferMode.Standard) { if (this.depthoutputpin != null) { //Destroy depth stencil if (this.depthoutputpin.IOObject[0] != null) { this.depthoutputpin.IOObject[0].Dispose(); } this.depthoutputpin.Dispose(); this.depthformatpin.Dispose(); this.depthformatpin = null; this.depthoutputpin = null; } } if (this.currentmode == eDepthBufferMode.ReadOnly) { if (this.depthinputpin != null) { this.depthinputpin.Dispose(); this.depthinputpin = null; } } if (this.currentmode == eDepthBufferMode.WriteOnly) { if (this.writeOnlyDepth != null) { this.writeOnlyDepth.Dispose(); this.writeOnlyDepth = null; } if (this.depthformatpin != null) { this.depthformatpin.Dispose(); this.depthformatpin = null; } } this.currentmode = spread[0]; if (this.currentmode == eDepthBufferMode.Standard) { OutputAttribute oattr = new OutputAttribute("Depth Buffer"); oattr.IsSingle = true; this.depthoutputpin = this.factory.CreateIOContainer <Pin <DX11Resource <DX11DepthStencil> > >(oattr); this.depthoutputpin.IOObject[0] = new DX11Resource <DX11DepthStencil>(); ConfigAttribute dfAttr = new ConfigAttribute("Depth Buffer Format"); dfAttr.EnumName = DX11EnumFormatHelper.NullDeviceFormats.GetEnumName(FormatSupport.DepthStencil); dfAttr.DefaultEnumEntry = DX11EnumFormatHelper.NullDeviceFormats.GetAllowedFormats(FormatSupport.DepthStencil)[0]; dfAttr.IsSingle = true; this.depthformatpin = this.factory.CreateIOContainer <IDiffSpread <EnumEntry> >(dfAttr); this.depthformatpin.IOObject[0] = new EnumEntry(dfAttr.EnumName, 1); this.depthformatpin.IOObject.Changed += depthformatpin_Changed; } if (this.currentmode == eDepthBufferMode.ReadOnly) { InputAttribute oattr = new InputAttribute("Depth Buffer"); oattr.IsSingle = true; this.depthinputpin = this.factory.CreateIOContainer <Pin <DX11Resource <DX11DepthStencil> > >(oattr); } if (this.currentmode == eDepthBufferMode.WriteOnly) { this.writeOnlyDepth = new DX11Resource <DX11WriteOnlyDepthStencil>(); ConfigAttribute dfAttr = new ConfigAttribute("Depth Buffer Format"); dfAttr.EnumName = DX11EnumFormatHelper.NullDeviceFormats.GetEnumName(FormatSupport.DepthStencil); dfAttr.DefaultEnumEntry = DX11EnumFormatHelper.NullDeviceFormats.GetAllowedFormats(FormatSupport.DepthStencil)[0]; dfAttr.IsSingle = true; this.depthformatpin = this.factory.CreateIOContainer <IDiffSpread <EnumEntry> >(dfAttr); this.depthformatpin.IOObject[0] = new EnumEntry(dfAttr.EnumName, 1); this.depthformatpin.IOObject.Changed += depthformatpin_Changed; } this.NeedReset = true; } }