private void UpdateCubeMapFacePassCBs()
        {
            for (int i = 0; i < 6; i++)
            {
                PassConstants cubeFacePassCB = _mainPassCB;

                Matrix view = _cubeMapCameras[i].View;
                Matrix proj = _cubeMapCameras[i].Proj;

                Matrix viewProj    = view * proj;
                Matrix invView     = Matrix.Invert(view);
                Matrix invProj     = Matrix.Invert(proj);
                Matrix invViewProj = Matrix.Invert(viewProj);

                cubeFacePassCB.View                = Matrix.Transpose(view);
                cubeFacePassCB.InvView             = Matrix.Transpose(invView);
                cubeFacePassCB.Proj                = Matrix.Transpose(proj);
                cubeFacePassCB.InvProj             = Matrix.Transpose(invProj);
                cubeFacePassCB.ViewProj            = Matrix.Transpose(viewProj);
                cubeFacePassCB.InvViewProj         = Matrix.Transpose(invViewProj);
                cubeFacePassCB.EyePosW             = _cubeMapCameras[i].Position;
                cubeFacePassCB.RenderTargetSize    = new Vector2(_dynamicCubeMap.Width, _dynamicCubeMap.Height);
                cubeFacePassCB.InvRenderTargetSize = new Vector2(1.0f / _dynamicCubeMap.Width, 1.0f / _dynamicCubeMap.Height);

                UploadBuffer <PassConstants> currPassCB = CurrFrameResource.PassCB;

                // Cube map pass cbuffers are stored in elements 1-6.
                currPassCB.CopyData(1 + i, ref cubeFacePassCB);
            }
        }
        private void UpdateMaterialCBs()
        {
            UploadBuffer <MaterialConstants> currMaterialCB = CurrFrameResource.MaterialCB;

            foreach (Material mat in _materials.Values)
            {
                // Only update the cbuffer data if the constants have changed. If the cbuffer
                // data changes, it needs to be updated for each FrameResource.
                if (mat.NumFramesDirty > 0)
                {
                    var matConstants = new MaterialConstants
                    {
                        DiffuseAlbedo = mat.DiffuseAlbedo,
                        FresnelR0     = mat.FresnelR0,
                        Roughness     = mat.Roughness,
                        MatTransform  = Matrix.Transpose(mat.MatTransform)
                    };

                    currMaterialCB.CopyData(mat.MatCBIndex, ref matConstants);

                    // Next FrameResource need to be updated too.
                    mat.NumFramesDirty--;
                }
            }
        }
Ejemplo n.º 3
0
        private void UpdateWaves(GameTimer gt)
        {
            // Every quarter second, generate a random wave.
            if ((Timer.TotalTime - _tBase) >= 0.25f)
            {
                _tBase += 0.25f;

                int i = MathHelper.Rand(4, _waves.RowCount - 5);
                int j = MathHelper.Rand(4, _waves.ColumnCount - 5);

                float r = MathHelper.Randf(0.2f, 0.5f);

                _waves.Disturb(i, j, r);
            }

            // Update the wave simulation.
            _waves.Update(gt.DeltaTime);

            // Update the wave vertex buffer with the new solution.
            UploadBuffer <Vertex> currWavesVB = CurrFrameResource.WavesVB;

            for (int i = 0; i < _waves.VertexCount; ++i)
            {
                var v = new Vertex
                {
                    Pos   = _waves.Position(i),
                    Color = Color.Blue.ToVector4()
                };
                currWavesVB.CopyData(i, ref v);
            }

            // Set the dynamic VB of the wave renderitem to the current frame VB.
            _wavesRitem.Geo.VertexBufferGPU = currWavesVB.Resource;
        }
Ejemplo n.º 4
0
        private void UpdateInstanceData()
        {
            UploadBuffer <InstanceData> currInstanceBuffer = CurrFrameResource.InstanceBuffer;

            foreach (RenderItem e in _allRitems)
            {
                int visibleInstanceCount = 0;

                foreach (InstanceData instance in e.Instances)
                {
                    var box = new BoundingBox(
                        Vector3.TransformCoordinate(e.Bounds.Minimum, instance.World),
                        Vector3.TransformCoordinate(e.Bounds.Maximum, instance.World));

                    // Perform the box/frustum intersection test in local space.
                    if (!_frustumCullingEnabled || _camera.Frustum.Intersects(ref box))
                    {
                        var data = new InstanceData
                        {
                            World         = Matrix.Transpose(instance.World),
                            TexTransform  = Matrix.Transpose(instance.TexTransform),
                            MaterialIndex = instance.MaterialIndex
                        };

                        // Write the instance data to structured buffer for the visible objects.
                        currInstanceBuffer.CopyData(visibleInstanceCount++, ref data);
                    }
                }

                e.InstanceCount = visibleInstanceCount;

                MainWindowCaption = $"Instancing and Culling    {e.InstanceCount} objects visible out of {e.Instances.Length}";
            }
        }
Ejemplo n.º 5
0
        public FrameResource(Device device, int passCount, int maxInstanceCount, int materialCount)
        {
            CmdListAlloc = device.CreateCommandAllocator(CommandListType.Direct);

            PassCB         = new UploadBuffer <PassConstants>(device, passCount, true);
            MaterialBuffer = new UploadBuffer <MaterialData>(device, materialCount, false);
            InstanceBuffer = new UploadBuffer <InstanceData>(device, maxInstanceCount, false);
        }
        public FrameResource(Device device, int passCount, int objectCount, int materialCount)
        {
            CmdListAlloc = device.CreateCommandAllocator(CommandListType.Direct);

            PassCB     = new UploadBuffer <PassConstants>(device, passCount, true);
            MaterialCB = new UploadBuffer <MaterialConstants>(device, materialCount, true);
            ObjectCB   = new UploadBuffer <ObjectConstants>(device, objectCount, true);
        }
Ejemplo n.º 7
0
        public FrameResource(Device device, int passCount, int objectCount, int waveVertCount)
        {
            CmdListAlloc = device.CreateCommandAllocator(CommandListType.Direct);

            PassCB   = new UploadBuffer <PassConstants>(device, passCount, true);
            ObjectCB = new UploadBuffer <ObjectConstants>(device, objectCount, true);

            WavesVB = new UploadBuffer <Vertex>(device, waveVertCount, false);
        }
        public FrameResource(Device device, int passCount, int objectCount, int skinnedObjectCount, int materialCount)
        {
            CmdListAlloc = device.CreateCommandAllocator(CommandListType.Direct);

            PassCB         = new UploadBuffer <PassConstants>(device, passCount, true);
            SsaoCB         = new UploadBuffer <SsaoConstants>(device, 1, true);
            MaterialBuffer = new UploadBuffer <MaterialData>(device, materialCount, false);
            ObjectCB       = new UploadBuffer <ObjectConstants>(device, objectCount, true);
            SkinnedCB      = new UploadBuffer <SkinnedConstants>(device, skinnedObjectCount, true);
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
        private void UpdateWaves(GameTimer gt)
        {
            // Every quarter second, generate a random wave.
            if ((Timer.TotalTime - _tBase) >= 0.25f)
            {
                _tBase += 0.25f;

                int i = MathHelper.Rand(4, _waves.RowCount - 5);
                int j = MathHelper.Rand(4, _waves.ColumnCount - 5);

                float r = MathHelper.Randf(0.2f, 0.5f);

                _waves.Disturb(i, j, r);
            }

            // Update the wave simulation.
            _waves.Update(gt.DeltaTime);

            // Update the wave vertex buffer with the new solution.
            UploadBuffer <Vertex> currWavesVB = CurrFrameResource.WavesVB;

            for (int i = 0; i < _waves.VertexCount; ++i)
            {
                var v = new Vertex
                {
                    Pos    = _waves.Position(i),
                    Normal = _waves.Normal(i),
                };
                // Derive tex-coords from position by
                // mapping [-w/2,w/2] --> [0,1]
                v.TexC = new Vector2(
                    0.5f + v.Pos.X / _waves.Width,
                    0.5f - v.Pos.Z / _waves.Depth);
                currWavesVB.CopyData(i, ref v);
            }

            // Set the dynamic VB of the wave renderitem to the current frame VB.
            _wavesRitem.Geo.VertexBufferGPU = currWavesVB.Resource;
        }