From eb7188802daf5909351d8a3c01b68303b655c1bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Mon, 30 Apr 2018 16:02:24 +0200 Subject: DRW: Remove DRWTextureFormat in favor or GPUTextureFormat. Because: - Less redundancy. - Better suffixes. Also a few modification to GPU_texture_create_* to simplify the API: - make the format explicit to the texture creation process. - remove the component count as it's specified in the GPUTextureFormat. --- source/blender/gpu/GPU_texture.h | 32 +++----- source/blender/gpu/GPU_viewport.h | 2 +- source/blender/gpu/intern/gpu_codegen.c | 2 +- source/blender/gpu/intern/gpu_draw.c | 14 ++-- source/blender/gpu/intern/gpu_framebuffer.c | 4 +- source/blender/gpu/intern/gpu_material.c | 2 +- source/blender/gpu/intern/gpu_texture.c | 116 ++++++++++++---------------- source/blender/gpu/intern/gpu_viewport.c | 12 +-- 8 files changed, 79 insertions(+), 105 deletions(-) (limited to 'source/blender/gpu') diff --git a/source/blender/gpu/GPU_texture.h b/source/blender/gpu/GPU_texture.h index a783a69ae6b..f6fe4fe28e5 100644 --- a/source/blender/gpu/GPU_texture.h +++ b/source/blender/gpu/GPU_texture.h @@ -145,26 +145,18 @@ typedef enum GPUTextureFormat { unsigned int GPU_texture_memory_usage_get(void); -GPUTexture *GPU_texture_create_1D(int w, const float *pixels, char err_out[256]); -GPUTexture *GPU_texture_create_1D_custom( - int w, int channels, GPUTextureFormat data_type, const float *pixels, char err_out[256]); -GPUTexture *GPU_texture_create_2D(int w, int h, const float *pixels, char err_out[256]); -GPUTexture *GPU_texture_create_2D_custom( - int w, int h, int channels, GPUTextureFormat data_type, const float *pixels, char err_out[256]); -GPUTexture *GPU_texture_create_2D_multisample(int w, int h, const float *pixels, int samples, char err_out[256]); -GPUTexture *GPU_texture_create_2D_custom_multisample( - int w, int h, int channels, GPUTextureFormat data_type, const float *pixels, int samples, char err_out[256]); -GPUTexture *GPU_texture_create_2D_array_custom( - int w, int h, int d, int channels, GPUTextureFormat data_type, const float *pixels, char err_out[256]); -GPUTexture *GPU_texture_create_3D(int w, int h, int d, const float *pixels, char err_out[256]); -GPUTexture *GPU_texture_create_3D_custom( - int w, int h, int d, int channels, GPUTextureFormat data_type, const float *pixels, char err_out[256]); -GPUTexture *GPU_texture_create_cube_custom( - int w, int channels, GPUTextureFormat data_type, const float *pixels, char err_out[256]); -GPUTexture *GPU_texture_create_depth(int w, int h, char err_out[256]); -GPUTexture *GPU_texture_create_depth_with_stencil(int w, int h, char err_out[256]); -GPUTexture *GPU_texture_create_depth_multisample(int w, int h, int samples, char err_out[256]); -GPUTexture *GPU_texture_create_depth_with_stencil_multisample(int w, int h, int samples, char err_out[256]); +GPUTexture *GPU_texture_create_1D( + int w, GPUTextureFormat data_type, const float *pixels, char err_out[256]); +GPUTexture *GPU_texture_create_2D( + int w, int h, GPUTextureFormat data_type, const float *pixels, char err_out[256]); +GPUTexture *GPU_texture_create_2D_multisample( + int w, int h, GPUTextureFormat data_type, const float *pixels, int samples, char err_out[256]); +GPUTexture *GPU_texture_create_2D_array( + int w, int h, int d, GPUTextureFormat data_type, const float *pixels, char err_out[256]); +GPUTexture *GPU_texture_create_3D( + int w, int h, int d, GPUTextureFormat data_type, const float *pixels, char err_out[256]); +GPUTexture *GPU_texture_create_cube( + int w, GPUTextureFormat data_type, const float *pixels, char err_out[256]); GPUTexture *GPU_texture_from_blender( struct Image *ima, struct ImageUser *iuser, int textarget, bool is_data, double time, int mipmap); diff --git a/source/blender/gpu/GPU_viewport.h b/source/blender/gpu/GPU_viewport.h index d63911374ae..921cd0e7369 100644 --- a/source/blender/gpu/GPU_viewport.h +++ b/source/blender/gpu/GPU_viewport.h @@ -122,7 +122,7 @@ bool GPU_viewport_do_update(GPUViewport *viewport); GPUTexture *GPU_viewport_color_texture(GPUViewport *viewport); /* Texture pool */ -GPUTexture *GPU_viewport_texture_pool_query(GPUViewport *viewport, void *engine, int width, int height, int channels, int format); +GPUTexture *GPU_viewport_texture_pool_query(GPUViewport *viewport, void *engine, int width, int height, int format); bool GPU_viewport_engines_data_validate(GPUViewport *viewport, unsigned int hash); void GPU_viewport_cache_release(GPUViewport *viewport); diff --git a/source/blender/gpu/intern/gpu_codegen.c b/source/blender/gpu/intern/gpu_codegen.c index da69d9cdb16..d66aaf0a33d 100644 --- a/source/blender/gpu/intern/gpu_codegen.c +++ b/source/blender/gpu/intern/gpu_codegen.c @@ -1272,7 +1272,7 @@ static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const GPUType #if 0 input->tex = GPU_texture_create_2D(link->texturesize, link->texturesize, link->ptr2, NULL); #endif - input->tex = GPU_texture_create_2D(link->texturesize, 1, link->ptr1, NULL); + input->tex = GPU_texture_create_2D(link->texturesize, 1, GPU_RGBA8, link->ptr1, NULL); input->textarget = GL_TEXTURE_2D; MEM_freeN(link->ptr1); diff --git a/source/blender/gpu/intern/gpu_draw.c b/source/blender/gpu/intern/gpu_draw.c index 8b23cf6f936..7bfebb702a1 100644 --- a/source/blender/gpu/intern/gpu_draw.c +++ b/source/blender/gpu/intern/gpu_draw.c @@ -914,12 +914,12 @@ void GPU_create_smoke(SmokeModifierData *smd, int highres) if (smoke_has_colors(sds->fluid)) { float *data = MEM_callocN(sizeof(float) * sds->total_cells * 4, "smokeColorTexture"); smoke_get_rgba(sds->fluid, data, 0); - sds->tex = GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], data, NULL); + sds->tex = GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], GPU_RGBA8, data, NULL); MEM_freeN(data); } /* density only */ else { - sds->tex = GPU_texture_create_3D_custom(sds->res[0], sds->res[1], sds->res[2], 1, + sds->tex = GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], GPU_R8, smoke_get_density(sds->fluid), NULL); /* Swizzle the RGBA components to read the Red channel so @@ -933,7 +933,7 @@ void GPU_create_smoke(SmokeModifierData *smd, int highres) GPU_texture_unbind(sds->tex); } sds->tex_flame = (smoke_has_fuel(sds->fluid)) ? - GPU_texture_create_3D_custom(sds->res[0], sds->res[1], sds->res[2], 1, + GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], GPU_R8, smoke_get_flame(sds->fluid), NULL) : NULL; } @@ -942,12 +942,12 @@ void GPU_create_smoke(SmokeModifierData *smd, int highres) if (smoke_turbulence_has_colors(sds->wt)) { float *data = MEM_callocN(sizeof(float) * smoke_turbulence_get_cells(sds->wt) * 4, "smokeColorTexture"); smoke_turbulence_get_rgba(sds->wt, data, 0); - sds->tex = GPU_texture_create_3D(sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], data, NULL); + sds->tex = GPU_texture_create_3D(sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], GPU_RGBA8, data, NULL); MEM_freeN(data); } /* density only */ else { - sds->tex = GPU_texture_create_3D_custom(sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], 1, + sds->tex = GPU_texture_create_3D(sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], GPU_R8, smoke_turbulence_get_density(sds->wt), NULL); /* Swizzle the RGBA components to read the Red channel so @@ -961,12 +961,12 @@ void GPU_create_smoke(SmokeModifierData *smd, int highres) GPU_texture_unbind(sds->tex); } sds->tex_flame = (smoke_turbulence_has_fuel(sds->wt)) ? - GPU_texture_create_3D_custom(sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], 1, + GPU_texture_create_3D(sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], GPU_R8, smoke_turbulence_get_flame(sds->wt), NULL) : NULL; } - sds->tex_shadow = GPU_texture_create_3D_custom(sds->res[0], sds->res[1], sds->res[2], 1, + sds->tex_shadow = GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], GPU_R8, sds->shadow, NULL); } #else // WITH_SMOKE diff --git a/source/blender/gpu/intern/gpu_framebuffer.c b/source/blender/gpu/intern/gpu_framebuffer.c index 3f89adfd8a5..75576c35f51 100644 --- a/source/blender/gpu/intern/gpu_framebuffer.c +++ b/source/blender/gpu/intern/gpu_framebuffer.c @@ -657,11 +657,11 @@ GPUOffScreen *GPU_offscreen_create(int width, int height, int samples, bool dept ofs = MEM_callocN(sizeof(GPUOffScreen), "GPUOffScreen"); - ofs->color = GPU_texture_create_2D_custom_multisample(width, height, 4, + ofs->color = GPU_texture_create_2D_multisample(width, height, (high_bitdepth) ? GPU_RGBA16F : GPU_RGBA8, NULL, samples, err_out); if (depth) { - ofs->depth = GPU_texture_create_depth_with_stencil_multisample(width, height, samples, err_out); + ofs->depth = GPU_texture_create_2D_multisample(width, height, GPU_DEPTH24_STENCIL8, NULL, samples, err_out); } if ((depth && !ofs->depth) || !ofs->color) { diff --git a/source/blender/gpu/intern/gpu_material.c b/source/blender/gpu/intern/gpu_material.c index d5f4f3269ed..c346d20632b 100644 --- a/source/blender/gpu/intern/gpu_material.c +++ b/source/blender/gpu/intern/gpu_material.c @@ -523,7 +523,7 @@ struct GPUUniformBuffer *GPU_material_sss_profile_get(GPUMaterial *material, int GPU_texture_free(material->sss_tex_profile); } - material->sss_tex_profile = GPU_texture_create_1D_custom(64, 4, GPU_RGBA16F, translucence_profile, NULL); + material->sss_tex_profile = GPU_texture_create_1D(64, GPU_RGBA16F, translucence_profile, NULL); MEM_freeN(translucence_profile); diff --git a/source/blender/gpu/intern/gpu_texture.c b/source/blender/gpu/intern/gpu_texture.c index bbb26756bf1..3f2fc52b4d6 100644 --- a/source/blender/gpu/intern/gpu_texture.c +++ b/source/blender/gpu/intern/gpu_texture.c @@ -252,6 +252,26 @@ static GLenum gpu_texture_get_format( } } +static int gpu_texture_get_component_count(GPUTextureFormat format) +{ + switch (format) { + case GPU_RGBA8: + case GPU_RGBA16F: + case GPU_RGBA32F: + return 4; + case GPU_RGB16F: + case GPU_R11F_G11F_B10F: + return 3; + case GPU_RG8: + case GPU_RG16F: + case GPU_RG16I: + case GPU_RG32F: + return 2; + default: + return 1; + } +} + static float *GPU_texture_3D_rescale(GPUTexture *tex, int w, int h, int d, int channels, const float *fpixels) { const unsigned int xf = w / tex->w, yf = h / tex->h, zf = d / tex->d; @@ -354,7 +374,7 @@ static bool gpu_texture_try_alloc( static GPUTexture *GPU_texture_create_nD( int w, int h, int d, int n, const float *fpixels, - GPUTextureFormat data_type, int components, int samples, + GPUTextureFormat data_type, int samples, const bool can_rescale, char err_out[256]) { if (samples) { @@ -369,7 +389,7 @@ static GPUTexture *GPU_texture_create_nD( tex->number = -1; tex->refcount = 1; tex->format = data_type; - tex->components = components; + tex->components = gpu_texture_get_component_count(data_type); tex->format_flag = 0; if (n == 2) { @@ -397,7 +417,7 @@ static GPUTexture *GPU_texture_create_nD( tex->target = GL_TEXTURE_2D_MULTISAMPLE; GLenum format, internalformat, data_format; - internalformat = gpu_texture_get_format(components, data_type, &format, &data_format, + internalformat = gpu_texture_get_format(tex->components, data_type, &format, &data_format, &tex->format_flag, &tex->bytesize); gpu_texture_memory_footprint_add(tex); @@ -434,7 +454,7 @@ static GPUTexture *GPU_texture_create_nD( } float *rescaled_fpixels = NULL; - bool valid = gpu_texture_try_alloc(tex, proxy, internalformat, format, data_format, components, can_rescale, + bool valid = gpu_texture_try_alloc(tex, proxy, internalformat, format, data_format, tex->components, can_rescale, fpixels, &rescaled_fpixels); if (!valid) { if (err_out) @@ -506,7 +526,7 @@ static GPUTexture *GPU_texture_cube_create( int w, int d, const float *fpixels_px, const float *fpixels_py, const float *fpixels_pz, const float *fpixels_nx, const float *fpixels_ny, const float *fpixels_nz, - GPUTextureFormat data_type, int components, + GPUTextureFormat data_type, char err_out[256]) { GLenum format, internalformat, data_format; @@ -519,7 +539,7 @@ static GPUTexture *GPU_texture_cube_create( tex->number = -1; tex->refcount = 1; tex->format = data_type; - tex->components = components; + tex->components = gpu_texture_get_component_count(data_type); tex->format_flag = GPU_FORMAT_CUBE; if (d == 0) { @@ -530,7 +550,7 @@ static GPUTexture *GPU_texture_cube_create( // tex->target_base = tex->target = GL_TEXTURE_CUBE_MAP_ARRAY; } - internalformat = gpu_texture_get_format(components, data_type, &format, &data_format, + internalformat = gpu_texture_get_format(tex->components, data_type, &format, &data_format, &tex->format_flag, &tex->bytesize); gpu_texture_memory_footprint_add(tex); @@ -692,59 +712,41 @@ GPUTexture *GPU_texture_from_preview(PreviewImage *prv, int mipmap) } -GPUTexture *GPU_texture_create_1D(int w, const float *pixels, char err_out[256]) +GPUTexture *GPU_texture_create_1D( + int w, GPUTextureFormat data_type, const float *pixels, char err_out[256]) { - return GPU_texture_create_nD(w, 0, 0, 1, pixels, GPU_RGBA8, 4, 0, false, err_out); + return GPU_texture_create_nD(w, 0, 0, 1, pixels, data_type, 0, false, err_out); } -GPUTexture *GPU_texture_create_1D_custom( - int w, int channels, GPUTextureFormat data_type, const float *pixels, char err_out[256]) +GPUTexture *GPU_texture_create_2D( + int w, int h, GPUTextureFormat data_type, const float *pixels, char err_out[256]) { - return GPU_texture_create_nD(w, 0, 0, 1, pixels, data_type, channels, 0, false, err_out); + return GPU_texture_create_nD(w, h, 0, 2, pixels, data_type, 0, false, err_out); } -GPUTexture *GPU_texture_create_2D(int w, int h, const float *pixels, char err_out[256]) +GPUTexture *GPU_texture_create_2D_multisample( + int w, int h, GPUTextureFormat data_type, const float *pixels, int samples, char err_out[256]) { - return GPU_texture_create_nD(w, h, 0, 2, pixels, GPU_RGBA8, 4, 0, false, err_out); + return GPU_texture_create_nD(w, h, 0, 2, pixels, data_type, samples, false, err_out); } -GPUTexture *GPU_texture_create_2D_custom( - int w, int h, int channels, GPUTextureFormat data_type, const float *pixels, char err_out[256]) +GPUTexture *GPU_texture_create_2D_array( + int w, int h, int d, GPUTextureFormat data_type, const float *pixels, char err_out[256]) { - return GPU_texture_create_nD(w, h, 0, 2, pixels, data_type, channels, 0, false, err_out); + return GPU_texture_create_nD(w, h, d, 2, pixels, data_type, 0, false, err_out); } -GPUTexture *GPU_texture_create_2D_multisample(int w, int h, const float *pixels, int samples, char err_out[256]) +GPUTexture *GPU_texture_create_3D( + int w, int h, int d, GPUTextureFormat data_type, const float *pixels, char err_out[256]) { - return GPU_texture_create_nD(w, h, 0, 2, pixels, GPU_RGBA8, 4, samples, false, err_out); + return GPU_texture_create_nD(w, h, d, 3, pixels, data_type, 0, true, err_out); } -GPUTexture *GPU_texture_create_2D_custom_multisample( - int w, int h, int channels, GPUTextureFormat data_type, const float *pixels, int samples, char err_out[256]) -{ - return GPU_texture_create_nD(w, h, 0, 2, pixels, data_type, channels, samples, false, err_out); -} - -GPUTexture *GPU_texture_create_2D_array_custom( - int w, int h, int d, int channels, GPUTextureFormat data_type, const float *pixels, char err_out[256]) -{ - return GPU_texture_create_nD(w, h, d, 2, pixels, data_type, channels, 0, false, err_out); -} - -GPUTexture *GPU_texture_create_3D(int w, int h, int d, const float *pixels, char err_out[256]) -{ - return GPU_texture_create_nD(w, h, d, 3, pixels, GPU_RGBA8, 4, 0, true, err_out); -} - -GPUTexture *GPU_texture_create_3D_custom( - int w, int h, int d, int channels, GPUTextureFormat data_type, const float *pixels, char err_out[256]) -{ - return GPU_texture_create_nD(w, h, d, 3, pixels, data_type, channels, 0, true, err_out); -} -GPUTexture *GPU_texture_create_cube_custom( - int w, int channels, GPUTextureFormat data_type, const float *fpixels, char err_out[256]) +GPUTexture *GPU_texture_create_cube( + int w, GPUTextureFormat data_type, const float *fpixels, char err_out[256]) { const float *fpixels_px, *fpixels_py, *fpixels_pz, *fpixels_nx, *fpixels_ny, *fpixels_nz; + const int channels = gpu_texture_get_component_count(data_type); if (fpixels) { fpixels_px = fpixels + 0 * w * w * channels; @@ -759,27 +761,7 @@ GPUTexture *GPU_texture_create_cube_custom( } return GPU_texture_cube_create(w, 0, fpixels_px, fpixels_py, fpixels_pz, fpixels_nx, fpixels_ny, fpixels_nz, - data_type, channels, err_out); -} - -GPUTexture *GPU_texture_create_depth(int w, int h, char err_out[256]) -{ - return GPU_texture_create_nD(w, h, 0, 2, NULL, GPU_DEPTH_COMPONENT24, 1, 0, false, err_out); -} - -GPUTexture *GPU_texture_create_depth_with_stencil(int w, int h, char err_out[256]) -{ - return GPU_texture_create_nD(w, h, 0, 2, NULL, GPU_DEPTH24_STENCIL8, 1, 0, false, err_out); -} - -GPUTexture *GPU_texture_create_depth_multisample(int w, int h, int samples, char err_out[256]) -{ - return GPU_texture_create_nD(w, h, 0, 2, NULL, GPU_DEPTH_COMPONENT24, 1, samples, false, err_out); -} - -GPUTexture *GPU_texture_create_depth_with_stencil_multisample(int w, int h, int samples, char err_out[256]) -{ - return GPU_texture_create_nD(w, h, 0, 2, NULL, GPU_DEPTH24_STENCIL8, 1, samples, false, err_out); + data_type, err_out); } void GPU_texture_update(GPUTexture *tex, const float *pixels) @@ -817,9 +799,9 @@ void GPU_invalid_tex_init(void) { memory_usage = 0; const float color[4] = {1.0f, 0.0f, 1.0f, 1.0f}; - GG.invalid_tex_1D = GPU_texture_create_1D(1, color, NULL); - GG.invalid_tex_2D = GPU_texture_create_2D(1, 1, color, NULL); - GG.invalid_tex_3D = GPU_texture_create_3D(1, 1, 1, color, NULL); + GG.invalid_tex_1D = GPU_texture_create_1D(1, GPU_RGBA8, color, NULL); + GG.invalid_tex_2D = GPU_texture_create_2D(1, 1, GPU_RGBA8, color, NULL); + GG.invalid_tex_3D = GPU_texture_create_3D(1, 1, 1, GPU_RGBA8, color, NULL); } void GPU_invalid_tex_bind(int mode) diff --git a/source/blender/gpu/intern/gpu_viewport.c b/source/blender/gpu/intern/gpu_viewport.c index dc898c85773..0bf215f31a8 100644 --- a/source/blender/gpu/intern/gpu_viewport.c +++ b/source/blender/gpu/intern/gpu_viewport.c @@ -291,7 +291,7 @@ double *GPU_viewport_cache_time_get(GPUViewport *viewport) * Try to find a texture coresponding to params into the texture pool. * If no texture was found, create one and add it to the pool. */ -GPUTexture *GPU_viewport_texture_pool_query(GPUViewport *viewport, void *engine, int width, int height, int channels, int format) +GPUTexture *GPU_viewport_texture_pool_query(GPUViewport *viewport, void *engine, int width, int height, int format) { GPUTexture *tex; @@ -314,7 +314,7 @@ GPUTexture *GPU_viewport_texture_pool_query(GPUViewport *viewport, void *engine, } } - tex = GPU_texture_create_2D_custom(width, height, channels, format, NULL, NULL); + tex = GPU_texture_create_2D(width, height, format, NULL, NULL); GPU_texture_bind(tex, 0); /* Doing filtering for depth does not make sense when not doing shadow mapping, * and enabling texture filtering on integer texture make them unreadable. */ @@ -391,8 +391,8 @@ static void gpu_viewport_default_fb_create(GPUViewport *viewport) int *size = viewport->size; bool ok = true; - dtxl->color = GPU_texture_create_2D(size[0], size[1], NULL, NULL); - dtxl->depth = GPU_texture_create_depth_with_stencil(size[0], size[1], NULL); + dtxl->color = GPU_texture_create_2D(size[0], size[1], GPU_RGBA8, NULL, NULL); + dtxl->depth = GPU_texture_create_2D(size[0], size[1], GPU_DEPTH24_STENCIL8, NULL, NULL); if (!(dtxl->depth && dtxl->color)) { ok = false; @@ -436,8 +436,8 @@ static void gpu_viewport_default_multisample_fb_create(GPUViewport *viewport) int samples = viewport->samples; bool ok = true; - dtxl->multisample_color = GPU_texture_create_2D_multisample(size[0], size[1], NULL, samples, NULL); - dtxl->multisample_depth = GPU_texture_create_depth_with_stencil_multisample(size[0], size[1], samples, NULL); + dtxl->multisample_color = GPU_texture_create_2D_multisample(size[0], size[1], GPU_RGBA8, NULL, samples, NULL); + dtxl->multisample_depth = GPU_texture_create_2D_multisample(size[0], size[1], GPU_DEPTH24_STENCIL8, NULL, samples, NULL); if (!(dtxl->multisample_depth && dtxl->multisample_color)) { ok = false; -- cgit v1.2.3