public override bool Register(ref RenderPassBuilder builder, ref Resolver resolver) { var resources = new PipelineResources(); var settings = resolver.GetComponent <PipelineSettings>(); resources.SceneColor = builder.CreatePrimaryOutputRelativeTexture( TextureDesc.CreateRenderTargetDesc(DataFormat.R8G8B8A8UnsignedNormalized, Rgba128.CornflowerBlue, settings.Msaa), ResourceState.RenderTarget, debugName: nameof(resources.SceneColor) ); resources.SceneDepth = builder.CreatePrimaryOutputRelativeTexture( TextureDesc.CreateDepthStencilDesc(DataFormat.Depth32Single, 1.0f, 0, false, settings.Msaa), ResourceState.DepthWrite, debugName: nameof(resources.SceneDepth) ); resolver.CreateComponent(resources); DefaultPipelineState = settings.Msaa.IsMultiSampled ? _texMsaa8x : _tex; var fovAngleY = 70.0f * MathF.PI / 180.0f; _frameConstants.Projection = Matrix4x4.CreatePerspectiveFieldOfView(fovAngleY, settings.AspectRatio, 0.001f, 100f); return(true); }
TextureHandle CreateDecalPrepassBuffer(RenderGraph renderGraph, bool msaa) { TextureDesc decalDesc = new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, clearBuffer = true, clearColor = Color.clear, bindTextureMS = false, enableMSAA = msaa, enableRandomWrite = !msaa, name = msaa ? "DecalPrepassBufferMSAA" : "DecalPrepassBuffer" }; return(renderGraph.CreateTexture(decalDesc)); }
TextureHandle CreateDepthBuffer(RenderGraph renderGraph, bool msaa) { TextureDesc depthDesc = new TextureDesc(Vector2.one, true, true) { depthBufferBits = DepthBits.Depth32, bindTextureMS = msaa, enableMSAA = msaa, clearBuffer = true, name = msaa ? "CameraDepthStencilMSAA" : "CameraDepthStencil" }; return(renderGraph.CreateTexture(depthDesc)); }
TextureHandle CreateNormalBuffer(RenderGraph renderGraph, bool msaa) { TextureDesc normalDesc = new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, clearBuffer = NeedClearGBuffer(), clearColor = Color.black, bindTextureMS = msaa, enableMSAA = msaa, enableRandomWrite = !msaa, name = msaa ? "NormalBufferMSAA" : "NormalBuffer" }; return(renderGraph.CreateTexture(normalDesc, msaa ? HDShaderIDs._NormalTextureMS : HDShaderIDs._NormalBufferTexture)); }
TextureHandle CreateMotionVectorBuffer(RenderGraph renderGraph, bool msaa, bool clear) { TextureDesc motionVectorDesc = new TextureDesc(Vector2.one, true, true) { colorFormat = Builtin.GetMotionVectorFormat(), bindTextureMS = msaa, enableMSAA = msaa, clearBuffer = clear, clearColor = Color.clear, name = msaa ? "Motion Vectors MSAA" : "Motion Vectors" }; return(renderGraph.CreateTexture(motionVectorDesc, HDShaderIDs._CameraMotionVectorsTexture)); }
static TextureDesc textureDesc(int rows) { TextureDesc desc = new TextureDesc(false); desc.Type = ResourceDimension.Tex2d; desc.Size = new CSize(0x100, rows); desc.Format = TextureFormat.Rgba16Float; desc.BindFlags = BindFlags.ShaderResource; return(desc); }
public void grab(IRenderDevice device, IDeviceContext context, ITexture texture, ConsumeTextureDelegate consume) { TextureDesc desc = texture.GetDesc(); // Create staging texture if needed if (null == stagingTexture || stagingDesc.Size != desc.Size || stagingDesc.Format != desc.Format) { ComUtils.clear(ref stagingTexture); desc.Type = ResourceDimension.Tex2d; desc.BindFlags = BindFlags.None; desc.CPUAccessFlags = CpuAccessFlags.Read; desc.Usage = Usage.Staging; stagingTexture = device.CreateTexture(ref desc, "ScreenGrabber staging"); stagingDesc = desc; } // In D3D12, Diligent engine fails instead of waiting for GPU. Need to wait manually, need a fence for that. if (RuntimeEnvironment.runningWindows && null == fence) { var fd = new FenceDesc(false); fence = device.CreateFence(ref fd); } // Finish the rendering, if any waitForGpu(context); // Unset the targets context.SetRenderTargets(0, null, null); waitForGpu(context); // Copy source texture to staging context.copyTexture(stagingTexture, texture); waitForGpu(context); // Map the texture Box box = new Box(false) { MaxX = desc.Size.cx, MaxY = desc.Size.cy }; MappedTextureSubresource mapped = context.MapTextureSubresource(stagingTexture, 0, 0, MapType.Read, MapFlags.DoNotWait, ref box); try { consume(desc.Format, desc.Size, mapped); } finally { context.UnmapTextureSubresource(stagingTexture, 0, 0); } }
public override bool Register(ref RenderPassBuilder builder, ref Resolver resolver) { if (!IsEnabled) { return(false); } var settings = resolver.GetComponent <RenderSettings>(); var resources = resolver.GetComponent <RenderResources>(); builder.CreatePrimaryOutputRelativeTexture(TextureDesc.CreateUnorderedAccessResourceDesc(DataFormat.Unknown, TextureDimension.Tex2D)); return(true); }
static ITextureView loadBmp(IRenderDevice device, byte[] payload, ref BITMAPINFOHEADER header, CSize size, out bool monochrome) { if (header.biWidth != size.cx) { throw new ArgumentException("Size in BMP doesn't match"); } uint[] rgba; if (header.biCompression == BitmapCompressionMode.BI_RGB && header.biHeight == size.cy * 2) { if (header.biBitCount == 32) { monochrome = false; rgba = decodeRgbIcon(payload, size); } else if (header.biBitCount == 1) { monochrome = true; return(Monochrome.load(device, payload, size)); } else { throw new NotImplementedException(); } } else { throw new NotImplementedException(); } TextureDesc desc = new TextureDesc(false); desc.Type = ResourceDimension.Tex2d; desc.Size = size; if (RuntimeEnvironment.operatingSystem == eOperatingSystem.Windows) { desc.Format = TextureFormat.Bgra8Unorm; } else { desc.Format = TextureFormat.Rgba8Unorm; GraphicsUtils.swapRedBlueChannels(rgba); } desc.Usage = Usage.Static; desc.BindFlags = BindFlags.ShaderResource; ITexture texture = device.CreateTexture(ref desc, rgba, size.cx * 4, "Mouse cursor"); return(texture.GetDefaultView(TextureViewType.ShaderResource)); }
public static ITextureView load(IRenderDevice device, byte[] payload, CSize size) { byte[] data = decodeMonochromeIcon(payload, size); TextureDesc desc = new TextureDesc(false); desc.Type = ResourceDimension.Tex2d; desc.Size = size; desc.Format = TextureFormat.R8Unorm; desc.Usage = Usage.Static; desc.BindFlags = BindFlags.ShaderResource; ITexture texture = device.CreateTexture(ref desc, data, size.cx, "Monochrome cursor"); return(texture.GetDefaultView(TextureViewType.ShaderResource)); }
/// <summary>Decode frames of the specified format index, and upload them to VRAM.</summary> public AnimatedCursorTexture load(IRenderDevice device, Stream stream, string name, int formatIndex = 0) { if (formatIndex < 0 || formatIndex >= formats.Length) { throw new ArgumentOutOfRangeException(); } iTextureArrayData data; switch (formats[formatIndex].format) { case eFormat.Bitmap: data = new AniBitmaps(stream, this.frames, formatIndex); break; default: throw new NotImplementedException(); } CSize size = formats[formatIndex].size; int frames = data.data.Length; ITexture textureArray; using ( data ) { TextureDesc desc = new TextureDesc(false); desc.Type = ResourceDimension.Tex2dArray; desc.Size = size; desc.ArraySizeOrDepth = (uint)frames; desc.Format = data.format; desc.Usage = Usage.Static; desc.BindFlags = BindFlags.ShaderResource; textureArray = device.CreateTexture(ref desc, data.data, (uint)frames, name); } ITextureView view = textureArray.GetDefaultView(TextureViewType.ShaderResource); double ticks = TimeSpan.TicksPerSecond; ticks /= 60; ticks *= header.JifRate; ticks = Math.Round(ticks); TimeSpan duration = TimeSpan.FromTicks((long)ticks); CPoint hotspot = this.frames[0].images[formatIndex].hotspot; return(new AnimatedCursorTexture(view, size, hotspot, frames, duration)); }
public RenderTarget(Context context, CSize size, TextureFormat format, int sampleCount, string name) { this.size = size; TextureDesc desc = new TextureDesc(false); desc.Type = ResourceDimension.Tex2d; desc.Size = size; desc.Format = format; desc.SampleCount = (uint)sampleCount; desc.BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource; using (var device = context.renderContext.device) texture = device.CreateTexture(ref desc, name); targetView = texture.GetDefaultView(TextureViewType.RenderTarget); }
private TextureHandle CreateDepthTexture(RenderGraph graph, Camera camera) { bool colorRT_sRGB = (QualitySettings.activeColorSpace == ColorSpace.Linear); //Texture description TextureDesc colorRTDesc = new TextureDesc(camera.pixelWidth, camera.pixelHeight); colorRTDesc.colorFormat = GraphicsFormatUtility.GetGraphicsFormat(RenderTextureFormat.Depth, colorRT_sRGB); colorRTDesc.depthBufferBits = DepthBits.Depth24; colorRTDesc.msaaSamples = MSAASamples.None; colorRTDesc.enableRandomWrite = false; colorRTDesc.clearBuffer = true; colorRTDesc.clearColor = Color.black; colorRTDesc.name = "Depth"; return(graph.CreateTexture(colorRTDesc)); }
void InitializePrepass(HDRenderPipelineAsset hdAsset) { m_DepthResolveMaterial = CoreUtils.CreateEngineMaterial(asset.renderPipelineResources.shaders.depthValuesPS); m_GBufferOutput = new GBufferOutput(); m_GBufferOutput.mrt = new TextureHandle[RenderGraph.kMaxMRTCount]; m_DBufferOutput = new DBufferOutput(); m_DBufferOutput.mrt = new TextureHandle[(int)Decal.DBufferMaterial.Count]; m_DepthBufferMipChainInfo = new HDUtils.PackedMipChainInfo(); m_DepthBufferMipChainInfo.Allocate(); m_DepthPyramidDesc = new TextureDesc(ComputeDepthBufferMipChainSize, true, true) { colorFormat = GraphicsFormat.R32_SFloat, enableRandomWrite = true, name = "CameraDepthBufferMipChain" }; }
public void CreateTexture(UInt32 width, UInt32 height, out IntPtr textureHandle) { // TODO : switch to shared textures when we go multiprocess //textureHandle = GS.CreateSharedTexture(width, height, GSColorFormat.GS_BGRA); lock (pendingTextureLock) { if (pendingTexture != null && pendingTexture.Width == width && pendingTexture.Height == height) { if (pendingTexture.Texture != null) { textureMap.Add(pendingTexture.Texture.OBSTexture, pendingTexture.Texture); textureHandle = pendingTexture.Texture.OBSTexture; pendingTexture = null; return; } else { textureHandle = IntPtr.Zero; return; } } else { if (pendingTexture != null) { // if we have a pending texture that was the wrong size, dispose if (pendingTexture.Texture != null) { pendingTexture.Texture.Dispose(); } } pendingTexture = new TextureDesc { Width = width, Height = height }; textureHandle = IntPtr.Zero; return; } } }
internal FormatTexture( ReadOnlyMemory <byte> bitData, TextureDesc desc, uint mipCount, LoaderFlags loaderFlags, bool isCubeMap, ReadOnlyMemory <SubresourceData> subresourceData, AlphaMode alphaMode, TexType underlyingTextureType) { Data = bitData; Desc = desc; MipCount = mipCount; LoaderFlags = loaderFlags; IsCubeMap = isCubeMap; SubresourceData = subresourceData; AlphaMode = alphaMode; UnderlyingTextureType = underlyingTextureType; }
// SavePngFrames savePng; protected override ITextureView createOutputTexture(IRenderDevice device, TextureFormat format, CSize size) { outputSize = size; TextureDesc desc = new TextureDesc(false) { Type = ResourceDimension.Tex2d, BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget, Format = format, Size = size }; frameTexture = device.CreateTexture(ref desc, $"Video frame RGB"); frameRtv = frameTexture.GetDefaultView(TextureViewType.RenderTarget); rgbFormat = format; // savePng = new SavePngFrames(); return(frameTexture.GetDefaultView(TextureViewType.ShaderResource)); }
public static void _Main() { #if DEBUG var layer = DebugLayerConfiguration.Debug; #else var layer = DebugLayerConfiguration.None; #endif _device = GraphicsDevice.Create(FeatureLevel.GraphicsLevel11_0, null, layer); var flags = Commit ? AllocFlags.ForceAllocateComitted : AllocFlags.ForceAllocateNotComitted; _sourceBuffer = _device.Allocator.AllocateBuffer(BuffLength, MemoryAccess.GpuOnly); _destBuffer = _device.Allocator.AllocateBuffer(BuffLength, MemoryAccess.GpuOnly); var texDesc = TextureDesc.CreateShaderResourceDesc(DataFormat.R8G8B8A8UInt, TextureDimension.Tex2D, TexLength, TexLength); texDesc.MipCount = 1; _sourceTexture = _device.Allocator.AllocateTexture(texDesc, ResourceState.CopySource, flags); _destTexture = _device.Allocator.AllocateTexture(texDesc, ResourceState.Common, flags); var contexts = new (CopyContext, CopyContext, ulong)[]
void InitPathTracing() { #if UNITY_EDITOR Undo.postprocessModifications += OnUndoRecorded; Undo.undoRedoPerformed += OnSceneEdit; SceneView.duringSceneGui += OnSceneGui; #endif // UNITY_EDITOR TextureDesc td = new TextureDesc(Vector2.one, true, true); td.colorFormat = GraphicsFormat.R32G32B32A32_SFloat; td.useMipMap = false; td.autoGenerateMips = false; // Texture storing the result of one iteration (one per frame) of path tracing td.name = "PathTracingFrameBuffer"; td.enableRandomWrite = true; m_FrameTexture = m_RenderGraph.CreateSharedTexture(td); // Texture storing the sky background, matching the rasterization one td.name = "PathTracingSkyBackgroundBuffer"; td.enableRandomWrite = false; m_SkyBGTexture = m_RenderGraph.CreateSharedTexture(td); // Textures used to importance sample the sky (aka environment sampling) td.name = "PathTracingSkySamplingBuffer"; td.colorFormat = GraphicsFormat.R32_SFloat; td.dimension = TextureDimension.Tex2D; td.enableRandomWrite = true; td.useDynamicScale = false; td.slices = 1; td.sizeMode = TextureSizeMode.Explicit; m_skySamplingSize = (int)m_Asset.currentPlatformRenderPipelineSettings.lightLoopSettings.skyReflectionSize * 2; td.width = m_skySamplingSize * 2; td.height = m_skySamplingSize; m_SkyCDFTexture = m_RenderGraph.CreateSharedTexture(td, true); td.width = m_skySamplingSize; td.height = 1; m_SkyMarginalTexture = m_RenderGraph.CreateSharedTexture(td, true); pathTracedAOVs = new List <Tuple <TextureHandle, HDCameraFrameHistoryType> >(3); }
TextureHandle m_SkyTexture; // stores the sky background void InitPathTracing(RenderGraph renderGraph) { #if UNITY_EDITOR Undo.postprocessModifications += OnUndoRecorded; Undo.undoRedoPerformed += OnSceneEdit; SceneView.duringSceneGui += OnSceneGui; #endif // UNITY_EDITOR TextureDesc td = new TextureDesc(Vector2.one, true, true); td.colorFormat = GraphicsFormat.R32G32B32A32_SFloat; td.useMipMap = false; td.autoGenerateMips = false; td.name = "PathTracingFrameBuffer"; td.enableRandomWrite = true; m_FrameTexture = renderGraph.CreateSharedTexture(td); td.name = "PathTracingSkyBuffer"; td.enableRandomWrite = false; m_SkyTexture = renderGraph.CreateSharedTexture(td); }
unsafe static bool ReadTexture(byte *binaryData, int length, ref int offset, out Texture2D texture) { texture = null; if (offset + sizeof(int) >= length) { return(false); } int initialOffset = offset; int dataLength = *(int *)binaryData; binaryData += sizeof(int); if (dataLength < sizeof(int) * 3) { return(false); } TextureDesc desc = *(TextureDesc *)binaryData; binaryData += sizeof(TextureDesc); offset += sizeof(TextureDesc); int remainingData = dataLength - (offset - initialOffset); if (remainingData < 1) { return(false); //we don't have enough data to read } var texData = new byte[remainingData]; fixed(byte *texDataPtr = texData) { UnsafeUtility.MemCpy(texDataPtr, binaryData, remainingData); } var tex = new Texture2D(desc.Width, desc.Height, desc.Format, false); tex.LoadRawTextureData(texData); return(true); }
public async static Task <Texture2D> LoadDDSAsync(string path) { TextureDesc textureDesc = await Task.Run(() => { return(LoadDDS(path)); }); Texture2D texture; if (textureDesc.Texture == IntPtr.Zero || textureDesc.Format == 0) { Debug.LogError($"{path} is bad texture!"); texture = new Texture2D(2, 2); } else { texture = Texture2D.CreateExternalTexture(textureDesc.Width, textureDesc.Height, NativeToUnityTextureFormat(textureDesc.Format), textureDesc.MipLevels > 1, false, textureDesc.Texture); texture.wrapMode = TextureWrapMode.Clamp; Textures[texture] = textureDesc.Texture; } return(texture); }
public override bool Register(ref RenderPassBuilder builder, ref Resolver resolver) { var resources = resolver.GetComponent <PipelineResources>(); var settings = resolver.GetComponent <PipelineSettings>(); if (settings.Msaa.IsMultiSampled) { resources.SampledOutput = builder.CreatePrimaryOutputRelativeTexture( TextureDesc.CreateRenderTargetDesc(DataFormat.R8G8B8A8UnsignedNormalized, Rgba128.CornflowerBlue), ResourceState.ResolveDestination, debugName: nameof(resources.SampledOutput) ); resolver.SetComponent(resources); builder.MarkUsage(resources.SceneColor, ResourceState.ResolveSource); return(true); } else { resolver.SetComponent(resources); resources.SampledOutput = resources.SceneColor; return(false); } }
public override bool Register(ref RenderPassBuilder builder, ref Resolver resolver) { var settings = resolver.GetComponent <RenderSettings>(); RenderResources resources; resources.SceneColor = builder.CreatePrimaryOutputRelativeTexture( TextureDesc.CreateRenderTargetDesc(BackBufferFormat.R8G8B8A8UnsignedNormalized, DefaultSkyColor, settings.Msaa), ResourceState.RenderTarget ); resources.SceneDepth = builder.CreatePrimaryOutputRelativeTexture( TextureDesc.CreateDepthStencilDesc(DataFormat.Depth32Single, 1, 0, false, settings.Msaa), ResourceState.DepthWrite ); resolver.CreateComponent(resources); DefaultPipelineState = settings.Msaa.IsMultiSampled ? MsaaPso : Pso; builder.SetOutput(resources.SceneColor); return(true); }
protected virtual void Dispose(bool disposing) { if (disposing) { // remove any pending texture requests lock (pendingTextureLock) { if (pendingTexture != null && pendingTexture.Texture != null) { pendingTexture.Texture.Dispose(); } pendingTexture = null; } if (browser != null) { browser.CloseBrowser(true); browser = null; } } isDisposed = true; }
public static FormatTexture CreateTgaTexture( ReadOnlyMemory <byte> tgaData, LoaderFlags loaderFlags = LoaderFlags.None ) { if (tgaData.Length < sizeof(TGAHeader)) { ThrowHelper.ThrowInvalidDataException("Too small"); } ReadOnlySpan <byte> span = tgaData.Span; TGAHeader header = MemoryMarshal.Read <TGAHeader>(span); if (IsCompressed(header.DataTypeCode)) { ThrowHelper.ThrowNotSupportedException("Compressed TGA textures are TODO"); // TODO } int size = header.Height * header.Width * (header.BitsPerPixel / 8); var data = new byte[size]; var buff = data; DXGI_FORMAT format = loaderFlags.HasFlag(LoaderFlags.ForceSrgb) ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM; switch (header.BitsPerPixel) { case 24: RbgToRgba(span.Slice(sizeof(TGAHeader)), buff); break; case 32: ArbgToRgba(span.Slice(sizeof(TGAHeader)), buff); break; case 16: Rgba16ToRgba(span.Slice(sizeof(TGAHeader)), buff); break; default: ThrowHelper.ThrowNotSupportedException("Unsupported format"); break; } var subresources = new SubresourceData[1]; subresources[0] = new SubresourceData(); var desc = new TextureDesc { Width = (uint)header.Width, Height = (uint)header.Height, DepthOrArraySize = 0, // is this right? Format = (DataFormat)format, Dimension = TextureDimension.Tex2D }; return(new FormatTexture( data, desc, 1, loaderFlags, false, subresources, header.BitsPerPixel == 24 ? AlphaMode.Opaque : AlphaMode.Unknown, TexType.Tga )); }
public void SetFlameMaterial(Material result) { TextureDesc[] descs = new TextureDesc[4]; var diffuse = result.Maps["Diffuse"].Data[0]; var data = MaterialCompiler.ImageToByteArray32(diffuse); descs[0] = new TextureDesc(); descs[0].Data = data; descs[0].width = diffuse.Width; descs[0].height = diffuse.Height; var normal = result.Maps["Normal"].Data[0]; data = MaterialCompiler.ImageToByteArray32(normal); descs[1] = new TextureDesc(); descs[1].Data = data; descs[1].width = normal.Width; descs[1].height = normal.Height; var height = result.Maps["Height"].Data[0]; var rough = result.Maps["Roughness"].Data[0]; var metal = result.Maps["Metallic"].Data[0]; var ao = result.Maps["AO"].Data[0]; int max_width = Math.Max( Math.Max( height.Width, rough.Width ), Math.Max( metal.Width, ao.Width)); int max_height = Math.Max( Math.Max( height.Height, rough.Height ), Math.Max( metal.Height, ao.Height)); height = MaterialCompiler.ResizeImage(height, max_width, max_height); rough = MaterialCompiler.ResizeImage(rough, max_width, max_height); metal = MaterialCompiler.ResizeImage(metal, max_width, max_height); ao = MaterialCompiler.ResizeImage(ao, max_width, max_height); data = MaterialCompiler.FromChannels( MaterialCompiler.ImageToByteArray32(height), MaterialCompiler.ImageToByteArray32(rough), MaterialCompiler.ImageToByteArray32(metal), MaterialCompiler.ImageToByteArray32(ao) ); descs[2] = new TextureDesc(); descs[2].width = max_width; descs[2].height = max_height; descs[2].Data = data; var emissive = result.Maps["Emissive"].Data[0]; data = MaterialCompiler.ImageToByteArray32(emissive); descs[3] = new TextureDesc(); descs[3].Data = data; descs[3].width = emissive.Width; descs[3].height = emissive.Height; editorApp.SetMaterial(descs); }