public override Task DisposeAsync()
        {
            EnsureNotDestroyed(nameof(EditorGameCameraPreviewService));

            // Unregister events
            var selectionService = Services.Get <IEditorGameEntitySelectionService>();

            if (selectionService != null)
            {
                selectionService.SelectionUpdated -= UpdateModifiedEntitiesList;
            }

            // Remove renderers
            var gameTopLevel   = game.SceneSystem.GraphicsCompositor.Game as EditorTopLevelCompositor;
            var editorTopLevel = game.EditorSceneSystem.GraphicsCompositor.Game as EditorTopLevelCompositor;

            if (gameTopLevel != null && editorTopLevel != null)
            {
                gameTopLevel.PostGizmoCompositors.Remove(generateIncrustRenderer);
                editorTopLevel.PostGizmoCompositors.Remove(renderIncrustRenderer);
            }

            defaultFont?.Dispose();
            defaultFont = null;

            spriteEffect?.Dispose();
            spriteEffect = null;

            borderPipelineState = null;
            borderVertexBuffer?.Dispose();
            borderVertexBuffer = null;

            return(base.DisposeAsync());
        }
        protected override Task <bool> Initialize(EditorServiceGame editorGame)
        {
            game = (EntityHierarchyEditorGame)editorGame;

            // create the default font
            var fontItem = OfflineRasterizedSpriteFontFactory.Create();

            fontItem.FontType.Size = 8;
            defaultFont            = OfflineRasterizedFontCompiler.Compile(game.Services.GetService <IFontFactory>(), fontItem, game.GraphicsDevice.ColorSpace == ColorSpace.Linear);

            incrustBatch = new SpriteBatch(game.GraphicsDevice);

            // SpriteEffect will be used to draw camera incrust border
            spriteEffect = new EffectInstance(new Graphics.Effect(game.GraphicsDevice, SpriteEffect.Bytecode));
            spriteEffect.Parameters.Set(TexturingKeys.Texture0, game.GraphicsDevice.GetSharedWhiteTexture());
            spriteEffect.UpdateEffect(game.GraphicsDevice);

            borderPipelineState = new MutablePipelineState(game.GraphicsDevice);
            borderPipelineState.State.RootSignature   = spriteEffect.RootSignature;
            borderPipelineState.State.EffectBytecode  = spriteEffect.Effect.Bytecode;
            borderPipelineState.State.InputElements   = VertexPositionTexture.Layout.CreateInputElements();
            borderPipelineState.State.PrimitiveType   = PrimitiveType.LineStrip;
            borderPipelineState.State.RasterizerState = RasterizerStates.CullNone;

            unsafe
            {
                var borderVertices = new[]
                {
                    new VertexPositionTexture(new Vector3(0.0f, 0.0f, 0.0f), Vector2.Zero),
                    new VertexPositionTexture(new Vector3(0.0f, 1.0f, 0.0f), Vector2.Zero),
                    new VertexPositionTexture(new Vector3(1.0f, 1.0f, 0.0f), Vector2.Zero),
                    new VertexPositionTexture(new Vector3(1.0f, 0.0f, 0.0f), Vector2.Zero),
                    new VertexPositionTexture(new Vector3(0.0f, 0.0f, 0.0f), Vector2.Zero),
                    new VertexPositionTexture(new Vector3(0.0f, 1.0f, 0.0f), Vector2.Zero), // extra vertex so that left-top corner is not missing (likely due to rasterization rule)
                };

                fixed(VertexPositionTexture *borderVerticesPtr = borderVertices)
                borderVertexBuffer = Graphics.Buffer.Vertex.New(game.GraphicsDevice, new DataPointer(borderVerticesPtr, VertexPositionTexture.Size * borderVertices.Length));
            }

            var editorTopLevel = game.EditorSceneSystem.GraphicsCompositor.Game as EditorTopLevelCompositor;

            if (editorTopLevel != null)
            {
                // Display it as incrust
                editorTopLevel.PostGizmoCompositors.Add(renderIncrustRenderer = new RenderIncrustRenderer(this));
            }

            Services.Get <IEditorGameEntitySelectionService>().SelectionUpdated += UpdateModifiedEntitiesList;

            return(Task.FromResult(true));
        }
Esempio n. 3
0
        public void UpdateTempStorage(VoxelStorageContext context)
        {
            storageUints       = (tempStorageCounter + 31) / 32;
            tempStorageCounter = 0;

            var resolution = ClipMapResolution;
            int fragments  = (int)(resolution.X * resolution.Y * resolution.Z) * ClipMapCount;

            if (VoxelUtils.DisposeBufferBySpecs(FragmentsBuffer, storageUints * fragments) && storageUints * fragments > 0)
            {
                FragmentsBuffer = Graphics.Buffer.Typed.New(context.device, storageUints * fragments, PixelFormat.R32_UInt, true);
            }
        }
 static unsafe void FetchBufferData(Graphics.Buffer buffer, CommandList commandList, DataPointer ptr)
 {
     if (buffer.Description.Usage == GraphicsResourceUsage.Staging)
     {
         // Directly if this is a staging resource
         buffer.GetData(commandList, buffer, ptr);
     }
     else
     {
         // Unefficient way to use the Copy method using dynamic staging texture
         using (var throughStaging = buffer.ToStaging())
             buffer.GetData(commandList, throughStaging, ptr);
     }
 }
        private unsafe void UpdateKernelBuffer(RenderDrawContext context)
        {
            int bufferSize = materialScatteringKernels.Length * sizeof(Vector4);

            if (materialScatteringKernelBuffer == null)
            {
                materialScatteringKernelBuffer = Graphics.Buffer.New(context.GraphicsDevice, bufferSize, 0, BufferFlags.ShaderResource, PixelFormat.R32G32B32A32_Float);
            }

            fixed(Vector4 *dataPtr = materialScatteringKernels)
            {
                DataBox        dataBox        = new DataBox((IntPtr)dataPtr, 0, 0);
                ResourceRegion resourceRegion = new ResourceRegion(0, 0, 0, bufferSize, 1, 1);  // TODO: PERFORMANCE: Only upload the actual number of elements active in the buffer?

                context.CommandList.UpdateSubresource(materialScatteringKernelBuffer, 0, dataBox, resourceRegion);
            }

            SetValueParameterForBothShaders(SubsurfaceScatteringBlurShaderKeys.KernelBuffer, materialScatteringKernelBuffer);
        }