private void BuildShadersAndInputLayout()
        {
            ShaderMacro[] alphaTestDefines =
            {
                new ShaderMacro("ALPHA_TEST", "1")
            };

            _shaders["standardVS"] = D3DUtil.CompileShader("Shaders\\Default.hlsl", "VS", "vs_5_1");
            _shaders["opaquePS"]   = D3DUtil.CompileShader("Shaders\\Default.hlsl", "PS", "ps_5_1");

            _shaders["shadowVS"]            = D3DUtil.CompileShader("Shaders\\Shadows.hlsl", "VS", "vs_5_1");
            _shaders["shadowOpaquePS"]      = D3DUtil.CompileShader("Shaders\\Shadows.hlsl", "PS", "ps_5_1");
            _shaders["shadowAlphaTestedPS"] = D3DUtil.CompileShader("Shaders\\Shadows.hlsl", "PS", "ps_5_1", alphaTestDefines);

            _shaders["debugVS"] = D3DUtil.CompileShader("Shaders\\ShadowDebug.hlsl", "VS", "vs_5_1");
            _shaders["debugPS"] = D3DUtil.CompileShader("Shaders\\ShadowDebug.hlsl", "PS", "ps_5_1");

            _shaders["skyVS"] = D3DUtil.CompileShader("Shaders\\Sky.hlsl", "VS", "vs_5_1");
            _shaders["skyPS"] = D3DUtil.CompileShader("Shaders\\Sky.hlsl", "PS", "ps_5_1");

            _inputLayout = new InputLayoutDescription(new[]
            {
                new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32_Float, 24, 0),
                new InputElement("TANGENT", 0, Format.R32G32B32_Float, 32, 0)
            });
        }
        private void DrawRenderItems(GraphicsCommandList cmdList, List <RenderItem> ritems)
        {
            int objCBByteSize = D3DUtil.CalcConstantBufferByteSize <ObjectConstants>();
            int matCBByteSize = D3DUtil.CalcConstantBufferByteSize <MaterialConstants>();

            Resource objectCB = CurrFrameResource.ObjectCB.Resource;
            Resource matCB    = CurrFrameResource.MaterialCB.Resource;

            foreach (RenderItem ri in ritems)
            {
                cmdList.SetVertexBuffer(0, ri.Geo.VertexBufferView);
                cmdList.SetIndexBuffer(ri.Geo.IndexBufferView);
                cmdList.PrimitiveTopology = ri.PrimitiveType;

                GpuDescriptorHandle tex = _srvDescriptorHeap.GPUDescriptorHandleForHeapStart + ri.Mat.DiffuseSrvHeapIndex * CbvSrvUavDescriptorSize;

                long objCBAddress = objectCB.GPUVirtualAddress + ri.ObjCBIndex * objCBByteSize;
                long matCBAddress = matCB.GPUVirtualAddress + ri.Mat.MatCBIndex * matCBByteSize;

                cmdList.SetGraphicsRootDescriptorTable(0, tex);
                cmdList.SetGraphicsRootConstantBufferView(1, objCBAddress);
                cmdList.SetGraphicsRootConstantBufferView(3, matCBAddress);

                cmdList.DrawIndexedInstanced(ri.IndexCount, 1, ri.StartIndexLocation, ri.BaseVertexLocation, 0);
            }
        }
        private void BuildShadersAndInputLayout()
        {
            ShaderMacro[] defines =
            {
                new ShaderMacro("FOG", "1")
            };

            ShaderMacro[] alphaTestDefines =
            {
                new ShaderMacro("FOG",        "1"),
                new ShaderMacro("ALPHA_TEST", "1")
            };

            ShaderMacro[] waveDefines =
            {
                new ShaderMacro("DISPLACEMENT_MAP", "1")
            };

            _shaders["standardVS"]     = D3DUtil.CompileShader("Shaders\\Default.hlsl", "VS", "vs_5_0");
            _shaders["wavesVS"]        = D3DUtil.CompileShader("Shaders\\Default.hlsl", "VS", "vs_5_0", waveDefines);
            _shaders["opaquePS"]       = D3DUtil.CompileShader("Shaders\\Default.hlsl", "PS", "ps_5_0", defines);
            _shaders["alphaTestedPS"]  = D3DUtil.CompileShader("Shaders\\Default.hlsl", "PS", "ps_5_0", alphaTestDefines);
            _shaders["wavesUpdateCS"]  = D3DUtil.CompileShader("Shaders\\WaveSim.hlsl", "UpdateWavesCS", "cs_5_0");
            _shaders["wavesDisturbCS"] = D3DUtil.CompileShader("Shaders\\WaveSim.hlsl", "DisturbWavesCS", "cs_5_0");
            _shaders["compositeVS"]    = D3DUtil.CompileShader("Shaders\\composite.hlsl", "VS", "vs_5_0");
            _shaders["compositePS"]    = D3DUtil.CompileShader("Shaders\\composite.hlsl", "PS", "ps_5_0");
            _shaders["sobelCS"]        = D3DUtil.CompileShader("Shaders\\Sobel.hlsl", "SobelCS", "cs_5_0");

            _inputLayout = new InputLayoutDescription(new[]
            {
                new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32_Float, 24, 0)
            });
        }
        private void DrawSceneToShadowMap()
        {
            CommandList.SetViewport(_shadowMap.Viewport);
            CommandList.SetScissorRectangles(_shadowMap.ScissorRectangle);

            // Change to DEPTH_WRITE.
            CommandList.ResourceBarrierTransition(_shadowMap.Resource, ResourceStates.GenericRead, ResourceStates.DepthWrite);

            int passCBByteSize = D3DUtil.CalcConstantBufferByteSize <PassConstants>();

            // Clear the depth buffer.
            CommandList.ClearDepthStencilView(_shadowMap.Dsv, ClearFlags.FlagsDepth | ClearFlags.FlagsStencil, 1.0f, 0);

            // Set null render target because we are only going to draw to
            // depth buffer. Setting a null render target will disable color writes.
            // Note the active PSO also must specify a render target count of 0.
            CommandList.SetRenderTargets((CpuDescriptorHandle?)null, _shadowMap.Dsv);

            // Bind the pass constant buffer for shadow map pass.
            Resource passCB        = CurrFrameResource.PassCB.Resource;
            long     passCBAddress = passCB.GPUVirtualAddress + passCBByteSize;

            CommandList.SetGraphicsRootConstantBufferView(1, passCBAddress);

            CommandList.PipelineState = _psos["shadow_opaque"];
            DrawRenderItems(CommandList, _ritemLayers[RenderLayer.Opaque]);

            // Change back to GENERIC_READ so we can read the texture in a shader.
            CommandList.ResourceBarrierTransition(_shadowMap.Resource, ResourceStates.DepthWrite, ResourceStates.GenericRead);
        }
        public static MeshGeometry New <TIndex>(
            Device device,
            GraphicsCommandList commandList,
            IEnumerable <TIndex> indices,
            string name = "Default")
            where TIndex : struct
        {
            TIndex[] indexArray = indices.ToArray();

            int      indexBufferByteSize = Utilities.SizeOf(indexArray);
            Resource indexBuffer         = D3DUtil.CreateDefaultBuffer(
                device,
                commandList,
                indexArray,
                indexBufferByteSize,
                out Resource indexBufferUploader);

            return(new MeshGeometry
            {
                Name = name,
                IndexCount = indexArray.Length,
                IndexFormat = GetIndexFormat <TIndex>(),
                IndexBufferByteSize = indexBufferByteSize,
                IndexBufferGPU = indexBuffer,
                IndexBufferCPU = indexArray,
                _toDispose = { indexBuffer, indexBufferUploader }
            });
        }
Пример #6
0
        private void BuildShadersAndInputLayout()
        {
            ShaderMacro[] defines =
            {
                new ShaderMacro("FOG", "1")
            };

            ShaderMacro[] alphaTestDefines =
            {
                new ShaderMacro("FOG",        "1"),
                new ShaderMacro("ALPHA_TEST", "1")
            };

            _shaders["standardVS"]    = D3DUtil.CompileShader("Shaders\\Default.hlsl", "VS", "vs_5_0");
            _shaders["opaquePS"]      = D3DUtil.CompileShader("Shaders\\Default.hlsl", "PS", "ps_5_0", defines);
            _shaders["alphaTestedPS"] = D3DUtil.CompileShader("Shaders\\Default.hlsl", "PS", "ps_5_0", alphaTestDefines);
            _shaders["horzBlurCS"]    = D3DUtil.CompileShader("Shaders\\Blur.hlsl", "HorzBlurCS", "cs_5_0");
            _shaders["vertBlurCS"]    = D3DUtil.CompileShader("Shaders\\Blur.hlsl", "VertBlurCS", "cs_5_0");

            _inputLayout = new InputLayoutDescription(new[]
            {
                new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32_Float, 24, 0)
            });
        }
        protected override void Draw(GameTimer gt)
        {
            CommandAllocator cmdListAlloc = CurrFrameResource.CmdListAlloc;

            // Reuse the memory associated with command recording.
            // We can only reset when the associated command lists have finished execution on the GPU.
            cmdListAlloc.Reset();

            // A command list can be reset after it has been added to the command queue via ExecuteCommandList.
            // Reusing the command list reuses memory.
            CommandList.Reset(cmdListAlloc, _psos["opaque"]);

            CommandList.SetViewport(Viewport);
            CommandList.SetScissorRectangles(ScissorRectangle);

            // Indicate a state transition on the resource usage.
            CommandList.ResourceBarrierTransition(CurrentBackBuffer, ResourceStates.Present, ResourceStates.RenderTarget);

            // Clear the back buffer and depth buffer.
            CommandList.ClearRenderTargetView(CurrentBackBufferView, new Color(_mainPassCB.FogColor));
            CommandList.ClearDepthStencilView(DepthStencilView, ClearFlags.FlagsDepth | ClearFlags.FlagsStencil, 1.0f, 0);

            // Specify the buffers we are going to render to.
            CommandList.SetRenderTargets(CurrentBackBufferView, DepthStencilView);

            CommandList.SetDescriptorHeaps(_descriptorHeaps.Length, _descriptorHeaps);

            CommandList.SetGraphicsRootSignature(_rootSignature);

            var passCBByteSize = D3DUtil.CalcConstantBufferByteSize <PassConstants>();

            // Draw opaque items--floors, walls, skull.
            Resource passCB = CurrFrameResource.PassCB.Resource;

            CommandList.SetGraphicsRootConstantBufferView(2, passCB.GPUVirtualAddress);

            DrawRenderItems(CommandList, _ritemLayers[RenderLayer.Opaque]);

            // Indicate a state transition on the resource usage.
            CommandList.ResourceBarrierTransition(CurrentBackBuffer, ResourceStates.RenderTarget, ResourceStates.Present);

            // Done recording commands.
            CommandList.Close();

            // Add the command list to the queue for execution.
            CommandQueue.ExecuteCommandList(CommandList);

            // Present the buffer to the screen. Presenting will automatically swap the back and front buffers.
            SwapChain.Present(0, PresentFlags.None);

            // Advance the fence value to mark commands up to this fence point.
            CurrFrameResource.Fence = ++CurrentFence;

            // Add an instruction to the command queue to set a new fence point.
            // Because we are on the GPU timeline, the new fence point won't be
            // set until the GPU finishes processing all the commands prior to this Signal().
            CommandQueue.Signal(Fence, CurrentFence);
        }
        private void BuildShadersAndInputLayout()
        {
            _shaders["standardVS"] = D3DUtil.CompileShader("Shaders\\Default.hlsl", "VS", "vs_5_0");
            _shaders["opaquePS"]   = D3DUtil.CompileShader("Shaders\\Default.hlsl", "PS", "ps_5_0");

            _inputLayout = new InputLayoutDescription(new[]
            {
                new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0)
            });
        }
Пример #9
0
        private void BuildConstantBufferViews()
        {
            int objCBByteSize = D3DUtil.CalcConstantBufferByteSize <ObjectConstants>();

            int objCount = _allRitems.Count;

            // Need a CBV descriptor for each object for each frame resource.
            for (int frameIndex = 0; frameIndex < NumFrameResources; frameIndex++)
            {
                Resource objectCB = _frameResources[frameIndex].ObjectCB.Resource;
                for (int i = 0; i < objCount; i++)
                {
                    long cbAddress = objectCB.GPUVirtualAddress;

                    // Offset to the ith object constant buffer in the buffer.
                    cbAddress += i * objCBByteSize;

                    // Offset to the object cbv in the descriptor heap.
                    int heapIndex = frameIndex * objCount + i;
                    CpuDescriptorHandle handle = _cbvHeap.CPUDescriptorHandleForHeapStart;
                    handle += heapIndex * CbvSrvUavDescriptorSize;

                    var cbvDesc = new ConstantBufferViewDescription
                    {
                        BufferLocation = cbAddress,
                        SizeInBytes    = objCBByteSize
                    };

                    Device.CreateConstantBufferView(cbvDesc, handle);
                }
            }

            int passCBByteSize = D3DUtil.CalcConstantBufferByteSize <PassConstants>();

            // Last three descriptors are the pass CBVs for each frame resource.
            for (int frameIndex = 0; frameIndex < NumFrameResources; frameIndex++)
            {
                Resource passCB    = _frameResources[frameIndex].PassCB.Resource;
                long     cbAddress = passCB.GPUVirtualAddress;

                // Offset to the pass cbv in the descriptor heap.
                int heapIndex = _passCbvOffset + frameIndex;
                CpuDescriptorHandle handle = _cbvHeap.CPUDescriptorHandleForHeapStart;
                handle += heapIndex * CbvSrvUavDescriptorSize;

                var cbvDesc = new ConstantBufferViewDescription
                {
                    BufferLocation = cbAddress,
                    SizeInBytes    = passCBByteSize
                };

                Device.CreateConstantBufferView(cbvDesc, handle);
            }
        }
Пример #10
0
        private void BuildShadersAndInputLayout()
        {
            _mvsByteCode = D3DUtil.CompileShader("Shaders\\Color.hlsl", "VS", "vs_5_0");
            _mpsByteCode = D3DUtil.CompileShader("Shaders\\Color.hlsl", "PS", "ps_5_0");

            _inputLayout = new InputLayoutDescription(new [] // TODO: API suggestion: Add params overload
            {
                new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 12, 0)
            });
        }
        private void BuildShadersAndInputLayout()
        {
            _shaders["tessVS"] = D3DUtil.CompileShader("Shaders\\BezierTessellation.hlsl", "VS", "vs_5_0");
            _shaders["tessHS"] = D3DUtil.CompileShader("Shaders\\BezierTessellation.hlsl", "HS", "hs_5_0");
            _shaders["tessDS"] = D3DUtil.CompileShader("Shaders\\BezierTessellation.hlsl", "DS", "ds_5_0");
            _shaders["tessPS"] = D3DUtil.CompileShader("Shaders\\BezierTessellation.hlsl", "PS", "ps_5_0");

            _inputLayout = new InputLayoutDescription(new[]
            {
                new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0)
            });
        }
Пример #12
0
        private void BuildConstantBuffers()
        {
            int sizeInBytes = D3DUtil.CalcConstantBufferByteSize <ObjectConstants>();

            _objectCB = new UploadBuffer <ObjectConstants>(Device, 1, true);

            var cbvDesc = new ConstantBufferViewDescription
            {
                BufferLocation = _objectCB.Resource.GPUVirtualAddress,
                SizeInBytes    = sizeInBytes
            };
            CpuDescriptorHandle cbvHeapHandle = _cbvHeap.CPUDescriptorHandleForHeapStart;

            Device.CreateConstantBufferView(cbvDesc, cbvHeapHandle);
        }
Пример #13
0
        private void DrawRenderItems(GraphicsCommandList cmdList, List <RenderItem> ritems)
        {
            int      objCBByteSize = D3DUtil.CalcConstantBufferByteSize <ObjectConstants>();
            Resource objectCB      = CurrFrameResource.ObjectCB.Resource;

            foreach (RenderItem ri in ritems)
            {
                cmdList.SetVertexBuffer(0, ri.Geo.VertexBufferView);
                cmdList.SetIndexBuffer(ri.Geo.IndexBufferView);
                cmdList.PrimitiveTopology = ri.PrimitiveType;

                long objCBAddress = objectCB.GPUVirtualAddress + ri.ObjCBIndex * objCBByteSize;
                cmdList.SetGraphicsRootConstantBufferView(0, objCBAddress);

                cmdList.DrawIndexedInstanced(ri.IndexCount, 1, ri.StartIndexLocation, ri.BaseVertexLocation, 0);
            }
        }
Пример #14
0
        private void BuildBuffers()
        {
            // Generate some data.
            var dataA = new Data[NumDataElements];
            var dataB = new Data[NumDataElements];

            for (int i = 0; i < NumDataElements; i++)
            {
                dataA[i].V1 = new Vector3(i, i, i);
                dataA[i].V2 = new Vector2(i, 0);

                dataB[i].V1 = new Vector3(-i, i, 0.0f);
                dataB[i].V2 = new Vector2(0, -i);
            }

            long byteSize = dataA.Length * Utilities.SizeOf <Data>();

            // Create some buffers to be used as SRVs.
            _inputBufferA = D3DUtil.CreateDefaultBuffer(
                Device,
                CommandList,
                dataA,
                byteSize,
                out _inputUploadBufferA);

            _inputBufferB = D3DUtil.CreateDefaultBuffer(
                Device,
                CommandList,
                dataB,
                byteSize,
                out _inputUploadBufferB);

            // Create the buffer that will be a UAV.
            _outputBuffer = Device.CreateCommittedResource(
                new HeapProperties(HeapType.Default),
                HeapFlags.None,
                ResourceDescription.Buffer(byteSize, ResourceFlags.AllowUnorderedAccess),
                ResourceStates.UnorderedAccess);

            _readBackBuffer = Device.CreateCommittedResource(
                new HeapProperties(HeapType.Readback),
                HeapFlags.None,
                ResourceDescription.Buffer(byteSize),
                ResourceStates.CopyDestination);
        }
        private void DrawSceneToCubeMap()
        {
            CommandList.SetViewport(_dynamicCubeMap.Viewport);
            CommandList.SetScissorRectangles(_dynamicCubeMap.ScissorRectangle);

            // Change to RENDER_TARGET.
            CommandList.ResourceBarrierTransition(_dynamicCubeMap.Resource, ResourceStates.GenericRead, ResourceStates.RenderTarget);

            int passCBByteSize = D3DUtil.CalcConstantBufferByteSize <PassConstants>();

            // For each cube map face.
            for (int i = 0; i < 6; i++)
            {
                // Clear the back buffer and depth buffer.
                CommandList.ClearRenderTargetView(_dynamicCubeMap.Rtvs[i], Color.LightSteelBlue);
                CommandList.ClearDepthStencilView(_cubeDSV, ClearFlags.FlagsDepth | ClearFlags.FlagsStencil, 1.0f, 0);

                // Specify the buffers we are going to render to.
                CommandList.SetRenderTargets(_dynamicCubeMap.Rtvs[i], _cubeDSV);

                // Bind the pass constant buffer for this cube map face so we use
                // the right view/proj matrix for this cube face.
                Resource passCB        = CurrFrameResource.PassCB.Resource;
                long     passCBAddress = passCB.GPUVirtualAddress + (1 + i) * passCBByteSize;
                CommandList.SetGraphicsRootConstantBufferView(1, passCBAddress);

                CommandList.PipelineState = _psos["opaque"];
                DrawRenderItems(CommandList, _ritemLayers[RenderLayer.Opaque]);

                CommandList.PipelineState = _psos["sky"];
                DrawRenderItems(CommandList, _ritemLayers[RenderLayer.Sky]);
            }

            // Change back to GENERIC_READ so we can read the texture in a shader.
            CommandList.ResourceBarrierTransition(_dynamicCubeMap.Resource, ResourceStates.RenderTarget, ResourceStates.GenericRead);
        }
        public UploadBuffer(Device device, int elementCount, bool isConstantBuffer)
        {
            // Constant buffer elements need to be multiples of 256 bytes.
            // This is because the hardware can only view constant data
            // at m*256 byte offsets and of n*256 byte lengths.
            // typedef struct D3D12_CONSTANT_BUFFER_VIEW_DESC {
            // UINT64 OffsetInBytes; // multiple of 256
            // UINT   SizeInBytes;   // multiple of 256
            // } D3D12_CONSTANT_BUFFER_VIEW_DESC;
            _elementByteSize = isConstantBuffer
                ? D3DUtil.CalcConstantBufferByteSize <T>()
                : Marshal.SizeOf(typeof(T));

            Resource = device.CreateCommittedResource(
                new HeapProperties(HeapType.Upload),
                HeapFlags.None,
                ResourceDescription.Buffer(_elementByteSize * elementCount),
                ResourceStates.GenericRead);

            _resourcePointer = Resource.Map(0);

            // We do not need to unmap until we are done with the resource. However, we must not write to
            // the resource while it is in use by the GPU (so we must use synchronization techniques).
        }
Пример #17
0
 private void BuildShadersAndInputLayout()
 {
     _shaders["vecAddCS"] = D3DUtil.CompileShader("Shaders\\VecAdd.hlsl", "CS", "cs_5_0");
 }