コード例 #1
0
 public void ReleaseDeviceDependentResources()
 {
     D3D11Utils.DisposeAndNull(ref this.vertexShader);
     D3D11Utils.DisposeAndNull(ref this.inputLayout);
     D3D11Utils.DisposeAndNull(ref this.pixelShader);
     D3D11Utils.DisposeAndNull(ref this.vertexBuffer);
 }
コード例 #2
0
        public void Render()
        {
            XMMatrix worldViewProjection = this.worldMatrix * this.viewMatrix * this.projectionMatrix;

            this.scene.SetWorldViewProj(worldViewProjection);

            var context = this.deviceResources.D3DContext;

            context.OutputMergerSetRenderTargets(new[] { this.deviceResources.D3DRenderTargetView }, this.deviceResources.D3DDepthStencilView);
            context.ClearRenderTargetView(this.deviceResources.D3DRenderTargetView, new float[] { 0.0f, 0.125f, 0.3f, 1.0f });
            context.ClearDepthStencilView(this.deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth, 1.0f, 0);

            //this.scene.Render();

            context.OutputMergerGetRenderTargets(1, out D3D11RenderTargetView[] origRTV, out D3D11DepthStencilView origDSV);

            this.oit.SetScene(this.scene);
            this.oit.SetRenderTarget(origRTV[0], origDSV);

            this.oit.Render();

            this.oit.SetScene(null);
            this.oit.SetRenderTarget(null, null);

            context.OutputMergerSetRenderTargets(origRTV, origDSV);

            D3D11Utils.DisposeAndNull(ref origRTV[0]);
            D3D11Utils.DisposeAndNull(ref origDSV);
        }
コード例 #3
0
        public void ReleaseDeviceDependentResources()
        {
            foreach (var textureKey in this.textureViews)
            {
                textureKey.Value.Dispose();
            }

            this.textureViews.Clear();

            foreach (var mesh in this.meshes)
            {
                mesh.ReleaseDeviceDependentResources();
            }

            this.meshes.Clear();

            D3D11Utils.DisposeAndNull(ref this.vertexShader);
            D3D11Utils.DisposeAndNull(ref this.inputLayout);
            D3D11Utils.DisposeAndNull(ref this.pixelShader);
            D3D11Utils.DisposeAndNull(ref this.constantBuffer);
            D3D11Utils.DisposeAndNull(ref this.sampler);
            D3D11Utils.DisposeAndNull(ref this.rasterizerState);
            D3D11Utils.DisposeAndNull(ref this.depthStencilState0);
            D3D11Utils.DisposeAndNull(ref this.depthStencilState1);
            D3D11Utils.DisposeAndNull(ref this.blendState0);
            D3D11Utils.DisposeAndNull(ref this.blendState1);
        }
コード例 #4
0
        public void ReleaseDeviceDependentResources()
        {
            D3D11Utils.DisposeAndNull(ref this.g_pShadowMapDepthStencilTexture);
            D3D11Utils.DisposeAndNull(ref this.g_pDepthTextureSRV);
            D3D11Utils.DisposeAndNull(ref this.g_pDepthStencilTextureDSV);

            D3D11Utils.DisposeAndNull(ref this.g_pSamplePoint);
            D3D11Utils.DisposeAndNull(ref this.g_pSampleLinear);
            D3D11Utils.DisposeAndNull(ref this.g_pSamplePointCmp);
            D3D11Utils.DisposeAndNull(ref this.g_pBlendStateNoBlend);
            D3D11Utils.DisposeAndNull(ref this.g_pBlendStateColorWritesOff);

            D3D11Utils.DisposeAndNull(ref this.g_pcbConstants);

            this.g_SceneMesh?.Release();
            this.g_SceneMesh = null;

            this.g_Poles?.Release();
            this.g_Poles = null;

            D3D11Utils.DisposeAndNull(ref this.g_pSceneVS);
            D3D11Utils.DisposeAndNull(ref this.g_pSceneVertexLayout);
            D3D11Utils.DisposeAndNull(ref this.g_pScenePS);
            D3D11Utils.DisposeAndNull(ref this.g_pShadowMapVS);
        }
コード例 #5
0
 public static void CreateTexture(
     DdsFile dds,
     D3D11Device device,
     D3D11DeviceContext context,
     out D3D11ShaderResourceView textureView)
 {
     CreateTexture(dds, device, context, 0, out D3D11Resource texture, out textureView, out _);
     D3D11Utils.DisposeAndNull(ref texture);
 }
コード例 #6
0
 private void ReleaseParticlePosVeloBuffers()
 {
     D3D11Utils.DisposeAndNull(ref this.g_pParticlePosVelo0);
     D3D11Utils.DisposeAndNull(ref this.g_pParticlePosVelo1);
     D3D11Utils.DisposeAndNull(ref this.g_pParticlePosVeloRV0);
     D3D11Utils.DisposeAndNull(ref this.g_pParticlePosVeloRV1);
     D3D11Utils.DisposeAndNull(ref this.g_pParticlePosVeloUAV0);
     D3D11Utils.DisposeAndNull(ref this.g_pParticlePosVeloUAV1);
 }
コード例 #7
0
        public void ReleaseDeviceDependentResources()
        {
            D3D11Utils.DisposeAndNull(ref this.m_pAuxBuf);
            D3D11Utils.DisposeAndNull(ref this.m_pAuxBufRV);
            D3D11Utils.DisposeAndNull(ref this.m_pAuxBufUAV);

            D3D11Utils.DisposeAndNull(ref this.m_pScanCS);
            D3D11Utils.DisposeAndNull(ref this.m_pScan2CS);
            D3D11Utils.DisposeAndNull(ref this.m_pScan3CS);
        }
コード例 #8
0
        protected override void ReleaseDeviceDependentResources()
        {
            base.ReleaseDeviceDependentResources();

            D3D11Utils.DisposeAndNull(ref this.vertexShader);
            D3D11Utils.DisposeAndNull(ref this.inputLayout);
            D3D11Utils.DisposeAndNull(ref this.pixelShader);
            D3D11Utils.DisposeAndNull(ref this.vertexBuffer);
            D3D11Utils.DisposeAndNull(ref this.indexBuffer);
        }
コード例 #9
0
        public void ReleaseDeviceDependentResources()
        {
            D3D11Utils.DisposeAndNull(ref this.vertexShader);
            D3D11Utils.DisposeAndNull(ref this.inputLayout);
            D3D11Utils.DisposeAndNull(ref this.pixelShader);
            D3D11Utils.DisposeAndNull(ref this.samplerLinear);
            D3D11Utils.DisposeAndNull(ref this.perObjectConstantBuffer);
            D3D11Utils.DisposeAndNull(ref this.perFrameConstantBuffer);

            // Delete additional render resources here...
        }
コード例 #10
0
 public void ReleaseDeviceDependentResources()
 {
     D3D11Utils.DisposeAndNull(ref this.fragmentCountPS);
     D3D11Utils.DisposeAndNull(ref this.createPrefixSumPass0CS);
     D3D11Utils.DisposeAndNull(ref this.createPrefixSumPass1CS);
     D3D11Utils.DisposeAndNull(ref this.fillDeepBufferPS);
     D3D11Utils.DisposeAndNull(ref this.sortAndRenderCS);
     D3D11Utils.DisposeAndNull(ref this.depthStencilState);
     D3D11Utils.DisposeAndNull(ref this.computeShaderConstantBuffer);
     D3D11Utils.DisposeAndNull(ref this.pixelShaderConstantBuffer);
 }
コード例 #11
0
        private void RenderParticles(XMMatrix mView, XMMatrix mProj)
        {
            var context = this.deviceResources.D3DContext;

            context.OutputMergerGetBlendState(out D3D11BlendState pBlendState0, out float[] BlendFactor0, out uint SampleMask0);
            context.OutputMergerGetDepthStencilState(out D3D11DepthStencilState pDepthStencilState0, out uint StencilRef0);

            context.VertexShaderSetShader(this.g_pRenderParticlesVS, null);
            context.GeometryShaderSetShader(this.g_pRenderParticlesGS, null);
            context.PixelShaderSetShader(this.g_pRenderParticlesPS, null);

            context.InputAssemblerSetInputLayout(this.g_pParticleVertexLayout);

            // Set IA parameters
            D3D11Buffer[] pBuffers = { this.g_pParticleBuffer };
            uint[]        stride   = { ParticleVertex.Size };
            uint[]        offset   = { 0 };
            context.InputAssemblerSetVertexBuffers(0, pBuffers, stride, offset);
            context.InputAssemblerSetPrimitiveTopology(D3D11PrimitiveTopology.PointList);

            D3D11ShaderResourceView[] aRViews = { this.g_pParticlePosVeloRV0 };
            context.VertexShaderSetShaderResources(0, aRViews);

            ConstantBufferGS pCBGS = new ConstantBufferGS
            {
                m_WorldViewProj = mView * mProj,
                m_InvView       = mView.Inverse()
            };

            context.UpdateSubresource(this.g_pcbGS, 0, null, pCBGS, 0, 0);
            context.GeometryShaderSetConstantBuffers(0, new[] { this.g_pcbGS });

            context.PixelShaderSetShaderResources(0, new[] { this.g_pParticleTexRV });
            context.PixelShaderSetSamplers(0, new[] { this.g_pSampleStateLinear });

            context.OutputMergerSetBlendState(this.g_pBlendingStateParticle, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, 0xFFFFFFFF);
            context.OutputMergerSetDepthStencilState(this.g_pDepthStencilState, 0);

            context.Draw(MaxParticles, 0);

            D3D11ShaderResourceView[] ppSRVNULL = { null };
            context.VertexShaderSetShaderResources(0, ppSRVNULL);
            context.PixelShaderSetShaderResources(0, ppSRVNULL);

            /*ID3D11Buffer* ppBufNULL[1] = { NULL };
             * pd3dImmediateContext->GSSetConstantBuffers( 0, 1, ppBufNULL );*/

            context.GeometryShaderSetShader(null, null);
            context.OutputMergerSetBlendState(pBlendState0, BlendFactor0, SampleMask0);
            context.OutputMergerSetDepthStencilState(pDepthStencilState0, StencilRef0);

            D3D11Utils.DisposeAndNull(ref pBlendState0);
            D3D11Utils.DisposeAndNull(ref pDepthStencilState0);
        }
コード例 #12
0
 public void ReleaseDeviceDependentResources()
 {
     D3D11Utils.DisposeAndNull(ref this.vertexShader);
     D3D11Utils.DisposeAndNull(ref this.inputLayout);
     D3D11Utils.DisposeAndNull(ref this.pixelShader);
     D3D11Utils.DisposeAndNull(ref this.vertexBuffer);
     D3D11Utils.DisposeAndNull(ref this.indexBuffer);
     D3D11Utils.DisposeAndNull(ref this.constantBuffer);
     D3D11Utils.DisposeAndNull(ref this.textureView);
     D3D11Utils.DisposeAndNull(ref this.sampler);
 }
コード例 #13
0
        static void CleanupResources()
        {
            D3D11Utils.ReleaseAndNull(ref g_pBuf0SRV);
            D3D11Utils.ReleaseAndNull(ref g_pBuf1SRV);
            D3D11Utils.ReleaseAndNull(ref g_pBufResultUAV);
            D3D11Utils.ReleaseAndNull(ref g_pBuf0);
            D3D11Utils.ReleaseAndNull(ref g_pBuf1);
            D3D11Utils.ReleaseAndNull(ref g_pBufResult);
            D3D11Utils.ReleaseAndNull(ref g_pCS);

            deviceResources?.Release();
            deviceResources = null;
        }
コード例 #14
0
        public void ReleaseDeviceDependentResources()
        {
            this.mesh?.Release();
            this.mesh = null;

            D3D11Utils.DisposeAndNull(ref this.vertexShader);
            D3D11Utils.DisposeAndNull(ref this.inputLayout);
            D3D11Utils.DisposeAndNull(ref this.pixelShader);
            D3D11Utils.DisposeAndNull(ref this.sampler);
            D3D11Utils.DisposeAndNull(ref this.constantBufferVSPerObject);
            D3D11Utils.DisposeAndNull(ref this.constantBufferPSPerObject);
            D3D11Utils.DisposeAndNull(ref this.constantBufferPSPerFrame);
        }
コード例 #15
0
        public void ReleaseDeviceDependentResources()
        {
            this.tessellator.ReleaseDeviceDependentResources();

            D3D11Utils.DisposeAndNull(ref this.g_pBaseVB);
            D3D11Utils.DisposeAndNull(ref this.g_pTessedVB);
            D3D11Utils.DisposeAndNull(ref this.g_pTessedIB);
            D3D11Utils.DisposeAndNull(ref this.g_pBaseVBLayout);
            D3D11Utils.DisposeAndNull(ref this.g_pVSCB);
            D3D11Utils.DisposeAndNull(ref this.g_pVS);
            D3D11Utils.DisposeAndNull(ref this.g_pBaseVS);
            D3D11Utils.DisposeAndNull(ref this.g_pPS);
            D3D11Utils.DisposeAndNull(ref this.g_pRasWireFrame);
        }
コード例 #16
0
 public void ReleaseWindowSizeDependentResources()
 {
     D3D11Utils.DisposeAndNull(ref this.fragmentCountBuffer);
     D3D11Utils.DisposeAndNull(ref this.prefixSum);
     D3D11Utils.DisposeAndNull(ref this.deepBuffer);
     D3D11Utils.DisposeAndNull(ref this.deepBufferColor);
     D3D11Utils.DisposeAndNull(ref this.fragmentCountRV);
     D3D11Utils.DisposeAndNull(ref this.fragmentCountUAV);
     D3D11Utils.DisposeAndNull(ref this.prefixSumUAV);
     D3D11Utils.DisposeAndNull(ref this.deepBufferUAV);
     D3D11Utils.DisposeAndNull(ref this.deepBufferColorUAV);
     D3D11Utils.DisposeAndNull(ref this.screenTexture);
     D3D11Utils.DisposeAndNull(ref this.screenTextureRTV);
     D3D11Utils.DisposeAndNull(ref this.screenTextureUAV);
 }
コード例 #17
0
 public void ReleaseDeviceDependentResources()
 {
     D3D11Utils.DisposeAndNull(ref this.g_pVertexShader);
     D3D11Utils.DisposeAndNull(ref this.g_pHullShaderInteger);
     D3D11Utils.DisposeAndNull(ref this.g_pHullShaderFracEven);
     D3D11Utils.DisposeAndNull(ref this.g_pHullShaderFracOdd);
     D3D11Utils.DisposeAndNull(ref this.g_pDomainShader);
     D3D11Utils.DisposeAndNull(ref this.g_pPixelShader);
     D3D11Utils.DisposeAndNull(ref this.g_pSolidColorPS);
     D3D11Utils.DisposeAndNull(ref this.g_pPatchLayout);
     D3D11Utils.DisposeAndNull(ref this.g_pcbPerFrame);
     D3D11Utils.DisposeAndNull(ref this.g_pRasterizerStateSolid);
     D3D11Utils.DisposeAndNull(ref this.g_pRasterizerStateWireframe);
     D3D11Utils.DisposeAndNull(ref this.g_pControlPointVB);
 }
コード例 #18
0
        public void LoadShader(string filename, D3D11InputElementDesc[] layoutDesc, out D3D11VertexShader shader, out D3D11InputLayout layout)
        {
            byte[] data = File.ReadAllBytes(filename);

            shader = this.d3dDevice.CreateVertexShader(data, null);

            try
            {
                this.CreateInputLayout(data, layoutDesc, out layout);
            }
            catch
            {
                D3D11Utils.DisposeAndNull(ref shader);
                throw;
            }
        }
コード例 #19
0
        static void CleanupResources()
        {
            D3D11Utils.ReleaseAndNull(ref g_pReadBackBuffer);
            D3D11Utils.ReleaseAndNull(ref g_pBuffer2UAV);
            D3D11Utils.ReleaseAndNull(ref g_pBuffer1UAV);
            D3D11Utils.ReleaseAndNull(ref g_pBuffer2SRV);
            D3D11Utils.ReleaseAndNull(ref g_pBuffer1SRV);
            D3D11Utils.ReleaseAndNull(ref g_pBuffer2);
            D3D11Utils.ReleaseAndNull(ref g_pBuffer1);
            D3D11Utils.ReleaseAndNull(ref g_pCB);
            D3D11Utils.ReleaseAndNull(ref g_pComputeShaderTranspose);
            D3D11Utils.ReleaseAndNull(ref g_pComputeShaderBitonic);

            deviceResources.Release();
            deviceResources = null;
        }
コード例 #20
0
 public void ReleaseDeviceDependentResources()
 {
     D3D11Utils.DisposeAndNull(ref this.g_pRenderParticlesVS);
     D3D11Utils.DisposeAndNull(ref this.g_pRenderParticlesGS);
     D3D11Utils.DisposeAndNull(ref this.g_pRenderParticlesPS);
     D3D11Utils.DisposeAndNull(ref this.g_pCalcCS);
     D3D11Utils.DisposeAndNull(ref this.g_pParticleVertexLayout);
     D3D11Utils.DisposeAndNull(ref this.g_pParticleBuffer);
     this.ReleaseParticlePosVeloBuffers();
     D3D11Utils.DisposeAndNull(ref this.g_pcbGS);
     D3D11Utils.DisposeAndNull(ref this.g_pcbCS);
     D3D11Utils.DisposeAndNull(ref this.g_pParticleTexRV);
     D3D11Utils.DisposeAndNull(ref this.g_pSampleStateLinear);
     D3D11Utils.DisposeAndNull(ref this.g_pBlendingStateParticle);
     D3D11Utils.DisposeAndNull(ref this.g_pDepthStencilState);
 }
コード例 #21
0
        public void ReleaseDeviceDependentResources()
        {
            this.s_ScanCS.ReleaseDeviceDependentResources();

            D3D11Utils.DisposeAndNull(ref this.s_pEdgeFactorCS);
            D3D11Utils.DisposeAndNull(ref this.s_pScatterVertexTriIDIndexIDCS);
            D3D11Utils.DisposeAndNull(ref this.s_pScatterIndexTriIDIndexIDCS);
            D3D11Utils.DisposeAndNull(ref this.s_pNumVerticesIndicesCSs[0]);
            D3D11Utils.DisposeAndNull(ref this.s_pNumVerticesIndicesCSs[1]);
            D3D11Utils.DisposeAndNull(ref this.s_pNumVerticesIndicesCSs[2]);
            D3D11Utils.DisposeAndNull(ref this.s_pNumVerticesIndicesCSs[3]);
            D3D11Utils.DisposeAndNull(ref this.s_pTessVerticesCSs[0]);
            D3D11Utils.DisposeAndNull(ref this.s_pTessVerticesCSs[1]);
            D3D11Utils.DisposeAndNull(ref this.s_pTessVerticesCSs[2]);
            D3D11Utils.DisposeAndNull(ref this.s_pTessVerticesCSs[3]);
            D3D11Utils.DisposeAndNull(ref this.s_pTessIndicesCSs[0]);
            D3D11Utils.DisposeAndNull(ref this.s_pTessIndicesCSs[1]);
            D3D11Utils.DisposeAndNull(ref this.s_pTessIndicesCSs[2]);
            D3D11Utils.DisposeAndNull(ref this.s_pTessIndicesCSs[3]);
            D3D11Utils.DisposeAndNull(ref this.s_pEdgeFactorCSCB);
            D3D11Utils.DisposeAndNull(ref this.s_pLookupTableCSCB);
            D3D11Utils.DisposeAndNull(ref this.s_pCSCB);
            D3D11Utils.DisposeAndNull(ref this.s_pCSReadBackBuf);

            D3D11Utils.DisposeAndNull(ref this.m_pEdgeFactorBuf);
            D3D11Utils.DisposeAndNull(ref this.m_pEdgeFactorBufSRV);
            D3D11Utils.DisposeAndNull(ref this.m_pEdgeFactorBufUAV);
            D3D11Utils.DisposeAndNull(ref this.m_pScanBuf0);
            D3D11Utils.DisposeAndNull(ref this.m_pScanBuf1);
            D3D11Utils.DisposeAndNull(ref this.m_pScanBuf0SRV);
            D3D11Utils.DisposeAndNull(ref this.m_pScanBuf1SRV);
            D3D11Utils.DisposeAndNull(ref this.m_pScanBuf0UAV);
            D3D11Utils.DisposeAndNull(ref this.m_pScanBuf1UAV);
            D3D11Utils.DisposeAndNull(ref this.m_pScatterVertexBuf);
            D3D11Utils.DisposeAndNull(ref this.m_pScatterIndexBuf);
            D3D11Utils.DisposeAndNull(ref this.m_pScatterVertexBufSRV);
            D3D11Utils.DisposeAndNull(ref this.m_pScatterIndexBufSRV);
            D3D11Utils.DisposeAndNull(ref this.m_pScatterVertexBufUAV);
            D3D11Utils.DisposeAndNull(ref this.m_pScatterIndexBufUAV);
            D3D11Utils.DisposeAndNull(ref this.m_pTessedVerticesBufSRV);
            D3D11Utils.DisposeAndNull(ref this.m_pTessedVerticesBufUAV);
            D3D11Utils.DisposeAndNull(ref this.m_pTessedIndicesBufUAV);
            D3D11Utils.DisposeAndNull(ref this.m_pBaseVBSRV);
        }
コード例 #22
0
        public void HandleDeviceLost()
        {
            D3D11Utils.DisposeAndNull(ref this.backBuffer);

            this.OnReleaseBackBuffer();

            if (this.deviceNotify != null)
            {
                this.deviceNotify.OnDeviceLost();
            }

            this.CreateDeviceResources();
            this.CreateWindowSizeDependentResources();

            if (this.deviceNotify != null)
            {
                this.deviceNotify.OnDeviceRestored();
            }
        }
コード例 #23
0
        private void CreateTexture(byte[] data, uint width, uint height, out D3D11Texture2D texture, out D3D11ShaderResourceView textureView)
        {
            D3D11Texture2DDesc textureDesc = new D3D11Texture2DDesc(DxgiFormat.R8G8B8A8UNorm, width, height, 1, 1);

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

            D3D11Texture2D          texture2D = this.d3dDevice.CreateTexture2D(textureDesc, textureSubResData);
            D3D11ShaderResourceView shaderResourceView;

            try
            {
                D3D11ShaderResourceViewDesc textureViewDesc = new D3D11ShaderResourceViewDesc
                {
                    Format        = textureDesc.Format,
                    ViewDimension = D3D11SrvDimension.Texture2D,
                    Texture2D     = new D3D11Texture2DSrv
                    {
                        MipLevels       = textureDesc.MipLevels,
                        MostDetailedMip = 0
                    }
                };

                shaderResourceView = this.d3dDevice.CreateShaderResourceView(texture2D, textureViewDesc);
            }
            catch
            {
                D3D11Utils.DisposeAndNull(ref texture2D);
                throw;
            }

            texture     = texture2D;
            textureView = shaderResourceView;
        }
コード例 #24
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;
        }
コード例 #25
0
        private static void CreateTextureFromDDS(
            D3D11Device device,
            D3D11DeviceContext context,
            DdsFile dds,
            byte[] bitData,
            int maxSize,
            D3D11Usage usage,
            D3D11BindOptions bindOptions,
            D3D11CpuAccessOptions cpuAccessOptions,
            D3D11ResourceMiscOptions miscOptions,
            bool forceSRGB,
            out D3D11Resource texture,
            out D3D11ShaderResourceView textureView)
        {
            int width  = dds.Width;
            int height = dds.Height;
            int depth  = dds.Depth;

            D3D11ResourceDimension resDim = (D3D11ResourceDimension)dds.ResourceDimension;
            int        arraySize          = Math.Max(1, dds.ArraySize);
            DxgiFormat format             = (DxgiFormat)dds.Format;
            bool       isCubeMap          = false;

            if (dds.Format == DdsFormat.Unknown)
            {
                if (dds.PixelFormat.RgbBitCount == 32)
                {
                    if (IsBitMask(dds.PixelFormat, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000))
                    {
                        format = DxgiFormat.B8G8R8X8UNorm;
                        int length = bitData.Length / 4;
                        var bytes  = new byte[length * 4];

                        for (int i = 0; i < length; i++)
                        {
                            bytes[i * 4 + 0] = bitData[i * 4 + 2];
                            bytes[i * 4 + 1] = bitData[i * 4 + 1];
                            bytes[i * 4 + 2] = bitData[i * 4 + 0];
                        }

                        bitData = bytes;
                    }
                }
                else if (dds.PixelFormat.RgbBitCount == 24)
                {
                    if (IsBitMask(dds.PixelFormat, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000))
                    {
                        format = DxgiFormat.B8G8R8X8UNorm;
                        int length = bitData.Length / 3;
                        var bytes  = new byte[length * 4];

                        for (int i = 0; i < length; i++)
                        {
                            bytes[i * 4 + 0] = bitData[i * 3 + 0];
                            bytes[i * 4 + 1] = bitData[i * 3 + 1];
                            bytes[i * 4 + 2] = bitData[i * 3 + 2];
                        }

                        bitData = bytes;
                    }
                }
            }

            int mipCount = Math.Max(1, dds.MipmapCount);

            switch (format)
            {
            case DxgiFormat.AI44:
            case DxgiFormat.IA44:
            case DxgiFormat.P8:
            case DxgiFormat.A8P8:
                throw new NotSupportedException(format.ToString() + " format is not supported.");

            default:
                if (DdsHelpers.GetBitsPerPixel((DdsFormat)format) == 0)
                {
                    throw new NotSupportedException(format.ToString() + " format is not supported.");
                }

                break;
            }

            switch (resDim)
            {
            case D3D11ResourceDimension.Texture1D:
                // D3DX writes 1D textures with a fixed Height of 1
                if ((dds.Options & DdsOptions.Height) != 0 && height != 1)
                {
                    throw new InvalidDataException();
                }

                height = 1;
                depth  = 1;
                break;

            case D3D11ResourceDimension.Texture2D:
                if ((dds.ResourceMiscOptions & DdsResourceMiscOptions.TextureCube) != 0)
                {
                    arraySize *= 6;
                    isCubeMap  = true;
                }

                depth = 1;
                break;

            case D3D11ResourceDimension.Texture3D:
                if ((dds.Options & DdsOptions.Depth) == 0)
                {
                    throw new InvalidDataException();
                }

                if (arraySize > 1)
                {
                    throw new NotSupportedException();
                }
                break;

            default:
                if ((dds.Options & DdsOptions.Depth) != 0)
                {
                    resDim = D3D11ResourceDimension.Texture3D;
                }
                else
                {
                    if ((dds.Caps2 & DdsAdditionalCaps.CubeMap) != 0)
                    {
                        // We require all six faces to be defined
                        if ((dds.Caps2 & DdsAdditionalCaps.CubeMapAllFaces) != DdsAdditionalCaps.CubeMapAllFaces)
                        {
                            throw new NotSupportedException();
                        }

                        arraySize = 6;
                        isCubeMap = true;
                    }

                    depth  = 1;
                    resDim = D3D11ResourceDimension.Texture2D;

                    // Note there's no way for a legacy Direct3D 9 DDS to express a '1D' texture
                }

                break;
            }

            if ((miscOptions & D3D11ResourceMiscOptions.TextureCube) != 0 &&
                resDim == D3D11ResourceDimension.Texture2D &&
                (arraySize % 6 == 0))
            {
                isCubeMap = true;
            }

            // Bound sizes (for security purposes we don't trust DDS file metadata larger than the D3D 11.x hardware requirements)
            if (mipCount > D3D11Constants.ReqMipLevels)
            {
                throw new NotSupportedException();
            }

            switch (resDim)
            {
            case D3D11ResourceDimension.Texture1D:
                if (arraySize > D3D11Constants.ReqTexture1DArrayAxisDimension ||
                    width > D3D11Constants.ReqTexture1DDimension)
                {
                    throw new NotSupportedException();
                }

                break;

            case D3D11ResourceDimension.Texture2D:
                if (isCubeMap)
                {
                    // This is the right bound because we set arraySize to (NumCubes*6) above
                    if (arraySize > D3D11Constants.ReqTexture2DArrayAxisDimension ||
                        width > D3D11Constants.ReqTextureCubeDimension ||
                        height > D3D11Constants.ReqTextureCubeDimension)
                    {
                        throw new NotSupportedException();
                    }
                }
                else if (arraySize > D3D11Constants.ReqTexture2DArrayAxisDimension ||
                         width > D3D11Constants.ReqTexture2DDimension ||
                         height > D3D11Constants.ReqTexture2DDimension)
                {
                    throw new NotSupportedException();
                }

                break;

            case D3D11ResourceDimension.Texture3D:
                if (arraySize > 1 ||
                    width > D3D11Constants.ReqTexture3DDimension ||
                    height > D3D11Constants.ReqTexture3DDimension ||
                    depth > D3D11Constants.ReqTexture3DDimension)
                {
                    throw new NotSupportedException();
                }

                break;

            default:
                throw new NotSupportedException();
            }

            bool autogen = false;

            if (mipCount == 1)
            {
                // See if format is supported for auto-gen mipmaps (varies by feature level)
                if (!device.CheckFormatSupport(format, out D3D11FormatSupport fmtSupport) &&
                    (fmtSupport & D3D11FormatSupport.MipAutogen) != 0)
                {
                    // 10level9 feature levels do not support auto-gen mipgen for volume textures
                    if (resDim != D3D11ResourceDimension.Texture3D ||
                        device.FeatureLevel >= D3D11FeatureLevel.FeatureLevel100)
                    {
                        autogen = true;
                    }
                }
            }

            if (autogen)
            {
                // Create texture with auto-generated mipmaps
                CreateD3DResources(
                    device,
                    resDim,
                    width,
                    height,
                    depth,
                    0,
                    arraySize,
                    format,
                    usage,
                    bindOptions | D3D11BindOptions.RenderTarget,
                    cpuAccessOptions,
                    miscOptions | D3D11ResourceMiscOptions.GenerateMips,
                    forceSRGB,
                    isCubeMap,
                    null,
                    out texture,
                    out textureView);

                try
                {
                    DdsHelpers.GetSurfaceInfo(width, height, (DdsFormat)format, out int numBytes, out int rowBytes, out int numRows);

                    if (numBytes > bitData.Length)
                    {
                        throw new EndOfStreamException();
                    }

                    D3D11ShaderResourceViewDesc desc = textureView.Description;
                    uint mipLevels = 1;

                    switch (desc.ViewDimension)
                    {
                    case D3D11SrvDimension.Texture1D:
                        mipLevels = desc.Texture1D.MipLevels;
                        break;

                    case D3D11SrvDimension.Texture1DArray:
                        mipLevels = desc.Texture1DArray.MipLevels;
                        break;

                    case D3D11SrvDimension.Texture2D:
                        mipLevels = desc.Texture2D.MipLevels;
                        break;

                    case D3D11SrvDimension.Texture2DArray:
                        mipLevels = desc.Texture2DArray.MipLevels;
                        break;

                    case D3D11SrvDimension.TextureCube:
                        mipLevels = desc.TextureCube.MipLevels;
                        break;

                    case D3D11SrvDimension.TextureCubeArray:
                        mipLevels = desc.TextureCubeArray.MipLevels;
                        break;

                    case D3D11SrvDimension.Texture3D:
                        mipLevels = desc.Texture3D.MipLevels;
                        break;

                    default:
                        throw new InvalidDataException();
                    }

                    if (arraySize > 1)
                    {
                        int pSrcBits = 0;

                        for (uint item = 0; item < (uint)arraySize; item++)
                        {
                            if (pSrcBits + numBytes > bitData.Length)
                            {
                                throw new EndOfStreamException();
                            }

                            var data = new byte[numBytes];
                            Array.Copy(bitData, pSrcBits, data, 0, numBytes);

                            uint res = D3D11Utils.CalcSubresource(0, item, mipLevels);
                            context.UpdateSubresource(texture, res, null, data, (uint)rowBytes, (uint)numBytes);

                            pSrcBits += numBytes;
                        }
                    }
                    else
                    {
                        context.UpdateSubresource(texture, 0, null, bitData, (uint)rowBytes, (uint)numBytes);
                    }

                    context.GenerateMips(textureView);
                }
                catch
                {
                    D3D11Utils.DisposeAndNull(ref textureView);
                    D3D11Utils.DisposeAndNull(ref texture);
                    throw;
                }
            }
            else
            {
                // Create the texture

                if (!FillInitData(
                        width,
                        height,
                        depth,
                        mipCount,
                        arraySize,
                        format,
                        maxSize,
                        bitData,
                        out int twidth,
                        out int theight,
                        out int tdepth,
                        out int skipMip,
                        out D3D11SubResourceData[] initData))
コード例 #26
0
        private static void CreateD3DResources(
            D3D11Device device,
            D3D11ResourceDimension resDim,
            int width,
            int height,
            int depth,
            int mipCount,
            int arraySize,
            DxgiFormat format,
            D3D11Usage usage,
            D3D11BindOptions bindFlags,
            D3D11CpuAccessOptions cpuAccessFlags,
            D3D11ResourceMiscOptions miscFlags,
            bool forceSRGB,
            bool isCubeMap,
            D3D11SubResourceData[] initData,
            out D3D11Resource texture,
            out D3D11ShaderResourceView textureView)
        {
            texture     = null;
            textureView = null;

            if (forceSRGB)
            {
                format = (DxgiFormat)DdsHelpers.MakeSrgb((DdsFormat)format);
            }

            switch (resDim)
            {
            case D3D11ResourceDimension.Texture1D:
            {
                var desc = new D3D11Texture1DDesc(
                    format,
                    (uint)width,
                    (uint)arraySize,
                    (uint)mipCount,
                    bindFlags,
                    usage,
                    cpuAccessFlags,
                    miscFlags & ~D3D11ResourceMiscOptions.TextureCube);

                texture = device.CreateTexture1D(desc, initData);

                try
                {
                    var SRVDesc = new D3D11ShaderResourceViewDesc
                    {
                        Format = format
                    };

                    if (arraySize > 1)
                    {
                        SRVDesc.ViewDimension  = D3D11SrvDimension.Texture1DArray;
                        SRVDesc.Texture1DArray = new D3D11Texture1DArraySrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                            ArraySize = (uint)arraySize
                        };
                    }
                    else
                    {
                        SRVDesc.ViewDimension = D3D11SrvDimension.Texture1D;
                        SRVDesc.Texture1D     = new D3D11Texture1DSrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels
                        };
                    }

                    textureView = device.CreateShaderResourceView(texture, SRVDesc);
                }
                catch
                {
                    D3D11Utils.DisposeAndNull(ref texture);
                    throw;
                }

                break;
            }

            case D3D11ResourceDimension.Texture2D:
            {
                var desc = new D3D11Texture2DDesc(
                    format,
                    (uint)width,
                    (uint)height,
                    (uint)arraySize,
                    (uint)mipCount,
                    bindFlags,
                    usage,
                    cpuAccessFlags,
                    1,
                    0);

                if (isCubeMap)
                {
                    desc.MiscOptions = miscFlags | D3D11ResourceMiscOptions.TextureCube;
                }
                else
                {
                    desc.MiscOptions = miscFlags & ~D3D11ResourceMiscOptions.TextureCube;
                }

                if (format == DxgiFormat.BC1UNorm || format == DxgiFormat.BC2UNorm || format == DxgiFormat.BC3UNorm)
                {
                    if ((width & 3) != 0 || (height & 3) != 0)
                    {
                        desc.Width  = (uint)(width + 3) & ~3U;
                        desc.Height = (uint)(height + 3) & ~3U;
                    }
                }

                texture = device.CreateTexture2D(desc, initData);

                try
                {
                    var SRVDesc = new D3D11ShaderResourceViewDesc
                    {
                        Format = format
                    };

                    if (isCubeMap)
                    {
                        if (arraySize > 6)
                        {
                            SRVDesc.ViewDimension    = D3D11SrvDimension.TextureCubeArray;
                            SRVDesc.TextureCubeArray = new D3D11TextureCubeArraySrv
                            {
                                MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                                // Earlier we set arraySize to (NumCubes * 6)
                                NumCubes = (uint)arraySize / 6
                            };
                        }
                        else
                        {
                            SRVDesc.ViewDimension = D3D11SrvDimension.TextureCube;
                            SRVDesc.TextureCube   = new D3D11TextureCubeSrv
                            {
                                MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                            };
                        }
                    }
                    else if (arraySize > 1)
                    {
                        SRVDesc.ViewDimension  = D3D11SrvDimension.Texture2DArray;
                        SRVDesc.Texture2DArray = new D3D11Texture2DArraySrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                            ArraySize = (uint)arraySize
                        };
                    }
                    else
                    {
                        SRVDesc.ViewDimension = D3D11SrvDimension.Texture2D;
                        SRVDesc.Texture2D     = new D3D11Texture2DSrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                        };
                    }

                    textureView = device.CreateShaderResourceView(texture, SRVDesc);
                }
                catch
                {
                    D3D11Utils.DisposeAndNull(ref texture);
                    throw;
                }

                break;
            }

            case D3D11ResourceDimension.Texture3D:
            {
                var desc = new D3D11Texture3DDesc(
                    format,
                    (uint)width,
                    (uint)height,
                    (uint)depth,
                    (uint)mipCount,
                    bindFlags,
                    usage,
                    cpuAccessFlags,
                    miscFlags & ~D3D11ResourceMiscOptions.TextureCube);

                texture = device.CreateTexture3D(desc, initData);

                try
                {
                    var SRVDesc = new D3D11ShaderResourceViewDesc
                    {
                        Format        = format,
                        ViewDimension = D3D11SrvDimension.Texture3D,
                        Texture3D     = new D3D11Texture3DSrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                        }
                    };

                    textureView = device.CreateShaderResourceView(texture, SRVDesc);
                }
                catch
                {
                    D3D11Utils.DisposeAndNull(ref texture);
                    throw;
                }

                break;
            }
            }
        }
コード例 #27
0
 public void ReleaseDeviceDependentResources()
 {
     D3D11Utils.DisposeAndNull(ref this.vertexBuffer);
     D3D11Utils.DisposeAndNull(ref this.indexBuffer);
 }
コード例 #28
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;
        }
コード例 #29
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;
        }
コード例 #30
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
            }
        }