public NativeEditorGraphicsContext(AAQuality antialiasingQuality) { this.antialiasingQuality = antialiasingQuality; GraphicsMode defaultGraphicsMode = this.GetGraphicsMode(this.antialiasingQuality); this.mainContextControl = new GLControl(defaultGraphicsMode); this.mainContextControl.VSync = false; this.mainContextControl.MakeCurrent(); }
/// <summary> /// Creates a new RenderTarget based on a set of <see cref="Duality.Resources.Texture">Textures</see> /// </summary> /// <param name="multisampling">The level of multisampling that is requested from this RenderTarget.</param> /// <param name="targets">An array of <see cref="Duality.Resources.Texture">Textures</see> used as data destination.</param> public RenderTarget(AAQuality multisampling, params ContentRef <Texture>[] targets) { this.multisampling = multisampling; if (targets != null) { foreach (var t in targets) { this.targets.Add(t); } } this.SetupNativeRes(); }
/// <summary> /// Creates a new RenderTarget based on a set of <see cref="Duality.Resources.Texture">Textures</see> /// </summary> /// <param name="multisampling">The level of multisampling that is requested from this RenderTarget.</param> /// <param name="targets">An array of <see cref="Duality.Resources.Texture">Textures</see> used as data destination.</param> public RenderTarget(AAQuality multisampling, params ContentRef <Texture>[] targets) { this.multisampling = multisampling; if (targets != null) { foreach (var t in targets) { this.targetInfo.Add(new TargetInfo(t)); } } this.SetupOpenGLRes(); }
/// <summary> /// Gets the fragment shader. /// </summary> public static String FragShader(Polynomial poly, ShadingType type, AAQuality aa, bool hardcodePoly, int iterations, float threshold) { var shader = String.Join("\n", new[] { "#version 120\n", "/* DO NOT EDIT - AUTOGENERATED */\n", FragArithmetic(), FragPolyRoots(poly, "poly", hardcodePoly), FragPolyRoots(Polynomial.Derivative(poly), "derv", hardcodePoly), FragIterate(iterations, threshold), FragColorize(type), FragShade(iterations), FragMainSampler(aa) }); WriteToFile("shader.fs", shader); return shader; }
public NativeEditorGraphicsContext(AAQuality antialiasingQuality) { this.antialiasingQuality = antialiasingQuality; GraphicsMode defaultGraphicsMode = this.GetGraphicsMode(this.antialiasingQuality); this.mainContextControl = new GLControl( defaultGraphicsMode, GraphicsBackend.MinOpenGLVersion.Major, GraphicsBackend.MinOpenGLVersion.Minor, GraphicsContextFlags.ForwardCompatible); this.mainContextControl.VSync = false; this.mainContextControl.MakeCurrent(); // Log some general info on the graphics context we've set up GraphicsBackend.LogOpenGLContextSpecs(this.mainContextControl.Context); // Determine OpenGL capabilities and log them GraphicsBackend.LogOpenGLSpecs(); }
private GraphicsMode GetGraphicsMode(AAQuality antialiasingQuality) { IEnumerable <GraphicsMode> modes = this.AvailableGraphicsModes; int highestAALevel = MathF.RoundToInt(MathF.Log(MathF.Max(modes.Max(m => m.Samples), 1.0f), 2.0f)); int targetAALevel = highestAALevel; switch (antialiasingQuality) { case AAQuality.High: targetAALevel = highestAALevel; break; case AAQuality.Medium: targetAALevel = highestAALevel / 2; break; case AAQuality.Low: targetAALevel = highestAALevel / 4; break; case AAQuality.Off: targetAALevel = 0; break; } int targetSampleCount = MathF.RoundToInt(MathF.Pow(2.0f, targetAALevel)); return(modes.LastOrDefault(m => m.Samples <= targetSampleCount) ?? modes.Last()); }
public NativeEditorGraphicsContext(AAQuality antialiasingQuality) { this.antialiasingQuality = antialiasingQuality; GraphicsMode defaultGraphicsMode = this.GetGraphicsMode(this.antialiasingQuality); this.mainContextControl = new GLControl( defaultGraphicsMode, GraphicsBackend.MinOpenGLVersion.Major, GraphicsBackend.MinOpenGLVersion.Minor, GraphicsContextFlags.ForwardCompatible); this.mainContextControl.VSync = false; this.mainContextControl.MakeCurrent(); // Log some general info on the graphics context we've set up GraphicsBackend.LogOpenGLContextSpecs(this.mainContextControl.Context); // Let's see what rendering features we have available GraphicsBackend.ActiveInstance.QueryOpenGLCapabilities(); }
void INativeRenderTarget.Setup(IReadOnlyList <INativeTexture> targets, AAQuality multisample, bool depthBuffer) { // Removed thread guards because of performance //DefaultOpenTKBackendPlugin.GuardSingleThreadState(); if (targets == null) { return; } if (targets.Count == 0) { return; } if (targets.All(i => i == null)) { return; } this.depthBuffer = depthBuffer; // Synchronize target information { this.targetInfos.Reserve(targets.Count); int localIndex = 0; for (int i = 0; i < targets.Count; i++) { if (targets[i] == null) { continue; } this.targetInfos.Count = Math.Max(this.targetInfos.Count, localIndex + 1); this.targetInfos.Data[localIndex].Target = targets[i] as NativeTexture; localIndex++; } } // Setup OpenGL resources this.SetupNonMultisampled(); }
void ICmpBenchmarkOverlayRenderer.DrawOverlay(Canvas canvas) { BenchmarkRenderSetup setup = this.renderSetup.Res; if (setup == null) { return; } // Update the displayed info text only when the data changes. // This is a benchmark. We don't want any allocation or perf // noise in our setup. if (this.displayedRenderSize != setup.RenderingSize || this.displayedRenderScale != setup.ResolutionScale || this.displayedAAQuality != setup.AntialiasingQuality) { this.displayedRenderSize = setup.RenderingSize; this.displayedRenderScale = setup.ResolutionScale; this.displayedAAQuality = setup.AntialiasingQuality; this.text.SourceText = string.Format( "Render Size: {0} x {1}/n" + "Res. Scaling: {2:F}x/n" + "AA Quality: {3}", this.displayedRenderSize.X, this.displayedRenderSize.Y, this.displayedRenderScale, this.displayedAAQuality); } canvas.DrawText(this.text, ref this.textBufferGlyphs, ref this.textBufferIcons, canvas.Width - 10, 10, 0, null, Alignment.TopRight, true); }
private PixelData RenderToTexture(int width, int height, AAQuality antialiasing, Action <IDrawDevice> renderMethod) { PixelData pixelData; using (Texture texture = new Texture(width, height, TextureSizeMode.NonPowerOfTwo, TextureMagFilter.Nearest, TextureMinFilter.Nearest)) using (RenderTarget renderTarget = new RenderTarget(antialiasing, texture)) using (DrawDevice device = new DrawDevice()) { device.Perspective = PerspectiveMode.Flat; device.VisibilityMask = VisibilityFlag.AllGroups | VisibilityFlag.ScreenOverlay; device.RenderMode = RenderMatrix.OrthoScreen; device.Target = renderTarget; device.ViewportRect = new Rect(renderTarget.Width, renderTarget.Height); device.PrepareForDrawcalls(); renderMethod(device); device.Render(ClearFlag.All, ColorRgba.TransparentBlack, 1.0f); pixelData = renderTarget.GetPixelData(); } return(pixelData); }
private PixelData RenderToTexture(int width, int height, AAQuality antialiasing, Action <IDrawDevice> renderMethod) { PixelData pixelData; using (Texture texture = new Texture(width, height, TextureSizeMode.NonPowerOfTwo, TextureMagFilter.Nearest, TextureMinFilter.Nearest)) using (RenderTarget renderTarget = new RenderTarget(antialiasing, false, texture)) using (DrawDevice device = new DrawDevice()) { device.Projection = ProjectionMode.Screen; device.VisibilityMask = VisibilityFlag.AllGroups | VisibilityFlag.ScreenOverlay; device.Target = renderTarget; device.TargetSize = renderTarget.Size; device.ViewportRect = new Rect(renderTarget.Size); device.PrepareForDrawcalls(); renderMethod(device); device.Render(); pixelData = renderTarget.GetPixelData(); } return(pixelData); }
private static String FragMainSampler(AAQuality aa) { var str = new StringBuilder(); str.AppendLine("uniform vec2 offset;"); str.AppendLine("uniform float zoom;"); str.AppendLine("uniform vec2 dims;"); str.AppendLine("varying vec2 uv;"); str.AppendLine(); str.AppendLine("vec3 plot_fractal(vec2 z)"); str.AppendLine("{"); int samples = (int)aa; if (samples == 1) str.AppendLine(" return shade(z);"); else { str.AppendLine(" vec2 d = vec2(zoom) / dims;"); str.AppendLine(" vec3 shading = vec3(0);"); str.AppendLine(); for (int y = 0; y < samples; ++y) for (int x = 0; x < samples; ++x) { double px = (x / (double)(samples - 1) - 0.5) / 2; double py = (y / (double)(samples - 1) - 0.5) / 2; str.AppendLine(String.Format(" shading += shade(z + vec2({0}, {1}) * d);", px, py)); } str.AppendLine(); str.AppendLine(String.Format(" return shading / {0};", samples * samples)); } str.AppendLine("}"); str.AppendLine(); str.AppendLine("void main(void)"); str.AppendLine("{"); str.AppendLine(" float ratio = dims.x / dims.y; /* Calculates aspect ratio */"); str.AppendLine(" vec2 z = (uv + vec2(-0.5)) * vec2(ratio, 1) * zoom + offset;"); str.AppendLine(" gl_FragColor = vec4(plot_fractal(z), 0); /* Draws fractal */"); str.AppendLine("}"); return str.ToString(); }
private void SetupOptions() { zoom = 2.4f; offset = Vector2.Zero; iterations = 128; aCoeff = Complex.One; kCoeff = Complex.Zero; aa = AAQuality.AAx1; hardcodePolynomial = true; intensity = 1; threshold = 3; palette = Color4.Red; shading = ShadingType.Standard; polynomial = Polynomial.Parse(DefaultPolynomial, new Dictionary<String, Double>()); }
void INativeRenderTarget.Setup(IReadOnlyList <INativeTexture> targets, AAQuality multisample, bool depthBuffer) { }
/// <summary> /// Creates a new RenderTarget based on a set of <see cref="Duality.Resources.Texture">Textures</see> /// </summary> /// <param name="multisampling">The level of multisampling that is requested from this RenderTarget.</param> /// <param name="targets">An array of <see cref="Duality.Resources.Texture">Textures</see> used as data destination.</param> public RenderTarget(AAQuality multisampling, params ContentRef<Texture>[] targets) { this.multisampling = multisampling; if (targets != null) foreach (var t in targets) this.targetInfo.Add(new TargetInfo(t)); this.SetupOpenGLRes(); }
void INativeRenderTarget.Setup(IReadOnlyList <INativeTexture> targets, AAQuality multisample, bool depthBuffer) { // Removed thread guards because of performance //DefaultOpenTKBackendPlugin.GuardSingleThreadState(); if (targets == null) { return; } if (targets.Count == 0) { return; } if (targets.All(i => i == null)) { return; } // ToDo: AA is disabled for now /*int highestAALevel = MathF.RoundToInt(MathF.Log(MathF.Max(MaxRenderTargetSamples, 1.0f), 2.0f)); * int targetAALevel = highestAALevel; * switch (multisample) { * case AAQuality.High: targetAALevel = highestAALevel; break; * case AAQuality.Medium: targetAALevel = highestAALevel / 2; break; * case AAQuality.Low: targetAALevel = highestAALevel / 4; break; * case AAQuality.Off: targetAALevel = 0; break; * } * int targetSampleCount = MathF.RoundToInt(MathF.Pow(2.0f, targetAALevel)); * GraphicsMode sampleMode = * GraphicsBackend.ActiveInstance.AvailableGraphicsModes.LastOrDefault(m => m.Samples <= targetSampleCount) ?? * GraphicsBackend.ActiveInstance.AvailableGraphicsModes.Last(); * this.samples = sampleMode.Samples;*/ this.samples = 0; this.depthBuffer = depthBuffer; // Synchronize target information { this.targetInfos.Reserve(targets.Count); int localIndex = 0; for (int i = 0; i < /*targets.Count*/ 1; i++) { if (targets[i] == null) { continue; } this.targetInfos.Count = Math.Max(this.targetInfos.Count, localIndex + 1); this.targetInfos.Data[localIndex].Target = targets[i] as NativeTexture; localIndex++; } } // ToDo // Setup OpenGL resources //if (this.samples > 0) // this.SetupMultisampled(); //else this.SetupNonMultisampled(); }
INativeEditorGraphicsContext IEditorGraphicsBackend.CreateContext(AAQuality antialiasingQuality) { return(new DummyNativeEditorGraphicsContext()); }
void INativeRenderTarget.Setup(IReadOnlyList <INativeTexture> targets, AAQuality multisample) { }
void INativeRenderTarget.Setup(IReadOnlyList <INativeTexture> targets, AAQuality multisample) { DefaultOpenTKBackendPlugin.GuardSingleThreadState(); if (targets == null) { return; } if (targets.Count == 0) { return; } if (targets.All(i => i == null)) { return; } int highestAALevel = MathF.RoundToInt(MathF.Log(MathF.Max(MaxRenderTargetSamples, 1.0f), 2.0f)); int targetAALevel = highestAALevel; switch (multisample) { case AAQuality.High: targetAALevel = highestAALevel; break; case AAQuality.Medium: targetAALevel = highestAALevel / 2; break; case AAQuality.Low: targetAALevel = highestAALevel / 4; break; case AAQuality.Off: targetAALevel = 0; break; } int targetSampleCount = MathF.RoundToInt(MathF.Pow(2.0f, targetAALevel)); GraphicsMode sampleMode = GraphicsBackend.ActiveInstance.AvailableGraphicsModes.LastOrDefault(m => m.Samples <= targetSampleCount) ?? GraphicsBackend.ActiveInstance.AvailableGraphicsModes.Last(); this.samples = sampleMode.Samples; // Synchronize target information { this.targetInfos.Reserve(targets.Count); int localIndex = 0; for (int i = 0; i < targets.Count; i++) { if (targets[i] == null) { continue; } this.targetInfos.Count = Math.Max(this.targetInfos.Count, localIndex + 1); this.targetInfos.Data[localIndex].Target = targets[i] as NativeTexture; localIndex++; } } #region Setup FBO & RBO: Non-multisampled if (this.samples == 0) { // Generate FBO if (this.handleMainFBO == 0) { GL.Ext.GenFramebuffers(1, out this.handleMainFBO); } GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, this.handleMainFBO); // Attach textures int oglWidth = 0; int oglHeight = 0; for (int i = 0; i < this.targetInfos.Count; i++) { NativeTexture tex = this.targetInfos[i].Target; FramebufferAttachment attachment = (FramebufferAttachment)((int)FramebufferAttachment.ColorAttachment0Ext + i); GL.Ext.FramebufferTexture2D( FramebufferTarget.FramebufferExt, attachment, TextureTarget.Texture2D, tex.Handle, 0); oglWidth = tex.Width; oglHeight = tex.Height; } // Generate Depth Renderbuffer if (this.handleDepthRBO == 0) { GL.Ext.GenRenderbuffers(1, out this.handleDepthRBO); } GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, this.handleDepthRBO); GL.Ext.RenderbufferStorage(RenderbufferTarget.RenderbufferExt, RenderbufferStorage.DepthComponent24, oglWidth, oglHeight); GL.Ext.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, this.handleDepthRBO); // Check status FramebufferErrorCode status = GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt); if (status != FramebufferErrorCode.FramebufferCompleteExt) { throw new BackendException(string.Format("Incomplete Framebuffer: {0}", status)); } GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, 0); GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); } #endregion #region Setup FBO & RBO: Multisampled if (this.samples > 0) { // Generate texture target FBO if (this.handleMainFBO == 0) { GL.Ext.GenFramebuffers(1, out this.handleMainFBO); } GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, this.handleMainFBO); // Attach textures int oglWidth = 0; int oglHeight = 0; for (int i = 0; i < this.targetInfos.Count; i++) { NativeTexture tex = this.targetInfos[i].Target; FramebufferAttachment attachment = (FramebufferAttachment)((int)FramebufferAttachment.ColorAttachment0Ext + i); GL.Ext.FramebufferTexture2D( FramebufferTarget.FramebufferExt, attachment, TextureTarget.Texture2D, tex.Handle, 0); oglWidth = tex.Width; oglHeight = tex.Height; } // Check status FramebufferErrorCode status = GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt); if (status != FramebufferErrorCode.FramebufferCompleteExt) { throw new BackendException(string.Format("Incomplete Framebuffer: {0}", status)); } // Generate rendering FBO if (this.handleMsaaFBO == 0) { GL.Ext.GenFramebuffers(1, out this.handleMsaaFBO); } GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, this.handleMsaaFBO); // Attach color renderbuffers for (int i = 0; i < this.targetInfos.Count; i++) { TargetInfo info = this.targetInfos.Data[i]; FramebufferAttachment attachment = (FramebufferAttachment)((int)FramebufferAttachment.ColorAttachment0Ext + i); RenderbufferStorage rbColorFormat = TexFormatToRboFormat(info.Target.Format); if (info.HandleMsaaColorRBO == 0) { GL.GenRenderbuffers(1, out info.HandleMsaaColorRBO); } GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, info.HandleMsaaColorRBO); GL.Ext.RenderbufferStorageMultisample(RenderbufferTarget.RenderbufferExt, this.samples, rbColorFormat, oglWidth, oglHeight); GL.Ext.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, attachment, RenderbufferTarget.RenderbufferExt, info.HandleMsaaColorRBO); this.targetInfos.Data[i] = info; } GL.Ext.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0); // Attach depth renderbuffer if (this.handleDepthRBO == 0) { GL.Ext.GenRenderbuffers(1, out this.handleDepthRBO); } GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, this.handleDepthRBO); GL.Ext.RenderbufferStorageMultisample(RenderbufferTarget.RenderbufferExt, this.samples, RenderbufferStorage.DepthComponent24, oglWidth, oglHeight); GL.Ext.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, this.handleDepthRBO); GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, 0); // Check status status = GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt); if (status != FramebufferErrorCode.FramebufferCompleteExt) { throw new BackendException(string.Format("Incomplete Multisample Framebuffer: {0}", status)); } GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); } #endregion }