public void OnDeviceInit() { ISrvBindable firstTex = MyManagers.FileTextures.GetTexture(m_listSubresourceFilenames[0], m_type, true); var srcDesc = firstTex.Srv.Description; Vector2I Size = firstTex.Size; Texture2DDescription desc = new Texture2DDescription(); desc.ArraySize = m_listSubresourceFilenames.Count; desc.BindFlags = BindFlags.ShaderResource; desc.CpuAccessFlags = CpuAccessFlags.None; desc.Format = srcDesc.Format; desc.Height = (int)Size.Y; desc.Width = (int)Size.X; desc.MipLevels = srcDesc.Texture2D.MipLevels; desc.SampleDescription.Count = 1; desc.SampleDescription.Quality = 0; desc.Usage = ResourceUsage.Default; m_resource = new Texture2D(MyRender11.Device, desc); m_resource.DebugName = m_resourceName; TextureFormat = srcDesc.Format; // foreach mip var mipmaps = srcDesc.Texture2D.MipLevels; int i = 0; foreach (var path in m_listSubresourceFilenames) { ISrvBindable tex = MyManagers.FileTextures.GetTexture(path, m_type, true); var tex2D = tex.Resource as Texture2D; MyRenderProxy.Assert(tex2D != null, "MyTextureArray supports only 2D textures. Inconsistent texture: " + tex.Name); bool consistent = MyResourceUtils.CheckTexturesConsistency(desc, tex2D.Description); if (!consistent) { string errorMsg = "All MyTextureArray has to have the same pixel format, width / height and # of mipmaps. Inconsistent textures: " + tex.Name + " / " + firstTex.Name; MyRenderProxy.Error(errorMsg); MyRender11.Log.WriteLine(errorMsg); } for (int m = 0; m < mipmaps; m++) { MyRender11.RC.CopySubresourceRegion(tex2D, Resource.CalculateSubResourceIndex(m, 0, mipmaps), null, Resource, Resource.CalculateSubResourceIndex(m, i, mipmaps)); int sizeX = Resource.CalculateMipSize(m, Size.X); int sizeY = Resource.CalculateMipSize(m, Size.Y); ByteSize += FormatHelper.ComputeScanlineCount(TextureFormat, sizeX) * 4 * FormatHelper.ComputeScanlineCount(TextureFormat, sizeY) * 4 * FormatHelper.SizeOfInBytes(TextureFormat); } i++; } m_srv = new ShaderResourceView(MyRender11.Device, Resource); }
public void OnDeviceInit() { MyFileTextureParams fileTexParams; bool ret = GetCorrectedFileTextureParams(out fileTexParams); //MyRenderProxy.Assert(ret, "It is not implemented mechanism, what to do, when none of the textures exist"); m_size = fileTexParams.Resolution; Texture2DDescription desc = new Texture2DDescription(); desc.ArraySize = m_listSubresourceFilenames.Count; desc.BindFlags = BindFlags.ShaderResource; desc.CpuAccessFlags = CpuAccessFlags.None; desc.Format = fileTexParams.Format; desc.Height = (int)Size.Y; desc.Width = (int)Size.X; var mipmaps = desc.MipLevels = fileTexParams.Mipmaps; desc.SampleDescription.Count = 1; desc.SampleDescription.Quality = 0; desc.Usage = ResourceUsage.Default; m_resource = new Texture2D(MyRender11.Device, desc); m_resource.DebugName = m_resourceName; TextureFormat = fileTexParams.Format; // foreach mip int i = 0; foreach (var path in m_listSubresourceFilenames) { bool isUsedCreatedGeneratedTexture = false; ISrvBindable tex = MyManagers.FileTextures.GetTexture(path, m_type, true); var tex2D = tex.Resource as Texture2D; MyRenderProxy.Assert(tex2D != null, "MyFileArrayTexture supports only 2D textures. Inconsistent texture: " + tex.Name); bool consistent = MyResourceUtils.CheckTexturesConsistency(desc, tex2D.Description); if (!consistent) { if (!string.IsNullOrEmpty(path) && MyFileSystem.FileExists(path)) { string msg = string.Format( "Texture {0} cannot be loaded. If this message is displayed on reloading textures, please restart the game. If it is not, please notify developers.", path); MyRenderProxy.Fail(msg); } } if (!consistent && m_recoverySystem.UseErrorTexture) // if the texture cannot be used, error texture will be used { tex = MyManagers.FileTextures.GetTexture(m_recoverySystem.TextureFilepath, m_type, true); tex2D = tex.Resource as Texture2D; MyRenderProxy.Assert(tex2D != null, "MyFileArrayTexture supports only 2D textures. Inconsistent texture: " + m_recoverySystem.TextureFilepath); consistent = MyResourceUtils.CheckTexturesConsistency(desc, tex2D.Description); } if (!consistent && m_recoverySystem.UseBytePattern) // if the texture cannot be used, byte pattern will be used to generate texture { tex = MyManagers.GeneratedTextures.CreateFromBytePattern("MyFileArrayTexture.Tmp", desc.Width, desc.Height, m_recoverySystem.FormatBytePattern, m_recoverySystem.BytePattern); tex2D = tex.Resource as Texture2D; MyRenderProxy.Assert(tex2D != null); consistent = MyResourceUtils.CheckTexturesConsistency(desc, tex2D.Description); isUsedCreatedGeneratedTexture = true; } if (!consistent) { Texture2DDescription desc1 = desc; Texture2DDescription desc2 = tex2D.Description; string errorMsg = string.Format("Textures ({0}) is not compatible within array texture! Width: ({1},{2}) Height: ({3},{4}) Mipmaps: ({5},{6}) Format: ({7},{8})", path, desc1.Width, desc2.Width, desc1.Height, desc2.Height, desc1.MipLevels, desc2.MipLevels, desc1.Format, desc2.Format); MyRenderProxy.Error(errorMsg); MyRender11.Log.WriteLine(errorMsg); } for (int m = 0; m < mipmaps; m++) { MyRender11.RC.CopySubresourceRegion(tex, Resource.CalculateSubResourceIndex(m, 0, mipmaps), null, Resource, Resource.CalculateSubResourceIndex(m, i, mipmaps)); int sizeX = Resource.CalculateMipSize(m, Size.X); int sizeY = Resource.CalculateMipSize(m, Size.Y); ByteSize += FormatHelper.ComputeScanlineCount(TextureFormat, sizeX) * 4 * FormatHelper.ComputeScanlineCount(TextureFormat, sizeY) * 4 * FormatHelper.SizeOfInBytes(TextureFormat); } if (isUsedCreatedGeneratedTexture) { IGeneratedTexture generatedTex = (IGeneratedTexture)tex; MyManagers.GeneratedTextures.DisposeTex(generatedTex); } i++; } m_srv = new ShaderResourceView(MyRender11.Device, Resource); }