public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            var d3dDevice = this.deviceResources.D3DDevice;

            // Create the shaders
            byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.cso");
            this.g_pVertexShader       = d3dDevice.CreateVertexShader(vertexShaderBytecode, null);
            this.g_pHullShaderInteger  = d3dDevice.CreateHullShader(File.ReadAllBytes("HullShaderInteger.cso"), null);
            this.g_pHullShaderFracEven = d3dDevice.CreateHullShader(File.ReadAllBytes("HullShaderFracEven.cso"), null);
            this.g_pHullShaderFracOdd  = d3dDevice.CreateHullShader(File.ReadAllBytes("HullShaderFracOdd.cso"), null);
            this.g_pDomainShader       = d3dDevice.CreateDomainShader(File.ReadAllBytes("DomainShader.cso"), null);
            this.g_pPixelShader        = d3dDevice.CreatePixelShader(File.ReadAllBytes("PixelShader.cso"), null);
            this.g_pSolidColorPS       = d3dDevice.CreatePixelShader(File.ReadAllBytes("SolidColorPS.cso"), null);

            // Create our vertex input layout - this matches the BEZIER_CONTROL_POINT structure
            D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.g_pPatchLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode);

            // Create constant buffers
            this.g_pcbPerFrame = d3dDevice.CreateBuffer(new D3D11BufferDesc(ConstantBufferConstants.Size, D3D11BindOptions.ConstantBuffer));

            // Create solid and wireframe rasterizer state objects
            D3D11RasterizerDesc rasterDesc = D3D11RasterizerDesc.Default;

            rasterDesc.CullMode           = D3D11CullMode.None;
            rasterDesc.IsDepthClipEnabled = true;

            rasterDesc.FillMode          = D3D11FillMode.Solid;
            this.g_pRasterizerStateSolid = d3dDevice.CreateRasterizerState(rasterDesc);

            rasterDesc.FillMode = D3D11FillMode.WireFrame;
            this.g_pRasterizerStateWireframe = d3dDevice.CreateRasterizerState(rasterDesc);

            D3D11BufferDesc vbdesc = D3D11BufferDesc.From(MobiusStrip.Points, D3D11BindOptions.VertexBuffer);

            this.g_pControlPointVB = d3dDevice.CreateBuffer(vbdesc, MobiusStrip.Points, 0, 0);

            XMFloat3 vecEye = new XMFloat3(1.0f, 1.5f, -3.5f);
            XMFloat3 vecAt  = new XMFloat3(0.0f, 0.0f, 0.0f);
            XMFloat3 vecUp  = new XMFloat3(0.0f, 1.0f, 0.0f);

            this.ViewMatrix = XMMatrix.LookAtLH(vecEye, vecAt, vecUp);
            this.EyePt      = vecEye;
        }
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.cso");
            this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null);

            D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "COLOR",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32A32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 12,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode);

            byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso");
            this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            var vertexBufferDesc = D3D11BufferDesc.From(MainGameComponent.Vertices, D3D11BindOptions.VertexBuffer);

            this.vertexBuffer = this.deviceResources.D3DDevice.CreateBuffer(vertexBufferDesc, MainGameComponent.Vertices, 0, 0);

            var indexBufferDesc = D3D11BufferDesc.From(MainGameComponent.Indices, D3D11BindOptions.IndexBuffer);

            this.indexBuffer = this.deviceResources.D3DDevice.CreateBuffer(indexBufferDesc, MainGameComponent.Indices, 0, 0);

            var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer);

            this.constantBuffer = this.deviceResources.D3DDevice.CreateBuffer(constantBufferDesc);

            this.worldMatrix1 = XMMatrix.Identity;
            this.worldMatrix2 = XMMatrix.Identity;

            XMVector eye = new XMVector(0.0f, 1.0f, -5.0f, 0.0f);
            XMVector at  = new XMVector(0.0f, 1.0f, 0.0f, 0.0f);
            XMVector up  = new XMVector(0.0f, 1.0f, 0.0f, 0.0f);

            this.viewMatrix = XMMatrix.LookAtLH(eye, at, up);
        }
Exemple #3
0
        private void RenderMesh(int meshIndex, int diffuseSlot, int normalSlot, int specularSlot)
        {
            if (meshIndex < 0 || meshIndex >= this.Meshes.Count)
            {
                return;
            }

            SdkMeshMesh mesh = this.Meshes[meshIndex];

            if (mesh.VertexBuffers.Length > D3D11Constants.InputAssemblerVertexInputResourceSlotCount)
            {
                return;
            }

            D3D11Buffer[] vb      = new D3D11Buffer[mesh.VertexBuffers.Length];
            uint[]        strides = new uint[mesh.VertexBuffers.Length];
            uint[]        offsets = new uint[mesh.VertexBuffers.Length];

            for (int i = 0; i < mesh.VertexBuffers.Length; i++)
            {
                vb[i]      = mesh.VertexBuffers[i].Buffer;
                strides[i] = mesh.VertexBuffers[i].StrideBytes;
                offsets[i] = 0;
            }

            D3D11Buffer ib       = mesh.IndexBuffer.Buffer;
            DxgiFormat  ibFormat = mesh.IndexBuffer.IndexFormat;

            this._d3dDeviceContext.InputAssemblerSetVertexBuffers(0, vb, strides, offsets);
            this._d3dDeviceContext.InputAssemblerSetIndexBuffer(ib, ibFormat, 0);


            foreach (SdkMeshSubset subset in mesh.Subsets)
            {
                this._d3dDeviceContext.InputAssemblerSetPrimitiveTopology(subset.PrimitiveTopology);

                SdkMeshMaterial material = this.Materials[subset.MaterialIndex];

                if (diffuseSlot != -1 && material.DiffuseTextureView != null)
                {
                    this._d3dDeviceContext.PixelShaderSetShaderResources((uint)diffuseSlot, new[] { material.DiffuseTextureView });
                }

                if (normalSlot != -1 && material.NormalTextureView != null)
                {
                    this._d3dDeviceContext.PixelShaderSetShaderResources((uint)normalSlot, new[] { material.NormalTextureView });
                }

                if (specularSlot != -1 && material.SpecularTextureView != null)
                {
                    this._d3dDeviceContext.PixelShaderSetShaderResources((uint)specularSlot, new[] { material.SpecularTextureView });
                }

                this._d3dDeviceContext.DrawIndexed((uint)subset.IndexCount, (uint)subset.IndexStart, subset.VertexStart);
            }
        }
        protected override void CreateDeviceDependentResources()
        {
            base.CreateDeviceDependentResources();

            byte[] vertexShaderBytecode = File.ReadAllBytes(Lesson3Game.ShadersDirectory + "Cubes.VertexShader.cso");
            this.vertexShader = this.DeviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null);

            D3D11InputElementDesc[] basicVertexLayoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "COLOR",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 12,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.inputLayout = this.DeviceResources.D3DDevice.CreateInputLayout(basicVertexLayoutDesc, vertexShaderBytecode);

            byte[] pixelShaderBytecode = File.ReadAllBytes(Lesson3Game.ShadersDirectory + "Cubes.PixelShader.cso");
            this.pixelShader = this.DeviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            var vertexBufferDesc = D3D11BufferDesc.From(cubeVertices, D3D11BindOptions.VertexBuffer);

            this.vertexBuffer = this.DeviceResources.D3DDevice.CreateBuffer(vertexBufferDesc, cubeVertices, 0, 0);

            var indexBufferDesc = D3D11BufferDesc.From(cubeIndices, D3D11BindOptions.IndexBuffer);

            this.indexBuffer = this.DeviceResources.D3DDevice.CreateBuffer(indexBufferDesc, cubeIndices, 0, 0);

            var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer);

            this.constantBuffer = this.DeviceResources.D3DDevice.CreateBuffer(constantBufferDesc);

            this.constantBufferData.View = new Float4X4(
                -1.00000000f, 0.00000000f, 0.00000000f, 0.00000000f,
                0.00000000f, 0.89442718f, 0.44721359f, 0.00000000f,
                0.00000000f, 0.44721359f, -0.89442718f, -2.23606800f,
                0.00000000f, 0.00000000f, 0.00000000f, 1.00000000f
                );
        }
Exemple #5
0
        private void CreateParticleBuffer()
        {
            var d3dDevice = this.deviceResources.D3DDevice;

            ParticleVertex[] pVertices = new ParticleVertex[MaxParticles];

            for (int i = 0; i < pVertices.Length; i++)
            {
                pVertices[i].Color = new XMFloat4(1, 1, 0.2f, 1);
            }

            D3D11BufferDesc vbdesc = D3D11BufferDesc.From(pVertices, D3D11BindOptions.VertexBuffer);

            this.g_pParticleBuffer = d3dDevice.CreateBuffer(vbdesc, pVertices, 0, 0);
        }
        public void CreateDeviceDependentResources(DeviceResources resources, Mesh3D mesh)
        {
            var vertices         = mesh.Vertices.ToArray();
            var vertexBufferDesc = D3D11BufferDesc.From(vertices, D3D11BindOptions.VertexBuffer);

            this.vertexBuffer = resources.D3DDevice.CreateBuffer(vertexBufferDesc, vertices, 0, 0);

            var indices         = mesh.Indices.ToArray();
            var indexBufferDesc = D3D11BufferDesc.From(indices, D3D11BindOptions.IndexBuffer);

            this.indexBuffer  = resources.D3DDevice.CreateBuffer(indexBufferDesc, indices, 0, 0);
            this.IndicesCount = (uint)indices.Length;

            this.Texture  = mesh.Texture;
            this.HasAlpha = mesh.HasAlpha;
        }
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.cso");
            this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null);

            D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode);

            byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso");
            this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            var vertexBufferDesc = new D3D11BufferDesc(MaxVertices * 12, D3D11BindOptions.VertexBuffer);

            this.vertexBuffer = this.deviceResources.D3DDevice.CreateBuffer(vertexBufferDesc);

            var indexBufferDesc = new D3D11BufferDesc(MaxIndices * 2, D3D11BindOptions.IndexBuffer);

            this.indexBuffer = this.deviceResources.D3DDevice.CreateBuffer(indexBufferDesc);

            var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer);

            this.constantBuffer = this.deviceResources.D3DDevice.CreateBuffer(constantBufferDesc);

            this.worldMatrix = XMMatrix.Identity;

            XMVector vecAt  = this.cameraOrigins[(int)this.CollisionGroup];
            XMVector vecEye = new(vecAt.X, vecAt.Y + 20.0f, (this.CollisionGroup == CollisionGroup.Frustum) ? (vecAt.Z + 20.0f) : (vecAt.Z - 20.0f), 0.0f);
            XMVector vecUp  = new(0.0f, 1.0f, 0.0f, 0.0f);

            this.viewMatrix = XMMatrix.LookAtLH(vecEye, vecAt, vecUp);
        }
Exemple #8
0
        private void RunComputeShader(
            D3D11ComputeShader pComputeShader,
            D3D11ShaderResourceView[] pShaderResourceViews,
            D3D11Buffer pNeverChangesCBCS,
            D3D11Buffer pCBCS,
            //T pCSData,
            //uint dwNumDataBytes,
            D3D11UnorderedAccessView pUnorderedAccessView,
            uint x,
            uint y,
            uint z)
        //where T : struct
        {
            var d3dContext = this.deviceResources.D3DContext;

            d3dContext.ComputeShaderSetShader(pComputeShader, null);
            d3dContext.ComputeShaderSetShaderResources(0, pShaderResourceViews);
            d3dContext.ComputeShaderSetUnorderedAccessViews(0, new[] { pUnorderedAccessView }, new[] { 0U });

            //if (pCBCS != null)
            //{
            //    d3dContext.UpdateSubresource(pCBCS, D3D11Utils.CalcSubresource(0, 0, 1), null, pCSData, dwNumDataBytes, dwNumDataBytes);
            //}

            if (pNeverChangesCBCS != null && pCBCS != null)
            {
                d3dContext.ComputeShaderSetConstantBuffers(0, new[] { pNeverChangesCBCS, pCBCS });
            }

            if (pNeverChangesCBCS != null && pCBCS == null)
            {
                d3dContext.ComputeShaderSetConstantBuffers(0, new[] { pNeverChangesCBCS });
            }

            if (pNeverChangesCBCS == null && pCBCS != null)
            {
                d3dContext.ComputeShaderSetConstantBuffers(0, new[] { pCBCS });
            }

            d3dContext.Dispatch(x, y, z);

            d3dContext.ComputeShaderSetUnorderedAccessViews(0, new D3D11UnorderedAccessView[] { null }, new[] { 0U });
            d3dContext.ComputeShaderSetShaderResources(0, new D3D11ShaderResourceView[] { null, null, null });
            d3dContext.ComputeShaderSetConstantBuffers(0, new D3D11Buffer[] { });
        }
Exemple #9
0
        private T[] CreateAndCopyToDebugBuf <T>(D3D11Buffer buffer) where T : struct
        {
            var d3dDevice  = this.deviceResources.D3DDevice;
            var d3dContext = this.deviceResources.D3DContext;

            var desc = buffer.Description;

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

            //int sizeT = Marshal.SizeOf<T>();
            int sizeT      = Unsafe.SizeOf <T>();
            int dataLength = (int)desc.ByteWidth / sizeT;
            var data       = new T[dataLength];

            using (var debugbuf = d3dDevice.CreateBuffer(desc))
            {
                d3dContext.CopyResource(debugbuf, buffer);

                D3D11MappedSubResource mappedResource = d3dContext.Map(debugbuf, 0, D3D11MapCpuPermission.Read, D3D11MapOptions.None);

                try
                {
                    //for (int i = 0; i < dataLength; i++)
                    //{
                    //    data[i] = Marshal.PtrToStructure<T>(mappedResource.Data + i * sizeT);
                    //}

                    var b = new byte[desc.ByteWidth];
                    Marshal.Copy(mappedResource.Data, b, 0, b.Length);

                    IntPtr d = Marshal.UnsafeAddrOfPinnedArrayElement(data, 0);
                    Marshal.Copy(b, 0, d, b.Length);
                }
                finally
                {
                    d3dContext.Unmap(debugbuf, 0);
                }
            }

            return(data);
        }
Exemple #10
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);
        }
Exemple #11
0
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            var d3dDevice = resources.D3DDevice;

            this.m_pScanCS  = d3dDevice.CreateComputeShader(File.ReadAllBytes("ScanInBucketComputeShader.cso"), null);
            this.m_pScan2CS = d3dDevice.CreateComputeShader(File.ReadAllBytes("ScanBucketResultComputeShader.cso"), null);
            this.m_pScan3CS = d3dDevice.CreateComputeShader(File.ReadAllBytes("ScanAddBucketResultComputeShader.cso"), null);

            this.m_pAuxBuf = d3dDevice.CreateBuffer(new D3D11BufferDesc
            {
                BindOptions         = D3D11BindOptions.ShaderResource | D3D11BindOptions.UnorderedAccess,
                StructureByteStride = 4 * 2, // If scan types other than uint2, remember change here
                ByteWidth           = 4 * 2 * 128,
                MiscOptions         = D3D11ResourceMiscOptions.BufferStructured,
                Usage = D3D11Usage.Default
            });

            this.m_pAuxBufRV = d3dDevice.CreateShaderResourceView(this.m_pAuxBuf, new D3D11ShaderResourceViewDesc
            {
                ViewDimension = D3D11SrvDimension.Buffer,
                Format        = DxgiFormat.Unknown,
                Buffer        = new D3D11BufferSrv
                {
                    FirstElement = 0,
                    NumElements  = 128
                }
            });

            this.m_pAuxBufUAV = d3dDevice.CreateUnorderedAccessView(this.m_pAuxBuf, new D3D11UnorderedAccessViewDesc
            {
                ViewDimension = D3D11UavDimension.Buffer,
                Format        = DxgiFormat.Unknown,
                Buffer        = new D3D11BufferUav
                {
                    FirstElement = 0,
                    NumElements  = 128
                }
            });
        }
Exemple #12
0
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            this.fragmentCountPS = this.deviceResources.D3DDevice.CreatePixelShader(
                File.ReadAllBytes("FragmentCountPS.cso"),
                null);

            this.createPrefixSumPass0CS = this.deviceResources.D3DDevice.CreateComputeShader(
                File.ReadAllBytes("CreatePrefixSumPass0CS.cso"),
                null);

            this.createPrefixSumPass1CS = this.deviceResources.D3DDevice.CreateComputeShader(
                File.ReadAllBytes("CreatePrefixSumPass1CS.cso"),
                null);

            this.fillDeepBufferPS = this.deviceResources.D3DDevice.CreatePixelShader(
                File.ReadAllBytes("FillDeepBufferPS.cso"),
                null);

            this.sortAndRenderCS = this.deviceResources.D3DDevice.CreateComputeShader(
                File.ReadAllBytes("SortAndRenderCS.cso"),
                null);

            var depthStencilDesc = new D3D11DepthStencilDesc
            {
                IsDepthEnabled   = false,
                IsStencilEnabled = false
            };

            this.depthStencilState = this.deviceResources.D3DDevice.CreateDepthStencilState(depthStencilDesc);

            this.computeShaderConstantBuffer = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(OitComputeShaderConstantBufferData.Size, D3D11BindOptions.ConstantBuffer));

            this.pixelShaderConstantBuffer = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(OitPixelShaderConstantBufferData.Size, D3D11BindOptions.ConstantBuffer));
        }
Exemple #13
0
        static D3D11ShaderResourceView CreateBufferSRV(D3D11Device pDevice, D3D11Buffer pBuffer)
        {
            var descBuf = pBuffer.Description;

            D3D11ShaderResourceViewDesc desc;

            if (descBuf.MiscOptions.HasFlag(D3D11ResourceMiscOptions.BufferAllowRawViews))
            {
                // This is a Raw Buffer
                desc = new D3D11ShaderResourceViewDesc(pBuffer, DxgiFormat.R32Typeless, 0, descBuf.ByteWidth / 4, D3D11BufferExSrvOptions.Raw);
            }
            else if (descBuf.MiscOptions.HasFlag(D3D11ResourceMiscOptions.BufferStructured))
            {
                // This is a Structured Buffer
                desc = new D3D11ShaderResourceViewDesc(pBuffer, DxgiFormat.Unknown, 0, descBuf.ByteWidth / descBuf.StructureByteStride, D3D11BufferExSrvOptions.None);
            }
            else
            {
                throw new InvalidOperationException();
            }

            return(pDevice.CreateShaderResourceView(pBuffer, desc));
        }
Exemple #14
0
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            var loader = new BasicLoader(this.deviceResources.D3DDevice);

            loader.LoadShader("Components.VertexShader.cso", null, out this.vertexShader, out this.inputLayout);

            var shapes = new BasicShapes(this.deviceResources.D3DDevice);

            shapes.CreateReferenceAxis(out this.vertexBuffer, out this.indexBuffer, out this.vertexCount, out this.indexCount);

            var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer);

            this.constantBuffer = this.deviceResources.D3DDevice.CreateBuffer(constantBufferDesc);

            loader.LoadShader("Components.PixelShader.cso", out this.pixelShader);

            loader.LoadTexture("texturedata.bin", 256, 256, out this.texture, out this.textureView);

            D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc(
                D3D11Filter.Anisotropic,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                0.0f,
                this.deviceResources.D3DFeatureLevel > D3D11FeatureLevel.FeatureLevel91 ? D3D11Constants.DefaultMaxAnisotropy : D3D11Constants.FeatureLevel91DefaultMaxAnisotropy,
                D3D11ComparisonFunction.Never,
                new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
                0.0f,
                float.MaxValue);

            this.sampler = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc);

            this.camera = new BasicCamera();
        }
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            XMFloat3 vCenter = new XMFloat3(0.25767413f, -28.503521f, 111.00689f);
            XMMatrix m       = XMMatrix.Translation(-vCenter.X, -vCenter.Y, -vCenter.Z);

            m *= XMMatrix.RotationY(XMMath.PI);
            m *= XMMatrix.RotationX(XMMath.PIDivTwo);
            this.centerMesh = m;

            // Load the mesh
            this.mesh = SdkMeshFile.FromFile(
                this.deviceResources.D3DDevice,
                this.deviceResources.D3DContext,
                "Tiny\\Tiny.sdkmesh");

            // Create the shaders
            byte[] vertexShaderBytecode = File.ReadAllBytes("BasicHLSL11_VS.cso");
            this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null);

            D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "NORMAL",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 12,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "TEXCOORD",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 24,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode);

            byte[] pixelShaderBytecode = File.ReadAllBytes("BasicHLSL11_PS.cso");
            this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            // Create a sampler state
            D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc(
                D3D11Filter.MinMagMipLinear,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                0.0f,
                1,
                D3D11ComparisonFunction.Always,
                new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
                0.0f,
                float.MaxValue);

            this.sampler = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc);

            // Setup constant buffers
            this.constantBufferVSPerObject = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(ConstantBufferVSPerObject.Size, D3D11BindOptions.ConstantBuffer));

            this.constantBufferPSPerObject = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(ConstantBufferPSPerObject.Size, D3D11BindOptions.ConstantBuffer));

            this.constantBufferPSPerFrame = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(ConstantBufferPSPerFrame.Size, D3D11BindOptions.ConstantBuffer));
        }
Exemple #16
0
        public void CreateCube(out D3D11Buffer vertexBuffer, out D3D11Buffer indexBuffer, out int vertexCount, out int indexCount)
        {
            BasicVertex[] cubeVertices = new BasicVertex[]
            {
                new BasicVertex(new Float3(-0.5f, 0.5f, -0.5f), new Float3(0.0f, 1.0f, 0.0f), new Float2(0.0f, 0.0f)),   // +Y (top face)
                new BasicVertex(new Float3(0.5f, 0.5f, -0.5f), new Float3(0.0f, 1.0f, 0.0f), new Float2(1.0f, 0.0f)),
                new BasicVertex(new Float3(0.5f, 0.5f, 0.5f), new Float3(0.0f, 1.0f, 0.0f), new Float2(1.0f, 1.0f)),
                new BasicVertex(new Float3(-0.5f, 0.5f, 0.5f), new Float3(0.0f, 1.0f, 0.0f), new Float2(0.0f, 1.0f)),

                new BasicVertex(new Float3(-0.5f, -0.5f, 0.5f), new Float3(0.0f, -1.0f, 0.0f), new Float2(0.0f, 0.0f)),    // -Y (bottom face)
                new BasicVertex(new Float3(0.5f, -0.5f, 0.5f), new Float3(0.0f, -1.0f, 0.0f), new Float2(1.0f, 0.0f)),
                new BasicVertex(new Float3(0.5f, -0.5f, -0.5f), new Float3(0.0f, -1.0f, 0.0f), new Float2(1.0f, 1.0f)),
                new BasicVertex(new Float3(-0.5f, -0.5f, -0.5f), new Float3(0.0f, -1.0f, 0.0f), new Float2(0.0f, 1.0f)),

                new BasicVertex(new Float3(0.5f, 0.5f, 0.5f), new Float3(1.0f, 0.0f, 0.0f), new Float2(0.0f, 0.0f)),     // +X (right face)
                new BasicVertex(new Float3(0.5f, 0.5f, -0.5f), new Float3(1.0f, 0.0f, 0.0f), new Float2(1.0f, 0.0f)),
                new BasicVertex(new Float3(0.5f, -0.5f, -0.5f), new Float3(1.0f, 0.0f, 0.0f), new Float2(1.0f, 1.0f)),
                new BasicVertex(new Float3(0.5f, -0.5f, 0.5f), new Float3(1.0f, 0.0f, 0.0f), new Float2(0.0f, 1.0f)),

                new BasicVertex(new Float3(-0.5f, 0.5f, -0.5f), new Float3(-1.0f, 0.0f, 0.0f), new Float2(0.0f, 0.0f)),    // -X (left face)
                new BasicVertex(new Float3(-0.5f, 0.5f, 0.5f), new Float3(-1.0f, 0.0f, 0.0f), new Float2(1.0f, 0.0f)),
                new BasicVertex(new Float3(-0.5f, -0.5f, 0.5f), new Float3(-1.0f, 0.0f, 0.0f), new Float2(1.0f, 1.0f)),
                new BasicVertex(new Float3(-0.5f, -0.5f, -0.5f), new Float3(-1.0f, 0.0f, 0.0f), new Float2(0.0f, 1.0f)),

                new BasicVertex(new Float3(-0.5f, 0.5f, 0.5f), new Float3(0.0f, 0.0f, 1.0f), new Float2(0.0f, 0.0f)),    // +Z (front face)
                new BasicVertex(new Float3(0.5f, 0.5f, 0.5f), new Float3(0.0f, 0.0f, 1.0f), new Float2(1.0f, 0.0f)),
                new BasicVertex(new Float3(0.5f, -0.5f, 0.5f), new Float3(0.0f, 0.0f, 1.0f), new Float2(1.0f, 1.0f)),
                new BasicVertex(new Float3(-0.5f, -0.5f, 0.5f), new Float3(0.0f, 0.0f, 1.0f), new Float2(0.0f, 1.0f)),

                new BasicVertex(new Float3(0.5f, 0.5f, -0.5f), new Float3(0.0f, 0.0f, -1.0f), new Float2(0.0f, 0.0f)),     // -Z (back face)
                new BasicVertex(new Float3(-0.5f, 0.5f, -0.5f), new Float3(0.0f, 0.0f, -1.0f), new Float2(1.0f, 0.0f)),
                new BasicVertex(new Float3(-0.5f, -0.5f, -0.5f), new Float3(0.0f, 0.0f, -1.0f), new Float2(1.0f, 1.0f)),
                new BasicVertex(new Float3(0.5f, -0.5f, -0.5f), new Float3(0.0f, 0.0f, -1.0f), new Float2(0.0f, 1.0f)),
            };

            ushort[] cubeIndices = new ushort[]
            {
                0, 1, 2,
                0, 2, 3,

                4, 5, 6,
                4, 6, 7,

                8, 9, 10,
                8, 10, 11,

                12, 13, 14,
                12, 14, 15,

                16, 17, 18,
                16, 18, 19,

                20, 21, 22,
                20, 22, 23
            };

            this.CreateVertexBuffer(cubeVertices, out vertexBuffer);

            try
            {
                this.CreateIndexBuffer(cubeIndices, out indexBuffer);
            }
            catch
            {
                D3D11Utils.DisposeAndNull(ref vertexBuffer);
                throw;
            }

            vertexCount = cubeVertices.Length;
            indexCount  = cubeIndices.Length;
        }
Exemple #17
0
        public void CreateWindowSizeDependentResources()
        {
            uint width  = this.deviceResources.BackBufferWidth;
            uint height = this.deviceResources.BackBufferHeight;

            // Create fragment count buffer
            this.fragmentCountBuffer = this.deviceResources.D3DDevice.CreateTexture2D(
                new D3D11Texture2DDesc(
                    DxgiFormat.R32UInt,
                    width,
                    height,
                    1,
                    1,
                    D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource));

            // Create prefix sum buffer
            this.prefixSum = this.deviceResources.D3DDevice.CreateBuffer(new D3D11BufferDesc(
                                                                             width * height * 4,
                                                                             D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource,
                                                                             D3D11Usage.Default,
                                                                             D3D11CpuAccessOptions.None,
                                                                             D3D11ResourceMiscOptions.None,
                                                                             4));

            // Create the deep frame buffer.
            // This simple allocation scheme for the deep frame buffer allocates space for 8 times the size of the
            // frame buffer, which means that it can hold an average of 8 fragments per pixel.  This will usually waste some
            // space, and in some cases of high overdraw the buffer could run into problems with overflow.  It
            // may be useful to make the buffer size more intelligent to avoid these problems.
            this.deepBuffer = this.deviceResources.D3DDevice.CreateBuffer(new D3D11BufferDesc(
                                                                              width * height * 8 * 4,
                                                                              D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource,
                                                                              D3D11Usage.Default,
                                                                              D3D11CpuAccessOptions.None,
                                                                              D3D11ResourceMiscOptions.None,
                                                                              4));

            // Create deep frame buffer for color
            this.deepBufferColor = this.deviceResources.D3DDevice.CreateBuffer(new D3D11BufferDesc(
                                                                                   width * height * 8 * 4 * 1,
                                                                                   D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource,
                                                                                   D3D11Usage.Default,
                                                                                   D3D11CpuAccessOptions.None,
                                                                                   D3D11ResourceMiscOptions.None,
                                                                                   4 * 1));

            this.fragmentCountRV = this.deviceResources.D3DDevice.CreateShaderResourceView(
                this.fragmentCountBuffer,
                new D3D11ShaderResourceViewDesc(
                    D3D11SrvDimension.Texture2D,
                    DxgiFormat.R32UInt,
                    0,
                    1));

            this.fragmentCountUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView(
                this.fragmentCountBuffer,
                new D3D11UnorderedAccessViewDesc(
                    D3D11UavDimension.Texture2D,
                    DxgiFormat.R32UInt,
                    0));

            this.prefixSumUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView(
                this.prefixSum,
                new D3D11UnorderedAccessViewDesc(
                    D3D11UavDimension.Buffer,
                    DxgiFormat.R32UInt,
                    0,
                    width * height));

            this.deepBufferUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView(
                this.deepBuffer,
                new D3D11UnorderedAccessViewDesc(
                    D3D11UavDimension.Buffer,
                    DxgiFormat.R32Float,
                    0,
                    width * height * 8));

            this.deepBufferColorUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView(
                this.deepBufferColor,
                new D3D11UnorderedAccessViewDesc(
                    D3D11UavDimension.Buffer,
                    DxgiFormat.B8G8R8A8UNorm,
                    0,
                    width * height * 8));

            this.screenTexture = this.deviceResources.D3DDevice.CreateTexture2D(
                new D3D11Texture2DDesc(
                    DxgiFormat.B8G8R8A8UNorm,
                    width,
                    height,
                    1,
                    1,
                    D3D11BindOptions.RenderTarget | D3D11BindOptions.UnorderedAccess));

            this.screenTextureRTV = this.deviceResources.D3DDevice.CreateRenderTargetView(this.screenTexture, null);
            this.screenTextureUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView(this.screenTexture, new D3D11UnorderedAccessViewDesc(D3D11UavDimension.Texture2D, DxgiFormat.B8G8R8A8UNorm, 0));
        }
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            byte[] vertexShaderBytecode = File.ReadAllBytes("SceneVS.cso");
            this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null);

            D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32A32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "COLOR",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32A32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0xffffffff,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.vertexLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode);

            byte[] pixelShaderBytecode = File.ReadAllBytes("ScenePS.cso");
            this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            // Set up vertex buffer
            float fRight = -10.0f;
            float fTop   = -10.0f;
            float fLeft  = 10.0f;
            float fLowH  = -5.0f;

            // Fill the vertex buffer
            var vertices = new SceneVertex[12];

            vertices[0].Pos = new XMFloat4(fLeft, fLowH, 50.0f, 1.0f);
            vertices[1].Pos = new XMFloat4(fLeft, fTop, 50.0f, 1.0f);
            vertices[2].Pos = new XMFloat4(fRight, fLowH, 50.0f, 1.0f);
            vertices[3].Pos = new XMFloat4(fRight, fTop, 50.0f, 1.0f);

            vertices[0].Color = new XMFloat4(1.0f, 0.0f, 0.0f, 0.5f);
            vertices[1].Color = new XMFloat4(1.0f, 0.0f, 0.0f, 0.5f);
            vertices[2].Color = new XMFloat4(1.0f, 0.0f, 0.0f, 0.5f);
            vertices[3].Color = new XMFloat4(1.0f, 0.0f, 0.0f, 0.5f);

            vertices[4].Pos = new XMFloat4(fLeft, fLowH, 60.0f, 1.0f);
            vertices[5].Pos = new XMFloat4(fLeft, fTop, 60.0f, 1.0f);
            vertices[6].Pos = new XMFloat4(fRight, fLowH, 40.0f, 1.0f);
            vertices[7].Pos = new XMFloat4(fRight, fTop, 40.0f, 1.0f);

            vertices[4].Color = new XMFloat4(0.0f, 1.0f, 0.0f, 0.5f);
            vertices[5].Color = new XMFloat4(0.0f, 1.0f, 0.0f, 0.5f);
            vertices[6].Color = new XMFloat4(0.0f, 1.0f, 0.0f, 0.5f);
            vertices[7].Color = new XMFloat4(0.0f, 1.0f, 0.0f, 0.5f);

            vertices[8].Pos  = new XMFloat4(fLeft, fLowH, 40.0f, 1.0f);
            vertices[9].Pos  = new XMFloat4(fLeft, fTop, 40.0f, 1.0f);
            vertices[10].Pos = new XMFloat4(fRight, fLowH, 60.0f, 1.0f);
            vertices[11].Pos = new XMFloat4(fRight, fTop, 60.0f, 1.0f);

            vertices[8].Color  = new XMFloat4(0.0f, 0.0f, 1.0f, 0.5f);
            vertices[9].Color  = new XMFloat4(0.0f, 0.0f, 1.0f, 0.5f);
            vertices[10].Color = new XMFloat4(0.0f, 0.0f, 1.0f, 0.5f);
            vertices[11].Color = new XMFloat4(0.0f, 0.0f, 1.0f, 0.5f);

            var vertexBufferDesc = D3D11BufferDesc.From(vertices, D3D11BindOptions.VertexBuffer, D3D11Usage.Immutable);

            this.vertexBuffer = this.deviceResources.D3DDevice.CreateBuffer(vertexBufferDesc, vertices, 0, 0);

            var constantBufferDesc = new D3D11BufferDesc(SceneVertexShaderConstantBufferData.Size, D3D11BindOptions.ConstantBuffer);

            this.vertexShaderConstantBuffer = this.deviceResources.D3DDevice.CreateBuffer(constantBufferDesc);
        }
Exemple #19
0
        static void CreateResources()
        {
            var d3dDevice = deviceResources.D3DDevice;

            // Create the Bitonic Sort Compute Shader
            g_pComputeShaderBitonic = d3dDevice.CreateComputeShader(File.ReadAllBytes("CSSortBitonic.cso"), null);

            // Create the Matrix Transpose Compute Shader
            g_pComputeShaderTranspose = d3dDevice.CreateComputeShader(File.ReadAllBytes("CSSortTranspose.cso"), null);

            // Create the Const Buffer
            var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer);

            g_pCB = d3dDevice.CreateBuffer(constantBufferDesc);

            // Create the Buffer of Elements
            // Create 2 buffers for switching between when performing the transpose
            var bufferDesc = new D3D11BufferDesc(
                NUM_ELEMENTS * sizeof(uint),
                D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource,
                D3D11Usage.Default,
                D3D11CpuAccessOptions.None,
                D3D11ResourceMiscOptions.BufferStructured,
                sizeof(uint));

            g_pBuffer1 = d3dDevice.CreateBuffer(bufferDesc);
            g_pBuffer2 = d3dDevice.CreateBuffer(bufferDesc);

            // Create the Shader Resource View for the Buffers
            // This is used for reading the buffer during the transpose
            var srvbufferDesc = new D3D11ShaderResourceViewDesc(D3D11SrvDimension.Buffer, DxgiFormat.Unknown)
            {
                Buffer = new D3D11BufferSrv {
                    ElementWidth = NUM_ELEMENTS
                }
            };

            g_pBuffer1SRV = d3dDevice.CreateShaderResourceView(g_pBuffer1, srvbufferDesc);
            g_pBuffer2SRV = d3dDevice.CreateShaderResourceView(g_pBuffer2, srvbufferDesc);

            // Create the Unordered Access View for the Buffers
            // This is used for writing the buffer during the sort and transpose
            var uavbufferDesc = new D3D11UnorderedAccessViewDesc(D3D11UavDimension.Buffer, DxgiFormat.Unknown)
            {
                Buffer = new D3D11BufferUav {
                    NumElements = NUM_ELEMENTS
                }
            };

            g_pBuffer1UAV = d3dDevice.CreateUnorderedAccessView(g_pBuffer1, uavbufferDesc);
            g_pBuffer2UAV = d3dDevice.CreateUnorderedAccessView(g_pBuffer2, uavbufferDesc);

            // Create the Readback Buffer
            // This is used to read the results back to the CPU
            var readbackBufferDesc = new D3D11BufferDesc(
                NUM_ELEMENTS * sizeof(uint),
                D3D11BindOptions.None,
                D3D11Usage.Staging,
                D3D11CpuAccessOptions.Read,
                D3D11ResourceMiscOptions.None,
                sizeof(uint));

            g_pReadBackBuffer = d3dDevice.CreateBuffer(readbackBufferDesc);
        }
Exemple #20
0
        public void PerEdgeTessellation(
            XMMatrix matWVP,
            ref D3D11Buffer ppTessedVerticesBuf,
            ref D3D11Buffer ppTessedIndicesBuf,
            out uint num_tessed_vertices,
            out uint num_tessed_indices)
        {
            var d3dDevice  = this.deviceResources.D3DDevice;
            var d3dContext = this.deviceResources.D3DContext;

            // Update per-edge tessellation factors
            {
                EdgeFactorConstantBuffer cbCS = default;
                cbCS.MatWVP = matWVP;
                cbCS.TessEdgeLengthScale = this.m_tess_edge_len_scale;
                cbCS.NumTriangles        = this.m_nVertices / 3;

                d3dContext.UpdateSubresource(
                    this.s_pEdgeFactorCSCB,
                    D3D11Utils.CalcSubresource(0, 0, 1),
                    null,
                    cbCS,
                    EdgeFactorConstantBuffer.Size,
                    EdgeFactorConstantBuffer.Size);

                this.RunComputeShader(
                    this.s_pEdgeFactorCS,
                    new[] { this.m_pBaseVBSRV },
                    null,
                    this.s_pEdgeFactorCSCB,
                    this.m_pEdgeFactorBufUAV,
                    (uint)Math.Ceiling(this.m_nVertices / 3 / 128.0f),
                    1U,
                    1U);
            }

            // How many vertices/indices are needed for the tessellated mesh?
            {
                uint[] cbCS = new uint[] { this.m_nVertices / 3, 0, 0, 0 };

                d3dContext.UpdateSubresource(
                    this.s_pCSCB,
                    D3D11Utils.CalcSubresource(0, 0, 1),
                    null,
                    cbCS,
                    4 * 4,
                    4 * 4);

                this.RunComputeShader(
                    this.s_pNumVerticesIndicesCSs[(int)this.PartitioningMode],
                    new[] { this.m_pEdgeFactorBufSRV },
                    this.s_pLookupTableCSCB,
                    this.s_pCSCB,
                    this.m_pScanBuf0UAV,
                    (uint)Math.Ceiling(this.m_nVertices / 3 / 128.0f),
                    1U,
                    1U);

                this.s_ScanCS.Scan(d3dContext, this.m_nVertices / 3, this.m_pScanBuf0SRV, this.m_pScanBuf0UAV, this.m_pScanBuf1SRV, this.m_pScanBuf1UAV);

                // read back the number of vertices/indices for tessellation output
                D3D11Box box = default;
                box.Left   = 4 * 2 * this.m_nVertices / 3 - 4 * 2;
                box.Right  = 4 * 2 * this.m_nVertices / 3;
                box.Top    = 0;
                box.Bottom = 1;
                box.Front  = 0;
                box.Back   = 1;

                d3dContext.CopySubresourceRegion(this.s_pCSReadBackBuf, 0, 0, 0, 0, this.m_pScanBuf0, 0, box);

                D3D11MappedSubResource mappedResource = d3dContext.Map(this.s_pCSReadBackBuf, 0, D3D11MapCpuPermission.Read, D3D11MapOptions.None);

                try
                {
                    num_tessed_vertices = (uint)Marshal.ReadInt32(mappedResource.Data + 4 * 0);
                    num_tessed_indices  = (uint)Marshal.ReadInt32(mappedResource.Data + 4 * 1);
                }
                finally
                {
                    d3dContext.Unmap(this.s_pCSReadBackBuf, 0);
                }
            }

            if (num_tessed_vertices == 0 || num_tessed_indices == 0)
            {
                return;
            }

            // Turn on this and set a breakpoint on the line beginning with "p = " and see what has been written to m_pScanBuf0
            if (DebugEnabled)
            {
#pragma warning disable IDE0059 // Assignation inutile d'une valeur
                var p = this.CreateAndCopyToDebugBuf <(uint v, uint t)>(this.m_pScanBuf0);
#pragma warning restore IDE0059 // Assignation inutile d'une valeur
            }

            // Generate buffers for scattering TriID and IndexID for both vertex data and index data,
            // also generate buffers for output tessellated vertex data and index data
            {
                if (this.m_pScatterVertexBuf == null || this.m_nCachedTessedVertices < num_tessed_vertices || this.m_nCachedTessedVertices > num_tessed_vertices * 2)
                {
                    D3D11Utils.DisposeAndNull(ref this.m_pScatterVertexBuf);
                    D3D11Utils.DisposeAndNull(ref this.m_pScatterVertexBufSRV);
                    D3D11Utils.DisposeAndNull(ref this.m_pScatterVertexBufUAV);

                    D3D11Utils.DisposeAndNull(ref ppTessedVerticesBuf);
                    D3D11Utils.DisposeAndNull(ref this.m_pTessedVerticesBufUAV);
                    D3D11Utils.DisposeAndNull(ref this.m_pTessedVerticesBufSRV);

                    this.m_pScatterVertexBuf = d3dDevice.CreateBuffer(new D3D11BufferDesc
                    {
                        BindOptions         = D3D11BindOptions.ShaderResource | D3D11BindOptions.UnorderedAccess,
                        ByteWidth           = 4 * 2 * num_tessed_vertices,
                        StructureByteStride = 4 * 2,
                        MiscOptions         = D3D11ResourceMiscOptions.BufferStructured,
                        Usage = D3D11Usage.Default
                    });

                    this.m_pScatterVertexBufSRV = d3dDevice.CreateShaderResourceView(this.m_pScatterVertexBuf, new D3D11ShaderResourceViewDesc
                    {
                        ViewDimension = D3D11SrvDimension.Buffer,
                        Format        = DxgiFormat.Unknown,
                        Buffer        = new D3D11BufferSrv
                        {
                            FirstElement = 0,
                            NumElements  = num_tessed_vertices
                        }
                    });

                    this.m_pScatterVertexBufUAV = d3dDevice.CreateUnorderedAccessView(this.m_pScatterVertexBuf, new D3D11UnorderedAccessViewDesc
                    {
                        ViewDimension = D3D11UavDimension.Buffer,
                        Format        = DxgiFormat.Unknown,
                        Buffer        = new D3D11BufferUav
                        {
                            FirstElement = 0,
                            NumElements  = num_tessed_vertices
                        }
                    });

                    // generate the output tessellated vertices buffer
                    ppTessedVerticesBuf = d3dDevice.CreateBuffer(new D3D11BufferDesc
                    {
                        BindOptions         = D3D11BindOptions.ShaderResource | D3D11BindOptions.UnorderedAccess,
                        ByteWidth           = 4 * 3 * num_tessed_vertices,
                        StructureByteStride = 4 * 3,
                        MiscOptions         = D3D11ResourceMiscOptions.BufferStructured,
                        Usage = D3D11Usage.Default
                    });

                    this.m_pTessedVerticesBufUAV = d3dDevice.CreateUnorderedAccessView(ppTessedVerticesBuf, new D3D11UnorderedAccessViewDesc
                    {
                        ViewDimension = D3D11UavDimension.Buffer,
                        Format        = DxgiFormat.Unknown,
                        Buffer        = new D3D11BufferUav
                        {
                            FirstElement = 0,
                            NumElements  = num_tessed_vertices
                        }
                    });

                    this.m_pTessedVerticesBufSRV = d3dDevice.CreateShaderResourceView(ppTessedVerticesBuf, new D3D11ShaderResourceViewDesc
                    {
                        ViewDimension = D3D11SrvDimension.Buffer,
                        Format        = DxgiFormat.Unknown,
                        Buffer        = new D3D11BufferSrv
                        {
                            FirstElement = 0,
                            NumElements  = num_tessed_vertices
                        }
                    });

                    this.m_nCachedTessedVertices = num_tessed_vertices;
                }

                if (this.m_pScatterIndexBuf == null || this.m_nCachedTessedIndices < num_tessed_indices)
                {
                    D3D11Utils.DisposeAndNull(ref this.m_pScatterIndexBuf);
                    D3D11Utils.DisposeAndNull(ref this.m_pScatterIndexBufSRV);
                    D3D11Utils.DisposeAndNull(ref this.m_pScatterIndexBufUAV);

                    D3D11Utils.DisposeAndNull(ref ppTessedIndicesBuf);
                    D3D11Utils.DisposeAndNull(ref this.m_pTessedIndicesBufUAV);

                    this.m_pScatterIndexBuf = d3dDevice.CreateBuffer(new D3D11BufferDesc
                    {
                        BindOptions         = D3D11BindOptions.ShaderResource | D3D11BindOptions.UnorderedAccess,
                        ByteWidth           = 4 * 2 * num_tessed_indices,
                        StructureByteStride = 4 * 2,
                        MiscOptions         = D3D11ResourceMiscOptions.BufferStructured,
                        Usage = D3D11Usage.Default
                    });

                    this.m_pScatterIndexBufSRV = d3dDevice.CreateShaderResourceView(this.m_pScatterIndexBuf, new D3D11ShaderResourceViewDesc
                    {
                        ViewDimension = D3D11SrvDimension.Buffer,
                        Format        = DxgiFormat.Unknown,
                        Buffer        = new D3D11BufferSrv
                        {
                            FirstElement = 0,
                            NumElements  = num_tessed_indices
                        }
                    });

                    this.m_pScatterIndexBufUAV = d3dDevice.CreateUnorderedAccessView(this.m_pScatterIndexBuf, new D3D11UnorderedAccessViewDesc
                    {
                        ViewDimension = D3D11UavDimension.Buffer,
                        Format        = DxgiFormat.Unknown,
                        Buffer        = new D3D11BufferUav
                        {
                            FirstElement = 0,
                            NumElements  = num_tessed_indices
                        }
                    });

                    // generate the output tessellated indices buffer
                    ppTessedIndicesBuf = d3dDevice.CreateBuffer(new D3D11BufferDesc
                    {
                        BindOptions = D3D11BindOptions.ShaderResource | D3D11BindOptions.UnorderedAccess | D3D11BindOptions.IndexBuffer,
                        ByteWidth   = 4 * num_tessed_indices,
                        MiscOptions = D3D11ResourceMiscOptions.BufferAllowRawViews,
                        Usage       = D3D11Usage.Default
                    });

                    this.m_pTessedIndicesBufUAV = d3dDevice.CreateUnorderedAccessView(ppTessedIndicesBuf, new D3D11UnorderedAccessViewDesc
                    {
                        ViewDimension = D3D11UavDimension.Buffer,
                        Format        = DxgiFormat.R32Typeless,
                        Buffer        = new D3D11BufferUav
                        {
                            FirstElement = 0,
                            NumElements  = num_tessed_indices,
                            Options      = D3D11BufferUavOptions.Raw
                        }
                    });

                    this.m_nCachedTessedIndices = num_tessed_indices;
                }
            }

            // Scatter TriID, IndexID
            {
                uint[] cbCS = new uint[] { this.m_nVertices / 3, 0, 0, 0 };
                D3D11ShaderResourceView[] aRViews = new[] { this.m_pScanBuf0SRV };

                d3dContext.UpdateSubresource(
                    this.s_pCSCB,
                    D3D11Utils.CalcSubresource(0, 0, 1),
                    null,
                    cbCS,
                    4 * 4,
                    4 * 4);

                // Scatter vertex TriID, IndexID
                this.RunComputeShader(
                    this.s_pScatterVertexTriIDIndexIDCS,
                    aRViews,
                    null,
                    this.s_pCSCB,
                    this.m_pScatterVertexBufUAV,
                    (uint)Math.Ceiling(this.m_nVertices / 3 / 128.0f),
                    1U,
                    1U);

                // Scatter index TriID, IndexID
                this.RunComputeShader(
                    this.s_pScatterIndexTriIDIndexIDCS,
                    aRViews,
                    null,
                    this.s_pCSCB,
                    this.m_pScatterIndexBufUAV,
                    (uint)Math.Ceiling(this.m_nVertices / 3 / 128.0f),
                    1U,
                    1U);
            }

            // Turn on this and set a breakpoint on the line beginning with "p = " and see what has been written to m_pScatterVertexBuf
            if (DebugEnabled)
            {
#pragma warning disable IDE0059 // Assignation inutile d'une valeur
                var p = this.CreateAndCopyToDebugBuf <(uint v, uint t)>(this.m_pScatterVertexBuf);
#pragma warning restore IDE0059 // Assignation inutile d'une valeur
            }

            // Tessellate vertex
            {
                uint[] cbCS = new uint[] { num_tessed_vertices, 0, 0, 0 };

                d3dContext.UpdateSubresource(
                    this.s_pCSCB,
                    D3D11Utils.CalcSubresource(0, 0, 1),
                    null,
                    cbCS,
                    4 * 4,
                    4 * 4);

                this.RunComputeShader(
                    this.s_pTessVerticesCSs[(int)this.PartitioningMode],
                    new[] { this.m_pScatterVertexBufSRV, this.m_pEdgeFactorBufSRV },
                    this.s_pLookupTableCSCB,
                    this.s_pCSCB,
                    this.m_pTessedVerticesBufUAV,
                    (uint)Math.Ceiling(num_tessed_vertices / 128.0f),
                    1U,
                    1U);
            }

            // Turn on this and set a breakpoint on the line beginning with "p = " and see what has been written to *ppTessedVerticesBuf
            if (DebugEnabled)
            {
#pragma warning disable IDE0059 // Assignation inutile d'une valeur
                var p = this.CreateAndCopyToDebugBuf <(uint id, float u, float v)>(ppTessedVerticesBuf);
#pragma warning restore IDE0059 // Assignation inutile d'une valeur
            }

            // Tessellate indices
            {
                uint[] cbCS = new uint[] { num_tessed_indices, 0, 0, 0 };

                d3dContext.UpdateSubresource(
                    this.s_pCSCB,
                    D3D11Utils.CalcSubresource(0, 0, 1),
                    null,
                    cbCS,
                    4 * 4,
                    4 * 4);

                this.RunComputeShader(
                    this.s_pTessIndicesCSs[(int)this.PartitioningMode],
                    new[] { this.m_pScatterIndexBufSRV, this.m_pEdgeFactorBufSRV, this.m_pScanBuf0SRV },
                    this.s_pLookupTableCSCB,
                    this.s_pCSCB,
                    this.m_pTessedIndicesBufUAV,
                    (uint)Math.Ceiling(num_tessed_indices / 128.0f),
                    1U,
                    1U);
            }

            // Turn on this and set a breakpoint on the line beginning with "p = " and see what has been written to *ppTessedIndicesBuf
            if (DebugEnabled)
            {
#pragma warning disable IDE0059 // Assignation inutile d'une valeur
                var p = this.CreateAndCopyToDebugBuf <int>(ppTessedIndicesBuf);
#pragma warning restore IDE0059 // Assignation inutile d'une valeur
            }
        }
 public void Release()
 {
     this.Buffer.Release();
     this.Buffer = null;
 }
Exemple #22
0
        public void CreateReferenceAxis(out D3D11Buffer vertexBuffer, out D3D11Buffer indexBuffer, out int vertexCount, out int indexCount)
        {
            BasicVertex[] axisVertices = new BasicVertex[]
            {
                new BasicVertex(new Float3(0.500f, 0.000f, 0.000f), new Float3(0.125f, 0.500f, 0.500f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.125f, 0.000f), new Float3(0.125f, 0.500f, 0.500f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.125f), new Float3(0.125f, 0.500f, 0.500f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.500f, 0.000f, 0.000f), new Float3(0.125f, -0.500f, 0.500f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.125f), new Float3(0.125f, -0.500f, 0.500f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.000f, -0.125f, 0.000f), new Float3(0.125f, -0.500f, 0.500f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.500f, 0.000f, 0.000f), new Float3(0.125f, -0.500f, -0.500f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.000f, -0.125f, 0.000f), new Float3(0.125f, -0.500f, -0.500f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.125f), new Float3(0.125f, -0.500f, -0.500f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.500f, 0.000f, 0.000f), new Float3(0.125f, 0.500f, -0.500f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.125f), new Float3(0.125f, 0.500f, -0.500f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.125f, 0.000f), new Float3(0.125f, 0.500f, -0.500f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.125f, 0.000f), new Float3(-0.125f, 0.000f, 0.000f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.125f), new Float3(-0.125f, 0.000f, 0.000f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.000f, -0.125f, 0.000f), new Float3(-0.125f, 0.000f, 0.000f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.125f), new Float3(-0.125f, 0.000f, 0.000f), new Float2(0.250f, 0.250f)),
                new BasicVertex(new Float3(-0.500f, 0.000f, 0.000f), new Float3(-0.125f, 0.500f, 0.500f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.125f), new Float3(-0.125f, 0.500f, 0.500f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.125f, 0.000f), new Float3(-0.125f, 0.500f, 0.500f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(-0.500f, 0.000f, 0.000f), new Float3(-0.125f, 0.500f, -0.500f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.125f, 0.000f), new Float3(-0.125f, 0.500f, -0.500f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.125f), new Float3(-0.125f, 0.500f, -0.500f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(-0.500f, 0.000f, 0.000f), new Float3(-0.125f, -0.500f, -0.500f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.125f), new Float3(-0.125f, -0.500f, -0.500f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(0.000f, -0.125f, 0.000f), new Float3(-0.125f, -0.500f, -0.500f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(-0.500f, 0.000f, 0.000f), new Float3(-0.125f, -0.500f, 0.500f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(0.000f, -0.125f, 0.000f), new Float3(-0.125f, -0.500f, 0.500f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.125f), new Float3(-0.125f, -0.500f, 0.500f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.125f), new Float3(0.125f, 0.000f, 0.000f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(0.000f, -0.125f, 0.000f), new Float3(0.125f, 0.000f, 0.000f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.125f), new Float3(0.125f, 0.000f, 0.000f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.125f, 0.000f), new Float3(0.125f, 0.000f, 0.000f), new Float2(0.250f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.500f, 0.000f), new Float3(0.500f, 0.125f, 0.500f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.125f), new Float3(0.500f, 0.125f, 0.500f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(0.125f, 0.000f, 0.000f), new Float3(0.500f, 0.125f, 0.500f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.500f, 0.000f), new Float3(0.500f, 0.125f, -0.500f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(0.125f, 0.000f, 0.000f), new Float3(0.500f, 0.125f, -0.500f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.125f), new Float3(0.500f, 0.125f, -0.500f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.500f, 0.000f), new Float3(-0.500f, 0.125f, -0.500f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.125f), new Float3(-0.500f, 0.125f, -0.500f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(-0.125f, 0.000f, 0.000f), new Float3(-0.500f, 0.125f, -0.500f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.500f, 0.000f), new Float3(-0.500f, 0.125f, 0.500f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(-0.125f, 0.000f, 0.000f), new Float3(-0.500f, 0.125f, 0.500f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.125f), new Float3(-0.500f, 0.125f, 0.500f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(0.125f, 0.000f, 0.000f), new Float3(0.000f, -0.125f, 0.000f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.125f), new Float3(0.000f, -0.125f, 0.000f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(-0.125f, 0.000f, 0.000f), new Float3(0.000f, -0.125f, 0.000f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.125f), new Float3(0.000f, -0.125f, 0.000f), new Float2(0.500f, 0.250f)),
                new BasicVertex(new Float3(0.000f, -0.500f, 0.000f), new Float3(0.500f, -0.125f, 0.500f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(0.125f, 0.000f, 0.000f), new Float3(0.500f, -0.125f, 0.500f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.125f), new Float3(0.500f, -0.125f, 0.500f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(0.000f, -0.500f, 0.000f), new Float3(-0.500f, -0.125f, 0.500f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.125f), new Float3(-0.500f, -0.125f, 0.500f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(-0.125f, 0.000f, 0.000f), new Float3(-0.500f, -0.125f, 0.500f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(0.000f, -0.500f, 0.000f), new Float3(-0.500f, -0.125f, -0.500f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(-0.125f, 0.000f, 0.000f), new Float3(-0.500f, -0.125f, -0.500f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.125f), new Float3(-0.500f, -0.125f, -0.500f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(0.000f, -0.500f, 0.000f), new Float3(0.500f, -0.125f, -0.500f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.125f), new Float3(0.500f, -0.125f, -0.500f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(0.125f, 0.000f, 0.000f), new Float3(0.500f, -0.125f, -0.500f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.125f), new Float3(0.000f, 0.125f, 0.000f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(-0.125f, 0.000f, 0.000f), new Float3(0.000f, 0.125f, 0.000f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.125f), new Float3(0.000f, 0.125f, 0.000f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(0.125f, 0.000f, 0.000f), new Float3(0.000f, 0.125f, 0.000f), new Float2(0.500f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.500f), new Float3(0.500f, 0.500f, 0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(0.125f, 0.000f, 0.000f), new Float3(0.500f, 0.500f, 0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.125f, 0.000f), new Float3(0.500f, 0.500f, 0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.500f), new Float3(-0.500f, 0.500f, 0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.125f, 0.000f), new Float3(-0.500f, 0.500f, 0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(-0.125f, 0.000f, 0.000f), new Float3(-0.500f, 0.500f, 0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.500f), new Float3(-0.500f, -0.500f, 0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(-0.125f, 0.000f, 0.000f), new Float3(-0.500f, -0.500f, 0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(0.000f, -0.125f, 0.000f), new Float3(-0.500f, -0.500f, 0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, 0.500f), new Float3(0.500f, -0.500f, 0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(0.000f, -0.125f, 0.000f), new Float3(0.500f, -0.500f, 0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(0.125f, 0.000f, 0.000f), new Float3(0.500f, -0.500f, 0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(0.125f, 0.000f, 0.000f), new Float3(0.000f, 0.000f, -0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(0.000f, -0.125f, 0.000f), new Float3(0.000f, 0.000f, -0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(-0.125f, 0.000f, 0.000f), new Float3(0.000f, 0.000f, -0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.125f, 0.000f), new Float3(0.000f, 0.000f, -0.125f), new Float2(0.750f, 0.250f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.500f), new Float3(0.500f, 0.500f, -0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.125f, 0.000f), new Float3(0.500f, 0.500f, -0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(0.125f, 0.000f, 0.000f), new Float3(0.500f, 0.500f, -0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.500f), new Float3(0.500f, -0.500f, -0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(0.125f, 0.000f, 0.000f), new Float3(0.500f, -0.500f, -0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(0.000f, -0.125f, 0.000f), new Float3(0.500f, -0.500f, -0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.500f), new Float3(-0.500f, -0.500f, -0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(0.000f, -0.125f, 0.000f), new Float3(-0.500f, -0.500f, -0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(-0.125f, 0.000f, 0.000f), new Float3(-0.500f, -0.500f, -0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.000f, -0.500f), new Float3(-0.500f, 0.500f, -0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(-0.125f, 0.000f, 0.000f), new Float3(-0.500f, 0.500f, -0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.125f, 0.000f), new Float3(-0.500f, 0.500f, -0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(0.000f, 0.125f, 0.000f), new Float3(0.000f, 0.000f, 0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(-0.125f, 0.000f, 0.000f), new Float3(0.000f, 0.000f, 0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(0.000f, -0.125f, 0.000f), new Float3(0.000f, 0.000f, 0.125f), new Float2(0.750f, 0.500f)),
                new BasicVertex(new Float3(0.125f, 0.000f, 0.000f), new Float3(0.000f, 0.000f, 0.125f), new Float2(0.750f, 0.500f)),
            };

            ushort[] axisIndices = new ushort[]
            {
                0, 2, 1,
                3, 5, 4,
                6, 8, 7,
                9, 11, 10,
                12, 14, 13,
                12, 15, 14,
                16, 18, 17,
                19, 21, 20,
                22, 24, 23,
                25, 27, 26,
                28, 30, 29,
                28, 31, 30,
                32, 34, 33,
                35, 37, 36,
                38, 40, 39,
                41, 43, 42,
                44, 46, 45,
                44, 47, 46,
                48, 50, 49,
                51, 53, 52,
                54, 56, 55,
                57, 59, 58,
                60, 62, 61,
                60, 63, 62,
                64, 66, 65,
                67, 69, 68,
                70, 72, 71,
                73, 75, 74,
                76, 78, 77,
                76, 79, 78,
                80, 82, 81,
                83, 85, 84,
                86, 88, 87,
                89, 91, 90,
                92, 94, 93,
                92, 95, 94,
            };

            this.CreateVertexBuffer(axisVertices, out vertexBuffer);

            try
            {
                this.CreateIndexBuffer(axisIndices, out indexBuffer);
            }
            catch
            {
                D3D11Utils.DisposeAndNull(ref vertexBuffer);
                throw;
            }

            vertexCount = axisVertices.Length;
            indexCount  = axisIndices.Length;
        }
Exemple #23
0
        public void CreateTangentSphere(out D3D11Buffer vertexBuffer, out D3D11Buffer indexBuffer, out int vertexCount, out int indexCount)
        {
            const int numSegments = 64;
            const int numSlices   = numSegments / 2;

            const int numVertices    = (numSlices + 1) * (numSegments + 1);
            var       sphereVertices = new TangentVertex[numVertices];

            for (int slice = 0; slice <= numSlices; slice++)
            {
                float v           = (float)slice / (float)numSlices;
                float inclination = v * BasicMath.PI;
                float y           = (float)Math.Cos(inclination);
                float r           = (float)Math.Sin(inclination);
                for (int segment = 0; segment <= numSegments; segment++)
                {
                    float u           = (float)segment / (float)numSegments;
                    float azimuth     = u * BasicMath.PI * 2.0f;
                    int   vertexIndex = slice * (numSegments + 1) + segment;
                    sphereVertices[vertexIndex].Position           = new Float3(r * (float)Math.Sin(azimuth), y, r * (float)Math.Cos(azimuth));
                    sphereVertices[vertexIndex].TextureCoordinates = new Float2(u, v);
                    sphereVertices[vertexIndex].UTangent           = new Float3((float)Math.Cos(azimuth), 0, -(float)Math.Sin(azimuth));
                    sphereVertices[vertexIndex].VTangent           = new Float3((float)Math.Cos(inclination) * (float)Math.Sin(azimuth), -(float)Math.Sin(inclination), (float)Math.Cos(inclination) * (float)Math.Cos(azimuth));
                }
            }

            const int numIndices    = numSlices * (numSegments - 2) * 6;
            var       sphereIndices = new ushort[numIndices];

            uint index = 0;

            for (int slice = 0; slice < numSlices; slice++)
            {
                ushort sliceBase0 = (ushort)((slice) * (numSegments + 1));
                ushort sliceBase1 = (ushort)((slice + 1) * (numSegments + 1));
                for (int segment = 0; segment < numSegments; segment++)
                {
                    if (slice > 0)
                    {
                        sphereIndices[index++] = (ushort)(sliceBase0 + segment);
                        sphereIndices[index++] = (ushort)(sliceBase0 + segment + 1);
                        sphereIndices[index++] = (ushort)(sliceBase1 + segment + 1);
                    }
                    if (slice < numSlices - 1)
                    {
                        sphereIndices[index++] = (ushort)(sliceBase0 + segment);
                        sphereIndices[index++] = (ushort)(sliceBase1 + segment + 1);
                        sphereIndices[index++] = (ushort)(sliceBase1 + segment);
                    }
                }
            }

            this.CreateTangentVertexBuffer(sphereVertices, out vertexBuffer);

            try
            {
                this.CreateIndexBuffer(sphereIndices, out indexBuffer);
            }
            catch
            {
                D3D11Utils.DisposeAndNull(ref vertexBuffer);
                throw;
            }

            vertexCount = numVertices;
            indexCount  = numIndices;
        }
Exemple #24
0
        public void SetBaseMesh(uint nVertices, D3D11Buffer pBaseVB)
        {
            var d3dDevice = this.deviceResources.D3DDevice;

            this.m_nVertices = nVertices;

            // shader resource view of base mesh vertex data
            this.m_pBaseVBSRV = d3dDevice.CreateShaderResourceView(pBaseVB, new D3D11ShaderResourceViewDesc
            {
                ViewDimension = D3D11SrvDimension.Buffer,
                Format        = DxgiFormat.R32G32B32A32Float,
                Buffer        = new D3D11BufferSrv
                {
                    FirstElement = 0,
                    NumElements  = this.m_nVertices
                }
            });

            // Buffer for edge tessellation factor
            this.m_pEdgeFactorBuf = d3dDevice.CreateBuffer(new D3D11BufferDesc
            {
                BindOptions         = D3D11BindOptions.ShaderResource | D3D11BindOptions.UnorderedAccess,
                ByteWidth           = 4 * 4 * this.m_nVertices / 3,
                StructureByteStride = 4 * 4,
                MiscOptions         = D3D11ResourceMiscOptions.BufferStructured,
                Usage = D3D11Usage.Default
            });

            // shader resource view of the buffer above
            this.m_pEdgeFactorBufSRV = d3dDevice.CreateShaderResourceView(this.m_pEdgeFactorBuf, new D3D11ShaderResourceViewDesc
            {
                ViewDimension = D3D11SrvDimension.Buffer,
                Format        = DxgiFormat.Unknown,
                Buffer        = new D3D11BufferSrv
                {
                    FirstElement = 0,
                    NumElements  = this.m_nVertices / 3
                }
            });

            // UAV of the buffer above
            this.m_pEdgeFactorBufUAV = d3dDevice.CreateUnorderedAccessView(this.m_pEdgeFactorBuf, new D3D11UnorderedAccessViewDesc
            {
                ViewDimension = D3D11UavDimension.Buffer,
                Format        = DxgiFormat.Unknown,
                Buffer        = new D3D11BufferUav
                {
                    FirstElement = 0,
                    NumElements  = this.m_nVertices / 3
                }
            });

            // Buffers for scan
            this.m_pScanBuf0 = d3dDevice.CreateBuffer(new D3D11BufferDesc
            {
                BindOptions         = D3D11BindOptions.ShaderResource | D3D11BindOptions.UnorderedAccess,
                ByteWidth           = 4 * 2 * this.m_nVertices / 3,
                StructureByteStride = 4 * 2,
                MiscOptions         = D3D11ResourceMiscOptions.BufferStructured,
                Usage = D3D11Usage.Default
            });

            this.m_pScanBuf1 = d3dDevice.CreateBuffer(new D3D11BufferDesc
            {
                BindOptions         = D3D11BindOptions.ShaderResource | D3D11BindOptions.UnorderedAccess,
                ByteWidth           = 4 * 2 * this.m_nVertices / 3,
                StructureByteStride = 4 * 2,
                MiscOptions         = D3D11ResourceMiscOptions.BufferStructured,
                Usage = D3D11Usage.Default
            });

            // shader resource views of the scan buffers
            this.m_pScanBuf0SRV = d3dDevice.CreateShaderResourceView(this.m_pScanBuf0, new D3D11ShaderResourceViewDesc
            {
                ViewDimension = D3D11SrvDimension.Buffer,
                Format        = DxgiFormat.Unknown,
                Buffer        = new D3D11BufferSrv
                {
                    FirstElement = 0,
                    NumElements  = this.m_nVertices / 3
                }
            });

            this.m_pScanBuf1SRV = d3dDevice.CreateShaderResourceView(this.m_pScanBuf1, new D3D11ShaderResourceViewDesc
            {
                ViewDimension = D3D11SrvDimension.Buffer,
                Format        = DxgiFormat.Unknown,
                Buffer        = new D3D11BufferSrv
                {
                    FirstElement = 0,
                    NumElements  = this.m_nVertices / 3
                }
            });

            // UAV of the scan buffers
            this.m_pScanBuf0UAV = d3dDevice.CreateUnorderedAccessView(this.m_pScanBuf0, new D3D11UnorderedAccessViewDesc
            {
                ViewDimension = D3D11UavDimension.Buffer,
                Format        = DxgiFormat.Unknown,
                Buffer        = new D3D11BufferUav
                {
                    FirstElement = 0,
                    NumElements  = this.m_nVertices / 3
                }
            });

            this.m_pScanBuf1UAV = d3dDevice.CreateUnorderedAccessView(this.m_pScanBuf1, new D3D11UnorderedAccessViewDesc
            {
                ViewDimension = D3D11UavDimension.Buffer,
                Format        = DxgiFormat.Unknown,
                Buffer        = new D3D11BufferUav
                {
                    FirstElement = 0,
                    NumElements  = this.m_nVertices / 3
                }
            });
        }
Exemple #25
0
        private void CreateIndexBuffer(ushort[] indexData, out D3D11Buffer indexBuffer)
        {
            D3D11BufferDesc indexBufferDesc = D3D11BufferDesc.From(indexData, D3D11BindOptions.IndexBuffer);

            indexBuffer = this.d3dDevice.CreateBuffer(indexBufferDesc, indexData, 0, 0);
        }
Exemple #26
0
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            // Create the shaders
            byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.cso");
            this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null);

            byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso");
            this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            // Create a layout for the object data
            D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "NORMAL",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 12,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "TEXCOORD",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 24,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode);

            // Create state objects
            D3D11SamplerDesc samplerDesc = new(
                D3D11Filter.MinMagMipLinear,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                0.0f,
                1,
                D3D11ComparisonFunction.Always,
                new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
                0.0f,
                float.MaxValue);

            this.samplerLinear = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc);

            // Create constant buffers
            this.perObjectConstantBuffer = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(PerObjectConstantBuffer.Size, D3D11BindOptions.ConstantBuffer));

            this.perFrameConstantBuffer = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(PerFrameConstantBuffer.Size, D3D11BindOptions.ConstantBuffer));

            // Create other render resources here
        }
Exemple #27
0
        public void CreateBox(Float3 radii, out D3D11Buffer vertexBuffer, out D3D11Buffer indexBuffer, out int vertexCount, out int indexCount)
        {
            BasicVertex[] boxVertices = new BasicVertex[]
            {
                // FLOOR
                new BasicVertex(new Float3(-radii.X, -radii.Y, radii.Z), new Float3(0.0f, 1.0f, 0.0f), new Float2(0.0f, 0.0f)),
                new BasicVertex(new Float3(radii.X, -radii.Y, radii.Z), new Float3(0.0f, 1.0f, 0.0f), new Float2(1.0f, 0.0f)),
                new BasicVertex(new Float3(-radii.X, -radii.Y, -radii.Z), new Float3(0.0f, 1.0f, 0.0f), new Float2(0.0f, 1.5f)),
                new BasicVertex(new Float3(radii.X, -radii.Y, -radii.Z), new Float3(0.0f, 1.0f, 0.0f), new Float2(1.0f, 1.5f)),
                // WALL
                new BasicVertex(new Float3(-radii.X, radii.Y, radii.Z), new Float3(0.0f, 0.0f, -1.0f), new Float2(0.0f, 0.0f)),
                new BasicVertex(new Float3(radii.X, radii.Y, radii.Z), new Float3(0.0f, 0.0f, -1.0f), new Float2(2.0f, 0.0f)),
                new BasicVertex(new Float3(-radii.X, -radii.Y, radii.Z), new Float3(0.0f, 0.0f, -1.0f), new Float2(0.0f, 1.5f)),
                new BasicVertex(new Float3(radii.X, -radii.Y, radii.Z), new Float3(0.0f, 0.0f, -1.0f), new Float2(2.0f, 1.5f)),
                // WALL
                new BasicVertex(new Float3(radii.X, radii.Y, radii.Z), new Float3(-1.0f, 0.0f, 0.0f), new Float2(0.0f, 0.0f)),
                new BasicVertex(new Float3(radii.X, radii.Y, -radii.Z), new Float3(-1.0f, 0.0f, 0.0f), new Float2(radii.Y, 0.0f)),
                new BasicVertex(new Float3(radii.X, -radii.Y, radii.Z), new Float3(-1.0f, 0.0f, 0.0f), new Float2(0.0f, 1.5f)),
                new BasicVertex(new Float3(radii.X, -radii.Y, -radii.Z), new Float3(-1.0f, 0.0f, 0.0f), new Float2(radii.Y, 1.5f)),
                // WALL
                new BasicVertex(new Float3(radii.X, radii.Y, -radii.Z), new Float3(0.0f, 0.0f, 1.0f), new Float2(0.0f, 0.0f)),
                new BasicVertex(new Float3(-radii.X, radii.Y, -radii.Z), new Float3(0.0f, 0.0f, 1.0f), new Float2(2.0f, 0.0f)),
                new BasicVertex(new Float3(radii.X, -radii.Y, -radii.Z), new Float3(0.0f, 0.0f, 1.0f), new Float2(0.0f, 1.5f)),
                new BasicVertex(new Float3(-radii.X, -radii.Y, -radii.Z), new Float3(0.0f, 0.0f, 1.0f), new Float2(2.0f, 1.5f)),
                // WALL
                new BasicVertex(new Float3(-radii.X, radii.Y, -radii.Z), new Float3(1.0f, 0.0f, 0.0f), new Float2(0.0f, 0.0f)),
                new BasicVertex(new Float3(-radii.X, radii.Y, radii.Z), new Float3(1.0f, 0.0f, 0.0f), new Float2(radii.Y, 0.0f)),
                new BasicVertex(new Float3(-radii.X, -radii.Y, -radii.Z), new Float3(1.0f, 0.0f, 0.0f), new Float2(0.0f, 1.5f)),
                new BasicVertex(new Float3(-radii.X, -radii.Y, radii.Z), new Float3(1.0f, 0.0f, 0.0f), new Float2(radii.Y, 1.5f)),
                // CEILING
                new BasicVertex(new Float3(-radii.X, radii.Y, -radii.Z), new Float3(0.0f, -1.0f, 0.0f), new Float2(-0.15f, 0.0f)),
                new BasicVertex(new Float3(radii.X, radii.Y, -radii.Z), new Float3(0.0f, -1.0f, 0.0f), new Float2(1.25f, 0.0f)),
                new BasicVertex(new Float3(-radii.X, radii.Y, radii.Z), new Float3(0.0f, -1.0f, 0.0f), new Float2(-0.15f, 2.1f)),
                new BasicVertex(new Float3(radii.X, radii.Y, radii.Z), new Float3(0.0f, -1.0f, 0.0f), new Float2(1.25f, 2.1f)),
            };

            ushort[] boxIndices = new ushort[]
            {
                0, 2, 1,
                1, 2, 3,

                4, 6, 5,
                5, 6, 7,

                8, 10, 9,
                9, 10, 11,

                12, 14, 13,
                13, 14, 15,

                16, 18, 17,
                17, 18, 19,

                20, 22, 21,
                21, 22, 23,
            };

            this.CreateVertexBuffer(boxVertices, out vertexBuffer);

            try
            {
                this.CreateIndexBuffer(boxIndices, out indexBuffer);
            }
            catch
            {
                D3D11Utils.DisposeAndNull(ref vertexBuffer);
                throw;
            }

            vertexCount = boxVertices.Length;
            indexCount  = boxIndices.Length;
        }
Exemple #28
0
        private void CreateTangentVertexBuffer(TangentVertex[] vertexData, out D3D11Buffer vertexBuffer)
        {
            D3D11BufferDesc vertexBufferDesc = D3D11BufferDesc.From(vertexData, D3D11BindOptions.VertexBuffer);

            vertexBuffer = this.d3dDevice.CreateBuffer(vertexBufferDesc, vertexData, 0, 0);
        }
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            //string fileName = Path.GetDirectoryName(this.OptFileName) + "\\" + Path.GetFileNameWithoutExtension(this.OptFileName) + "Exterior.opt";

            //OptFile opt;
            //if (File.Exists(fileName))
            //{
            //    opt = OptFile.FromFile(fileName);
            //}
            //else
            //{
            //    opt = OptFile.FromFile(this.OptFileName);
            //}

            OptFile opt = OptFile.FromFile(this.OptFileName);

            this.OptSize     = opt.Size * OptFile.ScaleFactor;
            this.OptSpanSize = opt.SpanSize.Scale(OptFile.ScaleFactor, OptFile.ScaleFactor, OptFile.ScaleFactor);

            Vector max = opt.MaxSize;
            Vector min = opt.MinSize;

            this.OptCenter = new Vector()
            {
                X = (max.X + min.X) / 2,
                Y = (max.Y + min.Y) / 2,
                Z = (max.Z + min.Z) / 2
            }.Scale(OptFile.ScaleFactor, OptFile.ScaleFactor, OptFile.ScaleFactor);

            this.CreateTextures(opt);
            this.CreateMeshes(opt);

            byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.cso");
            this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null);

            D3D11InputElementDesc[] basicVertexLayoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "NORMAL",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 12,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "TEXCOORD",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 24,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(basicVertexLayoutDesc, vertexShaderBytecode);

            byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso");
            this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer);

            this.constantBuffer = this.deviceResources.D3DDevice.CreateBuffer(constantBufferDesc);

            D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc(
                D3D11Filter.Anisotropic,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                0.0f,
                this.deviceResources.D3DFeatureLevel > D3D11FeatureLevel.FeatureLevel91 ? D3D11Constants.DefaultMaxAnisotropy : D3D11Constants.FeatureLevel91DefaultMaxAnisotropy,
                D3D11ComparisonFunction.Never,
                new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
                0.0f,
                float.MaxValue);

            this.sampler = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc);

            D3D11RasterizerDesc rasterizerDesc = D3D11RasterizerDesc.Default;

            rasterizerDesc.CullMode = D3D11CullMode.None;
            this.rasterizerState    = this.deviceResources.D3DDevice.CreateRasterizerState(rasterizerDesc);

            this.depthStencilState0 = this.deviceResources.D3DDevice.CreateDepthStencilState(D3D11DepthStencilDesc.Default);

            D3D11DepthStencilDesc depthStencilDesc = D3D11DepthStencilDesc.Default;

            depthStencilDesc.DepthWriteMask = D3D11DepthWriteMask.Zero;
            this.depthStencilState1         = this.deviceResources.D3DDevice.CreateDepthStencilState(depthStencilDesc);

            this.blendState0 = this.deviceResources.D3DDevice.CreateBlendState(D3D11BlendDesc.Default);

            D3D11BlendDesc blendDesc = D3D11BlendDesc.Default;

            D3D11RenderTargetBlendDesc[] blendDescRenderTargets = blendDesc.GetRenderTargets();
            blendDescRenderTargets[0].IsBlendEnabled        = true;
            blendDescRenderTargets[0].SourceBlend           = D3D11BlendValue.SourceAlpha;
            blendDescRenderTargets[0].DestinationBlend      = D3D11BlendValue.InverseSourceAlpha;
            blendDescRenderTargets[0].BlendOperation        = D3D11BlendOperation.Add;
            blendDescRenderTargets[0].SourceBlendAlpha      = D3D11BlendValue.One;
            blendDescRenderTargets[0].DestinationBlendAlpha = D3D11BlendValue.InverseSourceAlpha;
            blendDescRenderTargets[0].BlendOperationAlpha   = D3D11BlendOperation.Add;
            blendDescRenderTargets[0].RenderTargetWriteMask = D3D11ColorWriteEnables.All;
            blendDesc.SetRenderTargets(blendDescRenderTargets);
            this.blendState1 = this.deviceResources.D3DDevice.CreateBlendState(blendDesc);
        }
Exemple #30
0
        protected override void CreateDeviceDependentResources()
        {
            base.CreateDeviceDependentResources();

            byte[] vertexShaderBytecode = File.ReadAllBytes(Lesson4Game.ShadersDirectory + "Textures.VertexShader.cso");
            this.vertexShader = this.DeviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null);

            D3D11InputElementDesc[] basicVertexLayoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "NORMAL",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 12,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "TEXCOORD",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 24,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.inputLayout = this.DeviceResources.D3DDevice.CreateInputLayout(basicVertexLayoutDesc, vertexShaderBytecode);

            byte[] pixelShaderBytecode = File.ReadAllBytes(Lesson4Game.ShadersDirectory + "Textures.PixelShader.cso");
            this.pixelShader = this.DeviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            var vertexBufferDesc = D3D11BufferDesc.From(cubeVertices, D3D11BindOptions.VertexBuffer);

            this.vertexBuffer = this.DeviceResources.D3DDevice.CreateBuffer(vertexBufferDesc, cubeVertices, 0, 0);

            var indexBufferDesc = D3D11BufferDesc.From(cubeIndices, D3D11BindOptions.IndexBuffer);

            this.indexBuffer = this.DeviceResources.D3DDevice.CreateBuffer(indexBufferDesc, cubeIndices, 0, 0);

            var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer);

            this.constantBuffer = this.DeviceResources.D3DDevice.CreateBuffer(constantBufferDesc);

            this.constantBufferData.View = new Float4X4(
                -1.00000000f, 0.00000000f, 0.00000000f, 0.00000000f,
                0.00000000f, 0.89442718f, 0.44721359f, 0.00000000f,
                0.00000000f, 0.44721359f, -0.89442718f, -2.23606800f,
                0.00000000f, 0.00000000f, 0.00000000f, 1.00000000f
                );

            byte[] textureData = File.ReadAllBytes("../../texturedata.bin");

            D3D11Texture2DDesc textureDesc = new D3D11Texture2DDesc(DxgiFormat.R8G8B8A8UNorm, 256, 256, 1, 1);

            D3D11SubResourceData[] textureSubResData = new[]
            {
                new D3D11SubResourceData(textureData, 1024)
            };

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

                this.textureView = this.DeviceResources.D3DDevice.CreateShaderResourceView(texture, textureViewDesc);
            }

            D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc(
                D3D11Filter.Anisotropic,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                0.0f,
                this.DeviceResources.D3DFeatureLevel > D3D11FeatureLevel.FeatureLevel91 ? D3D11Constants.DefaultMaxAnisotropy : D3D11Constants.FeatureLevel91DefaultMaxAnisotropy,
                D3D11ComparisonFunction.Never,
                new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
                0.0f,
                float.MaxValue);

            this.sampler = this.DeviceResources.D3DDevice.CreateSamplerState(samplerDesc);
        }