public Renderer.Texture2D CreateTextureCube(Renderer.Device _Device) { if (m_Opts.m_type != ImageOptions.TYPE.TT_CUBIC) { throw new Exception("The image is not a cube map texture!"); } ImageUtility.PIXEL_FORMAT textureFormat = ImageUtility.PIXEL_FORMAT.UNKNOWN; ImageUtility.COMPONENT_FORMAT componentFormat; m_Opts.m_format.GetEquivalentRendererFormat(out textureFormat, out componentFormat); uint ArraySize = 6 * m_Opts.m_arraySize; uint MipsCount = m_Opts.m_curNumLevels; uint PixelSize = m_Opts.m_format.BitsCount >> 3; List <Renderer.PixelsBuffer> Content = new List <Renderer.PixelsBuffer>(); for (uint SliceIndex = 0; SliceIndex < ArraySize; SliceIndex++) { for (uint MipLevelIndex = 0; MipLevelIndex < MipsCount; MipLevelIndex++) { ImageSlice Slice = m_Slices[MipLevelIndex * ArraySize + SliceIndex]; // Stupidly stored in reverse order! Renderer.PixelsBuffer Pixels = new Renderer.PixelsBuffer((uint)(Slice.m_Width * Slice.m_Height * PixelSize)); Content.Add(Pixels); using (BinaryWriter Writer = Pixels.OpenStreamWrite()) Writer.Write(Slice.m_Content); } } Renderer.Texture2D Result = new Renderer.Texture2D(_Device, m_Opts.m_curWidth, m_Opts.m_curHeight, -6 * (int)m_Opts.m_arraySize, m_Opts.m_curNumLevels, textureFormat, componentFormat, false, false, Content.ToArray()); return(Result); }
/// <summary> /// Constructor /// </summary> /// <param name="_device">Device CAN be null, in which case the CPU version will be used</param> /// <param name="_texturePOT">Power-of-Two texture size</param> /// <param name="_dimensions">Dimensions of the noise vectors (1 for monochromatic noise, etc.)</param> public GeneratorSolidAngleGPU(Device _device, uint _texturePOT, uint _dimensions) { m_texturePOT = (int)_texturePOT; m_textureSize = 1U << m_texturePOT; m_textureSizeMask = m_textureSize - 1; m_textureTotalSize = m_textureSize * m_textureSize; m_vectorDimension = _dimensions; try { m_CS_Copy = new ComputeShader(_device, new System.IO.FileInfo(@"Shaders/SimulatedAnnealing.hlsl"), "CS__Copy"); m_CS_Mutate = new ComputeShader(_device, new System.IO.FileInfo(@"Shaders/SimulatedAnnealing.hlsl"), "CS__Mutate"); switch (m_vectorDimension) { case 1: m_CS_ComputeScore = new ComputeShader(_device, new System.IO.FileInfo(@"Shaders/SimulatedAnnealing.hlsl"), "CS__ComputeScore1D"); break; case 2: m_CS_ComputeScore = new ComputeShader(_device, new System.IO.FileInfo(@"Shaders/SimulatedAnnealing.hlsl"), "CS__ComputeScore2D"); break; default: throw new Exception("Unsupported vector dimension!"); } m_CS_AccumulateScore16 = new ComputeShader(_device, new System.IO.FileInfo(@"Shaders/SimulatedAnnealing.hlsl"), "CS__AccumulateScore16"); m_CS_AccumulateScore8 = new ComputeShader(_device, new System.IO.FileInfo(@"Shaders/SimulatedAnnealing.hlsl"), "CS__AccumulateScore8"); m_CS_AccumulateScore4 = new ComputeShader(_device, new System.IO.FileInfo(@"Shaders/SimulatedAnnealing.hlsl"), "CS__AccumulateScore4"); m_CS_AccumulateScore2 = new ComputeShader(_device, new System.IO.FileInfo(@"Shaders/SimulatedAnnealing.hlsl"), "CS__AccumulateScore2"); m_CB_Main = new ConstantBuffer <CB_Main>(_device, 0); m_CB_Mips = new ConstantBuffer <CB_Mips>(_device, 1); m_SB_Mutations = new StructuredBuffer <SB_Mutation>(_device, MAX_MUTATIONS_RATE, true, false); ImageUtility.PIXEL_FORMAT noiseFormat = ImageUtility.PIXEL_FORMAT.UNKNOWN; switch (m_vectorDimension) { case 1: noiseFormat = ImageUtility.PIXEL_FORMAT.R32F; break; case 2: noiseFormat = ImageUtility.PIXEL_FORMAT.RG32F; break; } m_texNoise0 = new Texture2D(_device, m_textureSize, m_textureSize, 1, 1, noiseFormat, ImageUtility.COMPONENT_FORMAT.AUTO, false, true, null); m_texNoise1 = new Texture2D(_device, m_textureSize, m_textureSize, 1, 1, noiseFormat, ImageUtility.COMPONENT_FORMAT.AUTO, false, true, null); m_texNoiseCPU = new Texture2D(_device, m_textureSize, m_textureSize, 1, 1, noiseFormat, ImageUtility.COMPONENT_FORMAT.AUTO, true, true, null); m_texNoiseScore = new Texture2D(_device, m_textureSize, m_textureSize, 1, 1 + _texturePOT, ImageUtility.PIXEL_FORMAT.R32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, true, null); m_texNoiseScore2 = new Texture2D(_device, m_textureSize, m_textureSize, 1, 1 + _texturePOT, ImageUtility.PIXEL_FORMAT.R32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, true, null); m_texNoiseScoreCPU = new Texture2D(_device, m_textureSize, m_textureSize, 1, 1 + _texturePOT, ImageUtility.PIXEL_FORMAT.R32F, ImageUtility.COMPONENT_FORMAT.AUTO, true, true, null); } catch (Exception _e) { throw new Exception("An error occurred while creating DirectX structures: " + _e.Message, _e); } }
public void GetEquivalentRendererFormat(out ImageUtility.PIXEL_FORMAT _pixelFormat, out ImageUtility.COMPONENT_FORMAT _componentFormat) { switch (m_type) { case Type.UINT: _componentFormat = ImageUtility.COMPONENT_FORMAT.UINT; break; case Type.SINT: _componentFormat = ImageUtility.COMPONENT_FORMAT.SINT; break; case Type.UNORM: _componentFormat = ImageUtility.COMPONENT_FORMAT.UNORM; break; case Type.UNORM_sRGB: _componentFormat = ImageUtility.COMPONENT_FORMAT.UNORM_sRGB; break; case Type.SNORM: _componentFormat = ImageUtility.COMPONENT_FORMAT.SNORM; break; default: _componentFormat = ImageUtility.COMPONENT_FORMAT.AUTO; break; } switch (m_layout) { // 8-bits formats case Layout.LAYOUT_8: _pixelFormat = ImageUtility.PIXEL_FORMAT.R8; break; case Layout.LAYOUT_8_8: _pixelFormat = ImageUtility.PIXEL_FORMAT.RG8; break; case Layout.LAYOUT_8_8_8: _pixelFormat = ImageUtility.PIXEL_FORMAT.RGB8; break; case Layout.LAYOUT_8_8_8_8: _pixelFormat = ImageUtility.PIXEL_FORMAT.RGBA8; break; // 16-bit formats case Layout.LAYOUT_16: _pixelFormat = m_type != Type.FLOAT ? ImageUtility.PIXEL_FORMAT.R16 : ImageUtility.PIXEL_FORMAT.R16F; break; case Layout.LAYOUT_16_16: _pixelFormat = m_type != Type.FLOAT ? ImageUtility.PIXEL_FORMAT.RG16 : ImageUtility.PIXEL_FORMAT.RG16F; break; case Layout.LAYOUT_16_16_16: _pixelFormat = m_type != Type.FLOAT ? ImageUtility.PIXEL_FORMAT.RGB16 : ImageUtility.PIXEL_FORMAT.RGB16F; break; case Layout.LAYOUT_16_16_16_16: _pixelFormat = m_type != Type.FLOAT ? ImageUtility.PIXEL_FORMAT.RGBA16 : ImageUtility.PIXEL_FORMAT.RGBA16F; break; // 32-bit formats case Layout.LAYOUT_32: _pixelFormat = m_type != Type.FLOAT ? ImageUtility.PIXEL_FORMAT.R32 : ImageUtility.PIXEL_FORMAT.R32F; break; case Layout.LAYOUT_32_32: _pixelFormat = m_type != Type.FLOAT ? ImageUtility.PIXEL_FORMAT.RG32 : ImageUtility.PIXEL_FORMAT.RG32F; break; case Layout.LAYOUT_32_32_32: _pixelFormat = m_type != Type.FLOAT ? ImageUtility.PIXEL_FORMAT.RGB32 : ImageUtility.PIXEL_FORMAT.RGB32F; break; case Layout.LAYOUT_32_32_32_32: _pixelFormat = m_type != Type.FLOAT ? ImageUtility.PIXEL_FORMAT.RGBA32 : ImageUtility.PIXEL_FORMAT.RGBA32F; break; // Compressed formats case Layout.LAYOUT_BC4: _pixelFormat = ImageUtility.PIXEL_FORMAT.BC4; break; case Layout.LAYOUT_BC5: _pixelFormat = ImageUtility.PIXEL_FORMAT.BC5; break; case Layout.LAYOUT_BC6: _pixelFormat = ImageUtility.PIXEL_FORMAT.BC6H; break; case Layout.LAYOUT_BC7: _pixelFormat = ImageUtility.PIXEL_FORMAT.BC7; break; default: throw new Exception("Unsupported image format " + ToString()); } }
/// <summary> /// Concatenates multiple tables into one single texture 2D array /// </summary> /// <param name="_tablesFileNames"></param> /// <param name="_targetFileName"></param> /// <param name="_foramt"></param> static void ExportTexture(FileInfo[] _tablesFileNames, FileInfo _targetFileName, ImageUtility.PIXEL_FORMAT _format) { // Load tables LTC[][,] tables = new LTC[_tablesFileNames.Length][, ]; for (int i = 0; i < _tablesFileNames.Length; i++) { int validResultsCount; LTC[,] table = FitterForm.LoadTable(_tablesFileNames[i], out validResultsCount); if (validResultsCount != table.Length) { throw new Exception("Not all table results are valid!"); } tables[i] = table; if (i != 0 && (table.GetLength(0) != tables[0].GetLength(0) || table.GetLength(1) != tables[0].GetLength(1))) { throw new Exception("Table dimensions mismatch!"); } } // Create the Texture2DArray uint W = (uint)tables[0].GetLength(0); uint H = (uint)tables[0].GetLength(1); ImageUtility.ImagesMatrix M = new ImageUtility.ImagesMatrix(); M.InitTexture2DArray(W, H, (uint)tables.Length, 1); M.AllocateImageFiles(_format, new ImageUtility.ColorProfile(ImageUtility.ColorProfile.STANDARD_PROFILE.LINEAR)); for (int i = 0; i < tables.Length; i++) { LTC[,] table = tables[i]; // ImageUtility.ImageFile I = new ImageUtility.ImageFile( W, H, _format, profile ); // M[(uint) i][0][0] = I; double largest = 0; ImageUtility.ImageFile I = M[(uint)i][0][0]; I.WritePixels((uint _X, uint _Y, ref float4 _color) => { LTC ltc = table[_X, _Y]; const double tol = 1e-6; // if ( Mathf.Abs( ltc.invM[2,2] - 1 ) > tol ) // throw new Exception( "Not one!" ); if (Mathf.Abs(ltc.invM[0, 1]) > tol || Mathf.Abs(ltc.invM[1, 0]) > tol || Mathf.Abs(ltc.invM[1, 2]) > tol || Mathf.Abs(ltc.invM[2, 1]) > tol) { throw new Exception("Not zero!"); } largest = Math.Max(largest, Math.Abs(ltc.invM[2, 2] - 1)); double factor = 1.0 / ltc.invM[2, 2]; _color.x = (float)(factor * ltc.invM[0, 0]); _color.y = (float)(factor * ltc.invM[0, 2]); _color.z = (float)(factor * ltc.invM[1, 1]); _color.w = (float)(factor * ltc.invM[2, 0]); }); } M.DDSSaveFile(_targetFileName, ImageUtility.COMPONENT_FORMAT.AUTO); }