internal SdkMeshMesh(D3D11Device device, SdkMeshRawFile rawFile, SdkMeshRawMesh rawMesh)
        {
            this.Name = rawMesh.Name;
            this.ComputeBoundingBox(rawFile, rawMesh);

            this.VertexBuffers = new SdkMeshVertexBuffer[rawMesh.NumVertexBuffers];

            for (int i = 0; i < rawMesh.NumVertexBuffers; i++)
            {
                this.VertexBuffers[i] = new SdkMeshVertexBuffer(device, rawFile, rawMesh, i);
            }

            this.IndexBuffer = new SdkMeshIndexBuffer(device, rawFile, rawMesh);

            foreach (int index in rawMesh.SubsetsIndices)
            {
                SdkMeshSubset subset = new SdkMeshSubset(rawFile.Subsets[index]);
                this.Subsets.Add(subset);
            }

            foreach (int index in rawMesh.FrameInfluencesIndices)
            {
                this.FrameInfluencesIndices.Add(index);
            }
        }
Пример #2
0
        public override void Update(D3D11Device device)
        {
            var rt = SetRenderTarget(device);

            if (_bitmap == null)
            {
                var pf = new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied);
                var bp = new BitmapProperties1(pf, device.Dpi.Height, device.Dpi.Width,
                                               BitmapOptions.CannotDraw | BitmapOptions.Target)
                ;

                using (var surface = rt.Surface)
                {
                    _bitmap = new Bitmap1(device.D2DDeviceContext, surface, bp);
                }
            }

            device.D2DDeviceContext.Target = _bitmap;
            device.D2DDeviceContext.BeginDraw();
            {
                _scene.Draw(device.D2DDeviceContext, new SharpDX.RectangleF(0, 0, _rect.Width, _rect.Height));
            }
            device.D2DDeviceContext.EndDraw();
            device.D2DDeviceContext.Target = null;
        }
Пример #3
0
        static D3D11UnorderedAccessView CreateBufferUAV(D3D11Device pDevice, D3D11Buffer pBuffer)
        {
            var descBuf = pBuffer.Description;

            D3D11UnorderedAccessViewDesc desc;

            if (descBuf.MiscOptions.HasFlag(D3D11ResourceMiscOptions.BufferAllowRawViews))
            {
                // This is a Raw Buffer
                // Format must be DXGI_FORMAT_R32_TYPELESS, when creating Raw Unordered Access View
                desc = new D3D11UnorderedAccessViewDesc(pBuffer, DxgiFormat.R32Typeless, 0, descBuf.ByteWidth / 4, D3D11BufferUavOptions.Raw);
            }
            else if (descBuf.MiscOptions.HasFlag(D3D11ResourceMiscOptions.BufferStructured))
            {
                // This is a Structured Buffer
                // Format must be must be DXGI_FORMAT_UNKNOWN, when creating a View of a Structured Buffer
                desc = new D3D11UnorderedAccessViewDesc(pBuffer, DxgiFormat.Unknown, 0, descBuf.ByteWidth / descBuf.StructureByteStride, D3D11BufferUavOptions.None);
            }
            else
            {
                throw new InvalidOperationException();
            }

            return(pDevice.CreateUnorderedAccessView(pBuffer, desc));
        }
        internal SdkMeshIndexBuffer(D3D11Device device, SdkMeshRawFile rawFile, SdkMeshRawMesh rawMesh)
        {
            int index = rawMesh.IndexBuffer;
            SdkMeshRawIndexBufferHeader header = rawFile.IndexBufferHeaders[index];

            byte[] bytes = rawFile.IndexBufferBytes[index];

            this.NumIndices = (int)header.NumIndices;
            this.SizeBytes  = (uint)header.SizeBytes;

            switch (header.IndexType)
            {
            case SdkMeshIndexType.IndexType16Bit:
                this.IndexFormat = DxgiFormat.R16UInt;
                break;

            case SdkMeshIndexType.IndexType32Bit:
                this.IndexFormat = DxgiFormat.R32UInt;
                break;

            default:
                this.IndexFormat = DxgiFormat.R16UInt;
                break;
            }

            var desc = new D3D11BufferDesc((uint)header.SizeBytes, D3D11BindOptions.IndexBuffer);
            var data = new D3D11SubResourceData(bytes, 0, 0);

            this.Buffer = device.CreateBuffer(desc, data);
        }
Пример #5
0
        public void DrawSubmesh(D3D11Device device)
        {
            // material constants
            Material.Setup(device);

            Mesh.DrawIndexed(device, DrawIndexOffset, DrawIndexCount);
        }
Пример #6
0
        public void RenderMsgPackCommands(D3D11Device device,
                                          Scene scene,
                                          ArraySegment <byte> commands)
        {
            m_backbuffer.Begin(device, scene, m_clear);
            while (commands.Count > 0)
            {
                try
                {
                    m_parsed.Clear();
                    var parsed = MsgPackParser.Parse(m_parsed, commands);

                    m_dispatcher.Dispatch(parsed);

                    commands = Skip(commands, parsed.Value.Bytes.Count);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    break;
                }
            }
            m_backbuffer.End();
            m_swapchain.Present();
        }
Пример #7
0
        private static void CreateTexture(D3D11Device device, D3D11DeviceContext deviceContext, string fileName, out D3D11ShaderResourceView textureView)
        {
            if (!File.Exists(fileName))
            {
                textureView = null;
                return;
            }

            string ext = Path.GetExtension(fileName);

            if (string.Equals(ext, ".dds", StringComparison.OrdinalIgnoreCase))
            {
                DdsDirectX.CreateTexture(fileName, device, deviceContext, out textureView);
            }
            else if (string.Equals(ext, ".jpg", StringComparison.OrdinalIgnoreCase) ||
                     string.Equals(ext, ".bmp", StringComparison.OrdinalIgnoreCase) ||
                     string.Equals(ext, ".png", StringComparison.OrdinalIgnoreCase) ||
                     string.Equals(ext, ".gif", StringComparison.OrdinalIgnoreCase))
            {
                CreateBitmapTexture(device, fileName, out textureView);
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Пример #8
0
        internal SdkMeshMaterial(D3D11Device device, D3D11DeviceContext deviceContext, string directory, SdkMeshRawMaterial rawMaterial)
        {
            this.Name = rawMaterial.Name;

            if (!string.IsNullOrEmpty(rawMaterial.DiffuseTexture))
            {
                this.DiffuseTextureName = Path.GetFileName(rawMaterial.DiffuseTexture);
                CreateTexture(device, deviceContext, Path.Combine(directory, Path.GetFileName(rawMaterial.DiffuseTexture)), out D3D11ShaderResourceView textureView);
                this.DiffuseTextureView = textureView;
            }

            if (!string.IsNullOrEmpty(rawMaterial.NormalTexture))
            {
                this.NormalTextureName = Path.GetFileName(rawMaterial.NormalTexture);
                CreateTexture(device, deviceContext, Path.Combine(directory, Path.GetFileName(rawMaterial.NormalTexture)), out D3D11ShaderResourceView textureView);
                this.NormalTextureView = textureView;
            }

            if (!string.IsNullOrEmpty(rawMaterial.SpecularTexture))
            {
                this.SpecularTextureName = Path.GetFileName(rawMaterial.SpecularTexture);
                CreateTexture(device, deviceContext, Path.Combine(directory, Path.GetFileName(rawMaterial.SpecularTexture)), out D3D11ShaderResourceView textureView);
                this.SpecularTextureView = textureView;
            }
        }
Пример #9
0
        public Backbuffer(D3D11Device device, Window window)
        {
            m_swapchain  = device.CreateSwapchain(window.WindowHandle);
            m_backbuffer = m_swapchain.CreateBitmap();

            m_dispatcher.RegisterInterface(typeof(IDrawProcessor), m_backbuffer);
        }
 public static void CreateTexture(
     DdsFile dds,
     D3D11Device device,
     D3D11DeviceContext context,
     out D3D11Resource texture,
     out D3D11ShaderResourceView textureView)
 {
     CreateTexture(dds, device, context, 0, out texture, out textureView, out _);
 }
Пример #11
0
        static D3D11Buffer CreateRawBuffer <T>(D3D11Device pDevice, T[] pInitData)
            where T : struct
        {
            var desc = D3D11BufferDesc.From(pInitData, D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource | D3D11BindOptions.IndexBuffer | D3D11BindOptions.VertexBuffer);

            desc.MiscOptions = D3D11ResourceMiscOptions.BufferAllowRawViews;

            return(pDevice.CreateBuffer(desc, pInitData, 0, 0));
        }
 public static void CreateTexture(
     DdsFile dds,
     D3D11Device device,
     D3D11DeviceContext context,
     out D3D11ShaderResourceView textureView)
 {
     CreateTexture(dds, device, context, 0, out D3D11Resource texture, out textureView, out _);
     D3D11Utils.DisposeAndNull(ref texture);
 }
Пример #13
0
 public override void Update(D3D11Device device)
 {
     SetRenderTarget(device);
     if (_drawable != null)
     {
         _drawable.Update(device);
         _drawable.Draw(device, 0, 0);
     }
 }
        public static void CreateTexture(
            string fileName,
            D3D11Device device,
            D3D11DeviceContext context,
            out D3D11ShaderResourceView textureView)
        {
            DdsFile dds = DdsFile.FromFile(fileName);

            CreateTexture(dds, device, context, out textureView);
        }
Пример #15
0
 protected D3D11RenderTarget SetRenderTarget(D3D11Device device)
 {
     if (_renderTarget == null)
     {
         _renderTarget = new D3D11RenderTarget();
         _renderTarget.Create(device, Width, Height);
     }
     _renderTarget.Setup(device, ClearColor);
     return(_renderTarget);
 }
        public static void CreateTexture(
            Stream stream,
            D3D11Device device,
            D3D11DeviceContext context,
            out D3D11ShaderResourceView textureView)
        {
            DdsFile dds = DdsFile.FromStream(stream);

            CreateTexture(dds, device, context, out textureView);
        }
Пример #17
0
        static D3D11Buffer CreateStructuredBuffer <T>(D3D11Device pDevice, T[] pInitData)
            where T : struct
        {
            var desc = D3D11BufferDesc.From(pInitData, D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource);

            desc.MiscOptions         = D3D11ResourceMiscOptions.BufferStructured;
            desc.StructureByteStride = (uint)Marshal.SizeOf(typeof(T));

            return(pDevice.CreateBuffer(desc, pInitData, 0, 0));
        }
Пример #18
0
        private static void CreateBitmapTexture(D3D11Device device, string fileName, out D3D11ShaderResourceView textureView)
        {
            int width;
            int height;

            byte[] bytes;

            using (var file = new Bitmap(fileName))
            {
                var rect   = new Rectangle(0, 0, file.Width, file.Height);
                int length = file.Width * file.Height;

                width  = file.Width;
                height = file.Height;
                bytes  = new byte[length * 4];

                using (var bitmap = file.Clone(rect, PixelFormat.Format32bppArgb))
                {
                    BitmapData data = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat);

                    try
                    {
                        Marshal.Copy(data.Scan0, bytes, 0, length * 4);
                    }
                    finally
                    {
                        bitmap.UnlockBits(data);
                    }
                }
            }

            D3D11SubResourceData[] textureSubResData = new[]
            {
                new D3D11SubResourceData(bytes, (uint)width * 4)
            };

            var textureDesc = new D3D11Texture2DDesc(DxgiFormat.B8G8R8A8UNorm, (uint)width, (uint)height, 1, 1);

            using (var texture = device.CreateTexture2D(textureDesc, textureSubResData))
            {
                var textureViewDesc = new D3D11ShaderResourceViewDesc
                {
                    Format        = textureDesc.Format,
                    ViewDimension = D3D11SrvDimension.Texture2D,
                    Texture2D     = new D3D11Texture2DSrv
                    {
                        MipLevels       = textureDesc.MipLevels,
                        MostDetailedMip = 0
                    }
                };

                textureView = device.CreateShaderResourceView(texture, textureViewDesc);
            }
        }
Пример #19
0
        public void Update(D3D11Device device)
        {
            if (_top != null)
            {
                _top.Update(device);
            }

            if (_bottom != null)
            {
                _bottom.Update(device);
            }
        }
Пример #20
0
        public void Draw(D3D11Device device, int left, int top)
        {
            if (_top != null)
            {
                _top.Draw(device, left, top);
            }

            if (_bottom != null)
            {
                _bottom.Draw(device, left, top + _rect.Height / 2);
            }
        }
Пример #21
0
 /// <summary>
 /// 終了処理
 /// </summary>
 static public void Dispose()
 {
     if (isFinishQuery_ != null)
     {
         isFinishQuery_.Dispose();
     }
     if (timerDisjointQuery_ != null)
     {
         timerDisjointQuery_.Dispose();
     }
     if (timerQuery_ != null)
     {
         timerQuery_.Dispose();
     }
     foreach (var s in rasterizerState_)
     {
         s.Dispose();
     }
     foreach (var s in blendState_)
     {
         s.Dispose();
     }
     foreach (var s in depthStencilState_)
     {
         s.Dispose();
     }
     foreach (var ary in samplerState_)
     {
         foreach (var s in ary)
         {
             s.Dispose();
         }
     }
     if (defaultColorBuffer_ != null)
     {
         defaultColorBuffer_.Dispose();
     }
     else
     {
         defaultRenderTarget_.Dispose();
     }
     defaultDepthStencil_.Dispose();
     if (swapChain_ != null)
     {
         swapChain_.Dispose();
     }
     if (proxyImage_ != null)
     {
         proxyImage_.Dispose();
     }
     D3D11Device.Dispose();
 }
Пример #22
0
        public void Draw(D3D11Device device)
        {
            // material constants
            Material.Setup(device);

            if (Mesh.SetIndices(device))
            {
                Mesh.DrawIndexed(device, 0, Mesh.IndexCount);
            }
            else
            {
                Mesh.Draw(device, 0, Mesh.VertexCount);
            }
        }
Пример #23
0
        public void Dispose()
        {
            if (m_scene != null)
            {
                m_scene.Dispose();
                m_scene = null;
            }

            if (m_device != null)
            {
                m_device.Dispose();
                m_device = null;
            }
        }
Пример #24
0
        public void Draw(D3D11Device device, int left, int top)
        {
            device.SetViewport(new Viewport(left, top, _rect.Width, _rect.Height));

            var camera = _camera.View * _camera.Projection;

            _scene.Draw(device, camera);

            if (_scene.Selected != null)
            {
                var s = Matrix.Scaling(1.0f);
                _manipulator.Draw(device, _scene.Selected.WorldMatrix * camera);
                _cursor.Draw(device, Matrix.Translation(_cursorPosition) * camera);
            }
        }
Пример #25
0
 public void Draw(D3D11Device device, int left, int top)
 {
     if (_renderTarget != null)
     {
         device.SetViewport(new Viewport(left, top, Width, Height));
         if (_mesh == null)
         {
             var shader   = ShaderLoader.Instance.CreateShader(ShaderType.Screen);
             var material = new D3D11Material("rect", shader);
             var mesh     = D3D11MeshFactory.CreateQuadrangle();
             _mesh = new Mesh(new Submesh(material, mesh));
             _mesh.Submeshes[0].Material.CreateSRV(_renderTarget);
         }
         _mesh.Draw(device);
     }
 }
        internal SdkMeshVertexBuffer(D3D11Device device, SdkMeshRawFile rawFile, SdkMeshRawMesh rawMesh, int i)
        {
            int index = rawMesh.VertexBuffers[i];
            SdkMeshRawVertexBufferHeader header = rawFile.VertexBufferHeaders[index];

            byte[] bytes = rawFile.VertexBufferBytes[index];

            this.NumVertices = (int)header.NumVertices;
            this.SizeBytes   = (uint)header.SizeBytes;
            this.StrideBytes = (uint)header.StrideBytes;
            this.Decl        = header.Decl.ToArray();

            var desc = new D3D11BufferDesc((uint)header.SizeBytes, D3D11BindOptions.VertexBuffer);
            var data = new D3D11SubResourceData(bytes, 0, 0);

            this.Buffer = device.CreateBuffer(desc, data);
        }
        public static void CreateTexture(
            DdsFile dds,
            D3D11Device device,
            D3D11DeviceContext context,
            int maxSize,
            D3D11Usage usage,
            D3D11BindOptions bindOptions,
            D3D11CpuAccessOptions cpuAccessOptions,
            D3D11ResourceMiscOptions miscOptions,
            bool forceSRGB,
            out D3D11Resource texture,
            out D3D11ShaderResourceView textureView,
            out DdsAlphaMode alphaMode)
        {
            if (dds == null)
            {
                throw new ArgumentNullException(nameof(dds));
            }

            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            CreateTextureFromDDS(
                device,
                context,
                dds,
                dds.Data,
                maxSize,
                usage,
                bindOptions,
                cpuAccessOptions,
                miscOptions,
                forceSRGB,
                out texture,
                out textureView);

            alphaMode = dds.AlphaMode;
        }
Пример #28
0
        /// <summary>
        /// 描画しながらついでにWorldMatrixを更新する
        /// </summary>
        /// <param name="renderer"></param>
        /// <param name="camera"></param>
        /// <param name="accumulated"></param>
        public void Draw(D3D11Device device, Matrix camera)
        {
            // world constants
            var mvp = WorldMatrix * camera;

            mvp.Transpose();
            _constants.SetVSConstants(device, 0, mvp);

            if (Mesh != null)
            {
                Mesh.Draw(device);
            }

            foreach (var child in Children)
            {
                child.Draw(device, camera);
            }
        }
Пример #29
0
        static D3D11Buffer CreateAndCopyToDebugBuf(D3D11Device pDevice, D3D11DeviceContext pd3dImmediateContext, D3D11Buffer pBuffer)
        {
            var desc = pBuffer.Description;

            desc.CpuAccessOptions = D3D11CpuAccessOptions.Read;
            desc.Usage            = D3D11Usage.Staging;
            desc.BindOptions      = D3D11BindOptions.None;
            desc.MiscOptions      = D3D11ResourceMiscOptions.None;

            D3D11Buffer debugbuf = pDevice.CreateBuffer(desc);

#if DEBUG
            debugbuf.SetDebugName("Debug");
#endif

            pd3dImmediateContext.CopyResource(debugbuf, pBuffer);

            return(debugbuf);
        }
Пример #30
0
        public void Draw(D3D11Device device)
        {
            if (Submeshes.Count == 0)
            {
                return;
            }

            if (Submeshes[0].DrawIndexCount > 0)
            {
                // shared indices
                var first = Submeshes[0];

                if (_skin != null)
                {
                    first.Mesh.Skinning(_skin.Matrices);
                }
                if (first.Mesh.SetVertices(device, first.Material.Shader))
                {
                    if (first.Mesh.SetIndices(device))
                    {
                        foreach (var submesh in Submeshes)
                        {
                            submesh.DrawSubmesh(device);
                        }
                    }
                }
            }
            else
            {
                foreach (var submesh in Submeshes)
                {
                    if (_skin != null)
                    {
                        submesh.Mesh.Skinning(_skin.Matrices);
                    }
                    if (submesh.Mesh.SetVertices(device, submesh.Material.Shader))
                    {
                        submesh.Draw(device);
                    }
                }
            }
        }