public unsafe ITexture2D GetGlTexture2D() { if (image is Ogl3TextureImage oglCgImage) { return(oglCgImage.GlTexture); } if (!isDirty) { return(glTexture2D); } var size = image.Size; if (glTexture2D == null || glTexture2D.Width != size.Width || glTexture2D.Height != size.Height) { glTexture2D?.Dispose(); var mipCount = image.Volatility != ResourceVolatility.Volatile ? GraphicsHelper.TextureMipCount(size.Width, size.Height) : 1; // To side-step the sRGB issue with GenerateMipmaps glTexture2D = infra.GlContext.Create.Texture2D(size.Width, size.Height, mipCount, format); } fixed(byte *pRawData = image.GetRawData()) glTexture2D.SetData(0, (IntPtr)pRawData, FormatColor.Bgra, FormatType.UnsignedByte); if (glTexture2D.MipCount != 1) { glTexture2D.GenerateMipmap(); } isDirty = false; return(glTexture2D); }
protected override void Unload() { if (handle != null) { handle.Dispose(); } }
public void TestCreationParameters() { var defaultBuilder = TextureFactory.LoadTexture2D().WithUsage(ResourceUsage.Immutable).WithFilePath(@"Tests\potTex.bmp"); var withStagingUsage = defaultBuilder.WithUsage(ResourceUsage.StagingReadWrite).WithPermittedBindings(GPUBindings.None); var withReadWriteBindings = defaultBuilder.WithUsage(ResourceUsage.Write).WithPermittedBindings(GPUBindings.ReadableShaderResource | GPUBindings.WritableShaderResource); ITexture2D tex = withStagingUsage.Create(); Assert.AreEqual(ResourceUsage.StagingReadWrite, tex.Usage); tex.Dispose(); tex = withReadWriteBindings.Create(); Assert.AreEqual(GPUBindings.ReadableShaderResource | GPUBindings.WritableShaderResource, tex.PermittedBindings); tex.Dispose(); #if !DEVELOPMENT && !RELEASE try { TextureFactory.LoadTexture2D() .WithUsage(ResourceUsage.Immutable) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } try { TextureFactory.LoadTexture2D() .WithFilePath(@"Tests\potTex.bmp") .WithUsage(ResourceUsage.DiscardWrite) .WithPermittedBindings(GPUBindings.None) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } try { TextureFactory.LoadTexture2D() .WithFilePath(@"Tests\potTex.bmp") .WithUsage(ResourceUsage.StagingRead) .WithPermittedBindings(GPUBindings.ReadableShaderResource) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } #endif }
void Closing() { brickTexture.Dispose(); mossTexture.Dispose(); shaderProgramTextured.Dispose(); shaderProgramColored.Dispose(); shaderProgram.Dispose(); vb1.Dispose(); vb2.Dispose(); vb3.Dispose(); renderer.Dispose(); }
public static ITexture2D LoadTexture(string filename, bool generateMips = false) { Assure.NotNull(filename); lock (staticMutationLock) { if (loadedTextures.ContainsKey(filename)) { ++textureRefCounts[filename]; return(loadedTextures[filename]); } } string fullFilePath = Path.Combine(MaterialsDir, filename); if (!File.Exists(fullFilePath)) { throw new FileNotFoundException("Given texture file '" + filename + "' is not in the materials folder."); } ITexture2D loadedTex = null; GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce; GC.Collect(2, GCCollectionMode.Forced, true); if (generateMips) { var mipmapFilePath = Path.Combine(MaterialsDir, "Mipmaps\\", Path.GetFileNameWithoutExtension(filename)) + ".mipmap"; if (File.Exists(mipmapFilePath)) { byte[] dataBytes = File.ReadAllBytes(mipmapFilePath); int texelTypeID; uint width, height; using (MemoryStream ms = new MemoryStream(dataBytes)) { BinaryReader br = new BinaryReader(ms); texelTypeID = br.ReadInt32(); width = br.ReadUInt32(); height = br.ReadUInt32(); } Type texelType = TexelFormat.AllFormats.First(kvp => kvp.Value.ResourceFormatIndex == texelTypeID).Key; var createTex2DMethod = typeof(TextureFactory) .GetMethod("NewTexture2D", BindingFlags.Public | BindingFlags.Static) .MakeGenericMethod(texelType); dynamic builder = createTex2DMethod.Invoke(null, new object[0]); loadedTex = builder.WithPermittedBindings(GPUBindings.ReadableShaderResource) .WithUsage(ResourceUsage.Immutable) .WithMipAllocation(true) .WithWidth(width) .WithHeight(height) .WithInitialData(new ArraySlice <byte>(dataBytes, 4U)) .Create(); } else { Logger.Log("No mipmap data found for file '" + fullFilePath + "'; "); LosgapSystem.InvokeOnMaster(() => { ITexture2D singleTex = TextureFactory.LoadTexture2D().WithFilePath(fullFilePath) .WithPermittedBindings(GPUBindings.None) .WithUsage(ResourceUsage.StagingReadWrite) .Create(); ITexture2D mipGenTarget = ((dynamic)singleTex).Clone(false) .WithMipAllocation(true) .WithMipGenerationTarget(true) .WithPermittedBindings(GPUBindings.RenderTarget | GPUBindings.ReadableShaderResource) .WithUsage(ResourceUsage.Write) .Create(); singleTex.GetType().GetMethod( "CopyTo", new[] { singleTex.GetType(), typeof(SubresourceBox), typeof(UInt32), typeof(UInt32), typeof(UInt32), typeof(UInt32) } ).Invoke( singleTex, new object[] { mipGenTarget, new SubresourceBox(0U, singleTex.Width, 0U, singleTex.Height), 0U, 0U, 0U, 0U } ); mipGenTarget.GenerateMips(); ITexture2D stagingTex = ((dynamic)mipGenTarget).Clone(false) .WithPermittedBindings(GPUBindings.None) .WithUsage(ResourceUsage.StagingReadWrite) .WithMipGenerationTarget(false) .Create(); mipGenTarget.CopyTo(stagingTex); using (MemoryStream ms = new MemoryStream()) { BinaryWriter bw = new BinaryWriter(ms); bw.Write(TexelFormat.AllFormats[mipGenTarget.TexelFormat].ResourceFormatIndex); bw.Write(stagingTex.Width); bw.Write(stagingTex.Height); bw.Write(stagingTex.ReadRaw()); ms.Seek(0L, SeekOrigin.Begin); File.WriteAllBytes(mipmapFilePath, ms.ToArray()); } loadedTex = ((dynamic)stagingTex).Clone(true) .WithPermittedBindings(GPUBindings.ReadableShaderResource) .WithUsage(ResourceUsage.Immutable) .Create(); singleTex.Dispose(); mipGenTarget.Dispose(); stagingTex.Dispose(); }); } } else { loadedTex = TextureFactory.LoadTexture2D().WithFilePath(fullFilePath) .WithPermittedBindings(GPUBindings.ReadableShaderResource) .WithUsage(ResourceUsage.Immutable) .Create(); } lock (staticMutationLock) { loadedTextures[filename] = loadedTex; textureRefCounts[filename] = 1U; } GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce; GC.Collect(2, GCCollectionMode.Forced, true); return(loadedTex); }
public void Detach() { _vertexArray.Dispose(); _texture.Dispose(); _shader.Dispose(); }
public void Detach() { _texture.Dispose(); _texture2.Dispose(); }
public void TestCreationParameters() { // Define variables and constants var defaultBuilder = TextureFactory.NewTexture2D <TexelFormat.RGBA32UInt>().WithUsage(ResourceUsage.DiscardWrite).WithWidth(100).WithHeight(256); var withStagingUsage = defaultBuilder.WithUsage(ResourceUsage.StagingReadWrite).WithPermittedBindings(GPUBindings.None); var withReadWriteBindings = defaultBuilder.WithUsage(ResourceUsage.Write).WithPermittedBindings(GPUBindings.ReadableShaderResource | GPUBindings.WritableShaderResource); var withDifferentFormat = defaultBuilder.WithTexelFormat <TexelFormat.Int8>(); var withWidth300 = defaultBuilder.WithWidth(300); var withMipAllocation = defaultBuilder.WithUsage(ResourceUsage.Write).WithWidth(1 << 9).WithMipAllocation(true); var withDynDetail = withMipAllocation.WithDynamicDetail(true); var withMipGen = withMipAllocation .WithUsage(ResourceUsage.Write) .WithPermittedBindings(GPUBindings.RenderTarget | GPUBindings.ReadableShaderResource) .WithMipGenerationTarget(true) .WithTexelFormat <TexelFormat.RGBA8UNorm>(); var withMS = defaultBuilder.WithMultisampling(true); // Set up context // Execute // Assert outcome ITexture2D tex = withStagingUsage.Create(); Assert.AreEqual(ResourceUsage.StagingReadWrite, tex.Usage); tex.Dispose(); tex = withReadWriteBindings.Create(); tex.Dispose(); Assert.AreEqual(GPUBindings.ReadableShaderResource | GPUBindings.WritableShaderResource, tex.PermittedBindings); tex = withDifferentFormat.Create(); tex.Dispose(); Assert.AreEqual(typeof(TexelFormat.Int8), tex.TexelFormat); tex = withWidth300.Create(); tex.Dispose(); Assert.AreEqual(300U, tex.Width); tex = withMipAllocation.Create(); tex.Dispose(); Assert.AreEqual(TextureUtils.GetNumMips(1 << 9, 256), tex.NumMips); tex = withDynDetail.Create(); tex.Dispose(); Assert.AreEqual(true, tex.IsGlobalDetailTarget); tex = withMipGen.Create(); tex.Dispose(); Assert.AreEqual(true, tex.IsMipGenTarget); tex = withMS.Create(); tex.Dispose(); Assert.AreEqual(true, tex.IsMultisampled); tex.Dispose(); ITexture2DArray ta = withStagingUsage.CreateArray(10); Assert.AreEqual(ResourceUsage.StagingReadWrite, ta.Usage); ta.Dispose(); ta = withReadWriteBindings.CreateArray(10); Assert.AreEqual(GPUBindings.ReadableShaderResource | GPUBindings.WritableShaderResource, ta.PermittedBindings); ta.Dispose(); ta = withDifferentFormat.WithUsage(ResourceUsage.Write).CreateArray(10); Assert.AreEqual(typeof(TexelFormat.Int8), ta.TexelFormat); ta.Dispose(); ta = withWidth300.WithUsage(ResourceUsage.Write).CreateArray(10); Assert.AreEqual(300U, ta.Width); ta.Dispose(); ta = withMipAllocation.CreateArray(10); Assert.AreEqual(TextureUtils.GetNumMips(1 << 9, 256), ta.NumMips); ta.Dispose(); ta = withDynDetail.CreateArray(10); Assert.AreEqual(true, ta.IsGlobalDetailTarget); ta.Dispose(); ta = withMipGen.CreateArray(10); Assert.AreEqual(true, ta.IsMipGenTarget); ta.Dispose(); ta = withMS.WithUsage(ResourceUsage.Write).CreateArray(10); Assert.AreEqual(true, ta.IsMultisampled); ta.Dispose(); ta = defaultBuilder.WithUsage(ResourceUsage.Immutable).WithInitialData(new TexelFormat.RGBA32UInt[10U * 100U * 256U]).CreateArray(10); Assert.AreEqual(10U, ta.ArrayLength); ta.Dispose(); #if !DEVELOPMENT && !RELEASE try { TextureFactory.NewTexture2D <TexelFormat.Float32>() .WithUsage(ResourceUsage.Immutable) .WithInitialData(null) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } try { TextureFactory.NewTexture2D <TexelFormat.Float32>() .WithUsage(ResourceUsage.DiscardWrite) .WithPermittedBindings(GPUBindings.None) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } try { TextureFactory.NewTexture2D <TexelFormat.Float32>() .WithUsage(ResourceUsage.StagingRead) .WithPermittedBindings(GPUBindings.ReadableShaderResource) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } try { TextureFactory.NewTexture2D <TexelFormat.Float32>() .WithWidth(0U) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } try { TextureFactory.NewTexture2D <TexelFormat.Float32>() .WithMipAllocation(true) .WithWidth(140) .WithHeight(1U) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } try { TextureFactory.NewTexture2D <TexelFormat.Float32>() .WithMipAllocation(true) .WithMipGenerationTarget(true) .WithWidth(1 << 4) .WithHeight(1 << 4) .WithUsage(ResourceUsage.Write) .WithPermittedBindings(GPUBindings.None) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } try { TextureFactory.NewTexture2D <TexelFormat.Float32>() .WithMipAllocation(false) .WithMipGenerationTarget(true) .WithWidth(1 << 4) .WithHeight(1 << 4) .WithUsage(ResourceUsage.Write) .WithPermittedBindings(GPUBindings.RenderTarget | GPUBindings.ReadableShaderResource) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } try { TextureFactory.NewTexture2D <TexelFormat.Float32>() .WithMipAllocation(true) .WithMipGenerationTarget(true) .WithWidth(1 << 4) .WithHeight(1 << 4) .WithUsage(ResourceUsage.Write) .WithPermittedBindings(GPUBindings.RenderTarget | GPUBindings.ReadableShaderResource) .WithInitialData(new TexelFormat.Float32[(1 << 5) - 1]) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } try { TextureFactory.NewTexture2D <TexelFormat.Float32>() .WithMipAllocation(true) .WithWidth(1 << 4) .WithHeight(1 << 4) .WithUsage(ResourceUsage.Immutable) .WithPermittedBindings(GPUBindings.ReadableShaderResource) .WithInitialData(new TexelFormat.Float32[1 << 4]) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } try { TextureFactory.NewTexture2D <TexelFormat.Float32>() .WithMipAllocation(false) .WithWidth(1 << 4) .WithHeight(1 << 4) .WithUsage(ResourceUsage.Immutable) .WithPermittedBindings(GPUBindings.ReadableShaderResource) .WithInitialData(new TexelFormat.Float32[(1 << 5) - 1]) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } try { TextureFactory.NewTexture2D <TexelFormat.Float32>() .WithMipAllocation(true) .WithWidth(32U) .WithHeight(32U) .WithMultisampling(true) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } try { TextureFactory.NewTexture2D <TexelFormat.Float32>() .WithMipAllocation(false) .WithWidth(2U) .WithHeight(2U) .WithMultisampling(true) .WithInitialData(new TexelFormat.Float32[4]) .Create(); Assert.Fail(); } catch (AssuranceFailedException) { } #endif }