OpenGL Render: Support full-sample Anti-Aliasing
authorCampbell Barton <ideasman42@gmail.com>
Wed, 11 Nov 2015 13:06:26 +0000 (00:06 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 11 Nov 2015 13:25:09 +0000 (00:25 +1100)
This brings back old (slower), higher quality method.
Useful since graphics cards often use a faster MSAA which only oversamples edges.

release/scripts/startup/bl_ui/space_info.py
source/blender/blenkernel/BKE_sequencer.h
source/blender/blenkernel/intern/sequencer.c
source/blender/editors/include/ED_view3d.h
source/blender/editors/render/render_opengl.c
source/blender/editors/sculpt_paint/paint_image_proj.c
source/blender/editors/space_view3d/view3d_draw.c
source/blender/windowmanager/intern/wm_files.c

index d295cc19fb7782e111deaa09188584bcece59b49..369656f2620d478d0a0788ed8d32f1d233273773 100644 (file)
@@ -262,8 +262,9 @@ class INFO_MT_opengl_render(Menu):
         layout = self.layout
 
         rd = context.scene.render
-
         layout.prop(rd, "use_antialiasing")
+        layout.prop(rd, "use_full_sample")
+
         layout.prop_menu_enum(rd, "antialiasing_samples")
         layout.prop_menu_enum(rd, "alpha_mode")
 
index 350edde407b601b94d99b7645d4faa8141c5087d..0a2a2c7131fee27bea3c2883e98b17e2a278d6cf 100644 (file)
@@ -108,6 +108,7 @@ typedef struct SeqRenderData {
        struct GPUOffScreen *gpu_offscreen;
        struct GPUFX *gpu_fx;
        int gpu_samples;
+       bool gpu_full_samples;
 } SeqRenderData;
 
 void BKE_sequencer_new_render_data(
@@ -418,7 +419,7 @@ struct Sequence *BKE_sequencer_add_movie_strip(struct bContext *C, ListBase *seq
 typedef struct ImBuf *(*SequencerDrawView)(
         struct Scene *, struct Object *, int, int,
         unsigned int, int, bool, bool, bool,
-        int, int, const char *,
+        int, int, bool, const char *,
         struct GPUFX *, struct GPUOffScreen *, char[256]);
 extern SequencerDrawView sequencer_view3d_cb;
 
index 6d583651e0ffdd391420448be30b99a05bdc2587..08975fe17f0f2b0145d0c7029e8955210563c83a 100644 (file)
@@ -565,6 +565,7 @@ void BKE_sequencer_new_render_data(
        r_context->view_id = 0;
        r_context->gpu_offscreen = NULL;
        r_context->gpu_samples = (scene->r.mode & R_OSA) ? scene->r.osa : 0;
+       r_context->gpu_full_samples = (r_context->gpu_samples) && (scene->r.scemode & R_FULL_SAMPLE);
 }
 
 /* ************************* iterator ************************** */
@@ -3221,7 +3222,7 @@ static ImBuf *seq_render_scene_strip(const SeqRenderData *context, Sequence *seq
                        context->scene->r.seq_prev_type,
                        (context->scene->r.seq_flag & R_SEQ_SOLID_TEX) != 0,
                        use_gpencil, true, scene->r.alphamode,
-                       context->gpu_samples, viewname,
+                       context->gpu_samples, context->gpu_full_samples, viewname,
                        context->gpu_fx, context->gpu_offscreen, err_out);
                if (ibuf == NULL) {
                        fprintf(stderr, "seq_render_scene_strip failed to get opengl buffer: %s\n", err_out);
index e5fe163ce4027629948b5e35596d5f340d59b91c..83a71c651cd41cd139ee681465fcd1e4a443e82e 100644 (file)
@@ -333,12 +333,12 @@ void ED_view3d_draw_offscreen(
 struct ImBuf *ED_view3d_draw_offscreen_imbuf(
         struct Scene *scene, struct View3D *v3d, struct ARegion *ar, int sizex, int sizey,
         unsigned int flag, bool draw_background,
-        int alpha_mode, int samples, const char *viewname,
+        int alpha_mode, int samples, bool full_samples, const char *viewname,
         struct GPUFX *fx, struct GPUOffScreen *ofs, char err_out[256]);
 struct ImBuf *ED_view3d_draw_offscreen_imbuf_simple(
         struct Scene *scene, struct Object *camera, int width, int height,
         unsigned int flag, int drawtype, bool use_solid_tex, bool use_gpencil, bool draw_background,
-        int alpha_mode, int samples, const char *viewname,
+        int alpha_mode, int samples, bool full_samples, const char *viewname,
         struct GPUFX *fx, struct GPUOffScreen *ofs, char err_out[256]);
 
 struct Base *ED_view3d_give_base_under_cursor(struct bContext *C, const int mval[2]);
index 2a48d30dfd18f89d25d1034d6b8f12a02b1b98f2..738ee1faed30f8cd1cc8b28084bec27fc207081f 100644 (file)
@@ -97,6 +97,7 @@ typedef struct OGLRender {
 
        GPUOffScreen *ofs;
        int ofs_samples;
+       bool ofs_full_samples;
        GPUFX *fx;
        int sizex, sizey;
        int write_still;
@@ -276,7 +277,7 @@ static void screen_opengl_render_doit(OGLRender *oglrender, RenderResult *rr)
                context.view_id = BKE_scene_multiview_view_id_get(&scene->r, viewname);
                context.gpu_offscreen = oglrender->ofs;
                context.gpu_fx = oglrender->fx;
-               context.gpu_samples = oglrender->ofs_samples;
+               context.gpu_full_samples = oglrender->ofs_full_samples;
 
                ibuf = BKE_sequencer_give_ibuf(&context, CFRA, chanshown);
 
@@ -341,12 +342,13 @@ static void screen_opengl_render_doit(OGLRender *oglrender, RenderResult *rr)
                /* shouldnt suddenly give errors mid-render but possible */
                char err_out[256] = "unknown";
                ImBuf *ibuf_view;
+               const int alpha_mode = (draw_sky) ? R_ADDSKY : R_ALPHAPREMUL;
 
                if (view_context) {
                        ibuf_view = ED_view3d_draw_offscreen_imbuf(
                               scene, v3d, ar, sizex, sizey,
                               IB_rect, draw_bgpic,
-                              (draw_sky) ? R_ADDSKY : R_ALPHAPREMUL, oglrender->ofs_samples, viewname,
+                              alpha_mode, oglrender->ofs_samples, oglrender->ofs_full_samples, viewname,
                               oglrender->fx, oglrender->ofs, err_out);
 
                        /* for stamp only */
@@ -358,7 +360,7 @@ static void screen_opengl_render_doit(OGLRender *oglrender, RenderResult *rr)
                        ibuf_view = ED_view3d_draw_offscreen_imbuf_simple(
                                scene, scene->camera, oglrender->sizex, oglrender->sizey,
                                IB_rect, OB_SOLID, false, true, true,
-                               (draw_sky) ? R_ADDSKY : R_ALPHAPREMUL, oglrender->ofs_samples, viewname,
+                               alpha_mode, oglrender->ofs_samples, oglrender->ofs_full_samples, viewname,
                                oglrender->fx, oglrender->ofs, err_out);
                        camera = scene->camera;
                }
@@ -471,6 +473,7 @@ static bool screen_opengl_render_init(bContext *C, wmOperator *op)
        OGLRender *oglrender;
        int sizex, sizey;
        const int samples = (scene->r.mode & R_OSA) ? scene->r.osa : 0;
+       const bool full_samples = (samples != 0) && (scene->r.scemode & R_FULL_SAMPLE);
        bool is_view_context = RNA_boolean_get(op->ptr, "view_context");
        const bool is_animation = RNA_boolean_get(op->ptr, "animation");
        const bool is_sequencer = RNA_boolean_get(op->ptr, "sequencer");
@@ -515,7 +518,7 @@ static bool screen_opengl_render_init(bContext *C, wmOperator *op)
        sizey = (scene->r.size * scene->r.ysch) / 100;
 
        /* corrects render size with actual size, not every card supports non-power-of-two dimensions */
-       ofs = GPU_offscreen_create(sizex, sizey, samples, err_out);
+       ofs = GPU_offscreen_create(sizex, sizey, full_samples ? 0 : samples, err_out);
 
        if (!ofs) {
                BKE_reportf(op->reports, RPT_ERROR, "Failed to create OpenGL off-screen buffer, %s", err_out);
@@ -528,6 +531,7 @@ static bool screen_opengl_render_init(bContext *C, wmOperator *op)
 
        oglrender->ofs = ofs;
        oglrender->ofs_samples = samples;
+       oglrender->ofs_full_samples = full_samples;
        oglrender->sizex = sizex;
        oglrender->sizey = sizey;
        oglrender->bmain = CTX_data_main(C);
index 3b6f88f053838fb39762caeb2e7824ad8701dfca..6ebf965b215e742e4c9f26004999741319e5cd5b 100644 (file)
@@ -5401,7 +5401,7 @@ static int texture_paint_image_from_view_exec(bContext *C, wmOperator *op)
 
        ibuf = ED_view3d_draw_offscreen_imbuf(
                scene, CTX_wm_view3d(C), CTX_wm_region(C),
-               w, h, IB_rect, false, R_ALPHAPREMUL, 0, NULL,
+               w, h, IB_rect, false, R_ALPHAPREMUL, 0, false, NULL,
                NULL, NULL, err_out);
        if (!ibuf) {
                /* Mostly happens when OpenGL offscreen buffer was failed to create, */
index 14861734bfa82e2c662712c8c28682969122c703..ab0d02aab66c9ae9a12932cc42bf498064da58fb 100644 (file)
@@ -48,6 +48,7 @@
 
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
+#include "BLI_jitter.h"
 #include "BLI_utildefines.h"
 #include "BLI_endian_switch.h"
 #include "BLI_threads.h"
@@ -3294,7 +3295,7 @@ void ED_view3d_draw_offscreen(
 ImBuf *ED_view3d_draw_offscreen_imbuf(
         Scene *scene, View3D *v3d, ARegion *ar, int sizex, int sizey,
         unsigned int flag, bool draw_background,
-        int alpha_mode, int samples, const char *viewname,
+        int alpha_mode, int samples, bool full_samples, const char *viewname,
         /* output vars */
         GPUFX *fx, GPUOffScreen *ofs, char err_out[256])
 {
@@ -3305,8 +3306,8 @@ ImBuf *ED_view3d_draw_offscreen_imbuf(
 
        /* view state */
        GPUFXSettings fx_settings = {NULL};
+       bool is_ortho = false;
        float winmat[4][4];
-       bool is_ortho;
 
        if (UNLIKELY(v3d == NULL))
                return NULL;
@@ -3316,7 +3317,7 @@ ImBuf *ED_view3d_draw_offscreen_imbuf(
                glPushAttrib(GL_LIGHTING_BIT);
 
                /* bind */
-               ofs = GPU_offscreen_create(sizex, sizey, samples, err_out);
+               ofs = GPU_offscreen_create(sizex, sizey, full_samples ? 0 : samples, err_out);
                if (ofs == NULL) {
                        glPopAttrib();
                        return NULL;
@@ -3327,6 +3328,9 @@ ImBuf *ED_view3d_draw_offscreen_imbuf(
 
        GPU_offscreen_bind(ofs, true);
 
+       /* read in pixels & stamp */
+       ibuf = IMB_allocImBuf(sizex, sizey, 32, flag);
+
        /* render 3d view */
        if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
                CameraParams params;
@@ -3361,18 +3365,82 @@ ImBuf *ED_view3d_draw_offscreen_imbuf(
                }
        }
 
-       ED_view3d_draw_offscreen(
-               scene, v3d, ar, sizex, sizey, NULL, winmat,
-               draw_background, draw_sky, !is_ortho, viewname,
-               fx, &fx_settings, ofs);
+       if ((samples && full_samples) == 0) {
+               /* Single-pass render, common case */
+               ED_view3d_draw_offscreen(
+                       scene, v3d, ar, sizex, sizey, NULL, winmat,
+                       draw_background, draw_sky, !is_ortho, viewname,
+                       fx, &fx_settings, ofs);
 
-       /* read in pixels & stamp */
-       ibuf = IMB_allocImBuf(sizex, sizey, 32, flag);
+               if (ibuf->rect_float) {
+                       GPU_offscreen_read_pixels(ofs, GL_FLOAT, ibuf->rect_float);
+               }
+               else if (ibuf->rect) {
+                       GPU_offscreen_read_pixels(ofs, GL_UNSIGNED_BYTE, ibuf->rect);
+               }
+       }
+       else {
+               /* Multi-pass render, use accumulation buffer & jitter for 'full' oversampling.
+                * Use becauise OpenGL may use a lower quality MSAA, and only oversample edges. */
+               static float jit_ofs[32][2];
+               float winmat_jitter[4][4];
+               /* use imbuf as temp storage, before writing into it from accumulation buffer */
+               unsigned char *rect_temp = ibuf->rect ? (void *)ibuf->rect : (void *)ibuf->rect_float;
+               unsigned int *accum_buffer = MEM_mallocN(sizex * sizey * sizeof(int[4]), "accum1");
+               unsigned int i;
+               int j;
+
+               BLI_jitter_init(jit_ofs, scene->r.osa);
+
+               /* first sample buffer, also initializes 'rv3d->persmat' */
+               ED_view3d_draw_offscreen(
+                       scene, v3d, ar, sizex, sizey, NULL, winmat,
+                       draw_background, draw_sky, !is_ortho, viewname,
+                       fx, &fx_settings, ofs);
+               GPU_offscreen_read_pixels(ofs, GL_UNSIGNED_BYTE, rect_temp);
+
+               i = sizex * sizey * 4;
+               while (i--) {
+                       accum_buffer[i] = rect_temp[i];
+               }
 
-       if (ibuf->rect_float)
-               GPU_offscreen_read_pixels(ofs, GL_FLOAT, ibuf->rect_float);
-       else if (ibuf->rect)
-               GPU_offscreen_read_pixels(ofs, GL_UNSIGNED_BYTE, ibuf->rect);
+               /* skip the first sample */
+               for (j = 1; j < samples; j++) {
+                       copy_m4_m4(winmat_jitter, winmat);
+                       window_translate_m4(
+                               winmat_jitter, rv3d->persmat,
+                               (jit_ofs[j][0] * 2.0f) / sizex,
+                               (jit_ofs[j][1] * 2.0f) / sizey);
+
+                       ED_view3d_draw_offscreen(
+                               scene, v3d, ar, sizex, sizey, NULL, winmat_jitter,
+                               draw_background, draw_sky, !is_ortho, viewname,
+                               fx, &fx_settings, ofs);
+                       GPU_offscreen_read_pixels(ofs, GL_UNSIGNED_BYTE, rect_temp);
+
+                       i = sizex * sizey * 4;
+                       while (i--) {
+                               accum_buffer[i] += rect_temp[i];
+                       }
+               }
+
+               if (ibuf->rect_float) {
+                       float *rect_float = ibuf->rect_float;
+                       i = sizex * sizey * 4;
+                       while (i--) {
+                               rect_float[i] = (float)(accum_buffer[i] / samples) * (1.0f / 255.0f);
+                       }
+               }
+               else {
+                       unsigned char *rect_ub = (unsigned char *)ibuf->rect;
+                       i = sizex * sizey * 4;
+                       while (i--) {
+                               rect_ub[i] = accum_buffer[i] / samples;
+                       }
+               }
+
+               MEM_freeN(accum_buffer);
+       }
 
        /* unbind */
        GPU_offscreen_unbind(ofs, true);
@@ -3400,7 +3468,7 @@ ImBuf *ED_view3d_draw_offscreen_imbuf(
 ImBuf *ED_view3d_draw_offscreen_imbuf_simple(
         Scene *scene, Object *camera, int width, int height,
         unsigned int flag, int drawtype, bool use_solid_tex, bool use_gpencil, bool draw_background,
-        int alpha_mode, int samples, const char *viewname,
+        int alpha_mode, int samples, bool full_samples, const char *viewname,
         GPUFX *fx, GPUOffScreen *ofs, char err_out[256])
 {
        View3D v3d = {NULL};
@@ -3450,7 +3518,7 @@ ImBuf *ED_view3d_draw_offscreen_imbuf_simple(
 
        return ED_view3d_draw_offscreen_imbuf(
                scene, &v3d, &ar, width, height, flag,
-               draw_background, alpha_mode, samples, viewname,
+               draw_background, alpha_mode, samples, full_samples, viewname,
                fx, ofs, err_out);
 }
 
index 87f8c17ca1fea3bc896a7f0e02e178db3d350059..e87be231025fb5c9f03bd4cd6bad9eb089a1430e 100644 (file)
@@ -935,14 +935,14 @@ static ImBuf *blend_file_thumb(Scene *scene, bScreen *screen, BlendThumbnail **t
                ibuf = ED_view3d_draw_offscreen_imbuf_simple(
                        scene, scene->camera,
                        BLEN_THUMB_SIZE * 2, BLEN_THUMB_SIZE * 2,
-                       IB_rect, OB_SOLID, false, false, false, R_ALPHAPREMUL, 0, NULL,
+                       IB_rect, OB_SOLID, false, false, false, R_ALPHAPREMUL, 0, false, NULL,
                        NULL, NULL, err_out);
        }
        else {
                ibuf = ED_view3d_draw_offscreen_imbuf(
                        scene, v3d, ar,
                        BLEN_THUMB_SIZE * 2, BLEN_THUMB_SIZE * 2,
-                       IB_rect, false, R_ALPHAPREMUL, 0, NULL,
+                       IB_rect, false, R_ALPHAPREMUL, 0, false, NULL,
                        NULL, NULL, err_out);
        }