From afbd3947071a33f59dda122f1ac396442a02c128 Mon Sep 17 00:00:00 2001 From: marha Date: Mon, 10 Oct 2011 07:52:38 +0200 Subject: fontconfig libX11 mesa pixman xkeyboard-config git updte 10 oct 2011 --- mesalib/src/gallium/auxiliary/util/u_blitter.c | 353 +++-- mesalib/src/gallium/auxiliary/util/u_blitter.h | 47 +- mesalib/src/gallium/auxiliary/util/u_format.c | 132 +- mesalib/src/gallium/auxiliary/util/u_format.csv | 61 + mesalib/src/gallium/auxiliary/util/u_format.h | 86 +- .../src/gallium/auxiliary/util/u_format_pack.py | 1388 ++++++++++---------- .../src/gallium/auxiliary/util/u_format_parse.py | 594 +++++---- .../src/gallium/auxiliary/util/u_format_table.py | 409 +++--- mesalib/src/gallium/auxiliary/util/u_tile.c | 138 ++ mesalib/src/gallium/auxiliary/util/u_tile.h | 291 ++-- 10 files changed, 2087 insertions(+), 1412 deletions(-) (limited to 'mesalib/src/gallium/auxiliary/util') diff --git a/mesalib/src/gallium/auxiliary/util/u_blitter.c b/mesalib/src/gallium/auxiliary/util/u_blitter.c index 58a52b3f2..89dae9556 100644 --- a/mesalib/src/gallium/auxiliary/util/u_blitter.c +++ b/mesalib/src/gallium/auxiliary/util/u_blitter.c @@ -68,6 +68,7 @@ struct blitter_context_priv /* Fragment shaders. */ /* The shader at index i outputs color to color buffers 0,1,...,i-1. */ void *fs_col[PIPE_MAX_COLOR_BUFS+1]; + void *fs_col_int[PIPE_MAX_COLOR_BUFS+1]; /* FS which outputs a color from a texture, where the index is PIPE_TEXTURE_* to be sampled. */ @@ -88,6 +89,8 @@ struct blitter_context_priv void *dsa_keep_depth_write_stencil; void *velem_state; + void *velem_uint_state; + void *velem_sint_state; /* Sampler state for clamping to a miplevel. */ void *sampler_state[PIPE_MAX_TEXTURE_LEVELS * 2]; @@ -104,6 +107,9 @@ struct blitter_context_priv /* Destination surface dimensions. */ unsigned dst_width; unsigned dst_height; + + boolean has_geometry_shader; + boolean vertex_has_integers; }; static void blitter_draw_rectangle(struct blitter_context *blitter, @@ -137,12 +143,20 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe) ctx->base.saved_rs_state = INVALID_PTR; ctx->base.saved_fs = INVALID_PTR; ctx->base.saved_vs = INVALID_PTR; + ctx->base.saved_gs = INVALID_PTR; ctx->base.saved_velem_state = INVALID_PTR; ctx->base.saved_fb_state.nr_cbufs = ~0; ctx->base.saved_num_sampler_views = ~0; ctx->base.saved_num_sampler_states = ~0; ctx->base.saved_num_vertex_buffers = ~0; + ctx->has_geometry_shader = + pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_GEOMETRY, + PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0; + ctx->vertex_has_integers = + pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_VERTEX, + PIPE_SHADER_CAP_INTEGERS); + /* blend state objects */ memset(&blend, 0, sizeof(blend)); ctx->blend_keep_color = pipe->create_blend_state(pipe, &blend); @@ -201,6 +215,30 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe) } ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]); + if (ctx->vertex_has_integers) { + memset(&velem[0], 0, sizeof(velem[0]) * 2); + for (i = 0; i < 2; i++) { + velem[i].src_offset = i * 4 * sizeof(float); + if (i == 0) { + velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; + } else { + velem[i].src_format = PIPE_FORMAT_R32G32B32A32_SINT; + } + } + ctx->velem_sint_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]); + + memset(&velem[0], 0, sizeof(velem[0]) * 2); + for (i = 0; i < 2; i++) { + velem[i].src_offset = i * 4 * sizeof(float); + if (i == 0) { + velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; + } else { + velem[i].src_format = PIPE_FORMAT_R32G32B32A32_UINT; + } + } + ctx->velem_uint_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]); + } + /* fragment shaders are created on-demand */ /* vertex shader */ @@ -243,6 +281,10 @@ void util_blitter_destroy(struct blitter_context *blitter) pipe->delete_rasterizer_state(pipe, ctx->rs_state); pipe->delete_vs_state(pipe, ctx->vs); pipe->delete_vertex_elements_state(pipe, ctx->velem_state); + if (ctx->vertex_has_integers) { + pipe->delete_vertex_elements_state(pipe, ctx->velem_sint_state); + pipe->delete_vertex_elements_state(pipe, ctx->velem_uint_state); + } for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) { if (ctx->fs_texfetch_col[i]) @@ -251,9 +293,12 @@ void util_blitter_destroy(struct blitter_context *blitter) pipe->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]); } - for (i = 0; i <= PIPE_MAX_COLOR_BUFS; i++) + for (i = 0; i <= PIPE_MAX_COLOR_BUFS; i++) { if (ctx->fs_col[i]) pipe->delete_fs_state(pipe, ctx->fs_col[i]); + if (ctx->fs_col_int[i]) + pipe->delete_fs_state(pipe, ctx->fs_col_int[i]); + } for (i = 0; i < PIPE_MAX_TEXTURE_LEVELS * 2; i++) if (ctx->sampler_state[i]) @@ -263,100 +308,140 @@ void util_blitter_destroy(struct blitter_context *blitter) FREE(ctx); } -static void blitter_check_saved_CSOs(struct blitter_context_priv *ctx) +static void blitter_set_running_flag(struct blitter_context_priv *ctx) { if (ctx->base.running) { - _debug_printf("u_blitter: Caught recursion on save. " - "This is a driver bug.\n"); + _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n", + __LINE__); } ctx->base.running = TRUE; +} - /* make sure these CSOs have been saved */ - assert(ctx->base.saved_blend_state != INVALID_PTR && - ctx->base.saved_dsa_state != INVALID_PTR && - ctx->base.saved_rs_state != INVALID_PTR && - ctx->base.saved_fs != INVALID_PTR && +static void blitter_unset_running_flag(struct blitter_context_priv *ctx) +{ + if (!ctx->base.running) { + _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n", + __LINE__); + } + ctx->base.running = FALSE; +} + +static void blitter_check_saved_vertex_states(struct blitter_context_priv *ctx) +{ + assert(ctx->base.saved_num_vertex_buffers != ~0 && + ctx->base.saved_velem_state != INVALID_PTR && ctx->base.saved_vs != INVALID_PTR && - ctx->base.saved_velem_state != INVALID_PTR); + (!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR) && + ctx->base.saved_rs_state != INVALID_PTR); } -static void blitter_restore_CSOs(struct blitter_context_priv *ctx) +static void blitter_restore_vertex_states(struct blitter_context_priv *ctx) { struct pipe_context *pipe = ctx->base.pipe; unsigned i; - /* restore the state objects which are always required to be saved */ - pipe->bind_rasterizer_state(pipe, ctx->base.saved_rs_state); - pipe->bind_vs_state(pipe, ctx->base.saved_vs); + /* Vertex buffers. */ + pipe->set_vertex_buffers(pipe, + ctx->base.saved_num_vertex_buffers, + ctx->base.saved_vertex_buffers); + + for (i = 0; i < ctx->base.saved_num_vertex_buffers; i++) { + if (ctx->base.saved_vertex_buffers[i].buffer) { + pipe_resource_reference(&ctx->base.saved_vertex_buffers[i].buffer, + NULL); + } + } + ctx->base.saved_num_vertex_buffers = ~0; + + /* Vertex elements. */ pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state); + ctx->base.saved_velem_state = INVALID_PTR; - ctx->base.saved_rs_state = INVALID_PTR; + /* Vertex shader. */ + pipe->bind_vs_state(pipe, ctx->base.saved_vs); ctx->base.saved_vs = INVALID_PTR; - ctx->base.saved_velem_state = INVALID_PTR; - /* restore the state objects which are required to be saved for clear/copy - */ - if (ctx->base.saved_blend_state != INVALID_PTR) { - pipe->bind_blend_state(pipe, ctx->base.saved_blend_state); - ctx->base.saved_blend_state = INVALID_PTR; - } - if (ctx->base.saved_dsa_state != INVALID_PTR) { - pipe->bind_depth_stencil_alpha_state(pipe, ctx->base.saved_dsa_state); - ctx->base.saved_dsa_state = INVALID_PTR; - } - if (ctx->base.saved_fs != INVALID_PTR) { - pipe->bind_fs_state(pipe, ctx->base.saved_fs); - ctx->base.saved_fs = INVALID_PTR; + /* Geometry shader. */ + if (ctx->has_geometry_shader) { + pipe->bind_gs_state(pipe, ctx->base.saved_gs); + ctx->base.saved_gs = INVALID_PTR; } + /* Rasterizer. */ + pipe->bind_rasterizer_state(pipe, ctx->base.saved_rs_state); + ctx->base.saved_rs_state = INVALID_PTR; +} + +static void blitter_check_saved_fragment_states(struct blitter_context_priv *ctx) +{ + assert(ctx->base.saved_fs != INVALID_PTR && + ctx->base.saved_dsa_state != INVALID_PTR && + ctx->base.saved_blend_state != INVALID_PTR); +} + +static void blitter_restore_fragment_states(struct blitter_context_priv *ctx) +{ + struct pipe_context *pipe = ctx->base.pipe; + + /* Fragment shader. */ + pipe->bind_fs_state(pipe, ctx->base.saved_fs); + ctx->base.saved_fs = INVALID_PTR; + + /* Depth, stencil, alpha. */ + pipe->bind_depth_stencil_alpha_state(pipe, ctx->base.saved_dsa_state); + ctx->base.saved_dsa_state = INVALID_PTR; + + /* Blend state. */ + pipe->bind_blend_state(pipe, ctx->base.saved_blend_state); + ctx->base.saved_blend_state = INVALID_PTR; + + /* Miscellaneous states. */ + /* XXX check whether these are saved and whether they need to be restored + * (depending on the operation) */ pipe->set_stencil_ref(pipe, &ctx->base.saved_stencil_ref); pipe->set_viewport_state(pipe, &ctx->base.saved_viewport); pipe->set_clip_state(pipe, &ctx->base.saved_clip); +} - if (ctx->base.saved_fb_state.nr_cbufs != ~0) { - pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state); - util_unreference_framebuffer_state(&ctx->base.saved_fb_state); - ctx->base.saved_fb_state.nr_cbufs = ~0; - } +static void blitter_check_saved_fb_state(struct blitter_context_priv *ctx) +{ + assert(ctx->base.saved_fb_state.nr_cbufs != ~0); +} - if (ctx->base.saved_num_sampler_states != ~0) { - pipe->bind_fragment_sampler_states(pipe, - ctx->base.saved_num_sampler_states, - ctx->base.saved_sampler_states); - ctx->base.saved_num_sampler_states = ~0; - } +static void blitter_restore_fb_state(struct blitter_context_priv *ctx) +{ + struct pipe_context *pipe = ctx->base.pipe; - if (ctx->base.saved_num_sampler_views != ~0) { - pipe->set_fragment_sampler_views(pipe, - ctx->base.saved_num_sampler_views, - ctx->base.saved_sampler_views); + pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state); + util_unreference_framebuffer_state(&ctx->base.saved_fb_state); +} - for (i = 0; i < ctx->base.saved_num_sampler_views; i++) - pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], - NULL); +static void blitter_check_saved_textures(struct blitter_context_priv *ctx) +{ + assert(ctx->base.saved_num_sampler_states != ~0 && + ctx->base.saved_num_sampler_views != ~0); +} - ctx->base.saved_num_sampler_views = ~0; - } +static void blitter_restore_textures(struct blitter_context_priv *ctx) +{ + struct pipe_context *pipe = ctx->base.pipe; + unsigned i; - if (ctx->base.saved_num_vertex_buffers != ~0) { - pipe->set_vertex_buffers(pipe, - ctx->base.saved_num_vertex_buffers, - ctx->base.saved_vertex_buffers); + /* Fragment sampler states. */ + pipe->bind_fragment_sampler_states(pipe, + ctx->base.saved_num_sampler_states, + ctx->base.saved_sampler_states); + ctx->base.saved_num_sampler_states = ~0; - for (i = 0; i < ctx->base.saved_num_vertex_buffers; i++) { - if (ctx->base.saved_vertex_buffers[i].buffer) { - pipe_resource_reference(&ctx->base.saved_vertex_buffers[i].buffer, - NULL); - } - } - ctx->base.saved_num_vertex_buffers = ~0; - } + /* Fragment sampler views. */ + pipe->set_fragment_sampler_views(pipe, + ctx->base.saved_num_sampler_views, + ctx->base.saved_sampler_views); - if (!ctx->base.running) { - _debug_printf("u_blitter: Caught recursion on restore. " - "This is a driver bug.\n"); - } - ctx->base.running = FALSE; + for (i = 0; i < ctx->base.saved_num_sampler_views; i++) + pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL); + + ctx->base.saved_num_sampler_views = ~0; } static void blitter_set_rectangle(struct blitter_context_priv *ctx, @@ -404,10 +489,11 @@ static void blitter_set_clear_color(struct blitter_context_priv *ctx, if (color) { for (i = 0; i < 4; i++) { - ctx->vertices[i][1][0] = color->f[0]; - ctx->vertices[i][1][1] = color->f[1]; - ctx->vertices[i][1][2] = color->f[2]; - ctx->vertices[i][1][3] = color->f[3]; + uint32_t *uiverts = (uint32_t *)ctx->vertices[i][1]; + uiverts[0] = color->ui[0]; + uiverts[1] = color->ui[1]; + uiverts[2] = color->ui[2]; + uiverts[3] = color->ui[3]; } } else { for (i = 0; i < 4; i++) { @@ -562,19 +648,28 @@ void **blitter_get_sampler_state(struct blitter_context_priv *ctx, } static INLINE -void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs) +void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs, + boolean int_format) { struct pipe_context *pipe = ctx->base.pipe; assert(num_cbufs <= PIPE_MAX_COLOR_BUFS); - if (!ctx->fs_col[num_cbufs]) - ctx->fs_col[num_cbufs] = - util_make_fragment_cloneinput_shader(pipe, num_cbufs, - TGSI_SEMANTIC_GENERIC, - TGSI_INTERPOLATE_LINEAR); - - return ctx->fs_col[num_cbufs]; + if (int_format) { + if (!ctx->fs_col_int[num_cbufs]) + ctx->fs_col_int[num_cbufs] = + util_make_fragment_cloneinput_shader(pipe, num_cbufs, + TGSI_SEMANTIC_GENERIC, + TGSI_INTERPOLATE_CONSTANT); + return ctx->fs_col_int[num_cbufs]; + } else { + if (!ctx->fs_col[num_cbufs]) + ctx->fs_col[num_cbufs] = + util_make_fragment_cloneinput_shader(pipe, num_cbufs, + TGSI_SEMANTIC_GENERIC, + TGSI_INTERPOLATE_LINEAR); + return ctx->fs_col[num_cbufs]; + } } /** Convert PIPE_TEXTURE_x to TGSI_TEXTURE_x */ @@ -674,6 +769,7 @@ static void util_blitter_clear_custom(struct blitter_context *blitter, unsigned width, unsigned height, unsigned num_cbufs, unsigned clear_buffers, + enum pipe_format cbuf_format, const union pipe_color_union *color, double depth, unsigned stencil, void *custom_blend, void *custom_dsa) @@ -681,12 +777,14 @@ static void util_blitter_clear_custom(struct blitter_context *blitter, struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; struct pipe_context *pipe = ctx->base.pipe; struct pipe_stencil_ref sr = { { 0 } }; - + boolean int_format = util_format_is_pure_integer(cbuf_format); assert(num_cbufs <= PIPE_MAX_COLOR_BUFS); - blitter_check_saved_CSOs(ctx); + blitter_set_running_flag(ctx); + blitter_check_saved_vertex_states(ctx); + blitter_check_saved_fragment_states(ctx); - /* bind CSOs */ + /* bind states */ if (custom_blend) { pipe->bind_blend_state(pipe, custom_blend); } else if (clear_buffers & PIPE_CLEAR_COLOR) { @@ -711,25 +809,37 @@ static void util_blitter_clear_custom(struct blitter_context *blitter, pipe->set_stencil_ref(pipe, &sr); pipe->bind_rasterizer_state(pipe, ctx->rs_state); - pipe->bind_vertex_elements_state(pipe, ctx->velem_state); - pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs)); + if (util_format_is_pure_sint(cbuf_format)) { + pipe->bind_vertex_elements_state(pipe, ctx->velem_sint_state); + } else if (util_format_is_pure_uint(cbuf_format)) { + pipe->bind_vertex_elements_state(pipe, ctx->velem_uint_state); + } else { + pipe->bind_vertex_elements_state(pipe, ctx->velem_state); + } + pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs, int_format)); pipe->bind_vs_state(pipe, ctx->vs); + if (ctx->has_geometry_shader) + pipe->bind_gs_state(pipe, NULL); blitter_set_dst_dimensions(ctx, width, height); blitter->draw_rectangle(blitter, 0, 0, width, height, depth, UTIL_BLITTER_ATTRIB_COLOR, color); - blitter_restore_CSOs(ctx); + + blitter_restore_vertex_states(ctx); + blitter_restore_fragment_states(ctx); + blitter_unset_running_flag(ctx); } void util_blitter_clear(struct blitter_context *blitter, unsigned width, unsigned height, unsigned num_cbufs, unsigned clear_buffers, + enum pipe_format cbuf_format, const union pipe_color_union *color, double depth, unsigned stencil) { util_blitter_clear_custom(blitter, width, height, num_cbufs, - clear_buffers, color, depth, stencil, + clear_buffers, cbuf_format, color, depth, stencil, NULL, NULL); } @@ -739,7 +849,7 @@ void util_blitter_clear_depth_custom(struct blitter_context *blitter, { static const union pipe_color_union color; util_blitter_clear_custom(blitter, width, height, 0, - 0, &color, depth, 0, NULL, custom_dsa); + 0, PIPE_FORMAT_NONE, &color, depth, 0, NULL, custom_dsa); } static @@ -817,10 +927,11 @@ void util_blitter_copy_texture(struct blitter_context *blitter, dstsurf = pipe->create_surface(pipe, dst, &surf_templ); /* Check whether the states are properly saved. */ - blitter_check_saved_CSOs(ctx); - assert(blitter->saved_fb_state.nr_cbufs != ~0); - assert(blitter->saved_num_sampler_views != ~0); - assert(blitter->saved_num_sampler_states != ~0); + blitter_set_running_flag(ctx); + blitter_check_saved_vertex_states(ctx); + blitter_check_saved_fragment_states(ctx); + blitter_check_saved_textures(ctx); + blitter_check_saved_fb_state(ctx); /* Initialize framebuffer state. */ fb_state.width = dstsurf->width; @@ -855,6 +966,8 @@ void util_blitter_copy_texture(struct blitter_context *blitter, /* Set rasterizer state, shaders, and textures. */ pipe->bind_rasterizer_state(pipe, ctx->rs_state); pipe->bind_vs_state(pipe, ctx->vs); + if (ctx->has_geometry_shader) + pipe->bind_gs_state(pipe, NULL); pipe->bind_fragment_sampler_states(pipe, 1, blitter_get_sampler_state(ctx, srclevel, normalized)); pipe->bind_vertex_elements_state(pipe, ctx->velem_state); @@ -918,7 +1031,11 @@ void util_blitter_copy_texture(struct blitter_context *blitter, break; } - blitter_restore_CSOs(ctx); + blitter_restore_vertex_states(ctx); + blitter_restore_fragment_states(ctx); + blitter_restore_textures(ctx); + blitter_restore_fb_state(ctx); + blitter_unset_running_flag(ctx); pipe_surface_reference(&dstsurf, NULL); pipe_sampler_view_reference(&view, NULL); @@ -940,15 +1057,19 @@ void util_blitter_clear_render_target(struct blitter_context *blitter, return; /* check the saved state */ - blitter_check_saved_CSOs(ctx); - assert(blitter->saved_fb_state.nr_cbufs != ~0); + blitter_set_running_flag(ctx); + blitter_check_saved_vertex_states(ctx); + blitter_check_saved_fragment_states(ctx); + blitter_check_saved_fb_state(ctx); - /* bind CSOs */ + /* bind states */ pipe->bind_blend_state(pipe, ctx->blend_write_color); pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); pipe->bind_rasterizer_state(pipe, ctx->rs_state); - pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1)); + pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE)); pipe->bind_vs_state(pipe, ctx->vs); + if (ctx->has_geometry_shader) + pipe->bind_gs_state(pipe, NULL); pipe->bind_vertex_elements_state(pipe, ctx->velem_state); /* set a framebuffer state */ @@ -962,7 +1083,11 @@ void util_blitter_clear_render_target(struct blitter_context *blitter, blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height); blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0, UTIL_BLITTER_ATTRIB_COLOR, color); - blitter_restore_CSOs(ctx); + + blitter_restore_vertex_states(ctx); + blitter_restore_fragment_states(ctx); + blitter_restore_fb_state(ctx); + blitter_unset_running_flag(ctx); } /* Clear a region of a depth stencil surface. */ @@ -984,10 +1109,12 @@ void util_blitter_clear_depth_stencil(struct blitter_context *blitter, return; /* check the saved state */ - blitter_check_saved_CSOs(ctx); - assert(blitter->saved_fb_state.nr_cbufs != ~0); + blitter_set_running_flag(ctx); + blitter_check_saved_vertex_states(ctx); + blitter_check_saved_fragment_states(ctx); + blitter_check_saved_fb_state(ctx); - /* bind CSOs */ + /* bind states */ pipe->bind_blend_state(pipe, ctx->blend_keep_color); if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) { sr.ref_value[0] = stencil & 0xff; @@ -1007,8 +1134,10 @@ void util_blitter_clear_depth_stencil(struct blitter_context *blitter, pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); pipe->bind_rasterizer_state(pipe, ctx->rs_state); - pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0)); + pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE)); pipe->bind_vs_state(pipe, ctx->vs); + if (ctx->has_geometry_shader) + pipe->bind_gs_state(pipe, NULL); pipe->bind_vertex_elements_state(pipe, ctx->velem_state); /* set a framebuffer state */ @@ -1022,7 +1151,11 @@ void util_blitter_clear_depth_stencil(struct blitter_context *blitter, blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height); blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, depth, UTIL_BLITTER_ATTRIB_NONE, NULL); - blitter_restore_CSOs(ctx); + + blitter_restore_vertex_states(ctx); + blitter_restore_fragment_states(ctx); + blitter_restore_fb_state(ctx); + blitter_unset_running_flag(ctx); } /* draw a rectangle across a region using a custom dsa stage - for r600g */ @@ -1040,16 +1173,20 @@ void util_blitter_custom_depth_stencil(struct blitter_context *blitter, return; /* check the saved state */ - blitter_check_saved_CSOs(ctx); - assert(blitter->saved_fb_state.nr_cbufs != ~0); + blitter_set_running_flag(ctx); + blitter_check_saved_vertex_states(ctx); + blitter_check_saved_fragment_states(ctx); + blitter_check_saved_fb_state(ctx); - /* bind CSOs */ + /* bind states */ pipe->bind_blend_state(pipe, ctx->blend_write_color); pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage); pipe->bind_rasterizer_state(pipe, ctx->rs_state); - pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0)); + pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE)); pipe->bind_vs_state(pipe, ctx->vs); + if (ctx->has_geometry_shader) + pipe->bind_gs_state(pipe, NULL); pipe->bind_vertex_elements_state(pipe, ctx->velem_state); /* set a framebuffer state */ @@ -1069,5 +1206,9 @@ void util_blitter_custom_depth_stencil(struct blitter_context *blitter, blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height); blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth, UTIL_BLITTER_ATTRIB_NONE, NULL); - blitter_restore_CSOs(ctx); + + blitter_restore_vertex_states(ctx); + blitter_restore_fragment_states(ctx); + blitter_restore_fb_state(ctx); + blitter_unset_running_flag(ctx); } diff --git a/mesalib/src/gallium/auxiliary/util/u_blitter.h b/mesalib/src/gallium/auxiliary/util/u_blitter.h index a9ad02364..798096e74 100644 --- a/mesalib/src/gallium/auxiliary/util/u_blitter.h +++ b/mesalib/src/gallium/auxiliary/util/u_blitter.h @@ -89,7 +89,7 @@ struct blitter_context void *saved_dsa_state; /**< depth stencil alpha state */ void *saved_velem_state; /**< vertex elements state */ void *saved_rs_state; /**< rasterizer state */ - void *saved_fs, *saved_vs; /**< fragment shader, vertex shader */ + void *saved_fs, *saved_vs, *saved_gs; /**< shaders */ struct pipe_framebuffer_state saved_fb_state; /**< framebuffer state */ struct pipe_stencil_ref saved_stencil_ref; /**< stencil ref */ @@ -126,24 +126,28 @@ struct pipe_context *util_blitter_get_pipe(struct blitter_context *blitter) } /* - * These states must be saved before any of the following functions is called: - * - blend state - * - depth stencil alpha state - * - rasterizer state - * - vertex shader - * - any other shader??? (XXX) - * - fragment shader + * These states must be saved before any of the following functions are called: * - vertex buffers * - vertex elements + * - vertex shader + * - geometry shader (if supported) + * - rasterizer state */ /** * Clear a specified set of currently bound buffers to specified values. + * + * These states must be saved in the blitter in addition to the state objects + * already required to be saved: + * - fragment shader + * - depth stencil alpha state + * - blend state */ void util_blitter_clear(struct blitter_context *blitter, unsigned width, unsigned height, unsigned num_cbufs, unsigned clear_buffers, + enum pipe_format cbuf_format, const union pipe_color_union *color, double depth, unsigned stencil); @@ -155,7 +159,7 @@ void util_blitter_clear_depth_custom(struct blitter_context *blitter, * Copy a block of pixels from one surface to another. * * You can copy from any color format to any other color format provided - * the former can be sampled and the latter can be rendered to. Otherwise, + * the former can be sampled from and the latter can be rendered to. Otherwise, * a software fallback path is taken and both surfaces must be of the same * format. * @@ -163,14 +167,18 @@ void util_blitter_clear_depth_custom(struct blitter_context *blitter, * cannot be copied unless you set ignore_stencil to FALSE. In that case, * a software fallback path is taken and both surfaces must be of the same * format. + * XXX implement hw-accel stencil copy using shader stencil export. * * Use pipe_screen->is_format_supported to know your options. * * These states must be saved in the blitter in addition to the state objects * already required to be saved: - * - framebuffer state + * - fragment shader + * - depth stencil alpha state + * - blend state * - fragment sampler states * - fragment sampler textures + * - framebuffer state */ void util_blitter_copy_texture(struct blitter_context *blitter, struct pipe_resource *dst, @@ -186,6 +194,9 @@ void util_blitter_copy_texture(struct blitter_context *blitter, * * These states must be saved in the blitter in addition to the state objects * already required to be saved: + * - fragment shader + * - depth stencil alpha state + * - blend state * - framebuffer state */ void util_blitter_clear_render_target(struct blitter_context *blitter, @@ -200,6 +211,9 @@ void util_blitter_clear_render_target(struct blitter_context *blitter, * * These states must be saved in the blitter in addition to the state objects * already required to be saved: + * - fragment shader + * - depth stencil alpha state + * - blend state * - framebuffer state */ void util_blitter_clear_depth_stencil(struct blitter_context *blitter, @@ -220,7 +234,7 @@ void util_blitter_custom_depth_stencil(struct blitter_context *blitter, * of the util_blitter_{clear, copy_region, fill_region} functions and then * forgotten. * - * CSOs not listed here are not affected by util_blitter. */ + * States not listed here are not affected by util_blitter. */ static INLINE void util_blitter_save_blend(struct blitter_context *blitter, @@ -271,6 +285,13 @@ void util_blitter_save_vertex_shader(struct blitter_context *blitter, blitter->saved_vs = vs; } +static INLINE +void util_blitter_save_geometry_shader(struct blitter_context *blitter, + void *gs) +{ + blitter->saved_gs = gs; +} + static INLINE void util_blitter_save_framebuffer(struct blitter_context *blitter, const struct pipe_framebuffer_state *state) @@ -322,8 +343,8 @@ util_blitter_save_fragment_sampler_views(struct blitter_context *blitter, static INLINE void util_blitter_save_vertex_buffers(struct blitter_context *blitter, - int num_vertex_buffers, - struct pipe_vertex_buffer *vertex_buffers) + int num_vertex_buffers, + struct pipe_vertex_buffer *vertex_buffers) { assert(num_vertex_buffers <= Elements(blitter->saved_vertex_buffers)); diff --git a/mesalib/src/gallium/auxiliary/util/u_format.c b/mesalib/src/gallium/auxiliary/util/u_format.c index 9bf42583e..730dc31ac 100644 --- a/mesalib/src/gallium/auxiliary/util/u_format.c +++ b/mesalib/src/gallium/auxiliary/util/u_format.c @@ -52,14 +52,8 @@ util_format_is_float(enum pipe_format format) return FALSE; } - /* Find the first non-void channel. */ - for (i = 0; i < 4; i++) { - if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) { - break; - } - } - - if (i == 4) { + i = util_format_get_first_non_void_channel(format); + if (i == -1) { return FALSE; } @@ -124,6 +118,45 @@ util_format_is_luminance(enum pipe_format format) return FALSE; } +boolean +util_format_is_pure_integer(enum pipe_format format) +{ + const struct util_format_description *desc = util_format_description(format); + int i; + + /* Find the first non-void channel. */ + i = util_format_get_first_non_void_channel(format); + if (i == -1) + return FALSE; + + return desc->channel[i].pure_integer ? TRUE : FALSE; +} + +boolean +util_format_is_pure_sint(enum pipe_format format) +{ + const struct util_format_description *desc = util_format_description(format); + int i; + + i = util_format_get_first_non_void_channel(format); + if (i == -1) + return FALSE; + + return (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED && desc->channel[i].pure_integer) ? TRUE : FALSE; +} + +boolean +util_format_is_pure_uint(enum pipe_format format) +{ + const struct util_format_description *desc = util_format_description(format); + int i; + + i = util_format_get_first_non_void_channel(format); + if (i == -1) + return FALSE; + + return (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED && desc->channel[i].pure_integer) ? TRUE : FALSE; +} boolean util_format_is_luminance_alpha(enum pipe_format format) @@ -262,6 +295,89 @@ util_format_write_4ub(enum pipe_format format, const uint8_t *src, unsigned src_ format_desc->pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, w, h); } +void +util_format_read_4ui(enum pipe_format format, + unsigned *dst, unsigned dst_stride, + const void *src, unsigned src_stride, + unsigned x, unsigned y, unsigned w, unsigned h) +{ + const struct util_format_description *format_desc; + const uint8_t *src_row; + unsigned *dst_row; + + format_desc = util_format_description(format); + + assert(x % format_desc->block.width == 0); + assert(y % format_desc->block.height == 0); + + src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8); + dst_row = dst; + + format_desc->unpack_rgba_uint(dst_row, dst_stride, src_row, src_stride, w, h); +} + +void +util_format_write_4ui(enum pipe_format format, + const unsigned int *src, unsigned src_stride, + void *dst, unsigned dst_stride, + unsigned x, unsigned y, unsigned w, unsigned h) +{ + const struct util_format_description *format_desc; + uint8_t *dst_row; + const unsigned *src_row; + + format_desc = util_format_description(format); + + assert(x % format_desc->block.width == 0); + assert(y % format_desc->block.height == 0); + + dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8); + src_row = src; + + format_desc->pack_rgba_uint(dst_row, dst_stride, src_row, src_stride, w, h); +} + +void +util_format_read_4i(enum pipe_format format, + int *dst, unsigned dst_stride, + const void *src, unsigned src_stride, + unsigned x, unsigned y, unsigned w, unsigned h) +{ + const struct util_format_description *format_desc; + const uint8_t *src_row; + int *dst_row; + + format_desc = util_format_description(format); + + assert(x % format_desc->block.width == 0); + assert(y % format_desc->block.height == 0); + + src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8); + dst_row = dst; + + format_desc->unpack_rgba_sint(dst_row, dst_stride, src_row, src_stride, w, h); +} + +void +util_format_write_4i(enum pipe_format format, + const int *src, unsigned src_stride, + void *dst, unsigned dst_stride, + unsigned x, unsigned y, unsigned w, unsigned h) +{ + const struct util_format_description *format_desc; + uint8_t *dst_row; + const int *src_row; + + format_desc = util_format_description(format); + + assert(x % format_desc->block.width == 0); + assert(y % format_desc->block.height == 0); + + dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8); + src_row = src; + + format_desc->pack_rgba_sint(dst_row, dst_stride, src_row, src_stride, w, h); +} boolean util_is_format_compatible(const struct util_format_description *src_desc, diff --git a/mesalib/src/gallium/auxiliary/util/u_format.csv b/mesalib/src/gallium/auxiliary/util/u_format.csv index 621a46dba..08aa0a49d 100644 --- a/mesalib/src/gallium/auxiliary/util/u_format.csv +++ b/mesalib/src/gallium/auxiliary/util/u_format.csv @@ -42,6 +42,7 @@ # - 'h': fixed # - 'f': FLOAT # - optionally followed by 'n' if it is normalized +# - optionally followed by 'p' if it is pure # - number of bits # - channel swizzle # - color space: rgb, yub, sz @@ -278,3 +279,63 @@ PIPE_FORMAT_R10G10B10A2_SNORM , plain, 1, 1, sn10, sn10, sn10, sn2, xyzw, PIPE_FORMAT_B10G10R10A2_USCALED , plain, 1, 1, u10, u10, u10, u2, zyxw, rgb PIPE_FORMAT_B10G10R10A2_SSCALED , plain, 1, 1, s10, s10, s10, s2, zyxw, rgb PIPE_FORMAT_B10G10R10A2_SNORM , plain, 1, 1, sn10, sn10, sn10, sn2, zyxw, rgb + +PIPE_FORMAT_R8_UINT , plain, 1, 1, up8, , , , x001, rgb +PIPE_FORMAT_R8G8_UINT , plain, 1, 1, up8, up8, , , xy01, rgb +PIPE_FORMAT_R8G8B8_UINT , plain, 1, 1, up8, up8, up8, , xyz1, rgb +PIPE_FORMAT_R8G8B8A8_UINT , plain, 1, 1, up8, up8, up8, up8, xyzw, rgb + +PIPE_FORMAT_R8_SINT , plain, 1, 1, sp8, , , , x001, rgb +PIPE_FORMAT_R8G8_SINT , plain, 1, 1, sp8, sp8, , , xy01, rgb +PIPE_FORMAT_R8G8B8_SINT , plain, 1, 1, sp8, sp8, sp8, , xyz1, rgb +PIPE_FORMAT_R8G8B8A8_SINT , plain, 1, 1, sp8, sp8, sp8, sp8, xyzw, rgb + +PIPE_FORMAT_R16_UINT , plain, 1, 1, up16, , , , x001, rgb +PIPE_FORMAT_R16G16_UINT , plain, 1, 1, up16, up16, , , xy01, rgb +PIPE_FORMAT_R16G16B16_UINT , plain, 1, 1, up16, up16, up16, , xyz1, rgb +PIPE_FORMAT_R16G16B16A16_UINT , plain, 1, 1, up16, up16, up16, up16, xyzw, rgb + +PIPE_FORMAT_R16_SINT , plain, 1, 1, sp16, , , , x001, rgb +PIPE_FORMAT_R16G16_SINT , plain, 1, 1, sp16, sp16, , , xy01, rgb +PIPE_FORMAT_R16G16B16_SINT , plain, 1, 1, sp16, sp16, sp16, , xyz1, rgb +PIPE_FORMAT_R16G16B16A16_SINT , plain, 1, 1, sp16, sp16, sp16, sp16, xyzw, rgb + +PIPE_FORMAT_R32_UINT , plain, 1, 1, up32, , , , x001, rgb +PIPE_FORMAT_R32G32_UINT , plain, 1, 1, up32, up32, , , xy01, rgb +PIPE_FORMAT_R32G32B32_UINT , plain, 1, 1, up32, up32, up32, , xyz1, rgb +PIPE_FORMAT_R32G32B32A32_UINT , plain, 1, 1, up32, up32, up32, up32, xyzw, rgb + +PIPE_FORMAT_R32_SINT , plain, 1, 1, sp32, , , , x001, rgb +PIPE_FORMAT_R32G32_SINT , plain, 1, 1, sp32, sp32, , , xy01, rgb +PIPE_FORMAT_R32G32B32_SINT , plain, 1, 1, sp32, sp32, sp32, , xyz1, rgb +PIPE_FORMAT_R32G32B32A32_SINT , plain, 1, 1, sp32, sp32, sp32, sp32, xyzw, rgb + +PIPE_FORMAT_A8_UINT , plain, 1, 1, up8, , , , 000x, rgb +PIPE_FORMAT_I8_UINT , plain, 1, 1, up8, , , , xxxx, rgb +PIPE_FORMAT_L8_UINT , plain, 1, 1, up8, , , , xxx1, rgb +PIPE_FORMAT_L8A8_UINT , plain, 1, 1, up8, up8, , , xxxy, rgb + +PIPE_FORMAT_A8_SINT , plain, 1, 1, sp8, , , , 000x, rgb +PIPE_FORMAT_I8_SINT , plain, 1, 1, sp8, , , , xxxx, rgb +PIPE_FORMAT_L8_SINT , plain, 1, 1, sp8, , , , xxx1, rgb +PIPE_FORMAT_L8A8_SINT , plain, 1, 1, sp8, sp8, , , xxxy, rgb + +PIPE_FORMAT_A16_UINT , plain, 1, 1, up16, , , , 000x, rgb +PIPE_FORMAT_I16_UINT , plain, 1, 1, up16, , , , xxxx, rgb +PIPE_FORMAT_L16_UINT , plain, 1, 1, up16, , , , xxx1, rgb +PIPE_FORMAT_L16A16_UINT , plain, 1, 1, up16, up16, , , xxxy, rgb + +PIPE_FORMAT_A16_SINT , plain, 1, 1, sp16, , , , 000x, rgb +PIPE_FORMAT_I16_SINT , plain, 1, 1, sp16, , , , xxxx, rgb +PIPE_FORMAT_L16_SINT , plain, 1, 1, sp16, , , , xxx1, rgb +PIPE_FORMAT_L16A16_SINT , plain, 1, 1, sp16, sp16, , , xxxy, rgb + +PIPE_FORMAT_A32_UINT , plain, 1, 1, up32, , , , 000x, rgb +PIPE_FORMAT_I32_UINT , plain, 1, 1, up32, , , , xxxx, rgb +PIPE_FORMAT_L32_UINT , plain, 1, 1, up32, , , , xxx1, rgb +PIPE_FORMAT_L32A32_UINT , plain, 1, 1, up32, up32, , , xxxy, rgb + +PIPE_FORMAT_A32_SINT , plain, 1, 1, sp32, , , , 000x, rgb +PIPE_FORMAT_I32_SINT , plain, 1, 1, sp32, , , , xxxx, rgb +PIPE_FORMAT_L32_SINT , plain, 1, 1, sp32, , , , xxx1, rgb +PIPE_FORMAT_L32A32_SINT , plain, 1, 1, sp32, sp32, , , xxxy, rgb \ No newline at end of file diff --git a/mesalib/src/gallium/auxiliary/util/u_format.h b/mesalib/src/gallium/auxiliary/util/u_format.h index 98528ea59..215a00ab5 100644 --- a/mesalib/src/gallium/auxiliary/util/u_format.h +++ b/mesalib/src/gallium/auxiliary/util/u_format.h @@ -120,8 +120,9 @@ enum util_format_colorspace { struct util_format_channel_description { - unsigned type:6; /**< UTIL_FORMAT_TYPE_x */ + unsigned type:5; /**< UTIL_FORMAT_TYPE_x */ unsigned normalized:1; + unsigned pure_integer:1; unsigned size:9; /**< bits per channel */ }; @@ -321,6 +322,37 @@ struct util_format_description const uint8_t *src, unsigned src_stride, unsigned width, unsigned height); + /** + * Unpack pixel blocks to R32G32B32A32_UINT. + * Note: strides are in bytes. + * + * Only defined for INT formats. + */ + void + (*unpack_rgba_uint)(unsigned *dst, unsigned dst_stride, + const uint8_t *src, unsigned src_stride, + unsigned width, unsigned height); + + void + (*pack_rgba_uint)(uint8_t *dst, unsigned dst_stride, + const unsigned *src, unsigned src_stride, + unsigned width, unsigned height); + + /** + * Unpack pixel blocks to R32G32B32A32_SINT. + * Note: strides are in bytes. + * + * Only defined for INT formats. + */ + void + (*unpack_rgba_sint)(signed *dst, unsigned dst_stride, + const uint8_t *src, unsigned src_stride, + unsigned width, unsigned height); + + void + (*pack_rgba_sint)(uint8_t *dst, unsigned dst_stride, + const int *src, unsigned src_stride, + unsigned width, unsigned height); }; @@ -511,6 +543,14 @@ util_format_is_luminance_alpha(enum pipe_format format); boolean util_format_is_intensity(enum pipe_format format); +boolean +util_format_is_pure_integer(enum pipe_format format); + +boolean +util_format_is_pure_sint(enum pipe_format format); + +boolean +util_format_is_pure_uint(enum pipe_format format); /** * Whether the src format can be blitted to destation format with a simple @@ -806,6 +846,26 @@ util_format_get_nr_components(enum pipe_format format) return desc->nr_channels; } +/** + * Return the index of the first non-void channel + * -1 if no non-void channels + */ +static INLINE int +util_format_get_first_non_void_channel(enum pipe_format format) +{ + const struct util_format_description *desc = util_format_description(format); + int i; + + for (i = 0; i < 4; i++) + if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) + break; + + if (i == 4) + return -1; + + return i; +} + /* * Format access functions. */ @@ -834,6 +894,30 @@ util_format_write_4ub(enum pipe_format format, void *dst, unsigned dst_stride, unsigned x, unsigned y, unsigned w, unsigned h); +void +util_format_read_4ui(enum pipe_format format, + unsigned *dst, unsigned dst_stride, + const void *src, unsigned src_stride, + unsigned x, unsigned y, unsigned w, unsigned h); + +void +util_format_write_4ui(enum pipe_format format, + const unsigned int *src, unsigned src_stride, + void *dst, unsigned dst_stride, + unsigned x, unsigned y, unsigned w, unsigned h); + +void +util_format_read_4i(enum pipe_format format, + int *dst, unsigned dst_stride, + const void *src, unsigned src_stride, + unsigned x, unsigned y, unsigned w, unsigned h); + +void +util_format_write_4i(enum pipe_format format, + const int *src, unsigned src_stride, + void *dst, unsigned dst_stride, + unsigned x, unsigned y, unsigned w, unsigned h); + /* * Generic format conversion; */ diff --git a/mesalib/src/gallium/auxiliary/util/u_format_pack.py b/mesalib/src/gallium/auxiliary/util/u_format_pack.py index 497efad5a..5cfbe323d 100644 --- a/mesalib/src/gallium/auxiliary/util/u_format_pack.py +++ b/mesalib/src/gallium/auxiliary/util/u_format_pack.py @@ -1,670 +1,718 @@ -#!/usr/bin/env python - -''' -/************************************************************************** - * - * Copyright 2009-2010 VMware, Inc. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - -/** - * @file - * Pixel format packing and unpacking functions. - * - * @author Jose Fonseca - */ -''' - - -from u_format_parse import * - - -def generate_format_type(format): - '''Generate a structure that describes the format.''' - - assert format.layout == PLAIN - - print 'union util_format_%s {' % format.short_name() - - if format.block_size() in (8, 16, 32, 64): - print ' uint%u_t value;' % (format.block_size(),) - - use_bitfields = False - for channel in format.channels: - if channel.size % 8 or not is_pot(channel.size): - use_bitfields = True - - print ' struct {' - for channel in format.channels: - if use_bitfields: - if channel.type == VOID: - if channel.size: - print ' unsigned %s:%u;' % (channel.name, channel.size) - elif channel.type == UNSIGNED: - print ' unsigned %s:%u;' % (channel.name, channel.size) - elif channel.type in (SIGNED, FIXED): - print ' int %s:%u;' % (channel.name, channel.size) - elif channel.type == FLOAT: - if channel.size == 64: - print ' double %s;' % (channel.name) - elif channel.size == 32: - print ' float %s;' % (channel.name) - else: - print ' unsigned %s:%u;' % (channel.name, channel.size) - else: - assert 0 - else: - assert channel.size % 8 == 0 and is_pot(channel.size) - if channel.type == VOID: - if channel.size: - print ' uint%u_t %s;' % (channel.size, channel.name) - elif channel.type == UNSIGNED: - print ' uint%u_t %s;' % (channel.size, channel.name) - elif channel.type in (SIGNED, FIXED): - print ' int%u_t %s;' % (channel.size, channel.name) - elif channel.type == FLOAT: - if channel.size == 64: - print ' double %s;' % (channel.name) - elif channel.size == 32: - print ' float %s;' % (channel.name) - elif channel.size == 16: - print ' uint16_t %s;' % (channel.name) - else: - assert 0 - else: - assert 0 - print ' } chan;' - print '};' - print - - -def bswap_format(format): - '''Generate a structure that describes the format.''' - - if format.is_bitmask() and not format.is_array() and format.block_size() > 8: - print '#ifdef PIPE_ARCH_BIG_ENDIAN' - print ' pixel.value = util_bswap%u(pixel.value);' % format.block_size() - print '#endif' - - -def is_format_supported(format): - '''Determines whether we actually have the plumbing necessary to generate the - to read/write to/from this format.''' - - # FIXME: Ideally we would support any format combination here. - - if format.layout != PLAIN: - return False - - for i in range(4): - channel = format.channels[i] - if channel.type not in (VOID, UNSIGNED, SIGNED, FLOAT, FIXED): - return False - if channel.type == FLOAT and channel.size not in (16, 32, 64): - return False - - return True - - -def native_type(format): - '''Get the native appropriate for a format.''' - - if format.layout == PLAIN: - if not format.is_array(): - # For arithmetic pixel formats return the integer type that matches the whole pixel - return 'uint%u_t' % format.block_size() - else: - # For array pixel formats return the integer type that matches the color channel - channel = format.channels[0] - if channel.type in (UNSIGNED, VOID): - return 'uint%u_t' % channel.size - elif channel.type in (SIGNED, FIXED): - return 'int%u_t' % channel.size - elif channel.type == FLOAT: - if channel.size == 16: - return 'uint16_t' - elif channel.size == 32: - return 'float' - elif channel.size == 64: - return 'double' - else: - assert False - else: - assert False - else: - assert False - - -def intermediate_native_type(bits, sign): - '''Find a native type adequate to hold intermediate results of the request bit size.''' - - bytes = 4 # don't use anything smaller than 32bits - while bytes * 8 < bits: - bytes *= 2 - bits = bytes*8 - - if sign: - return 'int%u_t' % bits - else: - return 'uint%u_t' % bits - - -def get_one_shift(type): - '''Get the number of the bit that matches unity for this type.''' - if type.type == 'FLOAT': - assert False - if not type.norm: - return 0 - if type.type == UNSIGNED: - return type.size - if type.type == SIGNED: - return type.size - 1 - if type.type == FIXED: - return type.size / 2 - assert False - - -def value_to_native(type, value): - '''Get the value of unity for this type.''' - if type.type == FLOAT: - return value - if type.type == FIXED: - return int(value * (1 << (type.size/2))) - if not type.norm: - return int(value) - if type.type == UNSIGNED: - return int(value * ((1 << type.size) - 1)) - if type.type == SIGNED: - return int(value * ((1 << (type.size - 1)) - 1)) - assert False - - -def native_to_constant(type, value): - '''Get the value of unity for this type.''' - if type.type == FLOAT: - if type.size <= 32: - return "%ff" % value - else: - return "%ff" % value - else: - return str(int(value)) - - -def get_one(type): - '''Get the value of unity for this type.''' - return value_to_native(type, 1) - - -def clamp_expr(src_channel, dst_channel, dst_native_type, value): - '''Generate the expression to clamp the value in the source type to the - destination type range.''' - - if src_channel == dst_channel: - return value - - src_min = src_channel.min() - src_max = src_channel.max() - dst_min = dst_channel.min() - dst_max = dst_channel.max() - - # Translate the destination range to the src native value - dst_min_native = value_to_native(src_channel, dst_min) - dst_max_native = value_to_native(src_channel, dst_max) - - if src_min < dst_min and src_max > dst_max: - return 'CLAMP(%s, %s, %s)' % (value, dst_min_native, dst_max_native) - - if src_max > dst_max: - return 'MIN2(%s, %s)' % (value, dst_max_native) - - if src_min < dst_min: - return 'MAX2(%s, %s)' % (value, dst_min_native) - - return value - - -def conversion_expr(src_channel, - dst_channel, dst_native_type, - value, - clamp=True, - src_colorspace = RGB, - dst_colorspace = RGB): - '''Generate the expression to convert a value between two types.''' - - if src_colorspace != dst_colorspace: - if src_colorspace == SRGB: - assert src_channel.type == UNSIGNED - assert src_channel.norm - assert src_channel.size == 8 - assert dst_colorspace == RGB - if dst_channel.type == FLOAT: - return 'util_format_srgb_8unorm_to_linear_float(%s)' % value - else: - assert dst_channel.type == UNSIGNED - assert dst_channel.norm - assert dst_channel.size == 8 - return 'util_format_srgb_to_linear_8unorm(%s)' % value - elif dst_colorspace == SRGB: - assert dst_channel.type == UNSIGNED - assert dst_channel.norm - assert dst_channel.size == 8 - assert src_colorspace == RGB - if src_channel.type == FLOAT: - return 'util_format_linear_float_to_srgb_8unorm(%s)' % value - else: - assert src_channel.type == UNSIGNED - assert src_channel.norm - assert src_channel.size == 8 - return 'util_format_linear_to_srgb_8unorm(%s)' % value - elif src_colorspace == ZS: - pass - elif dst_colorspace == ZS: - pass - else: - assert 0 - - if src_channel == dst_channel: - return value - - src_type = src_channel.type - src_size = src_channel.size - src_norm = src_channel.norm - - # Promote half to float - if src_type == FLOAT and src_size == 16: - value = 'util_half_to_float(%s)' % value - src_size = 32 - - # Special case for float <-> ubytes for more accurate results - # Done before clamping since these functions already take care of that - if src_type == UNSIGNED and src_norm and src_size == 8 and dst_channel.type == FLOAT and dst_channel.size == 32: - return 'ubyte_to_float(%s)' % value - if src_type == FLOAT and src_size == 32 and dst_channel.type == UNSIGNED and dst_channel.norm and dst_channel.size == 8: - return 'float_to_ubyte(%s)' % value - - if clamp: - if dst_channel.type != FLOAT or src_type != FLOAT: - value = clamp_expr(src_channel, dst_channel, dst_native_type, value) - - if src_type in (SIGNED, UNSIGNED) and dst_channel.type in (SIGNED, UNSIGNED): - if not src_norm and not dst_channel.norm: - # neither is normalized -- just cast - return '(%s)%s' % (dst_native_type, value) - - src_one = get_one(src_channel) - dst_one = get_one(dst_channel) - - if src_one > dst_one and src_norm and dst_channel.norm: - # We can just bitshift - src_shift = get_one_shift(src_channel) - dst_shift = get_one_shift(dst_channel) - value = '(%s >> %s)' % (value, src_shift - dst_shift) - else: - # We need to rescale using an intermediate type big enough to hold the multiplication of both - tmp_native_type = intermediate_native_type(src_size + dst_channel.size, src_channel.sign and dst_channel.sign) - value = '((%s)%s)' % (tmp_native_type, value) - value = '(%s * 0x%x / 0x%x)' % (value, dst_one, src_one) - value = '(%s)%s' % (dst_native_type, value) - return value - - # Promote to either float or double - if src_type != FLOAT: - if src_norm or src_type == FIXED: - one = get_one(src_channel) - if src_size <= 23: - value = '(%s * (1.0f/0x%x))' % (value, one) - if dst_channel.size <= 32: - value = '(float)%s' % value - src_size = 32 - else: - # bigger than single precision mantissa, use double - value = '(%s * (1.0/0x%x))' % (value, one) - src_size = 64 - src_norm = False - else: - if src_size <= 23 or dst_channel.size <= 32: - value = '(float)%s' % value - src_size = 32 - else: - # bigger than single precision mantissa, use double - value = '(double)%s' % value - src_size = 64 - src_type = FLOAT - - # Convert double or float to non-float - if dst_channel.type != FLOAT: - if dst_channel.norm or dst_channel.type == FIXED: - dst_one = get_one(dst_channel) - if dst_channel.size <= 23: - value = '(%s * 0x%x)' % (value, dst_one) - else: - # bigger than single precision mantissa, use double - value = '(%s * (double)0x%x)' % (value, dst_one) - value = '(%s)%s' % (dst_native_type, value) - else: - # Cast double to float when converting to either half or float - if dst_channel.size <= 32 and src_size > 32: - value = '(float)%s' % value - src_size = 32 - - if dst_channel.size == 16: - value = 'util_float_to_half(%s)' % value - elif dst_channel.size == 64 and src_size < 64: - value = '(double)%s' % value - - return value - - -def generate_unpack_kernel(format, dst_channel, dst_native_type): - - if not is_format_supported(format): - return - - assert format.layout == PLAIN - - src_native_type = native_type(format) - - if format.is_bitmask(): - depth = format.block_size() - print ' uint%u_t value = *(const uint%u_t *)src;' % (depth, depth) - - # Declare the intermediate variables - for i in range(format.nr_channels()): - src_channel = format.channels[i] - if src_channel.type == UNSIGNED: - print ' uint%u_t %s;' % (depth, src_channel.name) - elif src_channel.type == SIGNED: - print ' int%u_t %s;' % (depth, src_channel.name) - - if depth > 8: - print '#ifdef PIPE_ARCH_BIG_ENDIAN' - print ' value = util_bswap%u(value);' % depth - print '#endif' - - # Compute the intermediate unshifted values - shift = 0 - for i in range(format.nr_channels()): - src_channel = format.channels[i] - value = 'value' - if src_channel.type == UNSIGNED: - if shift: - value = '%s >> %u' % (value, shift) - if shift + src_channel.size < depth: - value = '(%s) & 0x%x' % (value, (1 << src_channel.size) - 1) - elif src_channel.type == SIGNED: - if shift + src_channel.size < depth: - # Align the sign bit - lshift = depth - (shift + src_channel.size) - value = '%s << %u' % (value, lshift) - # Cast to signed - value = '(int%u_t)(%s) ' % (depth, value) - if src_channel.size < depth: - # Align the LSB bit - rshift = depth - src_channel.size - value = '(%s) >> %u' % (value, rshift) - else: - value = None - - if value is not None: - print ' %s = %s;' % (src_channel.name, value) - - shift += src_channel.size - - # Convert, swizzle, and store final values - for i in range(4): - swizzle = format.swizzles[i] - if swizzle < 4: - src_channel = format.channels[swizzle] - src_colorspace = format.colorspace - if src_colorspace == SRGB and i == 3: - # Alpha channel is linear - src_colorspace = RGB - value = src_channel.name - value = conversion_expr(src_channel, - dst_channel, dst_native_type, - value, - src_colorspace = src_colorspace) - elif swizzle == SWIZZLE_0: - value = '0' - elif swizzle == SWIZZLE_1: - value = get_one(dst_channel) - elif swizzle == SWIZZLE_NONE: - value = '0' - else: - assert False - print ' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]) - - else: - print ' union util_format_%s pixel;' % format.short_name() - print ' memcpy(&pixel, src, sizeof pixel);' - bswap_format(format) - - for i in range(4): - swizzle = format.swizzles[i] - if swizzle < 4: - src_channel = format.channels[swizzle] - src_colorspace = format.colorspace - if src_colorspace == SRGB and i == 3: - # Alpha channel is linear - src_colorspace = RGB - value = 'pixel.chan.%s' % src_channel.name - value = conversion_expr(src_channel, - dst_channel, dst_native_type, - value, - src_colorspace = src_colorspace) - elif swizzle == SWIZZLE_0: - value = '0' - elif swizzle == SWIZZLE_1: - value = get_one(dst_channel) - elif swizzle == SWIZZLE_NONE: - value = '0' - else: - assert False - print ' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]) - - -def generate_pack_kernel(format, src_channel, src_native_type): - - if not is_format_supported(format): - return - - dst_native_type = native_type(format) - - assert format.layout == PLAIN - - inv_swizzle = format.inv_swizzles() - - if format.is_bitmask(): - depth = format.block_size() - print ' uint%u_t value = 0;' % depth - - shift = 0 - for i in range(4): - dst_channel = format.channels[i] - if inv_swizzle[i] is not None: - value ='src[%u]' % inv_swizzle[i] - dst_colorspace = format.colorspace - if dst_colorspace == SRGB and inv_swizzle[i] == 3: - # Alpha channel is linear - dst_colorspace = RGB - value = conversion_expr(src_channel, - dst_channel, dst_native_type, - value, - dst_colorspace = dst_colorspace) - if dst_channel.type in (UNSIGNED, SIGNED): - if shift + dst_channel.size < depth: - value = '(%s) & 0x%x' % (value, (1 << dst_channel.size) - 1) - if shift: - value = '(%s) << %u' % (value, shift) - if dst_channel.type == SIGNED: - # Cast to unsigned - value = '(uint%u_t)(%s) ' % (depth, value) - else: - value = None - if value is not None: - print ' value |= %s;' % (value) - - shift += dst_channel.size - - if depth > 8: - print '#ifdef PIPE_ARCH_BIG_ENDIAN' - print ' value = util_bswap%u(value);' % depth - print '#endif' - - print ' *(uint%u_t *)dst = value;' % depth - - else: - print ' union util_format_%s pixel;' % format.short_name() - - for i in range(4): - dst_channel = format.channels[i] - width = dst_channel.size - if inv_swizzle[i] is None: - continue - dst_colorspace = format.colorspace - if dst_colorspace == SRGB and inv_swizzle[i] == 3: - # Alpha channel is linear - dst_colorspace = RGB - value ='src[%u]' % inv_swizzle[i] - value = conversion_expr(src_channel, - dst_channel, dst_native_type, - value, - dst_colorspace = dst_colorspace) - print ' pixel.chan.%s = %s;' % (dst_channel.name, value) - - bswap_format(format) - print ' memcpy(dst, &pixel, sizeof pixel);' - - -def generate_format_unpack(format, dst_channel, dst_native_type, dst_suffix): - '''Generate the function to unpack pixels from a particular format''' - - name = format.short_name() - - print 'static INLINE void' - print 'util_format_%s_unpack_%s(%s *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, dst_suffix, dst_native_type) - print '{' - - if is_format_supported(format): - print ' unsigned x, y;' - print ' for(y = 0; y < height; y += %u) {' % (format.block_height,) - print ' %s *dst = dst_row;' % (dst_native_type) - print ' const uint8_t *src = src_row;' - print ' for(x = 0; x < width; x += %u) {' % (format.block_width,) - - generate_unpack_kernel(format, dst_channel, dst_native_type) - - print ' src += %u;' % (format.block_size() / 8,) - print ' dst += 4;' - print ' }' - print ' src_row += src_stride;' - print ' dst_row += dst_stride/sizeof(*dst_row);' - print ' }' - - print '}' - print - - -def generate_format_pack(format, src_channel, src_native_type, src_suffix): - '''Generate the function to pack pixels to a particular format''' - - name = format.short_name() - - print 'static INLINE void' - print 'util_format_%s_pack_%s(uint8_t *dst_row, unsigned dst_stride, const %s *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, src_suffix, src_native_type) - print '{' - - if is_format_supported(format): - print ' unsigned x, y;' - print ' for(y = 0; y < height; y += %u) {' % (format.block_height,) - print ' const %s *src = src_row;' % (src_native_type) - print ' uint8_t *dst = dst_row;' - print ' for(x = 0; x < width; x += %u) {' % (format.block_width,) - - generate_pack_kernel(format, src_channel, src_native_type) - - print ' src += 4;' - print ' dst += %u;' % (format.block_size() / 8,) - print ' }' - print ' dst_row += dst_stride;' - print ' src_row += src_stride/sizeof(*src_row);' - print ' }' - - print '}' - print - - -def generate_format_fetch(format, dst_channel, dst_native_type, dst_suffix): - '''Generate the function to unpack pixels from a particular format''' - - name = format.short_name() - - print 'static INLINE void' - print 'util_format_%s_fetch_%s(%s *dst, const uint8_t *src, unsigned i, unsigned j)' % (name, dst_suffix, dst_native_type) - print '{' - - if is_format_supported(format): - generate_unpack_kernel(format, dst_channel, dst_native_type) - - print '}' - print - - -def is_format_hand_written(format): - return format.layout in ('s3tc', 'rgtc', 'subsampled', 'other') or format.colorspace == ZS - - -def generate(formats): - print - print '#include "pipe/p_compiler.h"' - print '#include "u_math.h"' - print '#include "u_half.h"' - print '#include "u_format.h"' - print '#include "u_format_other.h"' - print '#include "u_format_srgb.h"' - print '#include "u_format_yuv.h"' - print '#include "u_format_zs.h"' - print - - for format in formats: - if not is_format_hand_written(format): - - if is_format_supported(format): - generate_format_type(format) - - channel = Channel(FLOAT, False, 32) - native_type = 'float' - suffix = 'rgba_float' - - generate_format_unpack(format, channel, native_type, suffix) - generate_format_pack(format, channel, native_type, suffix) - generate_format_fetch(format, channel, native_type, suffix) - - channel = Channel(UNSIGNED, True, 8) - native_type = 'uint8_t' - suffix = 'rgba_8unorm' - - generate_format_unpack(format, channel, native_type, suffix) - generate_format_pack(format, channel, native_type, suffix) - +#!/usr/bin/env python + +''' +/************************************************************************** + * + * Copyright 2009-2010 VMware, Inc. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + **************************************************************************/ + +/** + * @file + * Pixel format packing and unpacking functions. + * + * @author Jose Fonseca + */ +''' + + +from u_format_parse import * + + +def generate_format_type(format): + '''Generate a structure that describes the format.''' + + assert format.layout == PLAIN + + print 'union util_format_%s {' % format.short_name() + + if format.block_size() in (8, 16, 32, 64): + print ' uint%u_t value;' % (format.block_size(),) + + use_bitfields = False + for channel in format.channels: + if channel.size % 8 or not is_pot(channel.size): + use_bitfields = True + + print ' struct {' + for channel in format.channels: + if use_bitfields: + if channel.type == VOID: + if channel.size: + print ' unsigned %s:%u;' % (channel.name, channel.size) + elif channel.type == UNSIGNED: + print ' unsigned %s:%u;' % (channel.name, channel.size) + elif channel.type in (SIGNED, FIXED): + print ' int %s:%u;' % (channel.name, channel.size) + elif channel.type == FLOAT: + if channel.size == 64: + print ' double %s;' % (channel.name) + elif channel.size == 32: + print ' float %s;' % (channel.name) + else: + print ' unsigned %s:%u;' % (channel.name, channel.size) + else: + assert 0 + else: + assert channel.size % 8 == 0 and is_pot(channel.size) + if channel.type == VOID: + if channel.size: + print ' uint%u_t %s;' % (channel.size, channel.name) + elif channel.type == UNSIGNED: + print ' uint%u_t %s;' % (channel.size, channel.name) + elif channel.type in (SIGNED, FIXED): + print ' int%u_t %s;' % (channel.size, channel.name) + elif channel.type == FLOAT: + if channel.size == 64: + print ' double %s;' % (channel.name) + elif channel.size == 32: + print ' float %s;' % (channel.name) + elif channel.size == 16: + print ' uint16_t %s;' % (channel.name) + else: + assert 0 + else: + assert 0 + print ' } chan;' + print '};' + print + + +def bswap_format(format): + '''Generate a structure that describes the format.''' + + if format.is_bitmask() and not format.is_array() and format.block_size() > 8: + print '#ifdef PIPE_ARCH_BIG_ENDIAN' + print ' pixel.value = util_bswap%u(pixel.value);' % format.block_size() + print '#endif' + + +def is_format_supported(format): + '''Determines whether we actually have the plumbing necessary to generate the + to read/write to/from this format.''' + + # FIXME: Ideally we would support any format combination here. + + if format.layout != PLAIN: + return False + + for i in range(4): + channel = format.channels[i] + if channel.type not in (VOID, UNSIGNED, SIGNED, FLOAT, FIXED): + return False + if channel.type == FLOAT and channel.size not in (16, 32, 64): + return False + + return True + +def is_format_pure_unsigned(format): + for i in range(4): + channel = format.channels[i] + if channel.type not in (VOID, UNSIGNED): + return False + if channel.type == UNSIGNED and channel.pure == False: + return False + + return True + + +def is_format_pure_signed(format): + for i in range(4): + channel = format.channels[i] + if channel.type not in (VOID, SIGNED): + return False + if channel.type == SIGNED and channel.pure == False: + return False + + return True + +def native_type(format): + '''Get the native appropriate for a format.''' + + if format.layout == PLAIN: + if not format.is_array(): + # For arithmetic pixel formats return the integer type that matches the whole pixel + return 'uint%u_t' % format.block_size() + else: + # For array pixel formats return the integer type that matches the color channel + channel = format.channels[0] + if channel.type in (UNSIGNED, VOID): + return 'uint%u_t' % channel.size + elif channel.type in (SIGNED, FIXED): + return 'int%u_t' % channel.size + elif channel.type == FLOAT: + if channel.size == 16: + return 'uint16_t' + elif channel.size == 32: + return 'float' + elif channel.size == 64: + return 'double' + else: + assert False + else: + assert False + else: + assert False + + +def intermediate_native_type(bits, sign): + '''Find a native type adequate to hold intermediate results of the request bit size.''' + + bytes = 4 # don't use anything smaller than 32bits + while bytes * 8 < bits: + bytes *= 2 + bits = bytes*8 + + if sign: + return 'int%u_t' % bits + else: + return 'uint%u_t' % bits + + +def get_one_shift(type): + '''Get the number of the bit that matches unity for this type.''' + if type.type == 'FLOAT': + assert False + if not type.norm: + return 0 + if type.type == UNSIGNED: + return type.size + if type.type == SIGNED: + return type.size - 1 + if type.type == FIXED: + return type.size / 2 + assert False + + +def value_to_native(type, value): + '''Get the value of unity for this type.''' + if type.type == FLOAT: + return value + if type.type == FIXED: + return int(value * (1 << (type.size/2))) + if not type.norm: + return int(value) + if type.type == UNSIGNED: + return int(value * ((1 << type.size) - 1)) + if type.type == SIGNED: + return int(value * ((1 << (type.size - 1)) - 1)) + assert False + + +def native_to_constant(type, value): + '''Get the value of unity for this type.''' + if type.type == FLOAT: + if type.size <= 32: + return "%ff" % value + else: + return "%ff" % value + else: + return str(int(value)) + + +def get_one(type): + '''Get the value of unity for this type.''' + return value_to_native(type, 1) + + +def clamp_expr(src_channel, dst_channel, dst_native_type, value): + '''Generate the expression to clamp the value in the source type to the + destination type range.''' + + if src_channel == dst_channel: + return value + + src_min = src_channel.min() + src_max = src_channel.max() + dst_min = dst_channel.min() + dst_max = dst_channel.max() + + # Translate the destination range to the src native value + dst_min_native = value_to_native(src_channel, dst_min) + dst_max_native = value_to_native(src_channel, dst_max) + + if src_min < dst_min and src_max > dst_max: + return 'CLAMP(%s, %s, %s)' % (value, dst_min_native, dst_max_native) + + if src_max > dst_max: + return 'MIN2(%s, %s)' % (value, dst_max_native) + + if src_min < dst_min: + return 'MAX2(%s, %s)' % (value, dst_min_native) + + return value + + +def conversion_expr(src_channel, + dst_channel, dst_native_type, + value, + clamp=True, + src_colorspace = RGB, + dst_colorspace = RGB): + '''Generate the expression to convert a value between two types.''' + + if src_colorspace != dst_colorspace: + if src_colorspace == SRGB: + assert src_channel.type == UNSIGNED + assert src_channel.norm + assert src_channel.size == 8 + assert dst_colorspace == RGB + if dst_channel.type == FLOAT: + return 'util_format_srgb_8unorm_to_linear_float(%s)' % value + else: + assert dst_channel.type == UNSIGNED + assert dst_channel.norm + assert dst_channel.size == 8 + return 'util_format_srgb_to_linear_8unorm(%s)' % value + elif dst_colorspace == SRGB: + assert dst_channel.type == UNSIGNED + assert dst_channel.norm + assert dst_channel.size == 8 + assert src_colorspace == RGB + if src_channel.type == FLOAT: + return 'util_format_linear_float_to_srgb_8unorm(%s)' % value + else: + assert src_channel.type == UNSIGNED + assert src_channel.norm + assert src_channel.size == 8 + return 'util_format_linear_to_srgb_8unorm(%s)' % value + elif src_colorspace == ZS: + pass + elif dst_colorspace == ZS: + pass + else: + assert 0 + + if src_channel == dst_channel: + return value + + src_type = src_channel.type + src_size = src_channel.size + src_norm = src_channel.norm + src_pure = src_channel.pure + + # Promote half to float + if src_type == FLOAT and src_size == 16: + value = 'util_half_to_float(%s)' % value + src_size = 32 + + # Special case for float <-> ubytes for more accurate results + # Done before clamping since these functions already take care of that + if src_type == UNSIGNED and src_norm and src_size == 8 and dst_channel.type == FLOAT and dst_channel.size == 32: + return 'ubyte_to_float(%s)' % value + if src_type == FLOAT and src_size == 32 and dst_channel.type == UNSIGNED and dst_channel.norm and dst_channel.size == 8: + return 'float_to_ubyte(%s)' % value + + if clamp: + if dst_channel.type != FLOAT or src_type != FLOAT: + value = clamp_expr(src_channel, dst_channel, dst_native_type, value) + + if src_type in (SIGNED, UNSIGNED) and dst_channel.type in (SIGNED, UNSIGNED): + if not src_norm and not dst_channel.norm: + # neither is normalized -- just cast + return '(%s)%s' % (dst_native_type, value) + + src_one = get_one(src_channel) + dst_one = get_one(dst_channel) + + if src_one > dst_one and src_norm and dst_channel.norm: + # We can just bitshift + src_shift = get_one_shift(src_channel) + dst_shift = get_one_shift(dst_channel) + value = '(%s >> %s)' % (value, src_shift - dst_shift) + else: + # We need to rescale using an intermediate type big enough to hold the multiplication of both + tmp_native_type = intermediate_native_type(src_size + dst_channel.size, src_channel.sign and dst_channel.sign) + value = '((%s)%s)' % (tmp_native_type, value) + value = '(%s * 0x%x / 0x%x)' % (value, dst_one, src_one) + value = '(%s)%s' % (dst_native_type, value) + return value + + # Promote to either float or double + if src_type != FLOAT: + if src_norm or src_type == FIXED: + one = get_one(src_channel) + if src_size <= 23: + value = '(%s * (1.0f/0x%x))' % (value, one) + if dst_channel.size <= 32: + value = '(float)%s' % value + src_size = 32 + else: + # bigger than single precision mantissa, use double + value = '(%s * (1.0/0x%x))' % (value, one) + src_size = 64 + src_norm = False + else: + if src_size <= 23 or dst_channel.size <= 32: + value = '(float)%s' % value + src_size = 32 + else: + # bigger than single precision mantissa, use double + value = '(double)%s' % value + src_size = 64 + src_type = FLOAT + + # Convert double or float to non-float + if dst_channel.type != FLOAT: + if dst_channel.norm or dst_channel.type == FIXED: + dst_one = get_one(dst_channel) + if dst_channel.size <= 23: + value = '(%s * 0x%x)' % (value, dst_one) + else: + # bigger than single precision mantissa, use double + value = '(%s * (double)0x%x)' % (value, dst_one) + value = '(%s)%s' % (dst_native_type, value) + else: + # Cast double to float when converting to either half or float + if dst_channel.size <= 32 and src_size > 32: + value = '(float)%s' % value + src_size = 32 + + if dst_channel.size == 16: + value = 'util_float_to_half(%s)' % value + elif dst_channel.size == 64 and src_size < 64: + value = '(double)%s' % value + + return value + + +def generate_unpack_kernel(format, dst_channel, dst_native_type): + + if not is_format_supported(format): + return + + assert format.layout == PLAIN + + src_native_type = native_type(format) + + if format.is_bitmask(): + depth = format.block_size() + print ' uint%u_t value = *(const uint%u_t *)src;' % (depth, depth) + + # Declare the intermediate variables + for i in range(format.nr_channels()): + src_channel = format.channels[i] + if src_channel.type == UNSIGNED: + print ' uint%u_t %s;' % (depth, src_channel.name) + elif src_channel.type == SIGNED: + print ' int%u_t %s;' % (depth, src_channel.name) + + if depth > 8: + print '#ifdef PIPE_ARCH_BIG_ENDIAN' + print ' value = util_bswap%u(value);' % depth + print '#endif' + + # Compute the intermediate unshifted values + shift = 0 + for i in range(format.nr_channels()): + src_channel = format.channels[i] + value = 'value' + if src_channel.type == UNSIGNED: + if shift: + value = '%s >> %u' % (value, shift) + if shift + src_channel.size < depth: + value = '(%s) & 0x%x' % (value, (1 << src_channel.size) - 1) + elif src_channel.type == SIGNED: + if shift + src_channel.size < depth: + # Align the sign bit + lshift = depth - (shift + src_channel.size) + value = '%s << %u' % (value, lshift) + # Cast to signed + value = '(int%u_t)(%s) ' % (depth, value) + if src_channel.size < depth: + # Align the LSB bit + rshift = depth - src_channel.size + value = '(%s) >> %u' % (value, rshift) + else: + value = None + + if value is not None: + print ' %s = %s;' % (src_channel.name, value) + + shift += src_channel.size + + # Convert, swizzle, and store final values + for i in range(4): + swizzle = format.swizzles[i] + if swizzle < 4: + src_channel = format.channels[swizzle] + src_colorspace = format.colorspace + if src_colorspace == SRGB and i == 3: + # Alpha channel is linear + src_colorspace = RGB + value = src_channel.name + value = conversion_expr(src_channel, + dst_channel, dst_native_type, + value, + src_colorspace = src_colorspace) + elif swizzle == SWIZZLE_0: + value = '0' + elif swizzle == SWIZZLE_1: + value = get_one(dst_channel) + elif swizzle == SWIZZLE_NONE: + value = '0' + else: + assert False + print ' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]) + + else: + print ' union util_format_%s pixel;' % format.short_name() + print ' memcpy(&pixel, src, sizeof pixel);' + bswap_format(format) + + for i in range(4): + swizzle = format.swizzles[i] + if swizzle < 4: + src_channel = format.channels[swizzle] + src_colorspace = format.colorspace + if src_colorspace == SRGB and i == 3: + # Alpha channel is linear + src_colorspace = RGB + value = 'pixel.chan.%s' % src_channel.name + value = conversion_expr(src_channel, + dst_channel, dst_native_type, + value, + src_colorspace = src_colorspace) + elif swizzle == SWIZZLE_0: + value = '0' + elif swizzle == SWIZZLE_1: + value = get_one(dst_channel) + elif swizzle == SWIZZLE_NONE: + value = '0' + else: + assert False + print ' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]) + + +def generate_pack_kernel(format, src_channel, src_native_type): + + if not is_format_supported(format): + return + + dst_native_type = native_type(format) + + assert format.layout == PLAIN + + inv_swizzle = format.inv_swizzles() + + if format.is_bitmask(): + depth = format.block_size() + print ' uint%u_t value = 0;' % depth + + shift = 0 + for i in range(4): + dst_channel = format.channels[i] + if inv_swizzle[i] is not None: + value ='src[%u]' % inv_swizzle[i] + dst_colorspace = format.colorspace + if dst_colorspace == SRGB and inv_swizzle[i] == 3: + # Alpha channel is linear + dst_colorspace = RGB + value = conversion_expr(src_channel, + dst_channel, dst_native_type, + value, + dst_colorspace = dst_colorspace) + if dst_channel.type in (UNSIGNED, SIGNED): + if shift + dst_channel.size < depth: + value = '(%s) & 0x%x' % (value, (1 << dst_channel.size) - 1) + if shift: + value = '(%s) << %u' % (value, shift) + if dst_channel.type == SIGNED: + # Cast to unsigned + value = '(uint%u_t)(%s) ' % (depth, value) + else: + value = None + if value is not None: + print ' value |= %s;' % (value) + + shift += dst_channel.size + + if depth > 8: + print '#ifdef PIPE_ARCH_BIG_ENDIAN' + print ' value = util_bswap%u(value);' % depth + print '#endif' + + print ' *(uint%u_t *)dst = value;' % depth + + else: + print ' union util_format_%s pixel;' % format.short_name() + + for i in range(4): + dst_channel = format.channels[i] + width = dst_channel.size + if inv_swizzle[i] is None: + continue + dst_colorspace = format.colorspace + if dst_colorspace == SRGB and inv_swizzle[i] == 3: + # Alpha channel is linear + dst_colorspace = RGB + value ='src[%u]' % inv_swizzle[i] + value = conversion_expr(src_channel, + dst_channel, dst_native_type, + value, + dst_colorspace = dst_colorspace) + print ' pixel.chan.%s = %s;' % (dst_channel.name, value) + + bswap_format(format) + print ' memcpy(dst, &pixel, sizeof pixel);' + + +def generate_format_unpack(format, dst_channel, dst_native_type, dst_suffix): + '''Generate the function to unpack pixels from a particular format''' + + name = format.short_name() + + print 'static INLINE void' + print 'util_format_%s_unpack_%s(%s *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, dst_suffix, dst_native_type) + print '{' + + if is_format_supported(format): + print ' unsigned x, y;' + print ' for(y = 0; y < height; y += %u) {' % (format.block_height,) + print ' %s *dst = dst_row;' % (dst_native_type) + print ' const uint8_t *src = src_row;' + print ' for(x = 0; x < width; x += %u) {' % (format.block_width,) + + generate_unpack_kernel(format, dst_channel, dst_native_type) + + print ' src += %u;' % (format.block_size() / 8,) + print ' dst += 4;' + print ' }' + print ' src_row += src_stride;' + print ' dst_row += dst_stride/sizeof(*dst_row);' + print ' }' + + print '}' + print + + +def generate_format_pack(format, src_channel, src_native_type, src_suffix): + '''Generate the function to pack pixels to a particular format''' + + name = format.short_name() + + print 'static INLINE void' + print 'util_format_%s_pack_%s(uint8_t *dst_row, unsigned dst_stride, const %s *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, src_suffix, src_native_type) + print '{' + + if is_format_supported(format): + print ' unsigned x, y;' + print ' for(y = 0; y < height; y += %u) {' % (format.block_height,) + print ' const %s *src = src_row;' % (src_native_type) + print ' uint8_t *dst = dst_row;' + print ' for(x = 0; x < width; x += %u) {' % (format.block_width,) + + generate_pack_kernel(format, src_channel, src_native_type) + + print ' src += 4;' + print ' dst += %u;' % (format.block_size() / 8,) + print ' }' + print ' dst_row += dst_stride;' + print ' src_row += src_stride/sizeof(*src_row);' + print ' }' + + print '}' + print + + +def generate_format_fetch(format, dst_channel, dst_native_type, dst_suffix): + '''Generate the function to unpack pixels from a particular format''' + + name = format.short_name() + + print 'static INLINE void' + print 'util_format_%s_fetch_%s(%s *dst, const uint8_t *src, unsigned i, unsigned j)' % (name, dst_suffix, dst_native_type) + print '{' + + if is_format_supported(format): + generate_unpack_kernel(format, dst_channel, dst_native_type) + + print '}' + print + + +def is_format_hand_written(format): + return format.layout in ('s3tc', 'rgtc', 'subsampled', 'other') or format.colorspace == ZS + + +def generate(formats): + print + print '#include "pipe/p_compiler.h"' + print '#include "u_math.h"' + print '#include "u_half.h"' + print '#include "u_format.h"' + print '#include "u_format_other.h"' + print '#include "u_format_srgb.h"' + print '#include "u_format_yuv.h"' + print '#include "u_format_zs.h"' + print + + for format in formats: + if not is_format_hand_written(format): + + if is_format_supported(format): + generate_format_type(format) + + if is_format_pure_unsigned(format): + native_type = 'unsigned' + suffix = 'unsigned' + channel = Channel(UNSIGNED, False, True, 32) + + generate_format_unpack(format, channel, native_type, suffix) + generate_format_pack(format, channel, native_type, suffix) + + channel = Channel(SIGNED, False, True, 32) + native_type = 'int' + suffix = 'signed' + generate_format_unpack(format, channel, native_type, suffix) + generate_format_pack(format, channel, native_type, suffix) + elif is_format_pure_signed(format): + native_type = 'int' + suffix = 'signed' + channel = Channel(SIGNED, False, True, 32) + + generate_format_unpack(format, channel, native_type, suffix) + generate_format_pack(format, channel, native_type, suffix) + + native_type = 'unsigned' + suffix = 'unsigned' + channel = Channel(UNSIGNED, False, True, 32) + generate_format_unpack(format, channel, native_type, suffix) + generate_format_pack(format, channel, native_type, suffix) + else: + channel = Channel(FLOAT, False, False, 32) + native_type = 'float' + suffix = 'rgba_float' + + generate_format_unpack(format, channel, native_type, suffix) + generate_format_pack(format, channel, native_type, suffix) + generate_format_fetch(format, channel, native_type, suffix) + + channel = Channel(UNSIGNED, True, False, 8) + native_type = 'uint8_t' + suffix = 'rgba_8unorm' + + generate_format_unpack(format, channel, native_type, suffix) + generate_format_pack(format, channel, native_type, suffix) + diff --git a/mesalib/src/gallium/auxiliary/util/u_format_parse.py b/mesalib/src/gallium/auxiliary/util/u_format_parse.py index f47f65d26..73a4bcb21 100644 --- a/mesalib/src/gallium/auxiliary/util/u_format_parse.py +++ b/mesalib/src/gallium/auxiliary/util/u_format_parse.py @@ -1,291 +1,303 @@ -#!/usr/bin/env python - -''' -/************************************************************************** - * - * Copyright 2009 VMware, Inc. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ -''' - - -VOID, UNSIGNED, SIGNED, FIXED, FLOAT = range(5) - -SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_0, SWIZZLE_1, SWIZZLE_NONE, = range(7) - -PLAIN = 'plain' - -RGB = 'rgb' -SRGB = 'srgb' -YUV = 'yuv' -ZS = 'zs' - - -def is_pot(x): - return (x & (x - 1)) == 0 - - -VERY_LARGE = 99999999999999999999999 - - -class Channel: - '''Describe the channel of a color channel.''' - - def __init__(self, type, norm, size, name = ''): - self.type = type - self.norm = norm - self.size = size - self.sign = type in (SIGNED, FIXED, FLOAT) - self.name = name - - def __str__(self): - s = str(self.type) - if self.norm: - s += 'n' - s += str(self.size) - return s - - def __eq__(self, other): - return self.type == other.type and self.norm == other.norm and self.size == other.size - - def max(self): - '''Maximum representable number.''' - if self.type == FLOAT: - return VERY_LARGE - if self.type == FIXED: - return (1 << (self.size/2)) - 1 - if self.norm: - return 1 - if self.type == UNSIGNED: - return (1 << self.size) - 1 - if self.type == SIGNED: - return (1 << (self.size - 1)) - 1 - assert False - - def min(self): - '''Minimum representable number.''' - if self.type == FLOAT: - return -VERY_LARGE - if self.type == FIXED: - return -(1 << (self.size/2)) - if self.type == UNSIGNED: - return 0 - if self.norm: - return -1 - if self.type == SIGNED: - return -(1 << (self.size - 1)) - assert False - - -class Format: - '''Describe a pixel format.''' - - def __init__(self, name, layout, block_width, block_height, channels, swizzles, colorspace): - self.name = name - self.layout = layout - self.block_width = block_width - self.block_height = block_height - self.channels = channels - self.swizzles = swizzles - self.name = name - self.colorspace = colorspace - - def __str__(self): - return self.name - - def short_name(self): - '''Make up a short norm for a format, suitable to be used as suffix in - function names.''' - - name = self.name - if name.startswith('PIPE_FORMAT_'): - name = name[len('PIPE_FORMAT_'):] - name = name.lower() - return name - - def block_size(self): - size = 0 - for channel in self.channels: - size += channel.size - return size - - def nr_channels(self): - nr_channels = 0 - for channel in self.channels: - if channel.size: - nr_channels += 1 - return nr_channels - - def is_array(self): - if self.layout != PLAIN: - return False - ref_channel = self.channels[0] - for channel in self.channels[1:]: - if channel.size and (channel.size != ref_channel.size or channel.size % 8): - return False - return True - - def is_mixed(self): - if self.layout != PLAIN: - return False - ref_channel = self.channels[0] - if ref_channel.type == VOID: - ref_channel = self.channels[1] - for channel in self.channels[1:]: - if channel.type != VOID: - if channel.type != ref_channel.type: - return True - if channel.norm != ref_channel.norm: - return True - return False - - def is_pot(self): - return is_pot(self.block_size()) - - def is_int(self): - if self.layout != PLAIN: - return False - for channel in self.channels: - if channel.type not in (VOID, UNSIGNED, SIGNED): - return False - return True - - def is_float(self): - if self.layout != PLAIN: - return False - for channel in self.channels: - if channel.type not in (VOID, FLOAT): - return False - return True - - def is_bitmask(self): - if self.layout != PLAIN: - return False - if self.block_size() not in (8, 16, 32): - return False - for channel in self.channels: - if channel.type not in (VOID, UNSIGNED, SIGNED): - return False - return True - - def inv_swizzles(self): - '''Return an array[4] of inverse swizzle terms''' - inv_swizzle = [None]*4 - for i in range(4): - swizzle = self.swizzles[i] - if swizzle < 4: - inv_swizzle[swizzle] = i - return inv_swizzle - - def stride(self): - return self.block_size()/8 - - -_type_parse_map = { - '': VOID, - 'x': VOID, - 'u': UNSIGNED, - 's': SIGNED, - 'h': FIXED, - 'f': FLOAT, -} - -_swizzle_parse_map = { - 'x': SWIZZLE_X, - 'y': SWIZZLE_Y, - 'z': SWIZZLE_Z, - 'w': SWIZZLE_W, - '0': SWIZZLE_0, - '1': SWIZZLE_1, - '_': SWIZZLE_NONE, -} - -def parse(filename): - '''Parse the format descrition in CSV format in terms of the - Channel and Format classes above.''' - - stream = open(filename) - formats = [] - for line in stream: - try: - comment = line.index('#') - except ValueError: - pass - else: - line = line[:comment] - line = line.strip() - if not line: - continue - - fields = [field.strip() for field in line.split(',')] - - name = fields[0] - layout = fields[1] - block_width, block_height = map(int, fields[2:4]) - - swizzles = [_swizzle_parse_map[swizzle] for swizzle in fields[8]] - colorspace = fields[9] - - if layout == PLAIN: - names = ['']*4 - if colorspace in (RGB, SRGB): - for i in range(4): - swizzle = swizzles[i] - if swizzle < 4: - names[swizzle] += 'rgba'[i] - elif colorspace == ZS: - for i in range(4): - swizzle = swizzles[i] - if swizzle < 4: - names[swizzle] += 'zs'[i] - else: - assert False - for i in range(4): - if names[i] == '': - names[i] = 'x' - else: - names = ['x', 'y', 'z', 'w'] - - channels = [] - for i in range(0, 4): - field = fields[4 + i] - if field: - type = _type_parse_map[field[0]] - if field[1] == 'n': - norm = True - size = int(field[2:]) - else: - norm = False - size = int(field[1:]) - else: - type = VOID - norm = False - size = 0 - channel = Channel(type, norm, size, names[i]) - channels.append(channel) - - format = Format(name, layout, block_width, block_height, channels, swizzles, colorspace) - formats.append(format) - return formats - +#!/usr/bin/env python + +''' +/************************************************************************** + * + * Copyright 2009 VMware, Inc. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + **************************************************************************/ +''' + + +VOID, UNSIGNED, SIGNED, FIXED, FLOAT = range(5) + +SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_0, SWIZZLE_1, SWIZZLE_NONE, = range(7) + +PLAIN = 'plain' + +RGB = 'rgb' +SRGB = 'srgb' +YUV = 'yuv' +ZS = 'zs' + + +def is_pot(x): + return (x & (x - 1)) == 0 + + +VERY_LARGE = 99999999999999999999999 + + +class Channel: + '''Describe the channel of a color channel.''' + + def __init__(self, type, norm, pure, size, name = ''): + self.type = type + self.norm = norm + self.pure = pure + self.size = size + self.sign = type in (SIGNED, FIXED, FLOAT) + self.name = name + + def __str__(self): + s = str(self.type) + if self.norm: + s += 'n' + if self.pure: + s += 'p' + s += str(self.size) + return s + + def __eq__(self, other): + return self.type == other.type and self.norm == other.norm and self.pure == other.pure and self.size == other.size + + def max(self): + '''Maximum representable number.''' + if self.type == FLOAT: + return VERY_LARGE + if self.type == FIXED: + return (1 << (self.size/2)) - 1 + if self.norm: + return 1 + if self.type == UNSIGNED: + return (1 << self.size) - 1 + if self.type == SIGNED: + return (1 << (self.size - 1)) - 1 + assert False + + def min(self): + '''Minimum representable number.''' + if self.type == FLOAT: + return -VERY_LARGE + if self.type == FIXED: + return -(1 << (self.size/2)) + if self.type == UNSIGNED: + return 0 + if self.norm: + return -1 + if self.type == SIGNED: + return -(1 << (self.size - 1)) + assert False + + +class Format: + '''Describe a pixel format.''' + + def __init__(self, name, layout, block_width, block_height, channels, swizzles, colorspace): + self.name = name + self.layout = layout + self.block_width = block_width + self.block_height = block_height + self.channels = channels + self.swizzles = swizzles + self.name = name + self.colorspace = colorspace + + def __str__(self): + return self.name + + def short_name(self): + '''Make up a short norm for a format, suitable to be used as suffix in + function names.''' + + name = self.name + if name.startswith('PIPE_FORMAT_'): + name = name[len('PIPE_FORMAT_'):] + name = name.lower() + return name + + def block_size(self): + size = 0 + for channel in self.channels: + size += channel.size + return size + + def nr_channels(self): + nr_channels = 0 + for channel in self.channels: + if channel.size: + nr_channels += 1 + return nr_channels + + def is_array(self): + if self.layout != PLAIN: + return False + ref_channel = self.channels[0] + for channel in self.channels[1:]: + if channel.size and (channel.size != ref_channel.size or channel.size % 8): + return False + return True + + def is_mixed(self): + if self.layout != PLAIN: + return False + ref_channel = self.channels[0] + if ref_channel.type == VOID: + ref_channel = self.channels[1] + for channel in self.channels[1:]: + if channel.type != VOID: + if channel.type != ref_channel.type: + return True + if channel.norm != ref_channel.norm: + return True + if channel.pure != ref_channel.pure: + return True + return False + + def is_pot(self): + return is_pot(self.block_size()) + + def is_int(self): + if self.layout != PLAIN: + return False + for channel in self.channels: + if channel.type not in (VOID, UNSIGNED, SIGNED): + return False + return True + + def is_float(self): + if self.layout != PLAIN: + return False + for channel in self.channels: + if channel.type not in (VOID, FLOAT): + return False + return True + + def is_bitmask(self): + if self.layout != PLAIN: + return False + if self.block_size() not in (8, 16, 32): + return False + for channel in self.channels: + if channel.type not in (VOID, UNSIGNED, SIGNED): + return False + return True + + def inv_swizzles(self): + '''Return an array[4] of inverse swizzle terms''' + inv_swizzle = [None]*4 + for i in range(4): + swizzle = self.swizzles[i] + if swizzle < 4: + inv_swizzle[swizzle] = i + return inv_swizzle + + def stride(self): + return self.block_size()/8 + + +_type_parse_map = { + '': VOID, + 'x': VOID, + 'u': UNSIGNED, + 's': SIGNED, + 'h': FIXED, + 'f': FLOAT, +} + +_swizzle_parse_map = { + 'x': SWIZZLE_X, + 'y': SWIZZLE_Y, + 'z': SWIZZLE_Z, + 'w': SWIZZLE_W, + '0': SWIZZLE_0, + '1': SWIZZLE_1, + '_': SWIZZLE_NONE, +} + +def parse(filename): + '''Parse the format descrition in CSV format in terms of the + Channel and Format classes above.''' + + stream = open(filename) + formats = [] + for line in stream: + try: + comment = line.index('#') + except ValueError: + pass + else: + line = line[:comment] + line = line.strip() + if not line: + continue + + fields = [field.strip() for field in line.split(',')] + + name = fields[0] + layout = fields[1] + block_width, block_height = map(int, fields[2:4]) + + swizzles = [_swizzle_parse_map[swizzle] for swizzle in fields[8]] + colorspace = fields[9] + + if layout == PLAIN: + names = ['']*4 + if colorspace in (RGB, SRGB): + for i in range(4): + swizzle = swizzles[i] + if swizzle < 4: + names[swizzle] += 'rgba'[i] + elif colorspace == ZS: + for i in range(4): + swizzle = swizzles[i] + if swizzle < 4: + names[swizzle] += 'zs'[i] + else: + assert False + for i in range(4): + if names[i] == '': + names[i] = 'x' + else: + names = ['x', 'y', 'z', 'w'] + + channels = [] + for i in range(0, 4): + field = fields[4 + i] + if field: + type = _type_parse_map[field[0]] + if field[1] == 'n': + norm = True + pure = False + size = int(field[2:]) + elif field[1] == 'p': + pure = True + norm = False + size = int(field[2:]) + else: + norm = False + pure = False + size = int(field[1:]) + else: + type = VOID + norm = False + pure = False + size = 0 + channel = Channel(type, norm, pure, size, names[i]) + channels.append(channel) + + format = Format(name, layout, block_width, block_height, channels, swizzles, colorspace) + formats.append(format) + return formats + diff --git a/mesalib/src/gallium/auxiliary/util/u_format_table.py b/mesalib/src/gallium/auxiliary/util/u_format_table.py index addc47481..6c00b0df3 100644 --- a/mesalib/src/gallium/auxiliary/util/u_format_table.py +++ b/mesalib/src/gallium/auxiliary/util/u_format_table.py @@ -1,197 +1,212 @@ -#!/usr/bin/env python - -CopyRight = ''' -/************************************************************************** - * - * Copyright 2010 VMware, Inc. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ -''' - - -import sys - -from u_format_parse import * -import u_format_pack - - -def layout_map(layout): - return 'UTIL_FORMAT_LAYOUT_' + str(layout).upper() - - -def colorspace_map(colorspace): - return 'UTIL_FORMAT_COLORSPACE_' + str(colorspace).upper() - - -colorspace_channels_map = { - 'rgb': ['r', 'g', 'b', 'a'], - 'srgb': ['sr', 'sg', 'sb', 'a'], - 'zs': ['z', 's'], - 'yuv': ['y', 'u', 'v'], -} - - -type_map = { - VOID: "UTIL_FORMAT_TYPE_VOID", - UNSIGNED: "UTIL_FORMAT_TYPE_UNSIGNED", - SIGNED: "UTIL_FORMAT_TYPE_SIGNED", - FIXED: "UTIL_FORMAT_TYPE_FIXED", - FLOAT: "UTIL_FORMAT_TYPE_FLOAT", -} - - -def bool_map(value): - if value: - return "TRUE" - else: - return "FALSE" - - -swizzle_map = { - SWIZZLE_X: "UTIL_FORMAT_SWIZZLE_X", - SWIZZLE_Y: "UTIL_FORMAT_SWIZZLE_Y", - SWIZZLE_Z: "UTIL_FORMAT_SWIZZLE_Z", - SWIZZLE_W: "UTIL_FORMAT_SWIZZLE_W", - SWIZZLE_0: "UTIL_FORMAT_SWIZZLE_0", - SWIZZLE_1: "UTIL_FORMAT_SWIZZLE_1", - SWIZZLE_NONE: "UTIL_FORMAT_SWIZZLE_NONE", -} - - -def write_format_table(formats): - print '/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */' - print - # This will print the copyright message on the top of this file - print CopyRight.strip() - print - print '#include "u_format.h"' - print '#include "u_format_s3tc.h"' - print '#include "u_format_rgtc.h"' - print '#include "u_format_latc.h"' - print - - u_format_pack.generate(formats) - - for format in formats: - print 'const struct util_format_description' - print 'util_format_%s_description = {' % (format.short_name(),) - print " %s," % (format.name,) - print " \"%s\"," % (format.name,) - print " \"%s\"," % (format.short_name(),) - print " {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size()) - print " %s," % (layout_map(format.layout),) - print " %u,\t/* nr_channels */" % (format.nr_channels(),) - print " %s,\t/* is_array */" % (bool_map(format.is_array()),) - print " %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),) - print " %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),) - print " {" - for i in range(4): - channel = format.channels[i] - if i < 3: - sep = "," - else: - sep = "" - if channel.size: - print " {%s, %s, %u}%s\t/* %s = %s */" % (type_map[channel.type], bool_map(channel.norm), channel.size, sep, "xyzw"[i], channel.name) - else: - print " {0, 0, 0}%s" % (sep,) - print " }," - print " {" - for i in range(4): - swizzle = format.swizzles[i] - if i < 3: - sep = "," - else: - sep = "" - try: - comment = colorspace_channels_map[format.colorspace][i] - except (KeyError, IndexError): - comment = 'ignored' - print " %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment) - print " }," - print " %s," % (colorspace_map(format.colorspace),) - if format.colorspace != ZS: - print " &util_format_%s_unpack_rgba_8unorm," % format.short_name() - print " &util_format_%s_pack_rgba_8unorm," % format.short_name() - if format.layout == 's3tc' or format.layout == 'rgtc': - print " &util_format_%s_fetch_rgba_8unorm," % format.short_name() - else: - print " NULL, /* fetch_rgba_8unorm */" - print " &util_format_%s_unpack_rgba_float," % format.short_name() - print " &util_format_%s_pack_rgba_float," % format.short_name() - print " &util_format_%s_fetch_rgba_float," % format.short_name() - else: - print " NULL, /* unpack_rgba_8unorm */" - print " NULL, /* pack_rgba_8unorm */" - print " NULL, /* fetch_rgba_8unorm */" - print " NULL, /* unpack_rgba_float */" - print " NULL, /* pack_rgba_float */" - print " NULL, /* fetch_rgba_float */" - if format.colorspace == ZS and format.swizzles[0] != SWIZZLE_NONE: - print " &util_format_%s_unpack_z_32unorm," % format.short_name() - print " &util_format_%s_pack_z_32unorm," % format.short_name() - print " &util_format_%s_unpack_z_float," % format.short_name() - print " &util_format_%s_pack_z_float," % format.short_name() - else: - print " NULL, /* unpack_z_32unorm */" - print " NULL, /* pack_z_32unorm */" - print " NULL, /* unpack_z_float */" - print " NULL, /* pack_z_float */" - if format.colorspace == ZS and format.swizzles[1] != SWIZZLE_NONE: - print " &util_format_%s_unpack_s_8uscaled," % format.short_name() - print " &util_format_%s_pack_s_8uscaled" % format.short_name() - else: - print " NULL, /* unpack_s_8uscaled */" - print " NULL /* pack_s_8uscaled */" - print "};" - print - - print "const struct util_format_description *" - print "util_format_description(enum pipe_format format)" - print "{" - print " if (format >= PIPE_FORMAT_COUNT) {" - print " return NULL;" - print " }" - print - print " switch (format) {" - for format in formats: - print " case %s:" % format.name - print " return &util_format_%s_description;" % (format.short_name(),) - print " default:" - print " return NULL;" - print " }" - print "}" - print - - -def main(): - - formats = [] - for arg in sys.argv[1:]: - formats.extend(parse(arg)) - write_format_table(formats) - - -if __name__ == '__main__': - main() +#!/usr/bin/env python + +CopyRight = ''' +/************************************************************************** + * + * Copyright 2010 VMware, Inc. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + **************************************************************************/ +''' + + +import sys + +from u_format_parse import * +import u_format_pack + + +def layout_map(layout): + return 'UTIL_FORMAT_LAYOUT_' + str(layout).upper() + + +def colorspace_map(colorspace): + return 'UTIL_FORMAT_COLORSPACE_' + str(colorspace).upper() + + +colorspace_channels_map = { + 'rgb': ['r', 'g', 'b', 'a'], + 'srgb': ['sr', 'sg', 'sb', 'a'], + 'zs': ['z', 's'], + 'yuv': ['y', 'u', 'v'], +} + + +type_map = { + VOID: "UTIL_FORMAT_TYPE_VOID", + UNSIGNED: "UTIL_FORMAT_TYPE_UNSIGNED", + SIGNED: "UTIL_FORMAT_TYPE_SIGNED", + FIXED: "UTIL_FORMAT_TYPE_FIXED", + FLOAT: "UTIL_FORMAT_TYPE_FLOAT", +} + + +def bool_map(value): + if value: + return "TRUE" + else: + return "FALSE" + + +swizzle_map = { + SWIZZLE_X: "UTIL_FORMAT_SWIZZLE_X", + SWIZZLE_Y: "UTIL_FORMAT_SWIZZLE_Y", + SWIZZLE_Z: "UTIL_FORMAT_SWIZZLE_Z", + SWIZZLE_W: "UTIL_FORMAT_SWIZZLE_W", + SWIZZLE_0: "UTIL_FORMAT_SWIZZLE_0", + SWIZZLE_1: "UTIL_FORMAT_SWIZZLE_1", + SWIZZLE_NONE: "UTIL_FORMAT_SWIZZLE_NONE", +} + + +def write_format_table(formats): + print '/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */' + print + # This will print the copyright message on the top of this file + print CopyRight.strip() + print + print '#include "u_format.h"' + print '#include "u_format_s3tc.h"' + print '#include "u_format_rgtc.h"' + print '#include "u_format_latc.h"' + print + + u_format_pack.generate(formats) + + for format in formats: + print 'const struct util_format_description' + print 'util_format_%s_description = {' % (format.short_name(),) + print " %s," % (format.name,) + print " \"%s\"," % (format.name,) + print " \"%s\"," % (format.short_name(),) + print " {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size()) + print " %s," % (layout_map(format.layout),) + print " %u,\t/* nr_channels */" % (format.nr_channels(),) + print " %s,\t/* is_array */" % (bool_map(format.is_array()),) + print " %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),) + print " %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),) + print " {" + for i in range(4): + channel = format.channels[i] + if i < 3: + sep = "," + else: + sep = "" + if channel.size: + print " {%s, %s, %s, %u}%s\t/* %s = %s */" % (type_map[channel.type], bool_map(channel.norm), bool_map(channel.pure), channel.size, sep, "xyzw"[i], channel.name) + else: + print " {0, 0, 0}%s" % (sep,) + print " }," + print " {" + for i in range(4): + swizzle = format.swizzles[i] + if i < 3: + sep = "," + else: + sep = "" + try: + comment = colorspace_channels_map[format.colorspace][i] + except (KeyError, IndexError): + comment = 'ignored' + print " %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment) + print " }," + print " %s," % (colorspace_map(format.colorspace),) + if format.colorspace != ZS and format.channels[0].pure == False: + print " &util_format_%s_unpack_rgba_8unorm," % format.short_name() + print " &util_format_%s_pack_rgba_8unorm," % format.short_name() + if format.layout == 's3tc' or format.layout == 'rgtc': + print " &util_format_%s_fetch_rgba_8unorm," % format.short_name() + else: + print " NULL, /* fetch_rgba_8unorm */" + print " &util_format_%s_unpack_rgba_float," % format.short_name() + print " &util_format_%s_pack_rgba_float," % format.short_name() + print " &util_format_%s_fetch_rgba_float," % format.short_name() + else: + print " NULL, /* unpack_rgba_8unorm */" + print " NULL, /* pack_rgba_8unorm */" + print " NULL, /* fetch_rgba_8unorm */" + print " NULL, /* unpack_rgba_float */" + print " NULL, /* pack_rgba_float */" + print " NULL, /* fetch_rgba_float */" + if format.colorspace == ZS and format.swizzles[0] != SWIZZLE_NONE: + print " &util_format_%s_unpack_z_32unorm," % format.short_name() + print " &util_format_%s_pack_z_32unorm," % format.short_name() + print " &util_format_%s_unpack_z_float," % format.short_name() + print " &util_format_%s_pack_z_float," % format.short_name() + else: + print " NULL, /* unpack_z_32unorm */" + print " NULL, /* pack_z_32unorm */" + print " NULL, /* unpack_z_float */" + print " NULL, /* pack_z_float */" + if format.colorspace == ZS and format.swizzles[1] != SWIZZLE_NONE: + print " &util_format_%s_unpack_s_8uscaled," % format.short_name() + print " &util_format_%s_pack_s_8uscaled," % format.short_name() + else: + print " NULL, /* unpack_s_8uscaled */" + print " NULL, /* pack_s_8uscaled */" + if format.channels[0].pure == True and format.channels[0].type == UNSIGNED: + print " &util_format_%s_unpack_unsigned, /* unpack_rgba_uint */" % format.short_name() + print " &util_format_%s_pack_unsigned, /* pack_rgba_uint */" % format.short_name() + print " &util_format_%s_unpack_signed, /* unpack_rgba_sint */" % format.short_name() + print " &util_format_%s_pack_signed /* pack_rgba_sint */" % format.short_name() + elif format.channels[0].pure == True and format.channels[0].type == SIGNED: + print " &util_format_%s_unpack_unsigned, /* unpack_rgba_uint */" % format.short_name() + print " &util_format_%s_pack_unsigned, /* pack_rgba_uint */" % format.short_name() + print " &util_format_%s_unpack_signed, /* unpack_rgba_sint */" % format.short_name() + print " &util_format_%s_pack_signed /* pack_rgba_sint */" % format.short_name() + else: + print " NULL, /* unpack_rgba_uint */" + print " NULL, /* pack_rgba_uint */" + print " NULL, /* unpack_rgba_sint */" + print " NULL /* pack_rgba_sint */" + print "};" + print + + print "const struct util_format_description *" + print "util_format_description(enum pipe_format format)" + print "{" + print " if (format >= PIPE_FORMAT_COUNT) {" + print " return NULL;" + print " }" + print + print " switch (format) {" + for format in formats: + print " case %s:" % format.name + print " return &util_format_%s_description;" % (format.short_name(),) + print " default:" + print " return NULL;" + print " }" + print "}" + print + + +def main(): + + formats = [] + for arg in sys.argv[1:]: + formats.extend(parse(arg)) + write_format_table(formats) + + +if __name__ == '__main__': + main() diff --git a/mesalib/src/gallium/auxiliary/util/u_tile.c b/mesalib/src/gallium/auxiliary/util/u_tile.c index 23f12e5f4..6342bfbfc 100644 --- a/mesalib/src/gallium/auxiliary/util/u_tile.c +++ b/mesalib/src/gallium/auxiliary/util/u_tile.c @@ -389,6 +389,29 @@ pipe_tile_raw_to_rgba(enum pipe_format format, } } +void +pipe_tile_raw_to_unsigned(enum pipe_format format, + void *src, + uint w, uint h, + unsigned *dst, unsigned dst_stride) +{ + util_format_read_4ui(format, + dst, dst_stride * sizeof(float), + src, util_format_get_stride(format, w), + 0, 0, w, h); +} + +void +pipe_tile_raw_to_signed(enum pipe_format format, + void *src, + uint w, uint h, + int *dst, unsigned dst_stride) +{ + util_format_read_4i(format, + dst, dst_stride * sizeof(float), + src, util_format_get_stride(format, w), + 0, 0, w, h); +} void pipe_get_tile_rgba(struct pipe_context *pipe, @@ -492,6 +515,61 @@ pipe_put_tile_rgba_format(struct pipe_context *pipe, FREE(packed); } +void +pipe_put_tile_i_format(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + enum pipe_format format, + const int *p) +{ + unsigned src_stride = w * 4; + void *packed; + + if (u_clip_tile(x, y, &w, &h, &pt->box)) + return; + + packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format)); + + if (!packed) + return; + + util_format_write_4i(format, + p, src_stride * sizeof(float), + packed, util_format_get_stride(format, w), + 0, 0, w, h); + + pipe_put_tile_raw(pipe, pt, x, y, w, h, packed, 0); + + FREE(packed); +} + +void +pipe_put_tile_ui_format(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + enum pipe_format format, + const unsigned int *p) +{ + unsigned src_stride = w * 4; + void *packed; + + if (u_clip_tile(x, y, &w, &h, &pt->box)) + return; + + packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format)); + + if (!packed) + return; + + util_format_write_4ui(format, + p, src_stride * sizeof(float), + packed, util_format_get_stride(format, w), + 0, 0, w, h); + + pipe_put_tile_raw(pipe, pt, x, y, w, h, packed, 0); + + FREE(packed); +} /** * Get a block of Z values, converted to 32-bit range. @@ -688,3 +766,63 @@ pipe_put_tile_z(struct pipe_context *pipe, } +void +pipe_get_tile_ui_format(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + enum pipe_format format, + unsigned int *p) +{ + unsigned dst_stride = w * 4; + void *packed; + + if (u_clip_tile(x, y, &w, &h, &pt->box)) { + return; + } + + packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format)); + if (!packed) { + return; + } + + if (format == PIPE_FORMAT_UYVY || format == PIPE_FORMAT_YUYV) { + assert((x & 1) == 0); + } + + pipe_get_tile_raw(pipe, pt, x, y, w, h, packed, 0); + + pipe_tile_raw_to_unsigned(format, packed, w, h, p, dst_stride); + + FREE(packed); +} + + +void +pipe_get_tile_i_format(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + enum pipe_format format, + int *p) +{ + unsigned dst_stride = w * 4; + void *packed; + + if (u_clip_tile(x, y, &w, &h, &pt->box)) { + return; + } + + packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format)); + if (!packed) { + return; + } + + if (format == PIPE_FORMAT_UYVY || format == PIPE_FORMAT_YUYV) { + assert((x & 1) == 0); + } + + pipe_get_tile_raw(pipe, pt, x, y, w, h, packed, 0); + + pipe_tile_raw_to_signed(format, packed, w, h, p, dst_stride); + + FREE(packed); +} diff --git a/mesalib/src/gallium/auxiliary/util/u_tile.h b/mesalib/src/gallium/auxiliary/util/u_tile.h index 0abc95e2b..926f1695f 100644 --- a/mesalib/src/gallium/auxiliary/util/u_tile.h +++ b/mesalib/src/gallium/auxiliary/util/u_tile.h @@ -1,126 +1,165 @@ -/************************************************************************** - * - * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - -#ifndef P_TILE_H -#define P_TILE_H - -#include "pipe/p_compiler.h" -#include "pipe/p_format.h" -#include "pipe/p_state.h" - -struct pipe_context; -struct pipe_transfer; - -/** - * Clip tile against transfer dims. - * - * XXX: this only clips width and height! - * - * \return TRUE if tile is totally clipped, FALSE otherwise - */ -static INLINE boolean -u_clip_tile(uint x, uint y, uint *w, uint *h, const struct pipe_box *box) -{ - if (x >= box->width) - return TRUE; - if (y >= box->height) - return TRUE; - if (x + *w > box->width) - *w = box->width - x; - if (y + *h > box->height) - *h = box->height - y; - return FALSE; -} - -#ifdef __cplusplus -extern "C" { -#endif - -void -pipe_get_tile_raw(struct pipe_context *pipe, - struct pipe_transfer *pt, - uint x, uint y, uint w, uint h, - void *p, int dst_stride); - -void -pipe_put_tile_raw(struct pipe_context *pipe, - struct pipe_transfer *pt, - uint x, uint y, uint w, uint h, - const void *p, int src_stride); - - -void -pipe_get_tile_rgba(struct pipe_context *pipe, - struct pipe_transfer *pt, - uint x, uint y, uint w, uint h, - float *p); - -void -pipe_get_tile_rgba_format(struct pipe_context *pipe, - struct pipe_transfer *pt, - uint x, uint y, uint w, uint h, - enum pipe_format format, - float *p); - -void -pipe_put_tile_rgba(struct pipe_context *pipe, - struct pipe_transfer *pt, - uint x, uint y, uint w, uint h, - const float *p); - -void -pipe_put_tile_rgba_format(struct pipe_context *pipe, - struct pipe_transfer *pt, - uint x, uint y, uint w, uint h, - enum pipe_format format, - const float *p); - - -void -pipe_get_tile_z(struct pipe_context *pipe, - struct pipe_transfer *pt, - uint x, uint y, uint w, uint h, - uint *z); - -void -pipe_put_tile_z(struct pipe_context *pipe, - struct pipe_transfer *pt, - uint x, uint y, uint w, uint h, - const uint *z); - -void -pipe_tile_raw_to_rgba(enum pipe_format format, - void *src, - uint w, uint h, - float *dst, unsigned dst_stride); - - -#ifdef __cplusplus -} -#endif - -#endif +/************************************************************************** + * + * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + **************************************************************************/ + +#ifndef P_TILE_H +#define P_TILE_H + +#include "pipe/p_compiler.h" +#include "pipe/p_format.h" +#include "pipe/p_state.h" + +struct pipe_context; +struct pipe_transfer; + +/** + * Clip tile against transfer dims. + * + * XXX: this only clips width and height! + * + * \return TRUE if tile is totally clipped, FALSE otherwise + */ +static INLINE boolean +u_clip_tile(uint x, uint y, uint *w, uint *h, const struct pipe_box *box) +{ + if (x >= box->width) + return TRUE; + if (y >= box->height) + return TRUE; + if (x + *w > box->width) + *w = box->width - x; + if (y + *h > box->height) + *h = box->height - y; + return FALSE; +} + +#ifdef __cplusplus +extern "C" { +#endif + +void +pipe_get_tile_raw(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + void *p, int dst_stride); + +void +pipe_put_tile_raw(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + const void *p, int src_stride); + + +void +pipe_get_tile_rgba(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + float *p); + +void +pipe_get_tile_rgba_format(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + enum pipe_format format, + float *p); + +void +pipe_put_tile_rgba(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + const float *p); + +void +pipe_put_tile_rgba_format(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + enum pipe_format format, + const float *p); + + +void +pipe_get_tile_z(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + uint *z); + +void +pipe_put_tile_z(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + const uint *z); + +void +pipe_tile_raw_to_rgba(enum pipe_format format, + void *src, + uint w, uint h, + float *dst, unsigned dst_stride); + +void +pipe_tile_raw_to_unsigned(enum pipe_format format, + void *src, + uint w, uint h, + unsigned *dst, unsigned dst_stride); + +void +pipe_tile_raw_to_signed(enum pipe_format format, + void *src, + uint w, uint h, + int *dst, unsigned dst_stride); + +void +pipe_get_tile_ui_format(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + enum pipe_format format, + unsigned int *p); + +void +pipe_get_tile_i_format(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + enum pipe_format format, + int *p); + +void +pipe_put_tile_ui_format(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + enum pipe_format format, + const unsigned *p); + +void +pipe_put_tile_i_format(struct pipe_context *pipe, + struct pipe_transfer *pt, + uint x, uint y, uint w, uint h, + enum pipe_format format, + const int *p); + +#ifdef __cplusplus +} +#endif + +#endif -- cgit v1.2.3