// ReSharper disable once UnusedMember.Local Is used through reflection private void FillComponentBuffer <TComponent>(FieldInfo component, int componentIndex, IBuffer[] vertexComponentBuffers, string[] vertexComponentSemantics, ResourceFormat[] vertexComponentFormats) where TComponent : struct { TComponent[] initialData = new TComponent[vertices.Count]; for (int i = 0; i < vertices.Count; ++i) { initialData[i] = (TComponent)component.GetValue(vertices[i]); } VertexBuffer <TComponent> componentBuffer = BufferFactory.NewVertexBuffer <TComponent>() .WithInitialData(initialData) .WithUsage(ResourceUsage.Immutable); vertexComponentBuffers[componentIndex] = componentBuffer; vertexComponentSemantics[componentIndex] = component.GetCustomAttribute <VertexComponentAttribute>().SemanticName; ResourceFormat componentFormat = (ResourceFormat)component.GetCustomAttribute <VertexComponentAttribute>().D3DResourceFormat; if (componentFormat == ResourceFormat.Unknown) { componentFormat = BaseResource.GetFormatForType(typeof(TComponent)); if (componentFormat == ResourceFormat.Unknown) { throw new InvalidOperationException("Could not discern resource format for type '" + typeof(TComponent).Name + "' " + "with semantic '" + vertexComponentSemantics[componentIndex] + "', " + "please specify a resource format using the \"D3DResourceFormat\" field of the VertexComponent attribute."); } } vertexComponentFormats[componentIndex] = componentFormat; }
protected void CopyTo(BaseResource dest, SubresourceBox srcBox, uint srcSubresourceIndex, uint dstSubresourceIndex, uint dstX, uint dstY, uint dstZ) { Assure.NotNull(dest); Assure.False(this == dest && srcSubresourceIndex == dstSubresourceIndex, "Can not copy to/from same subresource."); dest.ThrowIfCannotBeCopyDestination(); lock (InstanceMutationLock) { if (IsDisposed) { Logger.Warn("Attempted copy manipulation from disposed resource of type: " + GetType().Name); return; } lock (dest.InstanceMutationLock) { if (IsDisposed) { Logger.Warn("Attempted copy manipulation to disposed resource of type: " + GetType().Name); return; } InteropUtils.CallNative( NativeMethods.ResourceFactory_CopySubresourceRegion, RenderingModule.DeviceContext, ResourceHandle, srcSubresourceIndex, (IntPtr)(&srcBox), dest.ResourceHandle, dstSubresourceIndex, dstX, dstY, dstZ ).ThrowOnFailure(); } } }
protected void CopyTo(BaseResource dest) { Assure.NotNull(dest); Assure.NotEqual(this, dest, "Can not copy to self."); Assure.Equal(Size, dest.Size, "Resources must be of equal size."); Assure.Equal(GetType(), dest.GetType(), "Resources must be of equal type."); dest.ThrowIfCannotBeCopyDestination(); lock (InstanceMutationLock) { if (IsDisposed) { Logger.Warn("Attempted copy manipulation from disposed resource of type: " + GetType().Name); return; } lock (dest.InstanceMutationLock) { if (dest.IsDisposed) { Logger.Warn("Attempted copy manipulation to disposed resource of type: " + GetType().Name); return; } InteropUtils.CallNative( NativeMethods.ResourceFactory_CopyResource, RenderingModule.DeviceContext, ResourceHandle, dest.ResourceHandle ).ThrowOnFailure(); } } }
private void SetUpGlowPlane() { if (glowPlaneVertices != null) { glowPlaneVertices.Dispose(); } if (glowPlaneInputLayout != InputLayoutHandle.NULL) { InteropUtils.CallNative(NativeMethods.ResourceFactory_ReleaseResource, (ResourceHandle)glowPlaneInputLayout).ThrowOnFailure(); } if (glowVS != null) { glowPlaneVertices = glowPlaneBuilder.WithInitialData(new[] { new LightPlaneVertex(new Vector3(-1f, -1f, 0f), new Vector2(0f, 1f)), new LightPlaneVertex(new Vector3(-1f, 1f, 0f), new Vector2(0f, 0f)), new LightPlaneVertex(new Vector3(1f, 1f, 0f), new Vector2(1f, 0f)), new LightPlaneVertex(new Vector3(1f, 1f, 0f), new Vector2(1f, 0f)), new LightPlaneVertex(new Vector3(1f, -1f, 0f), new Vector2(1f, 1f)), new LightPlaneVertex(new Vector3(-1f, -1f, 0f), new Vector2(0f, 1f)), }); InputElementDesc[] inputElements = { new InputElementDesc( "POSITION", 0U, BaseResource.GetFormatForType(typeof(Vector3)), 0U, true ), new InputElementDesc( "TEXCOORD", 0U, BaseResource.GetFormatForType(typeof(Vector2)), 0U, true ) }; InputLayoutHandle outInputLayoutPtr; InteropUtils.CallNative(NativeMethods.ShaderManager_CreateInputLayout, RenderingModule.Device, glowVS.Handle, inputElements, (uint)inputElements.Length, (IntPtr)(&outInputLayoutPtr) ).ThrowOnFailure(); glowPlaneInputLayout = outInputLayoutPtr; glowVS.GetBindingByIdentifier("POSITION").Bind(glowPlaneVertices); } }
/// <summary> /// Creates a new <see cref="Buffer{TElement}"/> with the supplied builder parameters. /// </summary> /// <remarks> /// In debug mode, this method will check a large number of <see cref="Assure">assurances</see> /// on the builder parameters before creating the resource. /// </remarks> /// <returns>A new <see cref="Buffer{TElement}"/>.</returns> public unsafe override Buffer <TElement> Create() { Assure.True(Usage != ResourceUsage.Immutable || InitialData != null, "You must supply initial data to an immutable resource."); Assure.False( (Usage == ResourceUsage.Immutable || Usage == ResourceUsage.DiscardWrite) && permittedBindings == GPUBindings.None, "An immutable or discard-write resource with no permitted bindings is useless." ); Assure.False( Usage.GetUsage() == 0x3 && permittedBindings != GPUBindings.None, "Staging resources can not be bound to the pipeline." ); Assure.GreaterThan(length, 0U, "Can not create a buffer with 0 elements."); InteropBool isStructured = (BaseResource.GetFormatForType(typeof(TElement)) == ResourceFormat.Unknown); InteropBool allowRawAccess = !isStructured && (int)(permittedBindings & (GPUBindings.WritableShaderResource | GPUBindings.ReadableShaderResource)) != 0; GCHandle?pinnedArrayHandle = null; IntPtr initialDataPtr = IntPtr.Zero; try { int elementSizeBytes = UnsafeUtils.SizeOf <TElement>(); if (InitialData != null) { pinnedArrayHandle = GCHandle.Alloc(InitialData.Value.ContainingArray, GCHandleType.Pinned); initialDataPtr = pinnedArrayHandle.Value.AddrOfPinnedObject() + (elementSizeBytes * (int)InitialData.Value.Offset); } BufferResourceHandle outResourceHandle; InteropUtils.CallNative(NativeMethods.ResourceFactory_CreateBuffer, RenderingModule.Device, (uint)elementSizeBytes, length, Usage.GetUsage(), Usage.GetCPUUsage(), (PipelineBindings)permittedBindings, isStructured, allowRawAccess, initialDataPtr, (IntPtr)(&outResourceHandle) ).ThrowOnFailure(); return(new Buffer <TElement>(outResourceHandle, Usage, (uint)elementSizeBytes, length, permittedBindings, isStructured)); } finally { if (pinnedArrayHandle != null) { pinnedArrayHandle.Value.Free(); } } }
/// <summary> /// Creates a new unordered access view to this texture. /// </summary> /// <param name="mipIndex">The mip-level to create a view to. Only one mip-level can be exposed in an <see cref="UnorderedAccessView"/>.</param> /// <returns>A new resource view that permits reading and writing data from/to this texture.</returns> public override UnorderedTextureAccessView CreateUnorderedAccessView(uint mipIndex) { if (mipIndex >= NumMips) { throw new ArgumentOutOfRangeException("mipIndex"); } if ((PermittedBindings & GPUBindings.WritableShaderResource) != GPUBindings.WritableShaderResource) { throw new InvalidOperationException("Can not create an unordered access view to a resource that was created without the " + GPUBindings.WritableShaderResource + " binding."); } UnorderedAccessViewHandle outViewHandle; if (IsArrayTexture) { InteropUtils.CallNative( NativeMethods.ResourceFactory_CreateUAVToTexture1DArray, RenderingModule.Device, (Texture1DResourceHandle)ResourceHandle, BaseResource.GetFormatForType(TexelFormat), mipIndex, ArrayIndex, 1U, (IntPtr)(&outViewHandle) ).ThrowOnFailure(); } else { InteropUtils.CallNative( NativeMethods.ResourceFactory_CreateUAVToTexture1D, RenderingModule.Device, (Texture1DResourceHandle)ResourceHandle, BaseResource.GetFormatForType(TexelFormat), mipIndex, (IntPtr)(&outViewHandle) ).ThrowOnFailure(); } return(new UnorderedTextureAccessView(outViewHandle, this, mipIndex)); }
private void CheckGeometryPassParameters(Texture2D <TexelFormat.RGBA32Float>[] currentGBuffer, Vector2 outputSizePixels) { if (preBloomBuffer == null || preBloomBufferRTV.ResourceOrViewDisposed || preBloomBufferSRV.ResourceOrViewDisposed || preBloomBuffer.Width != (uint)outputSizePixels.X || preBloomBuffer.Height != (uint)outputSizePixels.Y) { if (preBloomBufferRTV != null && !preBloomBufferRTV.IsDisposed) { preBloomBufferRTV.Dispose(); } if (preBloomBufferSRV != null && !preBloomBufferSRV.IsDisposed) { preBloomBufferSRV.Dispose(); } if (preBloomBuffer != null && !preBloomBuffer.IsDisposed) { preBloomBuffer.Dispose(); } if (dsThrowawayBufferDSV != null && !dsThrowawayBufferDSV.IsDisposed) { dsThrowawayBufferDSV.Dispose(); } if (dsThrowawayBuffer != null && !dsThrowawayBuffer.IsDisposed) { dsThrowawayBuffer.Dispose(); } preBloomBuffer = preBloomBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y); dsThrowawayBuffer = dsBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y); preBloomBufferSRV = preBloomBuffer.CreateView(); preBloomBufferRTV = preBloomBuffer.CreateRenderTargetView(0U); dsThrowawayBufferDSV = dsThrowawayBuffer.CreateDepthStencilView(0U); RecalculateLightTileOffsets(); } if (nonDepthOfFieldBackBuffer == null || nonDepthOfFieldBackBufferRTV.ResourceOrViewDisposed || nonDepthOfFieldBackBufferSRV.ResourceOrViewDisposed || nonDepthOfFieldBackBuffer.Width != (uint)outputSizePixels.X || nonDepthOfFieldBackBuffer.Height != (uint)outputSizePixels.Y) { if (nonDepthOfFieldBackBuffer != null && !nonDepthOfFieldBackBuffer.IsDisposed) { nonDepthOfFieldBackBuffer.Dispose(); } if (nonDepthOfFieldBackBufferRTV != null && !nonDepthOfFieldBackBufferRTV.IsDisposed) { nonDepthOfFieldBackBufferRTV.Dispose(); } if (nonDepthOfFieldBackBufferSRV != null && !nonDepthOfFieldBackBufferSRV.IsDisposed) { nonDepthOfFieldBackBufferSRV.Dispose(); } nonDepthOfFieldBackBuffer = preBloomBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y); nonDepthOfFieldBackBufferRTV = nonDepthOfFieldBackBuffer.CreateRenderTargetView(0U); nonDepthOfFieldBackBufferSRV = nonDepthOfFieldBackBuffer.CreateView(); } outputSizePixels /= 2f; if (reducedBloomBuffer == null || reducedBloomBufferRTV.ResourceOrViewDisposed || reducedBloomBufferSRV.ResourceOrViewDisposed || reducedBloomBuffer.Width != (uint)outputSizePixels.X || reducedBloomBuffer.Height != (uint)outputSizePixels.Y) { if (reducedBloomBufferRTV != null && !reducedBloomBufferRTV.IsDisposed) { reducedBloomBufferRTV.Dispose(); } if (reducedBloomBufferSRV != null && !reducedBloomBufferSRV.IsDisposed) { reducedBloomBufferSRV.Dispose(); } if (reducedBloomBuffer != null && !reducedBloomBuffer.IsDisposed) { reducedBloomBuffer.Dispose(); } reducedBloomBuffer = preBloomBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y); reducedBloomBufferSRV = reducedBloomBuffer.CreateView(); reducedBloomBufferRTV = reducedBloomBuffer.CreateRenderTargetView(0U); if (bloomTargetBufferSRV != null) { bloomTargetBufferSRV.Dispose(); } if (bloomTargetBuffer != null) { bloomTargetBuffer.Dispose(); } bloomTargetBuffer = reducedBloomBuffer.Clone(); bloomTargetBufferSRV = bloomTargetBuffer.CreateView(); bloomTargetBufferRTV = bloomTargetBuffer.CreateRenderTargetView(0U); if (bloomResizeCopyDSDSV != null) { bloomResizeCopyDSDSV.Dispose(); } if (bloomResizeCopyDSBuffer != null) { bloomResizeCopyDSBuffer.Dispose(); } bloomResizeCopyDSBuffer = dsBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y); bloomResizeCopyDSDSV = bloomResizeCopyDSBuffer.CreateDepthStencilView(0U); } if (depthOfFieldBackBuffer == null || depthOfFieldBackBufferRTV.ResourceOrViewDisposed || depthOfFieldBackBufferSRV.ResourceOrViewDisposed || depthOfFieldBackBuffer.Width != (uint)outputSizePixels.X || depthOfFieldBackBuffer.Height != (uint)outputSizePixels.Y) { if (depthOfFieldBackBuffer != null && !depthOfFieldBackBuffer.IsDisposed) { depthOfFieldBackBuffer.Dispose(); } if (depthOfFieldBackBufferRTV != null && !depthOfFieldBackBufferRTV.IsDisposed) { depthOfFieldBackBufferRTV.Dispose(); } if (depthOfFieldBackBufferSRV != null && !depthOfFieldBackBufferSRV.IsDisposed) { depthOfFieldBackBufferSRV.Dispose(); } depthOfFieldBackBuffer = preBloomBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y); depthOfFieldBackBufferRTV = depthOfFieldBackBuffer.CreateRenderTargetView(0U); depthOfFieldBackBufferSRV = depthOfFieldBackBuffer.CreateView(); } if (reducedNonDepthOfFieldBackBuffer == null || reducedNonDepthOfFieldBackBufferRTV.ResourceOrViewDisposed || reducedNonDepthOfFieldBackBufferSRV.ResourceOrViewDisposed || reducedNonDepthOfFieldBackBuffer.Width != (uint)outputSizePixels.X || reducedNonDepthOfFieldBackBuffer.Height != (uint)outputSizePixels.Y) { if (reducedNonDepthOfFieldBackBuffer != null && !reducedNonDepthOfFieldBackBuffer.IsDisposed) { reducedNonDepthOfFieldBackBuffer.Dispose(); } if (reducedNonDepthOfFieldBackBufferRTV != null && !reducedNonDepthOfFieldBackBufferRTV.IsDisposed) { reducedNonDepthOfFieldBackBufferRTV.Dispose(); } if (reducedNonDepthOfFieldBackBufferSRV != null && !reducedNonDepthOfFieldBackBufferSRV.IsDisposed) { reducedNonDepthOfFieldBackBufferSRV.Dispose(); } reducedNonDepthOfFieldBackBuffer = preBloomBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y); reducedNonDepthOfFieldBackBufferRTV = reducedNonDepthOfFieldBackBuffer.CreateRenderTargetView(0U); reducedNonDepthOfFieldBackBufferSRV = reducedNonDepthOfFieldBackBuffer.CreateView(); } if (outliningShader != null && outliningShaderResPackage.GetValue((ResourceViewBinding)outliningShader.GetBindingByIdentifier("GeomDepthBuffer")) != GeometryPass.PrimaryDSBufferSRV) { outliningShaderResPackage.SetValue((ResourceViewBinding)outliningShader.GetBindingByIdentifier("GeomDepthBuffer"), GeometryPass.PrimaryDSBufferSRV); outliningShaderUnbindResPackage.SetValue((ResourceViewBinding)outliningShader.GetBindingByIdentifier("GeomDepthBuffer"), null); } if (dofShader != null && dofShaderResPackage.GetValue((ResourceViewBinding)dofShader.GetBindingByIdentifier("SceneDepth")) != GeometryPass.PrimaryDSBufferSRV) { dofShaderResPackage.SetValue((ResourceViewBinding)dofShader.GetBindingByIdentifier("SceneDepth"), GeometryPass.PrimaryDSBufferSRV); dofShaderUnbindResPackage.SetValue((ResourceViewBinding)dofShader.GetBindingByIdentifier("SceneDepth"), null); } bool bufferChange = false; for (int i = 0; i < DLGeometryPass.NUM_GBUFFER_TEXTURES; ++i) { if (previousGBuffer[i] != currentGBuffer[i]) { bufferChange = true; break; } } if (!bufferChange) { return; } Array.Copy(currentGBuffer, previousGBuffer, DLGeometryPass.NUM_GBUFFER_TEXTURES); for (int i = 0; i < DLGeometryPass.NUM_GBUFFER_TEXTURES; ++i) { if (gBufferSRVArray[i] != null) { gBufferSRVArray[i].Dispose(); } gBufferSRVArray[i] = currentGBuffer[i].CreateView(); } if (lightPlaneVertices != null) { lightPlaneVertices.Dispose(); } if (lightPlaneInputLayout != InputLayoutHandle.NULL) { InteropUtils.CallNative(NativeMethods.ResourceFactory_ReleaseResource, (ResourceHandle)lightPlaneInputLayout).ThrowOnFailure(); } lightPlaneVertices = lightPlaneBuilder.WithInitialData(new[] { new LightPlaneVertex(new Vector3(-1f, -1f, 0f), new Vector2(0f, 1f)), new LightPlaneVertex(new Vector3(-1f, 1f, 0f), new Vector2(0f, 0f)), new LightPlaneVertex(new Vector3(1f, 1f, 0f), new Vector2(1f, 0f)), new LightPlaneVertex(new Vector3(1f, 1f, 0f), new Vector2(1f, 0f)), new LightPlaneVertex(new Vector3(1f, -1f, 0f), new Vector2(1f, 1f)), new LightPlaneVertex(new Vector3(-1f, -1f, 0f), new Vector2(0f, 1f)), }); InputElementDesc[] inputElements = { new InputElementDesc( "POSITION", 0U, BaseResource.GetFormatForType(typeof(Vector3)), 0U, true ), new InputElementDesc( "TEXCOORD", 0U, BaseResource.GetFormatForType(typeof(Vector2)), 0U, true ) }; InputLayoutHandle outInputLayoutPtr; InteropUtils.CallNative(NativeMethods.ShaderManager_CreateInputLayout, RenderingModule.Device, dlLightVS.Handle, inputElements, (uint)inputElements.Length, (IntPtr)(&outInputLayoutPtr) ).ThrowOnFailure(); lightPlaneInputLayout = outInputLayoutPtr; SetVSResources(); SetFSResources(); }