Esempio n. 1
0
        public static RenderTarget CreateRenderTarget(DrawSystem.D3DData d3d, string name, int width, int height)
        {
            var backBuffer = new Texture2D(d3d.Device, new Texture2DDescription()
            {
                Format            = DrawSystem.GetRenderTargetFormat(),
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = width,
                Height            = height,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.None
            });

            var depthBuffer = _CreateDepthBuffer(d3d, width, height);

            var res = new RenderTarget(name);

            res.ShaderResourceView = new ShaderResourceView(d3d.Device, backBuffer);
            res.TargetTexture      = backBuffer;
            res.TargetView         = new RenderTargetView(d3d.Device, backBuffer);
            res.DepthStencilView   = _CreateDepthStencilView(d3d, depthBuffer);

            res._AddDisposable(backBuffer);
            res._AddDisposable(depthBuffer);
            res._AddDisposable(res.ShaderResourceView);
            res._AddDisposable(res.TargetView);
            res._AddDisposable(res.DepthStencilView);

            return(res);
        }
Esempio n. 2
0
        public static TextureView FromFile(string uid, DrawSystem.D3DData d3d, string filePath)
        {
            var result = new TextureView(uid);

            var texRes = Texture2D.FromFile <Texture2D>(d3d.Device, filePath);

            result.View = new ShaderResourceView(d3d.Device, texRes);

            var desc = new SamplerStateDescription()
            {
                Filter             = Filter.MinMagMipLinear,
                AddressU           = TextureAddressMode.Wrap,
                AddressV           = TextureAddressMode.Wrap,
                AddressW           = TextureAddressMode.Wrap,
                BorderColor        = Color.Black,
                ComparisonFunction = Comparison.Never,
                MaximumAnisotropy  = 16,
                MipLodBias         = 0,
                MinimumLod         = 0,
                MaximumLod         = 16,
            };

            result.SamplerState = new SamplerState(d3d.Device, desc);

            result._AddDisposable(texRes);
            result._AddDisposable(result.View);
            result._AddDisposable(result.SamplerState);

            return(result);
        }
Esempio n. 3
0
        public DrawPassCtrl(DrawSystem.D3DData d3d, DrawResourceRepository repository, HmdDevice hmd, bool bStereoRendering, int multiThreadCount)
        {
            m_d3d              = d3d;
            m_repository       = repository;
            m_bStereoRendering = bStereoRendering;
            m_hmd              = hmd;
            m_factory          = new DrawContext.Factory(d3d, repository);

            if (bStereoRendering)
            {
                m_stereoContext = new StereoDrawContext(d3d, repository, hmd, m_factory.CreateDeferredDrawContext());
            }
            else
            {
                m_monoralContext = new MonoralDrawContext(d3d, repository, m_factory.CreateImmediateDrawContext());
            }

            // Init settings
            m_d3d.Device.QueryInterface <Device1>().MaximumFrameLatency = 1;

            m_subThreadCtxList = new List <_SubThreadContextData>();
            for (int index = 0; index < multiThreadCount; ++index)
            {
                var drawContext = m_factory.CreateDeferredDrawContext();
                m_subThreadCtxList.Add(new _SubThreadContextData()
                {
                    DrawContext = drawContext
                });
            }
        }
Esempio n. 4
0
        public static Buffer CreateConstantBuffer <Type>(DrawSystem.D3DData d3d, int length) where Type : struct
        {
            int size = Utilities.SizeOf <Type>();

            Debug.Assert(size % 16 == 0, "size of constant buffer must be aligned to 16 byte");
            return(CreateConstantBuffer(d3d, size * length));
        }
Esempio n. 5
0
        public static RenderTarget[] FromSwapTextureSet(DrawSystem.D3DData d3d, String name, HmdSwapTextureSet swapTextureSet)
        {
            int width  = swapTextureSet.Resolution.Width;
            int height = swapTextureSet.Resolution.Height;

            var depthBuffer      = _CreateDepthBuffer(d3d, width, height);
            var depthStencilView = _CreateDepthStencilView(d3d, depthBuffer);

            var resultList = new List <RenderTarget>();

            int texCount = swapTextureSet.Textures.Count();

            for (int texIndex = 0; texIndex < texCount; ++texIndex)
            {
                var texture = swapTextureSet.Textures[texIndex];

                var res = new RenderTarget(name + texIndex);
                res.ShaderResourceView = new ShaderResourceView(d3d.Device, texture);
                res.TargetTexture      = texture;
                res.TargetView         = swapTextureSet.RenderTargetView[texIndex];
                res.DepthStencilView   = depthStencilView;

                res._AddDisposable(res.ShaderResourceView);
                if (texIndex == 0)
                {
                    // depth buffer and view are shared by each render targets
                    res._AddDisposable(depthBuffer);
                    res._AddDisposable(res.DepthStencilView);
                }

                resultList.Add(res);
            }

            return(resultList.ToArray());
        }
Esempio n. 6
0
            private static BlendState _CreateBlendState(DrawSystem.D3DData d3d, DrawSystem.RenderMode mode)
            {
                switch (mode)
                {
                case DrawSystem.RenderMode.Opaque:
                {
                    var blendDesc = BlendStateDescription.Default();
                    return(new BlendState(d3d.Device, blendDesc));
                }

                case DrawSystem.RenderMode.Transparency:
                {
                    var blendDesc = new BlendStateDescription()
                    {
                        AlphaToCoverageEnable  = false,
                        IndependentBlendEnable = false,
                    };
                    blendDesc.RenderTarget[0] = new RenderTargetBlendDescription(true, BlendOption.SourceAlpha, BlendOption.InverseSourceAlpha, BlendOperation.Add, BlendOption.One, BlendOption.Zero, BlendOperation.Add, ColorWriteMaskFlags.All);
                    return(new BlendState(d3d.Device, blendDesc));
                }

                default:
                    return(null);
                }
            }
Esempio n. 7
0
        /// <summary>
        /// create a mesh data from vertex array
        /// </summary>
        /// <typeparam name="Type">type of vertex</typeparam>
        /// <param name="d3d">d3d data</param>
        /// <param name="topology">primitive topology</param>
        /// <param name="vertices">vertex array1</param>
        /// <returns>mesh data</returns>
        public static DrawSystem.MeshData CreateMeshData <Type1>(DrawSystem.D3DData d3d, PrimitiveTopology topology, Type1[] vertices1)
            where Type1 : struct
        {
            var data = DrawSystem.MeshData.Create(1);

            data.VertexCount = vertices1.Length;
            data.Buffers[0]  = new VertexBufferBinding(CreateVertexBuffer <Type1>(d3d, vertices1), Utilities.SizeOf <Type1>(), 0);
            data.Topology    = topology;
            return(data);
        }
Esempio n. 8
0
        public DrawResourceRepository(DrawSystem.D3DData d3d)
        {
            m_renderTargetMap = new ResourceMap <RenderTarget>();
            m_shaderMap       = new ResourceMap <Effect>();
            //m_modelMap = new ResourceMap<DrawModel>();
            m_texMap = new ResourceMap <TextureView>();

            // get a default render target
            var renderTarget = RenderTarget.CreateDefaultRenderTarget(d3d);

            AddResource(renderTarget);
        }
Esempio n. 9
0
        public static DrawSystem.MeshData CreateMeshData <Type1, Type2>(DrawSystem.D3DData d3d, PrimitiveTopology topology, Type1[] vertices1, Type2[] vertices2)
            where Type1 : struct
            where Type2 : struct
        {
            Debug.Assert(vertices1.Length == vertices2.Length, "the lengths of vertex buffer are unmatched");

            var data = DrawSystem.MeshData.Create(2);

            data.VertexCount = vertices1.Length;
            data.Buffers[0]  = new VertexBufferBinding(CreateVertexBuffer <Type1>(d3d, vertices1), Utilities.SizeOf <Type1>(), 0);
            data.Buffers[1]  = new VertexBufferBinding(CreateVertexBuffer <Type2>(d3d, vertices2), Utilities.SizeOf <Type2>(), 0);
            data.Topology    = topology;
            return(data);
        }
Esempio n. 10
0
        public static Buffer CreateVertexBuffer <Type>(DrawSystem.D3DData d3d, Type[] vertices) where Type : struct
        {
            var desc = new BufferDescription
            {
                BindFlags           = BindFlags.VertexBuffer,
                CpuAccessFlags      = CpuAccessFlags.None,
                OptionFlags         = ResourceOptionFlags.None,
                SizeInBytes         = Utilities.SizeOf <Type>() * vertices.Length,
                StructureByteStride = Utilities.SizeOf <Type>(),
                Usage = ResourceUsage.Default,
            };

            return(Buffer.Create(d3d.Device, vertices, desc));
        }
Esempio n. 11
0
        public static DepthStencilView _CreateDepthStencilView(DrawSystem.D3DData d3d, Texture2D depthBuffer)
        {
            var dsvDesc = new DepthStencilViewDescription
            {
                Dimension = DepthStencilViewDimension.Texture2D,
                Flags     = DepthStencilViewFlags.None,
                Format    = depthBuffer.Description.Format,
                Texture2D = new DepthStencilViewDescription.Texture2DResource()
                {
                    MipSlice = 0
                }
            };

            return(new DepthStencilView(d3d.Device, depthBuffer, dsvDesc));
        }
Esempio n. 12
0
        public Effect(String uid, DrawSystem.D3DData d3d, InputElement[] inputElements, String vertexShader, String pixelShader)
            : this(uid)
        {
            var include        = new _IncludeImpl("Shader");
            var vertexByteCode = ShaderBytecode.CompileFromFile(vertexShader, "main", "vs_4_0", ShaderFlags.Debug, EffectFlags.None, null, include);
            var pixelByteCode  = ShaderBytecode.CompileFromFile(pixelShader, "main", "ps_4_0", ShaderFlags.Debug, EffectFlags.None, null, include);

            VertexShader = new VertexShader(d3d.Device, vertexByteCode);
            PixelShader  = new PixelShader(d3d.Device, pixelByteCode);
            Layout       = new InputLayout(d3d.Device, ShaderSignature.GetInputSignature(vertexByteCode), inputElements);

            _AddDisposable(VertexShader);
            _AddDisposable(PixelShader);
            _AddDisposable(Layout);
        }
Esempio n. 13
0
 public static Texture2D _CreateDepthBuffer(DrawSystem.D3DData d3d, int width, int height)
 {
     return(new Texture2D(d3d.Device, new Texture2DDescription()
     {
         Format = Format.D32_Float,
         ArraySize = 1,
         MipLevels = 1,
         Width = width,
         Height = height,
         SampleDescription = new SampleDescription(1, 0),
         Usage = ResourceUsage.Default,
         BindFlags = BindFlags.DepthStencil,
         CpuAccessFlags = CpuAccessFlags.None,
         OptionFlags = ResourceOptionFlags.None
     }));
 }
Esempio n. 14
0
        /// <summary>
        /// create a default render target as ResourceBase
        /// </summary>
        /// <param name="d3d">Direct3D data</param>
        /// <returns>render target</returns>
        public static RenderTarget CreateDefaultRenderTarget(DrawSystem.D3DData d3d)
        {
            var res = new RenderTarget("Default");

            var backBuffer  = Texture2D.FromSwapChain <Texture2D>(d3d.SwapChain, 0);
            var depthBuffer = _CreateDepthBuffer(d3d, backBuffer.Description.Width, backBuffer.Description.Height);

            res.TargetView         = new RenderTargetView(d3d.Device, backBuffer);
            res.DepthStencilView   = _CreateDepthStencilView(d3d, depthBuffer);
            res.ShaderResourceView = null;
            res.TargetTexture      = backBuffer;
            res._AddDisposable(res.TargetView);
            res._AddDisposable(depthBuffer);
            res._AddDisposable(res.DepthStencilView);

            return(res);
        }
Esempio n. 15
0
        /// <summary>
        /// set up device
        /// </summary>
        /// <param name="d3d">d3d data</param>
        /// <param name="renderTarget">render target of back buffer</param>
        public void Setup(DrawSystem.D3DData d3d, RenderTarget renderTarget)
        {
            uint hmdCaps =
                (uint)LibOVR.ovrHmdCaps.LowPersistence
                | (uint)LibOVR.ovrHmdCaps.DynamicPrediction;

            LibOVR.ovrHmd_SetEnabledCaps(m_handle.Ptr, hmdCaps);

            uint trackingCaps = (uint)LibOVR.ovrTrackingCaps.Orientation | (uint)LibOVR.ovrTrackingCaps.MagYawCorrection
                                | (uint)LibOVR.ovrTrackingCaps.Position;

            if (LibOVR.ovrHmd_ConfigureTracking(m_handle.Ptr, trackingCaps, 0) != 0)
            {
                MessageBox.Show("Failed to ConfigureTracking()");
                Application.Exit();
            }

            m_eyeDescArray    = new LibOVR.ovrEyeRenderDesc[2];
            m_eyeDescArray[0] = LibOVR.ovrHmd_GetRenderDesc(m_handle.Ptr, LibOVR.ovrEyeType.Left, m_handle.Value.DefaultEyeFov[0]);
            m_eyeDescArray[1] = LibOVR.ovrHmd_GetRenderDesc(m_handle.Ptr, LibOVR.ovrEyeType.Right, m_handle.Value.DefaultEyeFov[1]);
        }
Esempio n. 16
0
        public StereoDrawContext(DrawSystem.D3DData d3d, DrawResourceRepository repository, HmdDevice hmd, DrawContext context)
        {
            m_d3d        = d3d;
            m_repository = repository;
            m_context    = context;
            m_hmd        = hmd;

            // Create render targets for each HMD eye
            m_textureSets = new HmdSwapTextureSet[2];
            var eyeResolution = hmd.EyeResolution;
            var resNames      = new string[] { "OVRLeftEye", "OVRRightEye" };

            for (int index = 0; index < 2; ++index)
            {
                var textureSet = m_hmd.CreateSwapTextureSet(d3d.Device, eyeResolution.Width, eyeResolution.Height);
                m_textureSets[index] = textureSet;

                var renderTargetList = RenderTarget.FromSwapTextureSet(m_d3d, resNames[index], textureSet);
                foreach (var renderTarget in renderTargetList)
                {
                    m_repository.AddResource(renderTarget);
                }
            }

            // Create temporaly render target
            var tmpRt = RenderTarget.CreateRenderTarget(d3d, "Temp", eyeResolution.Width, eyeResolution.Height);

            m_repository.AddResource(tmpRt);

            // Create texture for Mirroring
            Size defaultRenderTargetSize = m_repository.GetDefaultRenderTarget().Resolution;

            m_mirrorTexture = m_hmd.CreateMirrorTexture(m_d3d.Device, defaultRenderTargetSize.Width, defaultRenderTargetSize.Height);

            m_commandListTable = new List <CommandList>();
        }
 public MonoralDrawContext(DrawSystem.D3DData d3d, DrawResourceRepository repository, DrawContext context)
 {
     m_d3d        = d3d;
     m_repository = repository;
     m_context    = context;
 }
Esempio n. 18
0
 public static Buffer CreateConstantBuffer(DrawSystem.D3DData d3d, int size)
 {
     return(new Buffer(d3d.Device, size, ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0));
 }
Esempio n. 19
0
            public Factory(DrawSystem.D3DData d3d, DrawResourceRepository repository)
            {
                m_initParam.D3D        = d3d;
                m_initParam.Repository = repository;

                m_initParam.WorldVtxConst = DrawUtil.CreateConstantBuffer <_VertexShaderConst_World>(d3d, 1);
                m_initParam.WorldPixConst = DrawUtil.CreateConstantBuffer <_PixelShaderConst_World>(d3d, 1);

                m_initParam.RasterizerState = new RasterizerState(d3d.Device, new RasterizerStateDescription()
                {
                    CullMode = CullMode.Back,
                    FillMode = FillMode.Solid,
                    IsAntialiasedLineEnabled = false,                           // we do not use wireframe
                    IsDepthClipEnabled       = true,
                    IsMultisampleEnabled     = false,
                });

                var renderModes = new[] { DrawSystem.RenderMode.Opaque, DrawSystem.RenderMode.Transparency };

                m_initParam.BlendStates = new BlendState[renderModes.Length];
                foreach (DrawSystem.RenderMode mode in renderModes)
                {
                    int index = (int)mode;
                    m_initParam.BlendStates[index] = _CreateBlendState(d3d, mode);
                }
                m_initParam.DepthStencilStates = new DepthStencilState[renderModes.Length];
                foreach (DrawSystem.RenderMode mode in renderModes)
                {
                    int index = (int)mode;
                    m_initParam.DepthStencilStates[index] = _CreateDepthStencilState(d3d, mode);
                }

                {
                    var shader = new Effect(
                        "Std",
                        d3d,
                        new InputElement[]
                    {
                        new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                        new InputElement("NORMAL", 0, Format.R32G32B32_Float, 16, 0),
                        new InputElement("TEXCOORD", 0, Format.R32G32_Float, 28, 0),
                        new InputElement("TANGENT", 0, Format.R32G32B32_Float, 0, 1),
                        new InputElement("BONEINDEX", 0, Format.R32G32B32A32_UInt, 0, 2),
                        new InputElement("BONEWEIGHT", 0, Format.R32G32B32A32_Float, 16, 2),
                    },
                        "Shader/VS_Std.fx",
                        "Shader/PS_Std.fx");

                    repository.AddResource(shader);
                }

                {
                    var shader = new Effect(
                        "Minimap",
                        d3d,
                        new InputElement[]
                    {
                        new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                        new InputElement("NORMAL", 0, Format.R32G32B32_Float, 16, 0),
                        new InputElement("TEXCOORD", 0, Format.R32G32_Float, 28, 0),
                        new InputElement("TANGENT", 0, Format.R32G32B32_Float, 0, 1),
                        new InputElement("BONEINDEX", 0, Format.R32G32B32A32_UInt, 0, 2),
                        new InputElement("BONEWEIGHT", 0, Format.R32G32B32A32_Float, 16, 2),
                    },
                        "Shader/VS_Std.fx",
                        "Shader/PS_Minimap.fx");

                    repository.AddResource(shader);
                }

                {
                    var shader = new Effect(
                        "Debug",
                        d3d,
                        new InputElement[]
                    {
                        new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                        new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0),
                    },
                        "Shader/VS_Debug.fx",
                        "Shader/PS_Debug.fx");

                    repository.AddResource(shader);
                }
            }
Esempio n. 20
0
            private static DepthStencilState _CreateDepthStencilState(DrawSystem.D3DData d3d, DrawSystem.RenderMode mode)
            {
                switch (mode)
                {
                case DrawSystem.RenderMode.Opaque:
                {
                    return(new DepthStencilState(d3d.Device, new DepthStencilStateDescription()
                        {
                            BackFace = new DepthStencilOperationDescription()
                            {
                                // Stencil operations if pixel is back-facing
                                DepthFailOperation = StencilOperation.Decrement,
                                FailOperation = StencilOperation.Keep,
                                PassOperation = StencilOperation.Keep,
                                Comparison = SharpDX.Direct3D11.Comparison.Always,
                            },
                            FrontFace = new DepthStencilOperationDescription()
                            {
                                // Stencil operations if pixel is front-facing
                                DepthFailOperation = StencilOperation.Increment,
                                FailOperation = StencilOperation.Keep,
                                PassOperation = StencilOperation.Keep,
                                Comparison = SharpDX.Direct3D11.Comparison.Always,
                            },
                            IsDepthEnabled = true,
                            IsStencilEnabled = false,
                            StencilReadMask = 0xff,
                            StencilWriteMask = 0xff,
                            DepthComparison = Comparison.Less,
                            DepthWriteMask = DepthWriteMask.All,
                        }));
                }

                case DrawSystem.RenderMode.Transparency:
                {
                    return(new DepthStencilState(d3d.Device, new DepthStencilStateDescription()
                        {
                            BackFace = new DepthStencilOperationDescription()
                            {
                                // Stencil operations if pixel is back-facing
                                DepthFailOperation = StencilOperation.Decrement,
                                FailOperation = StencilOperation.Keep,
                                PassOperation = StencilOperation.Keep,
                                Comparison = SharpDX.Direct3D11.Comparison.Always,
                            },
                            FrontFace = new DepthStencilOperationDescription()
                            {
                                // Stencil operations if pixel is front-facing
                                DepthFailOperation = StencilOperation.Increment,
                                FailOperation = StencilOperation.Keep,
                                PassOperation = StencilOperation.Keep,
                                Comparison = SharpDX.Direct3D11.Comparison.Always,
                            },
                            IsDepthEnabled = false,                                     // disable depth
                            IsStencilEnabled = false,
                            StencilReadMask = 0xff,
                            StencilWriteMask = 0xff,
                            DepthComparison = Comparison.Less,
                            DepthWriteMask = DepthWriteMask.All,
                        }));
                }

                default:
                    return(null);
                }
            }