1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2016, Blender Foundation.
17  */
18 
19 /** \file
20  * \ingroup draw
21  */
22 
23 #include "draw_manager.h"
24 
25 #ifndef NDEBUG
26 /* Maybe gpu_texture.c is a better place for this. */
drw_texture_format_supports_framebuffer(eGPUTextureFormat format)27 static bool drw_texture_format_supports_framebuffer(eGPUTextureFormat format)
28 {
29   /* Some formats do not work with framebuffers. */
30   switch (format) {
31     /* Only add formats that are COMPATIBLE with FB.
32      * Generally they are multiple of 16bit. */
33     case GPU_R8:
34     case GPU_R8UI:
35     case GPU_R16F:
36     case GPU_R16I:
37     case GPU_R16UI:
38     case GPU_R16:
39     case GPU_R32F:
40     case GPU_R32UI:
41     case GPU_RG8:
42     case GPU_RG16:
43     case GPU_RG16F:
44     case GPU_RG16I:
45     case GPU_RG32F:
46     case GPU_R11F_G11F_B10F:
47     case GPU_RGBA8:
48     case GPU_RGBA16:
49     case GPU_RGBA16F:
50     case GPU_RGBA32F:
51     case GPU_DEPTH_COMPONENT16:
52     case GPU_DEPTH_COMPONENT24:
53     case GPU_DEPTH24_STENCIL8:
54     case GPU_DEPTH_COMPONENT32F:
55       return true;
56     default:
57       return false;
58   }
59 }
60 #endif
61 
drw_texture_set_parameters(GPUTexture * tex,DRWTextureFlag flags)62 void drw_texture_set_parameters(GPUTexture *tex, DRWTextureFlag flags)
63 {
64   if (tex == NULL) {
65     return;
66   }
67 
68   if (flags & DRW_TEX_MIPMAP) {
69     GPU_texture_mipmap_mode(tex, true, flags & DRW_TEX_FILTER);
70     GPU_texture_generate_mipmap(tex);
71   }
72   else {
73     GPU_texture_filter_mode(tex, flags & DRW_TEX_FILTER);
74   }
75   GPU_texture_anisotropic_filter(tex, false);
76   GPU_texture_wrap_mode(tex, flags & DRW_TEX_WRAP, true);
77   GPU_texture_compare_mode(tex, flags & DRW_TEX_COMPARE);
78 }
79 
DRW_texture_create_1d(int w,eGPUTextureFormat format,DRWTextureFlag flags,const float * fpixels)80 GPUTexture *DRW_texture_create_1d(int w,
81                                   eGPUTextureFormat format,
82                                   DRWTextureFlag flags,
83                                   const float *fpixels)
84 {
85   int mips = (flags & DRW_TEX_MIPMAP) ? 9999 : 1;
86   GPUTexture *tex = GPU_texture_create_1d(__func__, w, mips, format, fpixels);
87   drw_texture_set_parameters(tex, flags);
88 
89   return tex;
90 }
91 
DRW_texture_create_2d(int w,int h,eGPUTextureFormat format,DRWTextureFlag flags,const float * fpixels)92 GPUTexture *DRW_texture_create_2d(
93     int w, int h, eGPUTextureFormat format, DRWTextureFlag flags, const float *fpixels)
94 {
95   int mips = (flags & DRW_TEX_MIPMAP) ? 9999 : 1;
96   GPUTexture *tex = GPU_texture_create_2d(__func__, w, h, mips, format, fpixels);
97   drw_texture_set_parameters(tex, flags);
98 
99   return tex;
100 }
101 
DRW_texture_create_2d_array(int w,int h,int d,eGPUTextureFormat format,DRWTextureFlag flags,const float * fpixels)102 GPUTexture *DRW_texture_create_2d_array(
103     int w, int h, int d, eGPUTextureFormat format, DRWTextureFlag flags, const float *fpixels)
104 {
105   int mips = (flags & DRW_TEX_MIPMAP) ? 9999 : 1;
106   GPUTexture *tex = GPU_texture_create_2d_array(__func__, w, h, d, mips, format, fpixels);
107   drw_texture_set_parameters(tex, flags);
108 
109   return tex;
110 }
111 
DRW_texture_create_3d(int w,int h,int d,eGPUTextureFormat format,DRWTextureFlag flags,const float * fpixels)112 GPUTexture *DRW_texture_create_3d(
113     int w, int h, int d, eGPUTextureFormat format, DRWTextureFlag flags, const float *fpixels)
114 {
115   int mips = (flags & DRW_TEX_MIPMAP) ? 9999 : 1;
116   GPUTexture *tex = GPU_texture_create_3d(
117       __func__, w, h, d, mips, format, GPU_DATA_FLOAT, fpixels);
118   drw_texture_set_parameters(tex, flags);
119 
120   return tex;
121 }
122 
DRW_texture_create_cube(int w,eGPUTextureFormat format,DRWTextureFlag flags,const float * fpixels)123 GPUTexture *DRW_texture_create_cube(int w,
124                                     eGPUTextureFormat format,
125                                     DRWTextureFlag flags,
126                                     const float *fpixels)
127 {
128   int mips = (flags & DRW_TEX_MIPMAP) ? 9999 : 1;
129   GPUTexture *tex = GPU_texture_create_cube(__func__, w, mips, format, fpixels);
130   drw_texture_set_parameters(tex, flags);
131   return tex;
132 }
133 
DRW_texture_create_cube_array(int w,int d,eGPUTextureFormat format,DRWTextureFlag flags,const float * fpixels)134 GPUTexture *DRW_texture_create_cube_array(
135     int w, int d, eGPUTextureFormat format, DRWTextureFlag flags, const float *fpixels)
136 {
137   int mips = (flags & DRW_TEX_MIPMAP) ? 9999 : 1;
138   GPUTexture *tex = GPU_texture_create_cube_array(__func__, w, d, mips, format, fpixels);
139   drw_texture_set_parameters(tex, flags);
140   return tex;
141 }
142 
DRW_texture_pool_query_2d(int w,int h,eGPUTextureFormat format,DrawEngineType * engine_type)143 GPUTexture *DRW_texture_pool_query_2d(int w,
144                                       int h,
145                                       eGPUTextureFormat format,
146                                       DrawEngineType *engine_type)
147 {
148   BLI_assert(drw_texture_format_supports_framebuffer(format));
149   GPUTexture *tex = GPU_viewport_texture_pool_query(DST.viewport, engine_type, w, h, format);
150 
151   return tex;
152 }
153 
DRW_texture_pool_query_fullscreen(eGPUTextureFormat format,DrawEngineType * engine_type)154 GPUTexture *DRW_texture_pool_query_fullscreen(eGPUTextureFormat format,
155                                               DrawEngineType *engine_type)
156 {
157   const float *size = DRW_viewport_size_get();
158   return DRW_texture_pool_query_2d((int)size[0], (int)size[1], format, engine_type);
159 }
160 
DRW_texture_ensure_fullscreen_2d(GPUTexture ** tex,eGPUTextureFormat format,DRWTextureFlag flags)161 void DRW_texture_ensure_fullscreen_2d(GPUTexture **tex,
162                                       eGPUTextureFormat format,
163                                       DRWTextureFlag flags)
164 {
165   if (*(tex) == NULL) {
166     const float *size = DRW_viewport_size_get();
167     *(tex) = DRW_texture_create_2d((int)size[0], (int)size[1], format, flags, NULL);
168   }
169 }
170 
DRW_texture_ensure_2d(GPUTexture ** tex,int w,int h,eGPUTextureFormat format,DRWTextureFlag flags)171 void DRW_texture_ensure_2d(
172     GPUTexture **tex, int w, int h, eGPUTextureFormat format, DRWTextureFlag flags)
173 {
174   if (*(tex) == NULL) {
175     *(tex) = DRW_texture_create_2d(w, h, format, flags, NULL);
176   }
177 }
178 
DRW_texture_generate_mipmaps(GPUTexture * tex)179 void DRW_texture_generate_mipmaps(GPUTexture *tex)
180 {
181   GPU_texture_generate_mipmap(tex);
182 }
183 
DRW_texture_free(GPUTexture * tex)184 void DRW_texture_free(GPUTexture *tex)
185 {
186   GPU_texture_free(tex);
187 }
188