public void Dispose() { if (!disposed) { disposed = true; Background.Dispose(); CompressToSwap.Dispose(); SphereRenderer.Dispose(); CapsuleRenderer.Dispose(); BoxRenderer.Dispose(); TriangleRenderer.Dispose(); MeshRenderer.Dispose(); UILineRenderer.Dispose(); GlyphRenderer.Dispose(); dsv.Dispose(); depthBuffer.Dispose(); rtv.Dispose(); colorBuffer.Dispose(); resolvedSRV.Dispose(); resolvedRTV.Dispose(); resolvedColorBuffer.Dispose(); rasterizerState.Dispose(); opaqueDepthState.Dispose(); opaqueBlendState.Dispose(); a2cBlendState.Dispose(); uiDepthState.Dispose(); uiBlendState.Dispose(); Shapes.Dispose(); } }
public Renderer(RenderSurface surface) { Surface = surface; ContentArchive content; using (var stream = GetType().Assembly.GetManifestResourceStream("DemoRenderer.DemoRenderer.contentarchive")) { content = ContentArchive.Load(stream); } Shapes = new ShapesExtractor(looper, pool); SphereRenderer = new RayTracedRenderer <SphereInstance>(content, @"ShapeDrawing\RenderSpheres"); CapsuleRenderer = new RayTracedRenderer <CapsuleInstance>(content, @"ShapeDrawing\RenderCapsules"); CylinderRenderer = new RayTracedRenderer <CylinderInstance>(content, @"ShapeDrawing\RenderCylinders"); BoxRenderer = new BoxRenderer(content); TriangleRenderer = new TriangleRenderer(content); MeshRenderer = new MeshRenderer(Shapes.MeshCache, content); Lines = new LineExtractor(pool, looper); LineRenderer = new LineRenderer(content); Background = new BackgroundRenderer(content); CompressToSwap = new CompressToSwap(content); ImageRenderer = new ImageRenderer(content); ImageBatcher = new ImageBatcher(pool); GlyphRenderer = new GlyphRenderer(content); TextBatcher = new TextBatcher(); UILineRenderer = new UILineRenderer(content); UILineBatcher = new UILineBatcher(); OnResize(); }
protected override void DoDispose() { Background.Dispose(); CompressToSwap.Dispose(); Lines.Dispose(); SphereRenderer.Dispose(); CapsuleRenderer.Dispose(); CylinderRenderer.Dispose(); BoxRenderer.Dispose(); TriangleRenderer.Dispose(); MeshRenderer.Dispose(); UILineRenderer.Dispose(); GlyphRenderer.Dispose(); GL.DeleteFramebuffer(framebuffer); GL.DeleteTexture(depthBuffer); GL.DeleteTexture(colorBuffer); GL.DeleteFramebuffer(resolvedFramebuffer); GL.DeleteTexture(resolvedColorBuffer); Shapes.Dispose(); }
public void Render(Camera camera) { if (Surface.Resolution.X != depthBuffer.Description.Width || Surface.Resolution.Y != depthBuffer.Description.Height) { OnResize(); } var context = Surface.Context; Shapes.MeshCache.FlushPendingUploads(context); context.Rasterizer.SetViewport(0, 0, Surface.Resolution.X, Surface.Resolution.Y, 0.0f, 1.0f); //Note reversed depth. context.ClearDepthStencilView(dsv, DepthStencilClearFlags.Depth, 0, 0); context.ClearRenderTargetView(rtv, new SharpDX.Mathematics.Interop.RawColor4()); context.OutputMerger.SetRenderTargets(dsv, rtv); context.Rasterizer.State = rasterizerState; context.OutputMerger.SetDepthStencilState(opaqueDepthState); //All ray traced shapes use analytic coverage writes to get antialiasing. context.OutputMerger.SetBlendState(a2cBlendState); SphereRenderer.Render(context, camera, Surface.Resolution, SpanConverter.AsSpan(Shapes.spheres.Span), 0, Shapes.spheres.Count); CapsuleRenderer.Render(context, camera, Surface.Resolution, SpanConverter.AsSpan(Shapes.capsules.Span), 0, Shapes.capsules.Count); CylinderRenderer.Render(context, camera, Surface.Resolution, SpanConverter.AsSpan(Shapes.cylinders.Span), 0, Shapes.cylinders.Count); //Non-raytraced shapes just use regular opaque rendering. context.OutputMerger.SetBlendState(opaqueBlendState); BoxRenderer.Render(context, camera, Surface.Resolution, SpanConverter.AsSpan(Shapes.boxes.Span), 0, Shapes.boxes.Count); TriangleRenderer.Render(context, camera, Surface.Resolution, SpanConverter.AsSpan(Shapes.triangles.Span), 0, Shapes.triangles.Count); MeshRenderer.Render(context, camera, Surface.Resolution, SpanConverter.AsSpan(Shapes.meshes.Span), 0, Shapes.meshes.Count); LineRenderer.Render(context, camera, Surface.Resolution, SpanConverter.AsSpan(Lines.lines.Span), 0, Lines.lines.Count); Background.Render(context, camera); //Resolve MSAA rendering down to a single sample buffer for screenspace work. //Note that we're not bothering to properly handle tonemapping during the resolve. That's going to hurt quality a little, but the demos don't make use of very wide ranges. //(If for some reason you end up expanding the demos to make use of wider HDR, you can make this a custom resolve pretty easily.) context.ResolveSubresource(colorBuffer, 0, resolvedColorBuffer, 0, Format.R16G16B16A16_Float); context.OutputMerger.SetRenderTargets(resolvedRTV); //Glyph and screenspace line drawing rely on the same premultiplied alpha blending transparency. We'll handle their state out here. context.OutputMerger.SetBlendState(uiBlendState); context.OutputMerger.SetDepthStencilState(uiDepthState); ImageRenderer.PreparePipeline(context); ImageBatcher.Flush(context, Surface.Resolution, ImageRenderer); UILineBatcher.Flush(context, Surface.Resolution, UILineRenderer); GlyphRenderer.PreparePipeline(context); TextBatcher.Flush(context, Surface.Resolution, GlyphRenderer); //Note that, for now, the compress to swap handles its own depth state since it's the only post processing stage. context.OutputMerger.SetBlendState(opaqueBlendState); context.Rasterizer.State = rasterizerState; CompressToSwap.Render(context, resolvedSRV, Surface.RTV); }
protected override void RenderContents(RenderContext context) { Box2 boxBounds = LocalBounds.GetInflated(-40f); BoxRenderer.EmitBoxFill(context, LocalBounds, new Rgba(0.9f, 0.9f, 0.9f, 1.0f)); BoxRenderer.EmitBoxFillShadow(context, boxBounds.GetTranslated(5, 10), 0.5f, 30f); BoxRenderer.EmitBoxFill(context, boxBounds, new Rgba(0.8f, 0.8f, 0.8f, 1.0f)); base.RenderContents(context); }
public void Render(Camera camera) { if (Surface.Resolution.X != width || Surface.Resolution.Y != height) { OnResize(); } Shapes.MeshCache.FlushPendingUploads(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer); //Note reversed depth. GL.ClearDepth(0.0f); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.DepthTest); GL.DepthFunc(DepthFunction.Greater); //All ray traced shapes use analytic coverage writes to get antialiasing. GL.Enable(EnableCap.SampleAlphaToCoverage); SphereRenderer.Render(camera, Surface.Resolution, SpanConverter.AsSpan(Shapes.spheres.Span), 0, Shapes.spheres.Count); CapsuleRenderer.Render(camera, Surface.Resolution, SpanConverter.AsSpan(Shapes.capsules.Span), 0, Shapes.capsules.Count); CylinderRenderer.Render(camera, Surface.Resolution, SpanConverter.AsSpan(Shapes.cylinders.Span), 0, Shapes.cylinders.Count); //Non-raytraced shapes just use regular opaque rendering. GL.Disable(EnableCap.SampleAlphaToCoverage); BoxRenderer.Render(camera, Surface.Resolution, SpanConverter.AsSpan(Shapes.boxes.Span), 0, Shapes.boxes.Count); TriangleRenderer.Render(camera, Surface.Resolution, SpanConverter.AsSpan(Shapes.triangles.Span), 0, Shapes.triangles.Count); MeshRenderer.Render(camera, Surface.Resolution, SpanConverter.AsSpan(Shapes.meshes.Span), 0, Shapes.meshes.Count); LineRenderer.Render(camera, Surface.Resolution, SpanConverter.AsSpan(Lines.lines.Span), 0, Lines.lines.Count); Background.Render(camera); GL.Disable(EnableCap.CullFace); GL.Disable(EnableCap.DepthTest); //Resolve MSAA rendering down to a single sample buffer for screenspace work. //Note that we're not bothering to properly handle tonemapping during the resolve. That's going to hurt quality a little, but the demos don't make use of very wide ranges. //(If for some reason you end up expanding the demos to make use of wider HDR, you can make this a custom resolve pretty easily.) GL.BlitNamedFramebuffer(framebuffer, resolvedFramebuffer, 0, 0, width, height, 0, 0, width, height, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Nearest); GL.BindFramebuffer(FramebufferTarget.Framebuffer, resolvedFramebuffer); //Glyph and screenspace line drawing rely on the same premultiplied alpha blending transparency. We'll handle their state out here. GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha); GL.BlendEquation(BlendEquationMode.FuncAdd); ImageRenderer.PreparePipeline(); ImageBatcher.Flush(Surface.Resolution, ImageRenderer); UILineBatcher.Flush(Surface.Resolution, UILineRenderer); GlyphRenderer.PreparePipeline(); TextBatcher.Flush(Surface.Resolution, GlyphRenderer); GL.Disable(EnableCap.Blend); GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); CompressToSwap.Render(resolvedColorBuffer); }
public MainWindow() { _resource = new VanillaResource(); _eye = new Eye { DepthFar = 1024F, }; _viewTransformProvider = _eye.GetViewTransformProvider(); _projectionTransformProvider = _eye.GetPerspectiveTransformProvider(); _eyeInput = this.CreatePointerAxisInput(.5F, true).CreateSmoothAxisInput(); _moveInput = this.CreateKeyAxisInput(Keys.D, Keys.Space, Keys.S, Keys.A, Keys.LeftShift, Keys.W, true).CreateScaledAxisInput(10F).CreateSmoothAxisInput(); _world = new EmptyWorld { ChunkProvider = (x, z) => new EmptyChunk { X = x, Z = z, World = _world } }; _world.FillFast(0, 0, 0, 256, 0, 256, "bedrock"); _world.FillFast(0, 1, 0, 256, 10, 256, "iron_block"); _world.FillFast(0, 11, 0, 256, 11, 256, "diamond_block"); _world.Fill(15, 12, 14, 16, 13, 17, "gold_block"); _world.Fill(3, 12, 3, 5, 13, 5, "gold_block"); _playerObject = new() { OriginalAABB = new Box3d(-.5D, -.5D, -.5D, .5D, .5D, .5D), GravityScale = 1D, Position = (0D, 15D, 0) }; _floorObject = new BlockCollisionObject(_world); _boxRenderer = new(_viewTransformProvider, _projectionTransformProvider) { Color = Color4.Blue }; /*_cameraMotivatorRenderer = new CameraMotivatorRenderer(_eye) * { * RotationInput = _eyeInput, * PositionInput = _moveInput, * MovementSpeed = 1F, * Type = CameraType.Fps, * Controlable = true * };*/ this.AddCompletedRenderer(new CloudRenderer(_eye, _viewTransformProvider, _projectionTransformProvider, _resource)); this.AddCompletedRenderer(new WorldRenderer(_world, () => _atlases, _viewTransformProvider, _projectionTransformProvider) { Camera = _eye, AutoSetCenterChunk = true }); this.AddRenderObject(_boxRenderer); PointerGrabbed = true; }
public void Render(Camera camera) { if (Surface.Resolution.X != depthBuffer.Description.Width || Surface.Resolution.Y != depthBuffer.Description.Height) { OnResize(); } var context = Surface.Context; context.Rasterizer.SetViewport(0, 0, Surface.Resolution.X, Surface.Resolution.Y, 0.0f, 1.0f); //Note reversed depth. context.ClearDepthStencilView(dsv, DepthStencilClearFlags.Depth, 0, 0); //The background render is going to fill out the entire color buffer, but having a clear can be useful- e.g. clearing out MSAA history. //We don't use MSAA right now, but the cost of doing this clear is negligible and it avoids a surprise later. context.ClearRenderTargetView(rtv, new SharpDX.Mathematics.Interop.RawColor4()); context.OutputMerger.SetRenderTargets(dsv, rtv); context.Rasterizer.State = rasterizerState; context.OutputMerger.SetBlendState(opaqueBlendState); context.OutputMerger.SetDepthStencilState(opaqueDepthState); SphereRenderer.Render(context, camera, Surface.Resolution, Shapes.spheres.Span.Memory, 0, Shapes.spheres.Count); CapsuleRenderer.Render(context, camera, Surface.Resolution, Shapes.capsules.Span.Memory, 0, Shapes.capsules.Count); BoxRenderer.Render(context, camera, Surface.Resolution, Shapes.boxes.Span.Memory, 0, Shapes.boxes.Count); LineRenderer.Render(context, camera, Surface.Resolution, Lines.lines.Span.Memory, 0, Lines.lines.Count); Background.Render(context, camera); //Glyph and screenspace line drawing rely on the same premultiplied alpha blending transparency. We'll handle their state out here. context.OutputMerger.SetBlendState(uiBlendState); context.OutputMerger.SetDepthStencilState(uiDepthState); UILineBatcher.Flush(context, Surface.Resolution, UILineRenderer); GlyphRenderer.PreparePipeline(context); TextBatcher.Flush(context, Surface.Resolution, GlyphRenderer); //Note that, for now, the compress to swap handles its own depth state since it's the only post processing stage. context.OutputMerger.SetBlendState(opaqueBlendState); context.Rasterizer.State = rasterizerState; CompressToSwap.Render(context, srv, Surface.RTV); }
public Renderer(RenderSurface surface) { looper = new ParallelLooper(); Surface = surface; using (var stream = GetType().Assembly.GetManifestResourceStream("DemoRenderer.DemoRenderer.shaderarchive")) { ShaderCache = ShaderCache.Load(stream); } pool = new BufferPool(); Shapes = new ShapesExtractor(Surface.Device, looper, pool); SphereRenderer = new RayTracedRenderer <SphereInstance>(surface.Device, ShaderCache, @"ShapeDrawing\RenderSpheres.hlsl"); CapsuleRenderer = new RayTracedRenderer <CapsuleInstance>(surface.Device, ShaderCache, @"ShapeDrawing\RenderCapsules.hlsl"); BoxRenderer = new BoxRenderer(surface.Device, ShaderCache); TriangleRenderer = new TriangleRenderer(surface.Device, ShaderCache); MeshRenderer = new MeshRenderer(surface.Device, Shapes.MeshCache, ShaderCache); Lines = new LineExtractor(pool, looper); LineRenderer = new LineRenderer(surface.Device, ShaderCache); Background = new BackgroundRenderer(surface.Device, ShaderCache); CompressToSwap = new CompressToSwap(surface.Device, ShaderCache); GlyphRenderer = new GlyphRenderer(surface.Device, surface.Context, ShaderCache); TextBatcher = new TextBatcher(); UILineRenderer = new UILineRenderer(surface.Device, ShaderCache); UILineBatcher = new UILineBatcher(); OnResize(); var rasterizerStateDescription = RasterizerStateDescription.Default(); rasterizerStateDescription.IsFrontCounterClockwise = true; rasterizerStateDescription.CullMode = CullMode.Back; rasterizerState = new RasterizerState(Surface.Device, rasterizerStateDescription); rasterizerState.DebugName = "Default Rasterizer State"; var opaqueDepthStencilDescription = new DepthStencilStateDescription { IsDepthEnabled = true, DepthWriteMask = DepthWriteMask.All, //Note depth reversal. DepthComparison = Comparison.Greater, IsStencilEnabled = false }; opaqueDepthState = new DepthStencilState(Surface.Device, opaqueDepthStencilDescription); opaqueDepthState.DebugName = "Opaque Depth State"; var opaqueBlendStateDescription = BlendStateDescription.Default(); opaqueBlendState = new BlendState(Surface.Device, opaqueBlendStateDescription); opaqueBlendState.DebugName = "Opaque Blend State"; var a2cBlendStateDescription = BlendStateDescription.Default(); a2cBlendStateDescription.AlphaToCoverageEnable = true; a2cBlendState = new BlendState(Surface.Device, a2cBlendStateDescription); a2cBlendState.DebugName = "A2C Blend State"; var uiDepthStateDescription = new DepthStencilStateDescription { IsDepthEnabled = false, DepthWriteMask = DepthWriteMask.Zero, //Note depth reversal. DepthComparison = Comparison.Greater, IsStencilEnabled = false }; uiDepthState = new DepthStencilState(Surface.Device, uiDepthStateDescription); uiDepthState.DebugName = "UI Depth State"; //The UI will use premultiplied alpha. var uiBlendStateDescription = BlendStateDescription.Default(); uiBlendStateDescription.RenderTarget[0].IsBlendEnabled = true; uiBlendStateDescription.RenderTarget[0].SourceBlend = BlendOption.One; uiBlendStateDescription.RenderTarget[0].SourceAlphaBlend = BlendOption.One; uiBlendStateDescription.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha; uiBlendStateDescription.RenderTarget[0].DestinationAlphaBlend = BlendOption.InverseSourceAlpha; uiBlendStateDescription.RenderTarget[0].BlendOperation = BlendOperation.Add; uiBlendStateDescription.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add; uiBlendStateDescription.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; uiBlendState = new BlendState(Surface.Device, uiBlendStateDescription); uiBlendState.DebugName = "UI Blend State"; }