Use GLSL display for compositor backdrop and sequencer preview
authorSergey Sharybin <sergey.vfx@gmail.com>
Thu, 4 Apr 2013 12:20:13 +0000 (12:20 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Thu, 4 Apr 2013 12:20:13 +0000 (12:20 +0000)
Now only background images remained to be ported. Plus implement
GLSL for dithering and RGB curves.

source/blender/editors/include/BIF_glutil.h
source/blender/editors/screen/glutil.c
source/blender/editors/space_node/drawnode.c
source/blender/editors/space_sequencer/sequencer_draw.c
source/blender/imbuf/IMB_colormanagement.h
source/blender/imbuf/intern/colormanagement.c

index af2dc884508bd11963fe93a4b3be7003645be3e6..dfb02fa9c1b04873e7f0aedb740a3dd88c829a56 100644 (file)
@@ -35,6 +35,8 @@ struct rctf;
 
 struct ImBuf;
 struct bContext;
+struct ColorManagedViewSettings;
+struct ColorManagedDisplaySettings;
 
 void fdrawbezier(float vec[4][3]);
 void fdrawline(float x1, float y1, float x2, float y2);
@@ -212,6 +214,11 @@ void bgl_get_mats(bglMats *mats);
 
 /* **** Color management helper functions for GLSL display/transform ***** */
 
+/* Draw imbuf on a screen, preferably using GLSL display transform */
+void glaDrawImBuf_glsl(struct ImBuf *ibuf, float x, float y, int zoomfilter,
+                       struct ColorManagedViewSettings *view_settings,
+                       struct ColorManagedDisplaySettings *display_settings);
+
 /* Draw imbuf on a screen, preferably using GLSL display transform */
 void glaDrawImBuf_glsl_ctx(const struct bContext *C, struct ImBuf *ibuf, float x, float y, int zoomfilter);
 
index 3469604b3ddb127f3a84de52fde564114f959354..ac8f44a3ab127da75ab46263927467cf0e90981d 100644 (file)
@@ -1028,7 +1028,9 @@ void bglFlush(void)
 /* **** Color management helper functions for GLSL display/transform ***** */
 
 /* Draw given image buffer on a screen using GLSL for display transform */
-void glaDrawImBuf_glsl_ctx(const bContext *C, ImBuf *ibuf, float x, float y, int zoomfilter)
+void glaDrawImBuf_glsl(ImBuf *ibuf, float x, float y, int zoomfilter,
+                       ColorManagedViewSettings *view_settings,
+                       ColorManagedDisplaySettings *display_settings)
 {
        bool force_fallback = false;
        bool need_fallback = true;
@@ -1080,10 +1082,19 @@ void glaDrawImBuf_glsl_ctx(const bContext *C, ImBuf *ibuf, float x, float y, int
        if (force_fallback == false) {
                int ok;
 
-               if (ibuf->rect_float)
-                       ok = IMB_colormanagement_setup_glsl_draw_ctx(C, TRUE);
-               else
-                       ok = IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, ibuf->rect_colorspace, FALSE);
+               if (ibuf->rect_float) {
+                       if (ibuf->float_colorspace) {
+                               ok = IMB_colormanagement_setup_glsl_draw_from_space(view_settings, display_settings,
+                                                                                   ibuf->float_colorspace, TRUE);
+                       }
+                       else {
+                               ok = IMB_colormanagement_setup_glsl_draw(view_settings, display_settings, TRUE);
+                       }
+               }
+               else {
+                       ok = IMB_colormanagement_setup_glsl_draw_from_space(view_settings, display_settings,
+                                                                           ibuf->rect_colorspace, FALSE);
+               }
 
                if (ok) {
                        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
@@ -1121,7 +1132,7 @@ void glaDrawImBuf_glsl_ctx(const bContext *C, ImBuf *ibuf, float x, float y, int
                unsigned char *display_buffer;
                void *cache_handle;
 
-               display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
+               display_buffer = IMB_display_buffer_acquire(ibuf, view_settings, display_settings, &cache_handle);
 
                if (display_buffer)
                        glaDrawPixelsAuto(x, y, ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE,
@@ -1131,6 +1142,16 @@ void glaDrawImBuf_glsl_ctx(const bContext *C, ImBuf *ibuf, float x, float y, int
        }
 }
 
+void glaDrawImBuf_glsl_ctx(const bContext *C, ImBuf *ibuf, float x, float y, int zoomfilter)
+{
+       ColorManagedViewSettings *view_settings;
+       ColorManagedDisplaySettings *display_settings;
+
+       IMB_colormanagement_display_settings_from_ctx(C, &view_settings, &display_settings);
+
+       glaDrawImBuf_glsl(ibuf, x, y, zoomfilter, view_settings, display_settings);
+}
+
 /* Transform buffer from role to scene linear space using GLSL OCIO conversion
  *
  * See IMB_colormanagement_setup_transform_from_role_glsl description for
index d7206c9b1114abe27870a48d4c3127f8710740ea..cd3de834e78cd775a6a84e4ede30e10451032cd3 100644 (file)
@@ -2948,8 +2948,6 @@ void draw_nodespace_back_pix(const bContext *C, ARegion *ar, SpaceNode *snode)
                ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
                if (ibuf) {
                        float x, y; 
-                       unsigned char *display_buffer;
-                       void *cache_handle;
                        
                        glMatrixMode(GL_PROJECTION);
                        glPushMatrix();
@@ -2967,14 +2965,16 @@ void draw_nodespace_back_pix(const bContext *C, ARegion *ar, SpaceNode *snode)
                        
                        x = (ar->winx - snode->zoom * ibuf->x) / 2 + snode->xof;
                        y = (ar->winy - snode->zoom * ibuf->y) / 2 + snode->yof;
-                       
 
-                       display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
+                       if (ibuf->rect || ibuf->rect_float) {
+                               unsigned char *display_buffer = NULL;
+                               void *cache_handle = NULL;
 
-                       if (display_buffer) {
                                if (snode->flag & (SNODE_SHOW_R | SNODE_SHOW_G | SNODE_SHOW_B)) {
                                        int ofs;
 
+                                       display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
+
 #ifdef __BIG_ENDIAN__
                                        if      (snode->flag & SNODE_SHOW_R) ofs = 2;
                                        else if (snode->flag & SNODE_SHOW_G) ofs = 1;
@@ -2994,6 +2994,8 @@ void draw_nodespace_back_pix(const bContext *C, ARegion *ar, SpaceNode *snode)
                                        glPixelZoom(1.0f, 1.0f);
                                }
                                else if (snode->flag & SNODE_SHOW_ALPHA) {
+                                       display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
+
                                        glPixelZoom(snode->zoom, snode->zoom);
                                        /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
 #ifdef __BIG_ENDIAN__
@@ -3011,7 +3013,7 @@ void draw_nodespace_back_pix(const bContext *C, ARegion *ar, SpaceNode *snode)
                                        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                                        glPixelZoom(snode->zoom, snode->zoom);
                                        
-                                       glaDrawPixelsAuto(x, y, ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, display_buffer);
+                                       glaDrawImBuf_glsl_ctx(C, ibuf, x, y, GL_NEAREST);
                                        
                                        glPixelZoom(1.0f, 1.0f);
                                        glDisable(GL_BLEND);
@@ -3019,13 +3021,14 @@ void draw_nodespace_back_pix(const bContext *C, ARegion *ar, SpaceNode *snode)
                                else {
                                        glPixelZoom(snode->zoom, snode->zoom);
 
-                                       glaDrawPixelsAuto(x, y, ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, display_buffer);
+                                       glaDrawImBuf_glsl_ctx(C, ibuf, x, y, GL_NEAREST);
                                        
                                        glPixelZoom(1.0f, 1.0f);
                                }
-                       }
 
-                       IMB_display_buffer_release(cache_handle);
+                               if (cache_handle)
+                                       IMB_display_buffer_release(cache_handle);
+                       }
 
                        /** @note draw selected info on backdrop */
                        if (snode->edittree) {
index 141af5d5ba4b89865ac60878736b256175ed9127..3a7610cbf2a2310f3e38f14a40e73e08903c9292 100644 (file)
@@ -920,9 +920,11 @@ void draw_image_seq(const bContext *C, Scene *scene, ARegion *ar, SpaceSeq *sseq
        float col[3];
        GLuint texid;
        GLuint last_texid;
-       unsigned char *display_buffer;
+       void *display_buffer;
        void *cache_handle = NULL;
        const int is_imbuf = ED_space_sequencer_check_show_imbuf(sseq);
+       int format, type;
+       bool glsl_used = false;
 
        if (G.is_rendering == FALSE && (scene->r.seq_flag & R_SEQ_GL_PREV) == 0) {
                /* stop all running jobs, except screen one. currently previews frustrate Render
@@ -1040,9 +1042,46 @@ void draw_image_seq(const bContext *C, Scene *scene, ARegion *ar, SpaceSeq *sseq
                }
 
                display_buffer = (unsigned char *)ibuf->rect;
+               format = GL_RGBA;
+               type = GL_UNSIGNED_BYTE;
        }
        else {
-               display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
+               if (ibuf->rect_float) {
+                       display_buffer = ibuf->rect_float;
+
+                       if (ibuf->channels == 4) {
+                               format = GL_RGBA;
+                       }
+                       else if (ibuf->channels == 3) {
+                               format = GL_RGB;
+                       }
+                       else {
+                               BLI_assert(!"Incompatible number of channels for float buffer in sequencer");
+                               format = GL_RGBA;
+                               display_buffer = NULL;
+                       }
+
+                       type = GL_FLOAT;
+
+                       if (ibuf->float_colorspace) {
+                               glsl_used = IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, ibuf->float_colorspace, TRUE);
+                       }
+                       else {
+                               glsl_used = IMB_colormanagement_setup_glsl_draw_ctx(C, TRUE);
+                       }
+               }
+               else if (ibuf->rect) {
+                       display_buffer = ibuf->rect;
+                       format = GL_RGBA;
+                       type = GL_UNSIGNED_BYTE;
+
+                       glsl_used = IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, ibuf->rect_colorspace, FALSE);
+               }
+               else {
+                       format = GL_RGBA;
+                       type = GL_UNSIGNED_BYTE;
+                       display_buffer = NULL;
+               }
        }
 
        /* setting up the view - actual drawing starts here */
@@ -1068,7 +1107,7 @@ void draw_image_seq(const bContext *C, Scene *scene, ARegion *ar, SpaceSeq *sseq
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
-       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, ibuf->x, ibuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, display_buffer);
+       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, ibuf->x, ibuf->y, 0, format, type, display_buffer);
 
        glBegin(GL_QUADS);
 
@@ -1105,6 +1144,9 @@ void draw_image_seq(const bContext *C, Scene *scene, ARegion *ar, SpaceSeq *sseq
                glDisable(GL_BLEND);
        glDeleteTextures(1, &texid);
 
+       if (glsl_used)
+               IMB_colormanagement_finish_glsl_draw();
+
        if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
 
                float x1 = v2d->tot.xmin;
index 58b55b0f137880a328e636204e0785fdc6b878bc..314daed00dd5e7a98fc940cd7449a3259b7ee27a 100644 (file)
@@ -95,6 +95,10 @@ struct ImBuf *IMB_colormanagement_imbuf_for_write(struct ImBuf *ibuf, int save_a
 
 /* ** Public display buffers interfaces ** */
 
+void IMB_colormanagement_display_settings_from_ctx(const struct bContext *C,
+                                                   struct ColorManagedViewSettings **view_settings_r,
+                                                   struct ColorManagedDisplaySettings **display_settings_r);
+
 unsigned char *IMB_display_buffer_acquire(struct ImBuf *ibuf, const struct ColorManagedViewSettings *view_settings,
                                           const struct ColorManagedDisplaySettings *display_settings, void **cache_handle);
 unsigned char *IMB_display_buffer_acquire_ctx(const struct bContext *C, struct ImBuf *ibuf, void **cache_handle);
index a74f28e79c9ca490a16278dc1110d4a81ae4008c..b8de1cf75d198c7d6cfa1e60185b4882adeeda28 100644 (file)
@@ -662,8 +662,9 @@ void colormanage_cache_free(ImBuf *ibuf)
        }
 }
 
-static void display_transform_get_from_ctx(const bContext *C, ColorManagedViewSettings **view_settings_r,
-                                           ColorManagedDisplaySettings **display_settings_r)
+void IMB_colormanagement_display_settings_from_ctx(const bContext *C,
+                                                   ColorManagedViewSettings **view_settings_r,
+                                                   ColorManagedDisplaySettings **display_settings_r)
 {
        Scene *scene = CTX_data_scene(C);
        SpaceImage *sima = CTX_wm_space_image(C);
@@ -1963,7 +1964,7 @@ unsigned char *IMB_display_buffer_acquire_ctx(const bContext *C, ImBuf *ibuf, vo
        ColorManagedViewSettings *view_settings;
        ColorManagedDisplaySettings *display_settings;
 
-       display_transform_get_from_ctx(C, &view_settings, &display_settings);
+       IMB_colormanagement_display_settings_from_ctx(C, &view_settings, &display_settings);
 
        return IMB_display_buffer_acquire(ibuf, view_settings, display_settings, cache_handle);
 }
@@ -2820,7 +2821,7 @@ int IMB_colormanagement_setup_glsl_draw_from_space_ctx(const struct bContext *C,
        ColorManagedViewSettings *view_settings;
        ColorManagedDisplaySettings *display_settings;
 
-       display_transform_get_from_ctx(C, &view_settings, &display_settings);
+       IMB_colormanagement_display_settings_from_ctx(C, &view_settings, &display_settings);
 
        return IMB_colormanagement_setup_glsl_draw_from_space(view_settings, display_settings, from_colorspace, predivide);
 }