Ejemplo n.º 1
0
        private void OnUpdate()
        {
            if (_queue.Count == 0 || (_task != null && _task.Enabled))
            {
                return;
            }

            // TODO: add delay when processing the requests to reduce perf impact (eg. 0.2s before actual rendering)

            // Setup pipeline
            if (_atlases == null)
            {
                _atlases = new List <Atlas>(4);
            }
            if (_output == null)
            {
                _output = GPUDevice.CreateTexture();
                var desc = GPUTextureDescription.New2D(Width, Height, PixelFormat.R8G8B8A8_UNorm);
                _output.Init(ref desc);
            }
            if (_task == null)
            {
                _task        = Object.New <SceneRenderTask>();
                _task.Output = _output;
                _task.Begin += OnBegin;
                _task.End   += OnEnd;
            }

            // Kick off the rendering
            _task.Enabled = true;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Synchronizes size of the back buffer with the size of the control.
        /// </summary>
        public void SyncBackbufferSize()
        {
            float scale  = ResolutionScale * Platform.DpiScale;
            int   width  = Mathf.CeilToInt(Width * scale);
            int   height = Mathf.CeilToInt(Height * scale);

            if (_backBuffer == null || _backBuffer.Width == width && _backBuffer.Height == height)
            {
                return;
            }
            if (width < 1 || height < 1)
            {
                _backBuffer.ReleaseGPU();
                Object.Destroy(ref _backBufferOld);
                return;
            }

            // Cache old backbuffer to remove flickering effect
            if (_backBufferOld == null && _backBuffer.IsAllocated)
            {
                _backBufferOld = _backBuffer;
                _backBuffer    = GPUDevice.CreateTexture();
            }

            // Set timeout to remove old buffer
            _oldBackbufferLiveTimeLeft = 3;

            // Resize backbuffer
            var desc = GPUTextureDescription.New2D(width, height, BackBufferFormat);

            _backBuffer.Init(ref desc);
            _task.Output = _backBuffer;
        }
Ejemplo n.º 3
0
        public static GPUTexture CreateOutputTexture(Vector2 size)
        {
            var texture     = GPUDevice.CreateTexture();
            var description = GPUTextureDescription.New2D((int)size.X, (int)size.Y, PixelFormat.R8G8B8A8_UNorm);

            texture.Init(ref description);

            return(texture);
        }
Ejemplo n.º 4
0
        /// <inheritdoc />
        public override void Render(GPUContext context, SceneRenderTask task, GPUTexture input, GPUTexture output)
        {
            if (Viewport == null)
            {
                throw new NullReferenceException();
            }

            Profiler.BeginEventGPU("Editor Primitives");

            // Check if use MSAA
            var format = output.Format;

            GPUDevice.GetFeatures(format, out var formatSupport);
            bool enableMsaa = formatSupport.MSAALevelMax >= MSAALevel.X4 && Editor.Instance.Options.Options.Visual.EnableMSAAForDebugDraw;

            // Prepare
            var msaaLevel = enableMsaa ? MSAALevel.X4 : MSAALevel.None;
            var width     = output.Width;
            var height    = output.Height;
            var desc      = GPUTextureDescription.New2D(width, height, format, GPUTextureFlags.RenderTarget | GPUTextureFlags.ShaderResource, 1, 1, msaaLevel);
            var target    = RenderTargetPool.Get(ref desc);

            desc = GPUTextureDescription.New2D(width, height, PixelFormat.D24_UNorm_S8_UInt, GPUTextureFlags.DepthStencil, 1, 1, msaaLevel);
            var targetDepth = RenderTargetPool.Get(ref desc);

            // Copy frame and clear depth
            context.Draw(target, input);
            context.ClearDepth(targetDepth);

            // Draw gizmos and other editor primitives (collect draw calls only)
            _drawCallsCollector.Clear();
            for (int i = 0; i < Viewport.Gizmos.Count; i++)
            {
                Viewport.Gizmos[i].Draw(_drawCallsCollector);
            }
            Viewport.DrawEditorPrimitives(context, task, target, targetDepth, _drawCallsCollector);

            // Draw gizmos (actual drawing)
            _drawCallsCollector.ExecuteDrawCalls(context, task, target, DrawPass.GBuffer);
            _drawCallsCollector.ExecuteDrawCalls(context, task, target, DrawPass.Forward);

            // Resolve MSAA texture
            if (enableMsaa)
            {
                context.ResolveMultisample(target, output);
            }
            else
            {
                context.Draw(output, target);
            }

            // Cleanup
            RenderTargetPool.Release(targetDepth);
            RenderTargetPool.Release(target);

            Profiler.EndEventGPU();
        }
        public override void Initialize()
        {
            _output     = GPUDevice.CreateTexture();
            _cachedSize = SizeGetter();
            var description = GPUTextureDescription.New2D(_cachedSize.X, _cachedSize.Y, PixelFormat.R8G8B8A8_UNorm);

            _output.Init(ref description);
            _outputPromise.SetResult(_output);

            Task.Render  = OnRender;
            Task.Enabled = true;
        }
Ejemplo n.º 6
0
        /// <inheritdoc />
        public override void OnInit()
        {
            // Create cache folder
            if (!Directory.Exists(_cacheFolder))
            {
                Directory.CreateDirectory(_cacheFolder);
            }

            // Find atlases in a Editor cache directory
            var files   = Directory.GetFiles(_cacheFolder, "cache_*.flax", SearchOption.TopDirectoryOnly);
            int atlases = 0;

            for (int i = 0; i < files.Length; i++)
            {
                // Load asset
                var asset = FlaxEngine.Content.LoadAsync(files[i]);
                if (asset == null)
                {
                    continue;
                }

                // Validate type
                if (asset is PreviewsCache atlas)
                {
                    // Cache atlas
                    atlases++;
                    _cache.Add(atlas);
                }
                else
                {
                    // Skip asset
                    Editor.LogWarning(string.Format("Asset \'{0}\' is inside Editor\'s private directory for Assets Thumbnails Cache. Please move it.", asset.Path));
                }
            }
            Editor.Log(string.Format("Previews cache count: {0} (capacity for {1} icons)", atlases, atlases * PreviewsCache.AssetIconsPerAtlas));

            // Prepare at least one atlas
            if (_cache.Count == 0)
            {
                GetValidAtlas();
            }

            // Create render task but disabled for now
            _output = GPUDevice.CreateTexture("ThumbnailsOutput");
            var desc = GPUTextureDescription.New2D(PreviewsCache.AssetIconSize, PreviewsCache.AssetIconSize, PreviewsCache.AssetIconsAtlasFormat);

            _output.Init(ref desc);
            _task         = Object.New <CustomRenderTask>();
            _task.Order   = 50; // Render this task later
            _task.Enabled = false;
            _task.Render += OnRender;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RenderOutputControl"/> class.
        /// </summary>
        /// <param name="task">The task. Cannot be null.</param>
        /// <exception cref="System.ArgumentNullException">Invalid task.</exception>
        public RenderOutputControl(SceneRenderTask task)
        {
            if (task == null)
            {
                throw new ArgumentNullException();
            }

            _backBuffer = GPUDevice.CreateTexture();
            _resizeTime = ResizeCheckTime;

            _task                   = task;
            _task.Output            = _backBuffer;
            _task.CanSkipRendering += CanSkipRendering;
            _task.End              += OnEnd;
        }
Ejemplo n.º 8
0
        public RobotDevices()
        {
            var now        = DateTime.UtcNow;
            var timeOffset = (now.AddYears(152) - now).TotalSeconds;

            m_clock   = new ClockDevice("RoboSoft Basic Realtime Clock", timeOffset);
            m_cpu     = new LuaCPUDevice("RoboSoft Basic Lua 5.3 CPU");
            m_rom     = new ROMDevice("RoboSoft BIOS ROM", new AssetMount("rom", Assets.Sources.Where(source => source.Mod == null), "arcade/rom"));
            m_gpu     = new GPUDevice("RoboSoft Basic GPU");
            m_display = new DisplayDevice("RoboSoft Basic Monochrome Display", DISPLAY_WIDTH, DISPLAY_HEIGHT, new Palette(new uint[] {
                0x000000ff, 0xffffffff
            }));
            m_speaker = new SpeakerDevice("RoboSoft Basic Internal Speaker", SPEAKER_CHANNELS);

            var hddPath = Path.Combine(App.SavePath, "arcade/hdd");

            m_hardDrive = new HardDriveDevice("RoboSoft Basic Hard Drive", new FileMount("hdd", hddPath, HDD_CAPACITY, false));
            m_diskDrive = new DiskDriveDevice("RoboSoft Basic Disk Drive");
            m_keyboard  = new KeyboardDevice("RoboSoft Basic Keyboard");
            m_gamepad   = new GamepadDevice("RoboSoft Basic 2-Button Gamepad", 2, 2);
            m_score     = new ScoreDevice("RoboSoft Basic Highscore RAM");
        }
Ejemplo n.º 9
0
        public override void Initialize()
        {
            StringBuilder         sb    = new StringBuilder(512);
            ETrackedPropertyError error = ETrackedPropertyError.TrackedProp_Success;
            uint ret = _vrSystem.GetStringTrackedDeviceProperty(
                OVR.k_unTrackedDeviceIndex_Hmd,
                ETrackedDeviceProperty.Prop_TrackingSystemName_String,
                sb,
                512u,
                ref error);

            if (error != ETrackedPropertyError.TrackedProp_Success)
            {
                _deviceName = "<Unknown OpenVR Device>";
            }
            else
            {
                _deviceName = sb.ToString();
            }

            uint eyeWidth  = 0;
            uint eyeHeight = 0;

            _vrSystem.GetRecommendedRenderTargetSize(ref eyeWidth, ref eyeHeight);

            _leftEyeGT = GPUDevice.CreateTexture();
            var descLeftEye = GPUTextureDescription.New2D((int)eyeWidth, (int)eyeHeight, 1, PixelFormat.R8G8B8A8_UNorm, msaaLevel: _options.EyeRenderTargetSampleCount);

            _leftEyeGT.Init(ref descLeftEye);

            _rightEyeGT = GPUDevice.CreateTexture();
            var descRightEye = GPUTextureDescription.New2D((int)eyeWidth, (int)eyeHeight, 1, PixelFormat.R8G8B8A8_UNorm, msaaLevel: _options.EyeRenderTargetSampleCount);

            _rightEyeGT.Init(ref descRightEye);

            Matrix eyeToHeadLeft = ToSysMatrix(_vrSystem.GetEyeToHeadTransform(EVREye.Eye_Left));

            Matrix.Invert(ref eyeToHeadLeft, out _headToEyeLeft);

            Matrix eyeToHeadRight = ToSysMatrix(_vrSystem.GetEyeToHeadTransform(EVREye.Eye_Right));

            Matrix.Invert(ref eyeToHeadRight, out _headToEyeRight);

            // Default RH matrices

            /*_projLeft = ToSysMatrix(_vrSystem.GetProjectionMatrix(EVREye.Eye_Left, 0.1f, 10000f));
             * _projRight = ToSysMatrix(_vrSystem.GetProjectionMatrix(EVREye.Eye_Right, 0.1f, 10000f));*/

            // Build LH projection matrices (https://github.com/ValveSoftware/openvr/wiki/IVRSystem::GetProjectionRaw)
            float pLeft   = 0;
            float pRight  = 0;
            float pTop    = 0;
            float pBottom = 0;

            // Default values
            float zNear = 0.1f;
            float zFar  = 20000f;

            _vrSystem.GetProjectionRaw(EVREye.Eye_Left, ref pLeft, ref pRight, ref pTop, ref pBottom);
            _projLeft = Matrix.PerspectiveOffCenter(pLeft * zNear, pRight * zNear, pBottom * zNear * -1f, pTop * zNear * -1f, zNear, zFar);

            _vrSystem.GetProjectionRaw(EVREye.Eye_Right, ref pLeft, ref pRight, ref pTop, ref pBottom);
            _projRight = Matrix.PerspectiveOffCenter(pLeft * zNear, pRight * zNear, pBottom * zNear * -1f, pTop * zNear * -1f, zNear, zFar);
        }