Beispiel #1
0
 /// <summary>
 /// テクスチャを更新する必要が有る場合更新する
 /// レンダリングと同一タイミングで呼び出すこと。
 /// </summary>
 public void Update()
 {
     if (NeedRedraw)
     {
         using (MemoryStream ms = new MemoryStream())
         {
             lock (mapedBitmap)
             {
                 mapedBitmap.Save(ms, ImageFormat.Tiff);
             }
             ms.Seek(0, SeekOrigin.Begin);
             try
             {
                 if (SpriteTexture != null)
                 {
                     lock (SpriteTexture)
                     {
                         SpriteTexture.Dispose();
                         SpriteTexture = ShaderResourceView.FromStream(Context.DeviceManager.Device, ms, (int)ms.Length);
                     }
                 }
                 else
                 {
                     SpriteTexture = ShaderResourceView.FromStream(Context.DeviceManager.Device, ms, (int)ms.Length);
                 }
             }
             catch (Direct3D11Exception)
             {
                 //終了時になぜかスローされる。とりあえず無視
             }
         }
         NeedRedraw = false;
     }
 }
Beispiel #2
0
 /// <summary>
 /// If you need to update the texture update
 /// Calling the render with the same timing。
 /// </summary>
 public void Update()
 {
     if (this.NeedRedraw)
     {
         using (MemoryStream ms = new MemoryStream())
         {
             lock (this.mapedBitmap)
             {
                 this.mapedBitmap.Save(ms, ImageFormat.Tiff);
             }
             ms.Seek(0, SeekOrigin.Begin);
             try
             {
                 if (this.SpriteTexture != null)
                 {
                     lock (this.SpriteTexture)
                     {
                         this.SpriteTexture.Dispose();
                         this.SpriteTexture = ShaderResourceView.FromStream(this.Context.DeviceManager.Device, ms, (int)ms.Length);
                     }
                 }
                 else
                 {
                     this.SpriteTexture = ShaderResourceView.FromStream(this.Context.DeviceManager.Device, ms, (int)ms.Length);
                 }
             }
             catch (Direct3D11Exception)
             {
                 //Why is thrown at the end of。とりあえず無視
             }
         }
         this.NeedRedraw = false;
     }
 }
 protected override void InitializeInternal()
 {
     // todo: make sure the texture has actually loaded successfully - note that it may not be loading from a regular file, so we can't rely on FileNotFoundException bubbling up; wrap it in a local exception
     using (var stream = _fileSource.OpenRead())
         ShaderResourceView = ShaderResourceView.FromStream(DeviceManager.Device, stream, _fileSource.Size);
     _descr = ShaderResourceView.ResourceAs <Texture2D>().Description;
 }
Beispiel #4
0
 private ShaderResourceView getSubresourceById(string p)
 {
     using (Stream stream = subresourceManager.getSubresourceByName(p))
     {
         if (stream == null)
         {
             return(null);
         }
         return(ShaderResourceView.FromStream(device, stream, (int)stream.Length));
     }
 }
Beispiel #5
0
 public int LoadToon(string path)
 {
     using (Stream stream = _subresourceManager.getSubresourceByName(path))
     {
         if (stream == null)
         {
             return(0);
         }
         resourceViewsList.Add(ShaderResourceView.FromStream(_device, stream, (int)stream.Length));
         return(resourceViewsList.Count - 1);
     }
 }
 static private ShaderResourceView CreateTGATex(Device device, string path)
 {
     using (var tgaBitMap = Paloma.TargaImage.LoadTargaImage(path))
     {
         using (var stream = new MemoryStream())
         {
             tgaBitMap.Save(stream, ImageFormat.Png);
             stream.Position = 0;
             return(ShaderResourceView.FromStream(device, stream, (int)stream.Length));
         }
     }
 }
Beispiel #7
0
        /// <summary>
        /// ファイルから
        /// </summary>
        /// <param name="file_name"></param>
        public TextureBuffer(string file_name)
        {
            using (var stream = new System.IO.FileStream(file_name, System.IO.FileMode.Open, FileAccess.Read)) {
                byte[] data = new byte[stream.Length];
                stream.Read(data, 0, (int)stream.Length);
                MemoryStream s = new MemoryStream(data);

                // 名前からDDSかを判別
                string ext = Path.GetExtension(file_name).ToLower();
                if (ext == ".kstex")
                {
                    // ksTex
                    // byte配列からポインタを取得
                    var         handle = GCHandle.Alloc(data, GCHandleType.Pinned);
                    IntPtr      ptr    = handle.AddrOfPinnedObject();
                    ksTexHeader header = (ksTexHeader)Marshal.PtrToStructure(ptr, typeof(ksTexHeader));
                    int         offset = Marshal.SizeOf(header);

                    // ストリームの一をセットしてSRV生成
                    s.Position          = offset;
                    shaderResourceView_ = ShaderResourceView.FromStream(GraphicsCore.D3D11Device, s, header.data_size);
                    handle.Free();
                }
                else if (ext == ".tga")
                {
                    // tga
                    var       handle     = GCHandle.Alloc(data, GCHandleType.Pinned);
                    IntPtr    ptr        = handle.AddrOfPinnedObject();
                    tgaHeader header     = (tgaHeader)Marshal.PtrToStructure(ptr, typeof(tgaHeader));
                    int       offset     = Marshal.SizeOf(header);
                    int       image_size = header.width * header.height * 4;                    // 32bit固定
                    byte[]    image_buf  = new byte[image_size];
                    // 上下反転
                    int w            = header.width;
                    int h            = header.height;
                    int dst_row_size = w * 4;
                    int bpp          = header.depth / 8;
                    int src_row_size = w * bpp;
                    Func <int, int, int> getAddrFunc;
                    if ((header.flag & (1 << 5)) == 0)
                    {
                        getAddrFunc = (height, current_heght) => {
                            return((height - 1 - current_heght) * dst_row_size);
                        };
                    }
                    else
                    {
                        getAddrFunc = (height, current_heght) => {
                            return(current_heght * dst_row_size);
                        };
                    }
                    for (int ch = 0; ch < h; ch++)
                    {
                        int src_start = src_row_size * ch + offset;
                        int dst_start = getAddrFunc(h, ch);
                        for (int cw = 0; cw < w; cw++)
                        {
                            int src = src_start + cw * bpp;
                            int dst = dst_start + cw * 4;
                            image_buf[dst + 0] = data[src + 0];
                            image_buf[dst + 1] = data[src + 1];
                            image_buf[dst + 2] = data[src + 2];
                            image_buf[dst + 3] = (bpp == 4) ? data[src + 3] : (byte)255;
                        }
                    }
                    byte[][][] data_ary = new byte[][][] {
                        new byte[][] {
                            image_buf,
                        },
                    };
                    BufferDesc desc = new BufferDesc()
                    {
                        width    = header.width,
                        height   = header.height,
                        format   = Format.B8G8R8A8_UNorm,
                        initData = data_ary,
                        mips     = 0,
                    };
                    Initialize(desc);
                    handle.Free();
                }
                else
                {
                    // その他フォーマット
                    shaderResourceView_ = ShaderResourceView.FromStream(GraphicsCore.D3D11Device, s, (int)stream.Length);
                }

                // 情報取得
                if (shaderResourceView_.Resource is Texture2D)
                {
                    var tex = (Texture2D)shaderResourceView_.Resource;
                    width_  = tex.Description.Width;
                    height_ = tex.Description.Height;
                    format_ = tex.Description.Format;
                    mips_   = tex.Description.MipLevels;
                }
                else if (shaderResourceView_.Resource is Texture3D)
                {
                    var tex = (Texture3D)shaderResourceView_.Resource;
                    width_  = tex.Description.Width;
                    height_ = tex.Description.Height;
                    format_ = tex.Description.Format;
                    mips_   = tex.Description.MipLevels;
                }

                buffer_ = shaderResourceView_.Resource;
            }
        }
Beispiel #8
0
        public void Init(Device device)
        {
            _color = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);
            using (Stream stream = Application.GetResourceStream(new Uri("pack://*****:*****@"Content\Images\numbers.png");
            using (var bytecode = ShaderBytecodeExtension.CompileFromResource("pack://*****:*****@"Shader\Visible\Neatline.Numbers.hlsl", "VS", "vs_5_0", shaderFlags, EffectFlags.None))
            {
                _vertexShader = ToDispose(new VertexShader(device, bytecode));
                _inputLayout  = ToDispose(new InputLayout(device, bytecode, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0, InputClassification.PerVertexData, 0) }));
            }
            using (var bytecode = ShaderBytecodeExtension.CompileFromResource("pack://*****:*****@"Shader\Visible\Neatline.Numbers.hlsl", "PS", "ps_5_0", shaderFlags, EffectFlags.None))
            {
                _pixelShader = ToDispose(new PixelShader(device, bytecode));
            }

            _globalBuffer = ToDispose(new Buffer(device, Matrix.SizeInBytes + 2 * Vector4.SizeInBytes, ResourceUsage.Dynamic, BindFlags.ConstantBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0));

            using (var dataStream = new DataStream(Vector4.SizeInBytes * 4, true, true))
            {
                dataStream.Write(new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
                dataStream.Write(new Vector4(0.0f, 1.0f, 0.0f, 0.0f));
                dataStream.Write(new Vector4(1.0f, 1.0f, 1.0f, 0.0f));
                dataStream.Write(new Vector4(1.0f, 0.0f, 1.0f, 1.0f));

                dataStream.Position = 0;
                _buffer             = ToDispose(new Buffer(device, dataStream, Vector4.SizeInBytes * 4, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0));
                _bufferBinding      = new VertexBufferBinding(_buffer, Vector4.SizeInBytes, 0);
            }
            using (var dataStream = new DataStream(sizeof(int) * 6, true, true))
            {
                dataStream.Write(0);
                dataStream.Write(1);
                dataStream.Write(2);
                dataStream.Write(2);
                dataStream.Write(3);
                dataStream.Write(0);
                dataStream.Position = 0;
                _indexBuffer        = ToDispose(new Buffer(device, dataStream, sizeof(int) * 6, ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0));
            }

            // Create a texture sampler state description.
            var samplerDesc = new SamplerStateDescription()
            {
                //Filter = Filter.MinMagMipLinear,
                Filter             = Filter.MinMagMipPoint,
                AddressU           = TextureAddressMode.Wrap,
                AddressV           = TextureAddressMode.Wrap,
                AddressW           = TextureAddressMode.Wrap,
                MipLodBias         = 0,
                MaximumAnisotropy  = 1,
                ComparisonFunction = Comparison.Always,
                BorderColor        = new Color4(0, 0, 0, 0),
                MinimumLod         = 0,
                MaximumLod         = 0
            };

            // Create the texture sampler state.
            _sampleState = new SamplerState(device, samplerDesc);
        }