From a74c81ada38d0a38ab3b4566b20ee4370a032eeb Mon Sep 17 00:00:00 2001
From: Robert Bragg <robert@linux.intel.com>
Date: Sun, 21 Aug 2011 21:27:13 +0100
Subject: [PATCH] onscreen: Support multisample based onscreen rendering

This adds support for multisample based rendering of onscreen windows
whereby multiple point samples per pixel can be requested and if the
hardware supports that it results in reduced aliasing (especially
considering the jagged edges of polygons)

Reviewed-by: Neil Roberts <neil@linux.intel.com>
---
 cogl/cogl-framebuffer-private.h |  1 +
 cogl/cogl-onscreen-template.c   | 18 ++++++++++++++++++
 cogl/cogl-onscreen-template.h   | 28 ++++++++++++++++++++++++++++
 cogl/winsys/cogl-winsys-egl.c   |  8 ++++++++
 cogl/winsys/cogl-winsys-glx.c   |  9 +++++++++
 cogl/winsys/cogl-winsys-wgl.c   |  4 ++++
 6 files changed, 68 insertions(+)

diff --git a/cogl/cogl-framebuffer-private.h b/cogl/cogl-framebuffer-private.h
index 8d4bbc3d1..fb8ddcf58 100644
--- a/cogl/cogl-framebuffer-private.h
+++ b/cogl/cogl-framebuffer-private.h
@@ -51,6 +51,7 @@ typedef struct
 {
   CoglSwapChain *swap_chain;
   gboolean need_stencil;
+  int samples_per_pixel;
 } CoglFramebufferConfig;
 
 struct _CoglFramebuffer
diff --git a/cogl/cogl-onscreen-template.c b/cogl/cogl-onscreen-template.c
index b9fbd2356..92caadea8 100644
--- a/cogl/cogl-onscreen-template.c
+++ b/cogl/cogl-onscreen-template.c
@@ -65,6 +65,24 @@ cogl_onscreen_template_new (CoglSwapChain *swap_chain)
     onscreen_template->config.swap_chain = cogl_swap_chain_new ();
 
   onscreen_template->config.need_stencil = TRUE;
+  onscreen_template->config.samples_per_pixel = 0;
+
+  user_config = getenv ("COGL_POINT_SAMPLES_PER_PIXEL");
+  if (user_config)
+    {
+      unsigned long samples_per_pixel = strtoul (user_config, NULL, 10);
+      if (samples_per_pixel != ULONG_MAX)
+        onscreen_template->config.samples_per_pixel =
+          samples_per_pixel;
+    }
 
   return _cogl_onscreen_template_object_new (onscreen_template);
 }
+
+void
+cogl_onscreen_template_set_samples_per_pixel (
+                                        CoglOnscreenTemplate *onscreen_template,
+                                        int samples_per_pixel)
+{
+  onscreen_template->config.samples_per_pixel = samples_per_pixel;
+}
diff --git a/cogl/cogl-onscreen-template.h b/cogl/cogl-onscreen-template.h
index 4769f9f1b..e992b7a11 100644
--- a/cogl/cogl-onscreen-template.h
+++ b/cogl/cogl-onscreen-template.h
@@ -43,6 +43,34 @@ typedef struct _CoglOnscreenTemplate	      CoglOnscreenTemplate;
 CoglOnscreenTemplate *
 cogl_onscreen_template_new (CoglSwapChain *swap_chain);
 
+/**
+ * cogl_onscreen_template_set_samples_per_pixel:
+ * @onscreen: A #CoglOnscreenTemplate template framebuffer
+ * @n: The minimum number of samples per pixel
+ *
+ * Requires that any future CoglOnscreen framebuffers derived from
+ * this template must support making at least @n samples per pixel
+ * which will all contribute to the final resolved color for that
+ * pixel.
+ *
+ * By default this value is usually set to 0 and that is referred to
+ * as "single-sample" rendering. A value of 1 or greater is referred
+ * to as "multisample" rendering.
+ *
+ * <note>There are some semantic differences between single-sample
+ * rendering and multisampling with just 1 point sample such as it
+ * being redundant to use the cogl_framebuffer_resolve_samples() and
+ * cogl_framebuffer_resolve_samples_region() apis with single-sample
+ * rendering.</note>
+ *
+ * Since: 1.10
+ * Stability: unstable
+ */
+void
+cogl_onscreen_template_set_samples_per_pixel (
+                                          CoglOnscreenTemplate *onscreen_template,
+                                          int n);
+
 G_END_DECLS
 
 #endif /* __COGL_ONSCREEN_TEMPLATE_H__ */
diff --git a/cogl/winsys/cogl-winsys-egl.c b/cogl/winsys/cogl-winsys-egl.c
index 6b40fc387..5a9c93740 100644
--- a/cogl/winsys/cogl-winsys-egl.c
+++ b/cogl/winsys/cogl-winsys-egl.c
@@ -644,6 +644,14 @@ egl_attributes_from_framebuffer_config (CoglDisplay *display,
   attributes[i++] = EGL_SURFACE_TYPE;
   attributes[i++] = EGL_WINDOW_BIT;
 
+  if (config->samples_per_pixel)
+    {
+       attributes[i++] = EGL_SAMPLE_BUFFERS;
+       attributes[i++] = 1;
+       attributes[i++] = EGL_SAMPLES;
+       attributes[i++] = config->samples_per_pixel;
+    }
+
   attributes[i++] = EGL_NONE;
 
   g_assert (i < MAX_EGL_CONFIG_ATTRIBS);
diff --git a/cogl/winsys/cogl-winsys-glx.c b/cogl/winsys/cogl-winsys-glx.c
index 1a42605e1..8b645be59 100644
--- a/cogl/winsys/cogl-winsys-glx.c
+++ b/cogl/winsys/cogl-winsys-glx.c
@@ -485,6 +485,15 @@ glx_attributes_from_framebuffer_config (CoglDisplay *display,
   attributes[i++] = GLX_STENCIL_SIZE;
   attributes[i++] = config->need_stencil ? 1: GLX_DONT_CARE;
 
+  if (glx_renderer->glx_major == 1 && glx_renderer->glx_minor >= 4 &&
+      config->samples_per_pixel)
+    {
+       attributes[i++] = GLX_SAMPLE_BUFFERS;
+       attributes[i++] = 1;
+       attributes[i++] = GLX_SAMPLES;
+       attributes[i++] = config->samples_per_pixel;
+    }
+
   attributes[i++] = None;
 
   g_assert (i < MAX_GLX_CONFIG_ATTRIBS);
diff --git a/cogl/winsys/cogl-winsys-wgl.c b/cogl/winsys/cogl-winsys-wgl.c
index 8306f2784..3124bcd9c 100644
--- a/cogl/winsys/cogl-winsys-wgl.c
+++ b/cogl/winsys/cogl-winsys-wgl.c
@@ -324,6 +324,10 @@ choose_pixel_format (CoglFramebufferConfig *config,
 
   num_formats = DescribePixelFormat (dc, 0, sizeof (best_pfd), NULL);
 
+  /* XXX: currently we don't support multisampling on windows... */
+  if (config->samples_per_pixel)
+    return best_pf;
+
   for (i = 1; i <= num_formats; i++)
     {
       memset (pfd, 0, sizeof (*pfd));