X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fgallium%2Fauxiliary%2Futil%2Fu_blitter.c;h=447d8d2281fd6af303d6aa04446b53575a400028;hb=ba72554f3e576c1674d52ab16d8d2edff9398b71;hp=35b8edba75cd73b8b2a87fcb593c325cae6c9e3a;hpb=61706915a3b5644faf7a5e67f47c9c593620bf8c;p=mesa.git diff --git a/src/gallium/auxiliary/util/u_blitter.c b/src/gallium/auxiliary/util/u_blitter.c index 35b8edba75c..447d8d2281f 100644 --- a/src/gallium/auxiliary/util/u_blitter.c +++ b/src/gallium/auxiliary/util/u_blitter.c @@ -17,7 +17,7 @@ * 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 + * 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. @@ -27,7 +27,7 @@ /** * @file * Blitter utility to facilitate acceleration of the clear, clear_render_target, - * clear_depth_stencil, and resource_copy_region functions. + * clear_depth_stencil, resource_copy_region, and blit functions. * * @author Marek Olšák */ @@ -51,12 +51,16 @@ #define INVALID_PTR ((void*)~0) +#define GET_CLEAR_BLEND_STATE_IDX(clear_buffers) \ + ((clear_buffers) / PIPE_CLEAR_COLOR0) + +#define NUM_RESOLVE_FRAG_SHADERS 5 /* MSAA 2x, 4x, 8x, 16x, 32x */ +#define GET_MSAA_RESOLVE_FS_IDX(nr_samples) (util_logbase2(nr_samples)-1) + struct blitter_context_priv { struct blitter_context base; - struct u_upload_mgr *upload; - float vertices[4][2][4]; /**< {pos, color} or {pos, texcoord} */ /* Templates for various state objects. */ @@ -64,16 +68,18 @@ struct blitter_context_priv /* Constant state objects. */ /* Vertex shaders. */ void *vs; /**< Vertex shader which passes {pos, generic} to the output.*/ - void *vs_pos_only; /**< Vertex shader which passes pos to the output.*/ + void *vs_pos_only[4]; /**< Vertex shader which passes pos to the output.*/ + void *vs_layered; /**< Vertex shader which sets LAYER = INSTANCEID. */ /* 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]; + void *fs_empty; + void *fs_write_one_cbuf; + void *fs_write_all_cbufs; - /* FS which outputs a color from a texture, - where the index is PIPE_TEXTURE_* to be sampled. */ - void *fs_texfetch_col[PIPE_MAX_TEXTURE_TYPES]; + /* FS which outputs a color from a texture. + * The first index indicates the texture type / destination type, + * the second index is the PIPE_TEXTURE_* to be sampled. */ + void *fs_texfetch_col[5][PIPE_MAX_TEXTURE_TYPES]; /* FS which outputs a depth from a texture, where the index is PIPE_TEXTURE_* to be sampled. */ @@ -82,14 +88,17 @@ struct blitter_context_priv void *fs_texfetch_stencil[PIPE_MAX_TEXTURE_TYPES]; /* FS which outputs one sample from a multisample texture. */ - void *fs_texfetch_col_msaa[PIPE_MAX_TEXTURE_TYPES]; + void *fs_texfetch_col_msaa[5][PIPE_MAX_TEXTURE_TYPES]; void *fs_texfetch_depth_msaa[PIPE_MAX_TEXTURE_TYPES]; void *fs_texfetch_depthstencil_msaa[PIPE_MAX_TEXTURE_TYPES]; void *fs_texfetch_stencil_msaa[PIPE_MAX_TEXTURE_TYPES]; + /* FS which outputs an average of all samples. */ + void *fs_resolve[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2]; + /* Blend state. */ - void *blend_write_color; /**< blend state with writemask of RGBA */ - void *blend_keep_color; /**< blend state with writemask of 0 */ + void *blend[PIPE_MASK_RGBA+1][2]; /**< blend state with writemask */ + void *blend_clear[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR)+1]; /* Depth stencil alpha state. */ void *dsa_write_depth_stencil; @@ -99,16 +108,16 @@ struct blitter_context_priv /* Vertex elements states. */ void *velem_state; - void *velem_uint_state; - void *velem_sint_state; - void *velem_state_readbuf; + void *velem_state_readbuf[4]; /**< X, XY, XYZ, XYZW */ /* Sampler state. */ void *sampler_state; + void *sampler_state_linear; + void *sampler_state_rect; + void *sampler_state_rect_linear; /* Rasterizer state. */ - void *rs_state; - void *rs_discard_state; + void *rs_state, *rs_state_scissor, *rs_discard_state; /* Viewport state. */ struct pipe_viewport_state viewport; @@ -118,11 +127,22 @@ struct blitter_context_priv unsigned dst_height; boolean has_geometry_shader; - boolean vertex_has_integers; + boolean has_tessellation; + boolean has_layered; boolean has_stream_out; boolean has_stencil_export; + boolean has_texture_multisample; + boolean cached_all_shaders; + + /* The Draw module overrides these functions. + * Always create the blitter before Draw. */ + void (*bind_fs_state)(struct pipe_context *, void *); + void (*delete_fs_state)(struct pipe_context *, void *); }; +static struct pipe_surface * +util_blitter_get_next_surface_layer(struct pipe_context *pipe, + struct pipe_surface *surf); struct blitter_context *util_blitter_create(struct pipe_context *pipe) { @@ -132,7 +152,7 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe) struct pipe_rasterizer_state rs_state; struct pipe_sampler_state sampler_state; struct pipe_vertex_element velem[2]; - unsigned i; + unsigned i, j; ctx = CALLOC_STRUCT(blitter_context_priv); if (!ctx) @@ -140,6 +160,10 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe) ctx->base.pipe = pipe; ctx->base.draw_rectangle = util_blitter_draw_rectangle; + ctx->base.get_next_surface_layer = util_blitter_get_next_surface_layer; + + ctx->bind_fs_state = pipe->bind_fs_state; + ctx->delete_fs_state = pipe->delete_fs_state; /* init state objects for them to be considered invalid */ ctx->base.saved_blend_state = INVALID_PTR; @@ -152,15 +176,16 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe) 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->base.saved_num_so_targets = ~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); + + ctx->has_tessellation = + pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_TESS_CTRL, + PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0; + ctx->has_stream_out = pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0; @@ -169,12 +194,28 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe) pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT); + ctx->has_texture_multisample = + pipe->screen->get_param(pipe->screen, PIPE_CAP_TEXTURE_MULTISAMPLE); + /* blend state objects */ memset(&blend, 0, sizeof(blend)); - ctx->blend_keep_color = pipe->create_blend_state(pipe, &blend); - blend.rt[0].colormask = PIPE_MASK_RGBA; - ctx->blend_write_color = pipe->create_blend_state(pipe, &blend); + for (i = 0; i <= PIPE_MASK_RGBA; i++) { + for (j = 0; j < 2; j++) { + memset(&blend.rt[0], 0, sizeof(blend.rt[0])); + blend.rt[0].colormask = i; + if (j) { + blend.rt[0].blend_enable = 1; + blend.rt[0].rgb_func = PIPE_BLEND_ADD; + blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA; + blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA; + blend.rt[0].alpha_func = PIPE_BLEND_ADD; + blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA; + blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA; + } + ctx->blend[i][j] = pipe->create_blend_state(pipe, &blend); + } + } /* depth stencil alpha state objects */ memset(&dsa, 0, sizeof(dsa)); @@ -209,92 +250,179 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe) sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; sampler_state.normalized_coords = 1; ctx->sampler_state = pipe->create_sampler_state(pipe, &sampler_state); + sampler_state.normalized_coords = 0; + ctx->sampler_state_rect = pipe->create_sampler_state(pipe, &sampler_state); + + sampler_state.min_img_filter = PIPE_TEX_FILTER_LINEAR; + sampler_state.mag_img_filter = PIPE_TEX_FILTER_LINEAR; + sampler_state.normalized_coords = 1; + ctx->sampler_state_linear = pipe->create_sampler_state(pipe, &sampler_state); + sampler_state.normalized_coords = 0; + ctx->sampler_state_rect_linear = pipe->create_sampler_state(pipe, &sampler_state); /* rasterizer state */ memset(&rs_state, 0, sizeof(rs_state)); rs_state.cull_face = PIPE_FACE_NONE; - rs_state.gl_rasterization_rules = 1; + rs_state.half_pixel_center = 1; + rs_state.bottom_edge_rule = 1; rs_state.flatshade = 1; rs_state.depth_clip = 1; ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state); + rs_state.scissor = 1; + ctx->rs_state_scissor = pipe->create_rasterizer_state(pipe, &rs_state); + if (ctx->has_stream_out) { + rs_state.scissor = 0; rs_state.rasterizer_discard = 1; ctx->rs_discard_state = pipe->create_rasterizer_state(pipe, &rs_state); } + ctx->base.cb_slot = 0; /* 0 for now */ + ctx->base.vb_slot = 0; /* 0 for now */ + /* vertex elements states */ memset(&velem[0], 0, sizeof(velem[0]) * 2); for (i = 0; i < 2; i++) { velem[i].src_offset = i * 4 * sizeof(float); velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; + velem[i].vertex_buffer_index = ctx->base.vb_slot; } 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); - velem[0].src_offset = 0; - velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; - velem[1].src_offset = 4 * sizeof(float); - velem[1].src_format = PIPE_FORMAT_R32G32B32A32_SINT; - ctx->velem_sint_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]); + if (ctx->has_stream_out) { + static enum pipe_format formats[4] = { + PIPE_FORMAT_R32_UINT, + PIPE_FORMAT_R32G32_UINT, + PIPE_FORMAT_R32G32B32_UINT, + PIPE_FORMAT_R32G32B32A32_UINT + }; - memset(&velem[0], 0, sizeof(velem[0]) * 2); - velem[0].src_offset = 0; - velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; - velem[1].src_offset = 4 * sizeof(float); - velem[1].src_format = PIPE_FORMAT_R32G32B32A32_UINT; - ctx->velem_uint_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]); + for (i = 0; i < 4; i++) { + velem[0].src_format = formats[i]; + velem[0].vertex_buffer_index = ctx->base.vb_slot; + ctx->velem_state_readbuf[i] = + pipe->create_vertex_elements_state(pipe, 1, &velem[0]); + } } - if (ctx->has_stream_out) { - velem[0].src_format = PIPE_FORMAT_R32_UINT; - ctx->velem_state_readbuf = pipe->create_vertex_elements_state(pipe, 1, &velem[0]); + ctx->has_layered = + pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_INSTANCEID) && + pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT); + + /* set invariant vertex coordinates */ + for (i = 0; i < 4; i++) + ctx->vertices[i][0][3] = 1; /*v.w*/ + + return &ctx->base; +} + +static void bind_vs_pos_only(struct blitter_context_priv *ctx, + unsigned num_so_channels) +{ + struct pipe_context *pipe = ctx->base.pipe; + int index = num_so_channels ? num_so_channels - 1 : 0; + + if (!ctx->vs_pos_only[index]) { + struct pipe_stream_output_info so; + const uint semantic_names[] = { TGSI_SEMANTIC_POSITION }; + const uint semantic_indices[] = { 0 }; + + memset(&so, 0, sizeof(so)); + so.num_outputs = 1; + so.output[0].num_components = num_so_channels; + so.stride[0] = num_so_channels; + + ctx->vs_pos_only[index] = + util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names, + semantic_indices, FALSE, + &so); } - /* fragment shaders are created on-demand */ + pipe->bind_vs_state(pipe, ctx->vs_pos_only[index]); +} - /* vertex shaders */ - { +static void bind_vs_passthrough(struct blitter_context_priv *ctx) +{ + struct pipe_context *pipe = ctx->base.pipe; + + if (!ctx->vs) { const uint semantic_names[] = { TGSI_SEMANTIC_POSITION, TGSI_SEMANTIC_GENERIC }; const uint semantic_indices[] = { 0, 0 }; ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names, - semantic_indices); + semantic_indices, FALSE); } - if (ctx->has_stream_out) { - struct pipe_stream_output_info so; - const uint semantic_names[] = { TGSI_SEMANTIC_POSITION }; - const uint semantic_indices[] = { 0 }; - memset(&so, 0, sizeof(so)); - so.num_outputs = 1; - so.output[0].num_components = 1; - so.stride[0] = 1; + pipe->bind_vs_state(pipe, ctx->vs); +} - ctx->vs_pos_only = - util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names, - semantic_indices, &so); +static void bind_vs_layered(struct blitter_context_priv *ctx) +{ + struct pipe_context *pipe = ctx->base.pipe; + + if (!ctx->vs_layered) { + ctx->vs_layered = util_make_layered_clear_vertex_shader(pipe); } - /* set invariant vertex coordinates */ - for (i = 0; i < 4; i++) - ctx->vertices[i][0][3] = 1; /*v.w*/ + pipe->bind_vs_state(pipe, ctx->vs_layered); +} + +static void bind_fs_empty(struct blitter_context_priv *ctx) +{ + struct pipe_context *pipe = ctx->base.pipe; - ctx->upload = u_upload_create(pipe, 65536, 4, PIPE_BIND_VERTEX_BUFFER); + if (!ctx->fs_empty) { + assert(!ctx->cached_all_shaders); + ctx->fs_empty = util_make_empty_fragment_shader(pipe); + } - return &ctx->base; + ctx->bind_fs_state(pipe, ctx->fs_empty); +} + +static void bind_fs_write_one_cbuf(struct blitter_context_priv *ctx) +{ + struct pipe_context *pipe = ctx->base.pipe; + + if (!ctx->fs_write_one_cbuf) { + assert(!ctx->cached_all_shaders); + ctx->fs_write_one_cbuf = + util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC, + TGSI_INTERPOLATE_CONSTANT, FALSE); + } + + ctx->bind_fs_state(pipe, ctx->fs_write_one_cbuf); +} + +static void bind_fs_write_all_cbufs(struct blitter_context_priv *ctx) +{ + struct pipe_context *pipe = ctx->base.pipe; + + if (!ctx->fs_write_all_cbufs) { + assert(!ctx->cached_all_shaders); + ctx->fs_write_all_cbufs = + util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC, + TGSI_INTERPOLATE_CONSTANT, TRUE); + } + + ctx->bind_fs_state(pipe, ctx->fs_write_all_cbufs); } void util_blitter_destroy(struct blitter_context *blitter) { struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; struct pipe_context *pipe = blitter->pipe; - int i; + unsigned i, j, f; + + for (i = 0; i <= PIPE_MASK_RGBA; i++) + for (j = 0; j < 2; j++) + pipe->delete_blend_state(pipe, ctx->blend[i][j]); - pipe->delete_blend_state(pipe, ctx->blend_write_color); - pipe->delete_blend_state(pipe, ctx->blend_keep_color); + for (i = 0; i < ARRAY_SIZE(ctx->blend_clear); i++) { + if (ctx->blend_clear[i]) + pipe->delete_blend_state(pipe, ctx->blend_clear[i]); + } pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil); @@ -302,87 +430,116 @@ void util_blitter_destroy(struct blitter_context *blitter) pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil); pipe->delete_rasterizer_state(pipe, ctx->rs_state); + pipe->delete_rasterizer_state(pipe, ctx->rs_state_scissor); if (ctx->rs_discard_state) pipe->delete_rasterizer_state(pipe, ctx->rs_discard_state); - pipe->delete_vs_state(pipe, ctx->vs); - if (ctx->vs_pos_only) - pipe->delete_vs_state(pipe, ctx->vs_pos_only); + if (ctx->vs) + pipe->delete_vs_state(pipe, ctx->vs); + for (i = 0; i < 4; i++) + if (ctx->vs_pos_only[i]) + pipe->delete_vs_state(pipe, ctx->vs_pos_only[i]); + if (ctx->vs_layered) + pipe->delete_vs_state(pipe, ctx->vs_layered); 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 < 4; i++) { + if (ctx->velem_state_readbuf[i]) { + pipe->delete_vertex_elements_state(pipe, ctx->velem_state_readbuf[i]); + } } - if (ctx->velem_state_readbuf) - pipe->delete_vertex_elements_state(pipe, ctx->velem_state_readbuf); for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) { - if (ctx->fs_texfetch_col[i]) - pipe->delete_fs_state(pipe, ctx->fs_texfetch_col[i]); + for (unsigned type = 0; type < ARRAY_SIZE(ctx->fs_texfetch_col); ++type) { + if (ctx->fs_texfetch_col[type][i]) + ctx->delete_fs_state(pipe, ctx->fs_texfetch_col[type][i]); + if (ctx->fs_texfetch_col_msaa[type][i]) + ctx->delete_fs_state(pipe, ctx->fs_texfetch_col_msaa[type][i]); + } + if (ctx->fs_texfetch_depth[i]) - pipe->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]); + ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]); if (ctx->fs_texfetch_depthstencil[i]) - pipe->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil[i]); + ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil[i]); if (ctx->fs_texfetch_stencil[i]) - pipe->delete_fs_state(pipe, ctx->fs_texfetch_stencil[i]); + ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil[i]); + + if (ctx->fs_texfetch_depth_msaa[i]) + ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth_msaa[i]); + if (ctx->fs_texfetch_depthstencil_msaa[i]) + ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil_msaa[i]); + if (ctx->fs_texfetch_stencil_msaa[i]) + ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil_msaa[i]); + + for (j = 0; j< ARRAY_SIZE(ctx->fs_resolve[i]); j++) + for (f = 0; f < 2; f++) + if (ctx->fs_resolve[i][j][f]) + ctx->delete_fs_state(pipe, ctx->fs_resolve[i][j][f]); } - 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]); - } + if (ctx->fs_empty) + ctx->delete_fs_state(pipe, ctx->fs_empty); + if (ctx->fs_write_one_cbuf) + ctx->delete_fs_state(pipe, ctx->fs_write_one_cbuf); + if (ctx->fs_write_all_cbufs) + ctx->delete_fs_state(pipe, ctx->fs_write_all_cbufs); + pipe->delete_sampler_state(pipe, ctx->sampler_state_rect_linear); + pipe->delete_sampler_state(pipe, ctx->sampler_state_rect); + pipe->delete_sampler_state(pipe, ctx->sampler_state_linear); pipe->delete_sampler_state(pipe, ctx->sampler_state); - u_upload_destroy(ctx->upload); FREE(ctx); } -static void blitter_set_running_flag(struct blitter_context_priv *ctx) +void util_blitter_set_texture_multisample(struct blitter_context *blitter, + boolean supported) +{ + struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; + + ctx->has_texture_multisample = supported; +} + +void util_blitter_set_running_flag(struct blitter_context *blitter) { - if (ctx->base.running) { + if (blitter->running) { _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n", __LINE__); } - ctx->base.running = TRUE; + blitter->running = TRUE; + + blitter->pipe->set_active_query_state(blitter->pipe, false); } -static void blitter_unset_running_flag(struct blitter_context_priv *ctx) +void util_blitter_unset_running_flag(struct blitter_context *blitter) { - if (!ctx->base.running) { + if (!blitter->running) { _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n", __LINE__); } - ctx->base.running = FALSE; + blitter->running = FALSE; + + blitter->pipe->set_active_query_state(blitter->pipe, true); } static void blitter_check_saved_vertex_states(struct blitter_context_priv *ctx) { - assert(ctx->base.saved_num_vertex_buffers != ~0); assert(ctx->base.saved_velem_state != INVALID_PTR); assert(ctx->base.saved_vs != INVALID_PTR); assert(!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR); - assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0); + assert(!ctx->has_tessellation || ctx->base.saved_tcs != INVALID_PTR); + assert(!ctx->has_tessellation || ctx->base.saved_tes != INVALID_PTR); + assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0u); assert(ctx->base.saved_rs_state != INVALID_PTR); } -static void blitter_restore_vertex_states(struct blitter_context_priv *ctx) +void util_blitter_restore_vertex_states(struct blitter_context *blitter) { + struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; struct pipe_context *pipe = ctx->base.pipe; unsigned i; - /* 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 buffer. */ + pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, + &ctx->base.saved_vertex_buffer); + pipe_resource_reference(&ctx->base.saved_vertex_buffer.buffer, NULL); /* Vertex elements. */ pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state); @@ -398,11 +555,21 @@ static void blitter_restore_vertex_states(struct blitter_context_priv *ctx) ctx->base.saved_gs = INVALID_PTR; } + if (ctx->has_tessellation) { + pipe->bind_tcs_state(pipe, ctx->base.saved_tcs); + pipe->bind_tes_state(pipe, ctx->base.saved_tes); + ctx->base.saved_tcs = INVALID_PTR; + ctx->base.saved_tes = INVALID_PTR; + } + /* Stream outputs. */ if (ctx->has_stream_out) { + unsigned offsets[PIPE_MAX_SO_BUFFERS]; + for (i = 0; i < ctx->base.saved_num_so_targets; i++) + offsets[i] = (unsigned)-1; pipe->set_stream_output_targets(pipe, ctx->base.saved_num_so_targets, - ctx->base.saved_so_targets, ~0); + ctx->base.saved_so_targets, offsets); for (i = 0; i < ctx->base.saved_num_so_targets; i++) pipe_so_target_reference(&ctx->base.saved_so_targets[i], NULL); @@ -422,12 +589,13 @@ static void blitter_check_saved_fragment_states(struct blitter_context_priv *ctx assert(ctx->base.saved_blend_state != INVALID_PTR); } -static void blitter_restore_fragment_states(struct blitter_context_priv *ctx) +void util_blitter_restore_fragment_states(struct blitter_context *blitter) { + struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; struct pipe_context *pipe = ctx->base.pipe; /* Fragment shader. */ - pipe->bind_fs_state(pipe, ctx->base.saved_fs); + ctx->bind_fs_state(pipe, ctx->base.saved_fs); ctx->base.saved_fs = INVALID_PTR; /* Depth, stencil, alpha. */ @@ -448,16 +616,39 @@ static void blitter_restore_fragment_states(struct blitter_context_priv *ctx) /* 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_viewport_states(pipe, 0, 1, &ctx->base.saved_viewport); } static void blitter_check_saved_fb_state(struct blitter_context_priv *ctx) { - assert(ctx->base.saved_fb_state.nr_cbufs != ~0); + assert(ctx->base.saved_fb_state.nr_cbufs != ~0u); +} + +static void blitter_disable_render_cond(struct blitter_context_priv *ctx) +{ + struct pipe_context *pipe = ctx->base.pipe; + + if (ctx->base.saved_render_cond_query) { + pipe->render_condition(pipe, NULL, FALSE, 0); + } +} + +void util_blitter_restore_render_cond(struct blitter_context *blitter) +{ + struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; + struct pipe_context *pipe = ctx->base.pipe; + + if (ctx->base.saved_render_cond_query) { + pipe->render_condition(pipe, ctx->base.saved_render_cond_query, + ctx->base.saved_render_cond_cond, + ctx->base.saved_render_cond_mode); + ctx->base.saved_render_cond_query = NULL; + } } -static void blitter_restore_fb_state(struct blitter_context_priv *ctx) +void util_blitter_restore_fb_state(struct blitter_context *blitter) { + struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; struct pipe_context *pipe = ctx->base.pipe; pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state); @@ -466,25 +657,27 @@ static void blitter_restore_fb_state(struct blitter_context_priv *ctx) static void blitter_check_saved_textures(struct blitter_context_priv *ctx) { - assert(ctx->base.saved_num_sampler_states != ~0); - assert(ctx->base.saved_num_sampler_views != ~0); + assert(ctx->base.saved_num_sampler_states != ~0u); + assert(ctx->base.saved_num_sampler_views != ~0u); } -static void blitter_restore_textures(struct blitter_context_priv *ctx) +void util_blitter_restore_textures(struct blitter_context *blitter) { + struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; struct pipe_context *pipe = ctx->base.pipe; unsigned i; /* Fragment sampler states. */ - pipe->bind_fragment_sampler_states(pipe, - ctx->base.saved_num_sampler_states, - ctx->base.saved_sampler_states); + pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, + ctx->base.saved_num_sampler_states, + ctx->base.saved_sampler_states); + ctx->base.saved_num_sampler_states = ~0; /* Fragment sampler views. */ - pipe->set_fragment_sampler_views(pipe, - ctx->base.saved_num_sampler_views, - ctx->base.saved_sampler_views); + pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, + ctx->base.saved_num_sampler_views, + ctx->base.saved_sampler_views); for (i = 0; i < ctx->base.saved_num_sampler_views; i++) pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL); @@ -492,9 +685,17 @@ static void blitter_restore_textures(struct blitter_context_priv *ctx) ctx->base.saved_num_sampler_views = ~0; } +void util_blitter_restore_constant_buffer_state(struct blitter_context *blitter) +{ + struct pipe_context *pipe = blitter->pipe; + + pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, blitter->cb_slot, + &blitter->saved_fs_constant_buffer); + pipe_resource_reference(&blitter->saved_fs_constant_buffer.buffer, NULL); +} + static void blitter_set_rectangle(struct blitter_context_priv *ctx, - unsigned x1, unsigned y1, - unsigned x2, unsigned y2, + int x1, int y1, int x2, int y2, float depth) { int i; @@ -519,12 +720,10 @@ static void blitter_set_rectangle(struct blitter_context_priv *ctx, ctx->viewport.scale[0] = 0.5f * ctx->dst_width; ctx->viewport.scale[1] = 0.5f * ctx->dst_height; ctx->viewport.scale[2] = 1.0f; - ctx->viewport.scale[3] = 1.0f; ctx->viewport.translate[0] = 0.5f * ctx->dst_width; ctx->viewport.translate[1] = 0.5f * ctx->dst_height; ctx->viewport.translate[2] = 0.0f; - ctx->viewport.translate[3] = 0.0f; - ctx->base.pipe->set_viewport_state(ctx->base.pipe, &ctx->viewport); + ctx->base.pipe->set_viewport_states(ctx->base.pipe, 0, 1, &ctx->viewport); } static void blitter_set_clear_color(struct blitter_context_priv *ctx, @@ -552,8 +751,7 @@ static void blitter_set_clear_color(struct blitter_context_priv *ctx, static void get_texcoords(struct pipe_sampler_view *src, unsigned src_width0, unsigned src_height0, - unsigned x1, unsigned y1, - unsigned x2, unsigned y2, + int x1, int y1, int x2, int y2, float out[4]) { struct pipe_resource *tex = src->texture; @@ -567,10 +765,10 @@ static void get_texcoords(struct pipe_sampler_view *src, out[2] = x2 / (float)u_minify(src_width0, level); out[3] = y2 / (float)u_minify(src_height0, level); } else { - out[0] = x1; - out[1] = y1; - out[2] = x2; - out[3] = y2; + out[0] = (float) x1; + out[1] = (float) y1; + out[2] = (float) x2; + out[3] = (float) y2; } } @@ -593,9 +791,8 @@ static void set_texcoords_in_vertices(const float coord[4], static void blitter_set_texcoords(struct blitter_context_priv *ctx, struct pipe_sampler_view *src, unsigned src_width0, unsigned src_height0, - unsigned layer, unsigned sample, - unsigned x1, unsigned y1, - unsigned x2, unsigned y2) + float layer, unsigned sample, + int x1, int y1, int x2, int y2) { unsigned i; float coord[4]; @@ -603,12 +800,14 @@ static void blitter_set_texcoords(struct blitter_context_priv *ctx, get_texcoords(src, src_width0, src_height0, x1, y1, x2, y2, coord); - if (src->texture->target == PIPE_TEXTURE_CUBE) { + if (src->texture->target == PIPE_TEXTURE_CUBE || + src->texture->target == PIPE_TEXTURE_CUBE_ARRAY) { set_texcoords_in_vertices(coord, &face_coord[0][0], 2); - util_map_texcoords2d_onto_cubemap(layer, + util_map_texcoords2d_onto_cubemap((unsigned)layer % 6, /* pointer, stride in floats */ &face_coord[0][0], 2, - &ctx->vertices[0][1][0], 8); + &ctx->vertices[0][1][0], 8, + FALSE); } else { set_texcoords_in_vertices(coord, &ctx->vertices[0][1][0], 8); } @@ -626,19 +825,24 @@ static void blitter_set_texcoords(struct blitter_context_priv *ctx, case PIPE_TEXTURE_1D_ARRAY: for (i = 0; i < 4; i++) - ctx->vertices[i][1][1] = layer; /*t*/ + ctx->vertices[i][1][1] = (float) layer; /*t*/ break; case PIPE_TEXTURE_2D_ARRAY: for (i = 0; i < 4; i++) { - ctx->vertices[i][1][2] = layer; /*r*/ - ctx->vertices[i][1][3] = sample; /*q*/ + ctx->vertices[i][1][2] = (float) layer; /*r*/ + ctx->vertices[i][1][3] = (float) sample; /*q*/ } break; + case PIPE_TEXTURE_CUBE_ARRAY: + for (i = 0; i < 4; i++) + ctx->vertices[i][1][3] = (float) ((unsigned)layer / 6); /*w*/ + break; + case PIPE_TEXTURE_2D: for (i = 0; i < 4; i++) { - ctx->vertices[i][1][2] = sample; /*r*/ + ctx->vertices[i][1][3] = (float) sample; /*r*/ } break; @@ -653,95 +857,137 @@ static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx, ctx->dst_height = height; } -static INLINE -void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs, - boolean int_format) +static void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx, + enum pipe_format src_format, + enum pipe_format dst_format, + enum pipe_texture_target target, + unsigned src_nr_samples, + unsigned dst_nr_samples, + unsigned filter) { struct pipe_context *pipe = ctx->base.pipe; - - assert(num_cbufs <= PIPE_MAX_COLOR_BUFS); - - 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]; + unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(target, src_nr_samples); + enum tgsi_return_type stype; + enum tgsi_return_type dtype; + unsigned type; + + assert(target < PIPE_MAX_TEXTURE_TYPES); + + if (util_format_is_pure_uint(src_format)) { + stype = TGSI_RETURN_TYPE_UINT; + if (util_format_is_pure_uint(dst_format)) { + dtype = TGSI_RETURN_TYPE_UINT; + type = 0; + } else { + assert(util_format_is_pure_sint(dst_format)); + dtype = TGSI_RETURN_TYPE_SINT; + type = 1; + } + } else if (util_format_is_pure_sint(src_format)) { + stype = TGSI_RETURN_TYPE_SINT; + if (util_format_is_pure_sint(dst_format)) { + dtype = TGSI_RETURN_TYPE_SINT; + type = 2; + } else { + assert(util_format_is_pure_uint(dst_format)); + dtype = TGSI_RETURN_TYPE_UINT; + type = 3; + } } 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]; + assert(!util_format_is_pure_uint(dst_format) && + !util_format_is_pure_sint(dst_format)); + dtype = stype = TGSI_RETURN_TYPE_FLOAT; + type = 4; } -} - -static INLINE -void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx, - struct pipe_resource *tex) -{ - struct pipe_context *pipe = ctx->base.pipe; - assert(tex->target < PIPE_MAX_TEXTURE_TYPES); + if (src_nr_samples > 1) { + void **shader; - if (tex->nr_samples > 1) { - void **shader = &ctx->fs_texfetch_col_msaa[tex->target]; - - /* Create the fragment shader on-demand. */ - if (!*shader) { - unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, - tex->nr_samples); - - *shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex); + /* OpenGL requires that integer textures just copy 1 sample instead + * of averaging. + */ + if (dst_nr_samples <= 1 && + stype != TGSI_RETURN_TYPE_UINT && + stype != TGSI_RETURN_TYPE_SINT) { + /* The destination has one sample, so we'll do color resolve. */ + unsigned index = GET_MSAA_RESOLVE_FS_IDX(src_nr_samples); + + assert(filter < 2); + + shader = &ctx->fs_resolve[target][index][filter]; + + if (!*shader) { + assert(!ctx->cached_all_shaders); + if (filter == PIPE_TEX_FILTER_LINEAR) { + *shader = util_make_fs_msaa_resolve_bilinear(pipe, tgsi_tex, + src_nr_samples, + stype); + } + else { + *shader = util_make_fs_msaa_resolve(pipe, tgsi_tex, + src_nr_samples, + stype); + } + } + } + else { + /* The destination has multiple samples, we'll do + * an MSAA->MSAA copy. + */ + shader = &ctx->fs_texfetch_col_msaa[type][target]; + + /* Create the fragment shader on-demand. */ + if (!*shader) { + assert(!ctx->cached_all_shaders); + *shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex, stype, dtype); + } } return *shader; } else { - void **shader = &ctx->fs_texfetch_col[tex->target]; + void **shader = &ctx->fs_texfetch_col[type][target]; /* Create the fragment shader on-demand. */ if (!*shader) { - unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, 0); - - *shader = - util_make_fragment_tex_shader(pipe, tgsi_tex, - TGSI_INTERPOLATE_LINEAR); + assert(!ctx->cached_all_shaders); + *shader = util_make_fragment_tex_shader(pipe, tgsi_tex, + TGSI_INTERPOLATE_LINEAR, + stype, dtype); } return *shader; } } -static INLINE +static inline void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx, - struct pipe_resource *tex) + enum pipe_texture_target target, + unsigned nr_samples) { struct pipe_context *pipe = ctx->base.pipe; - assert(tex->target < PIPE_MAX_TEXTURE_TYPES); + assert(target < PIPE_MAX_TEXTURE_TYPES); - if (tex->nr_samples > 1) { - void **shader = &ctx->fs_texfetch_depth_msaa[tex->target]; + if (nr_samples > 1) { + void **shader = &ctx->fs_texfetch_depth_msaa[target]; /* Create the fragment shader on-demand. */ if (!*shader) { - unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, - tex->nr_samples); - - *shader = - util_make_fs_blit_msaa_depth(pipe, tgsi_tex); + unsigned tgsi_tex; + assert(!ctx->cached_all_shaders); + tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples); + *shader = util_make_fs_blit_msaa_depth(pipe, tgsi_tex); } return *shader; } else { - void **shader = &ctx->fs_texfetch_depth[tex->target]; + void **shader = &ctx->fs_texfetch_depth[target]; /* Create the fragment shader on-demand. */ if (!*shader) { - unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, 0); - + unsigned tgsi_tex; + assert(!ctx->cached_all_shaders); + tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0); *shader = util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex, TGSI_INTERPOLATE_LINEAR); @@ -751,34 +997,35 @@ void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx, } } -static INLINE +static inline void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx, - struct pipe_resource *tex) + enum pipe_texture_target target, + unsigned nr_samples) { struct pipe_context *pipe = ctx->base.pipe; - assert(tex->target < PIPE_MAX_TEXTURE_TYPES); + assert(target < PIPE_MAX_TEXTURE_TYPES); - if (tex->nr_samples > 1) { - void **shader = &ctx->fs_texfetch_depthstencil_msaa[tex->target]; + if (nr_samples > 1) { + void **shader = &ctx->fs_texfetch_depthstencil_msaa[target]; /* Create the fragment shader on-demand. */ if (!*shader) { - unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, - tex->nr_samples); - - *shader = - util_make_fs_blit_msaa_depthstencil(pipe, tgsi_tex); + unsigned tgsi_tex; + assert(!ctx->cached_all_shaders); + tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples); + *shader = util_make_fs_blit_msaa_depthstencil(pipe, tgsi_tex); } return *shader; } else { - void **shader = &ctx->fs_texfetch_depthstencil[tex->target]; + void **shader = &ctx->fs_texfetch_depthstencil[target]; /* Create the fragment shader on-demand. */ if (!*shader) { - unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, 0); - + unsigned tgsi_tex; + assert(!ctx->cached_all_shaders); + tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0); *shader = util_make_fragment_tex_shader_writedepthstencil(pipe, tgsi_tex, TGSI_INTERPOLATE_LINEAR); @@ -788,34 +1035,35 @@ void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx, } } -static INLINE +static inline void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx, - struct pipe_resource *tex) + enum pipe_texture_target target, + unsigned nr_samples) { struct pipe_context *pipe = ctx->base.pipe; - assert(tex->target < PIPE_MAX_TEXTURE_TYPES); + assert(target < PIPE_MAX_TEXTURE_TYPES); - if (tex->nr_samples > 1) { - void **shader = &ctx->fs_texfetch_stencil_msaa[tex->target]; + if (nr_samples > 1) { + void **shader = &ctx->fs_texfetch_stencil_msaa[target]; /* Create the fragment shader on-demand. */ if (!*shader) { - unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, - tex->nr_samples); - - *shader = - util_make_fs_blit_msaa_stencil(pipe, tgsi_tex); + unsigned tgsi_tex; + assert(!ctx->cached_all_shaders); + tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples); + *shader = util_make_fs_blit_msaa_stencil(pipe, tgsi_tex); } return *shader; } else { - void **shader = &ctx->fs_texfetch_stencil[tex->target]; + void **shader = &ctx->fs_texfetch_stencil[target]; /* Create the fragment shader on-demand. */ if (!*shader) { - unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, 0); - + unsigned tgsi_tex; + assert(!ctx->cached_all_shaders); + tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0); *shader = util_make_fragment_tex_shader_writestencil(pipe, tgsi_tex, TGSI_INTERPOLATE_LINEAR); @@ -825,40 +1073,155 @@ void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx, } } -static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx) + +/** + * Generate and save all fragment shaders that we will ever need for + * blitting. Drivers which use the 'draw' fallbacks will typically use + * this to make sure we generate/use shaders that don't go through the + * draw module's wrapper functions. + */ +void util_blitter_cache_all_shaders(struct blitter_context *blitter) +{ + struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; + struct pipe_context *pipe = blitter->pipe; + struct pipe_screen *screen = pipe->screen; + unsigned samples, j, f, target, max_samples; + boolean has_arraytex, has_cubearraytex; + + max_samples = ctx->has_texture_multisample ? 2 : 1; + has_arraytex = screen->get_param(screen, + PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS) != 0; + has_cubearraytex = screen->get_param(screen, + PIPE_CAP_CUBE_MAP_ARRAY) != 0; + + /* It only matters if i <= 1 or > 1. */ + for (samples = 1; samples <= max_samples; samples++) { + for (target = PIPE_TEXTURE_1D; target < PIPE_MAX_TEXTURE_TYPES; target++) { + if (!has_arraytex && + (target == PIPE_TEXTURE_1D_ARRAY || + target == PIPE_TEXTURE_2D_ARRAY)) { + continue; + } + if (!has_cubearraytex && + (target == PIPE_TEXTURE_CUBE_ARRAY)) + continue; + + if (samples > 1 && + (target != PIPE_TEXTURE_2D && + target != PIPE_TEXTURE_2D_ARRAY)) + continue; + + /* If samples == 1, the shaders read one texel. If samples >= 1, + * they read one sample. + */ + blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT, + PIPE_FORMAT_R32_FLOAT, target, + samples, samples, 0); + blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT, + PIPE_FORMAT_R32_UINT, target, + samples, samples, 0); + blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT, + PIPE_FORMAT_R32_SINT, target, + samples, samples, 0); + blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT, + PIPE_FORMAT_R32_SINT, target, + samples, samples, 0); + blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT, + PIPE_FORMAT_R32_UINT, target, + samples, samples, 0); + blitter_get_fs_texfetch_depth(ctx, target, samples); + if (ctx->has_stencil_export) { + blitter_get_fs_texfetch_depthstencil(ctx, target, samples); + blitter_get_fs_texfetch_stencil(ctx, target, samples); + } + + if (samples == 1) + continue; + + /* MSAA resolve shaders. */ + for (j = 2; j < 32; j++) { + if (!screen->is_format_supported(screen, PIPE_FORMAT_R32_FLOAT, + target, j, + PIPE_BIND_SAMPLER_VIEW)) { + continue; + } + + for (f = 0; f < 2; f++) { + blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT, + PIPE_FORMAT_R32_FLOAT, target, + j, 1, f); + blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT, + PIPE_FORMAT_R32_UINT, target, + j, 1, f); + blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT, + PIPE_FORMAT_R32_SINT, target, + j, 1, f); + } + } + } + } + + ctx->fs_empty = util_make_empty_fragment_shader(pipe); + + ctx->fs_write_one_cbuf = + util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC, + TGSI_INTERPOLATE_CONSTANT, FALSE); + + ctx->fs_write_all_cbufs = + util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC, + TGSI_INTERPOLATE_CONSTANT, TRUE); + + ctx->cached_all_shaders = TRUE; +} + +static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx, + boolean scissor, + boolean vs_layered) { struct pipe_context *pipe = ctx->base.pipe; - pipe->bind_rasterizer_state(pipe, ctx->rs_state); - pipe->bind_vs_state(pipe, ctx->vs); + pipe->bind_rasterizer_state(pipe, scissor ? ctx->rs_state_scissor + : ctx->rs_state); + if (vs_layered) + bind_vs_layered(ctx); + else + bind_vs_passthrough(ctx); + if (ctx->has_geometry_shader) pipe->bind_gs_state(pipe, NULL); + if (ctx->has_tessellation) { + pipe->bind_tcs_state(pipe, NULL); + pipe->bind_tes_state(pipe, NULL); + } if (ctx->has_stream_out) - pipe->set_stream_output_targets(pipe, 0, NULL, 0); + pipe->set_stream_output_targets(pipe, 0, NULL, NULL); } static void blitter_draw(struct blitter_context_priv *ctx, - unsigned x1, unsigned y1, - unsigned x2, unsigned y2, - float depth) + int x1, int y1, int x2, int y2, float depth, + unsigned num_instances) { - struct pipe_resource *buf = NULL; - unsigned offset = 0; + struct pipe_context *pipe = ctx->base.pipe; + struct pipe_vertex_buffer vb = {0}; blitter_set_rectangle(ctx, x1, y1, x2, y2, depth); - u_upload_data(ctx->upload, 0, sizeof(ctx->vertices), ctx->vertices, - &offset, &buf); - u_upload_unmap(ctx->upload); - util_draw_vertex_buffer(ctx->base.pipe, NULL, buf, offset, - PIPE_PRIM_TRIANGLE_FAN, 4, 2); - pipe_resource_reference(&buf, NULL); + vb.stride = 8 * sizeof(float); + + u_upload_data(pipe->stream_uploader, 0, sizeof(ctx->vertices), 4, ctx->vertices, + &vb.buffer_offset, &vb.buffer); + if (!vb.buffer) + return; + u_upload_unmap(pipe->stream_uploader); + + pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb); + util_draw_arrays_instanced(pipe, PIPE_PRIM_TRIANGLE_FAN, 0, 4, + 0, num_instances); + pipe_resource_reference(&vb.buffer, NULL); } void util_blitter_draw_rectangle(struct blitter_context *blitter, - unsigned x1, unsigned y1, - unsigned x2, unsigned y2, - float depth, + int x1, int y1, int x2, int y2, float depth, enum blitter_attrib_type type, const union pipe_color_union *attrib) { @@ -876,35 +1239,62 @@ void util_blitter_draw_rectangle(struct blitter_context *blitter, default:; } - blitter_draw(ctx, x1, y1, x2, y2, depth); + blitter_draw(ctx, x1, y1, x2, y2, depth, 1); } -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) +static void *get_clear_blend_state(struct blitter_context_priv *ctx, + unsigned clear_buffers) +{ + struct pipe_context *pipe = ctx->base.pipe; + int index; + + clear_buffers &= PIPE_CLEAR_COLOR; + + /* Return an existing blend state. */ + if (!clear_buffers) + return ctx->blend[0][0]; + + index = GET_CLEAR_BLEND_STATE_IDX(clear_buffers); + + if (ctx->blend_clear[index]) + return ctx->blend_clear[index]; + + /* Create a new one. */ + { + struct pipe_blend_state blend = {0}; + unsigned i; + + blend.independent_blend_enable = 1; + + for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) { + if (clear_buffers & (PIPE_CLEAR_COLOR0 << i)) { + blend.rt[i].colormask = PIPE_MASK_RGBA; + } + } + + ctx->blend_clear[index] = pipe->create_blend_state(pipe, &blend); + } + return ctx->blend_clear[index]; +} + +void util_blitter_common_clear_setup(struct blitter_context *blitter, + unsigned width, unsigned height, + unsigned clear_buffers, + void *custom_blend, void *custom_dsa) { 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_set_running_flag(ctx); + util_blitter_set_running_flag(blitter); blitter_check_saved_vertex_states(ctx); blitter_check_saved_fragment_states(ctx); + blitter_disable_render_cond(ctx); /* bind states */ if (custom_blend) { pipe->bind_blend_state(pipe, custom_blend); - } else if (clear_buffers & PIPE_CLEAR_COLOR) { - pipe->bind_blend_state(pipe, ctx->blend_write_color); } else { - pipe->bind_blend_state(pipe, ctx->blend_keep_color); + pipe->bind_blend_state(pipe, get_clear_blend_state(ctx, clear_buffers)); } if (custom_dsa) { @@ -919,39 +1309,58 @@ static void util_blitter_clear_custom(struct blitter_context *blitter, pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); } + pipe->set_sample_mask(pipe, ~0); + blitter_set_dst_dimensions(ctx, width, height); +} + +static void util_blitter_clear_custom(struct blitter_context *blitter, + unsigned width, unsigned height, + unsigned num_layers, + unsigned clear_buffers, + const union pipe_color_union *color, + double depth, unsigned stencil, + void *custom_blend, void *custom_dsa) +{ + struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; + struct pipe_context *pipe = ctx->base.pipe; + struct pipe_stencil_ref sr = { { 0 } }; + + assert(ctx->has_layered || num_layers <= 1); + + util_blitter_common_clear_setup(blitter, width, height, clear_buffers, + custom_blend, custom_dsa); + sr.ref_value[0] = stencil & 0xff; pipe->set_stencil_ref(pipe, &sr); - 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->set_sample_mask(pipe, ~0); + pipe->bind_vertex_elements_state(pipe, ctx->velem_state); + bind_fs_write_all_cbufs(ctx); - blitter_set_common_draw_rect_state(ctx); - blitter_set_dst_dimensions(ctx, width, height); - blitter->draw_rectangle(blitter, 0, 0, width, height, depth, - UTIL_BLITTER_ATTRIB_COLOR, color); + if (num_layers > 1 && ctx->has_layered) { + blitter_set_common_draw_rect_state(ctx, FALSE, TRUE); + blitter_set_clear_color(ctx, color); + blitter_draw(ctx, 0, 0, width, height, depth, num_layers); + } + else { + blitter_set_common_draw_rect_state(ctx, FALSE, FALSE); + blitter->draw_rectangle(blitter, 0, 0, width, height, (float) depth, + UTIL_BLITTER_ATTRIB_COLOR, color); + } - blitter_restore_vertex_states(ctx); - blitter_restore_fragment_states(ctx); - blitter_unset_running_flag(ctx); + util_blitter_restore_vertex_states(blitter); + util_blitter_restore_fragment_states(blitter); + util_blitter_restore_render_cond(blitter); + util_blitter_unset_running_flag(blitter); } void util_blitter_clear(struct blitter_context *blitter, - unsigned width, unsigned height, - unsigned num_cbufs, + unsigned width, unsigned height, unsigned num_layers, 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, cbuf_format, color, depth, stencil, + util_blitter_clear_custom(blitter, width, height, num_layers, + clear_buffers, color, depth, stencil, NULL, NULL); } @@ -959,119 +1368,113 @@ void util_blitter_custom_clear_depth(struct blitter_context *blitter, unsigned width, unsigned height, double depth, void *custom_dsa) { - static const union pipe_color_union color; - util_blitter_clear_custom(blitter, width, height, 0, - 0, PIPE_FORMAT_NONE, &color, depth, 0, NULL, custom_dsa); -} - -static -boolean is_overlap(unsigned dstx, unsigned dsty, unsigned dstz, - const struct pipe_box *srcbox) -{ - struct pipe_box src = *srcbox; - - if (src.width < 0) { - src.x += src.width; - src.width = -src.width; - } - if (src.height < 0) { - src.y += src.height; - src.height = -src.height; - } - if (src.depth < 0) { - src.z += src.depth; - src.depth = -src.depth; - } - return src.x < dstx+src.width && src.x+src.width > dstx && - src.y < dsty+src.height && src.y+src.height > dsty && - src.z < dstz+src.depth && src.z+src.depth > dstz; + static const union pipe_color_union color; + util_blitter_clear_custom(blitter, width, height, 0, 0, &color, depth, 0, + NULL, custom_dsa); } void util_blitter_default_dst_texture(struct pipe_surface *dst_templ, struct pipe_resource *dst, unsigned dstlevel, - unsigned dstz, - const struct pipe_box *srcbox) + unsigned dstz) { - memset(dst_templ, 0, sizeof(*dst_templ)); - dst_templ->format = dst->format; - if (util_format_is_depth_or_stencil(dst->format)) { - dst_templ->usage = PIPE_BIND_DEPTH_STENCIL; - } else { - dst_templ->usage = PIPE_BIND_RENDER_TARGET; - } - dst_templ->format = util_format_linear(dst->format); - dst_templ->u.tex.level = dstlevel; - dst_templ->u.tex.first_layer = dstz; - dst_templ->u.tex.last_layer = dstz + srcbox->depth - 1; + memset(dst_templ, 0, sizeof(*dst_templ)); + dst_templ->format = util_format_linear(dst->format); + dst_templ->u.tex.level = dstlevel; + dst_templ->u.tex.first_layer = dstz; + dst_templ->u.tex.last_layer = dstz; +} + +static struct pipe_surface * +util_blitter_get_next_surface_layer(struct pipe_context *pipe, + struct pipe_surface *surf) +{ + struct pipe_surface dst_templ; + + memset(&dst_templ, 0, sizeof(dst_templ)); + dst_templ.format = surf->format; + dst_templ.u.tex.level = surf->u.tex.level; + dst_templ.u.tex.first_layer = surf->u.tex.first_layer + 1; + dst_templ.u.tex.last_layer = surf->u.tex.last_layer + 1; + + return pipe->create_surface(pipe, surf->texture, &dst_templ); } void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ, struct pipe_resource *src, unsigned srclevel) { - memset(src_templ, 0, sizeof(*src_templ)); - src_templ->format = util_format_linear(src->format); - src_templ->u.tex.first_level = srclevel; - src_templ->u.tex.last_level = srclevel; - src_templ->u.tex.first_layer = 0; - src_templ->u.tex.last_layer = - src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1 - : src->array_size - 1; - src_templ->swizzle_r = PIPE_SWIZZLE_RED; - src_templ->swizzle_g = PIPE_SWIZZLE_GREEN; - src_templ->swizzle_b = PIPE_SWIZZLE_BLUE; - src_templ->swizzle_a = PIPE_SWIZZLE_ALPHA; + memset(src_templ, 0, sizeof(*src_templ)); + src_templ->target = src->target; + src_templ->format = util_format_linear(src->format); + src_templ->u.tex.first_level = srclevel; + src_templ->u.tex.last_level = srclevel; + src_templ->u.tex.first_layer = 0; + src_templ->u.tex.last_layer = + src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1 + : src->array_size - 1; + src_templ->swizzle_r = PIPE_SWIZZLE_X; + src_templ->swizzle_g = PIPE_SWIZZLE_Y; + src_templ->swizzle_b = PIPE_SWIZZLE_Z; + src_templ->swizzle_a = PIPE_SWIZZLE_W; } -boolean util_blitter_is_copy_supported(struct blitter_context *blitter, - const struct pipe_resource *dst, - const struct pipe_resource *src, - unsigned mask) +static boolean is_blit_generic_supported(struct blitter_context *blitter, + const struct pipe_resource *dst, + enum pipe_format dst_format, + const struct pipe_resource *src, + enum pipe_format src_format, + unsigned mask) { struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; struct pipe_screen *screen = ctx->base.pipe->screen; if (dst) { unsigned bind; - boolean is_stencil; const struct util_format_description *desc = - util_format_description(dst->format); - - is_stencil = util_format_has_stencil(desc); + util_format_description(dst_format); + boolean dst_has_stencil = util_format_has_stencil(desc); /* Stencil export must be supported for stencil copy. */ - if ((mask & PIPE_MASK_S) && is_stencil && !ctx->has_stencil_export) { + if ((mask & PIPE_MASK_S) && dst_has_stencil && + !ctx->has_stencil_export) { return FALSE; } - if (is_stencil || util_format_has_depth(desc)) + if (dst_has_stencil || util_format_has_depth(desc)) bind = PIPE_BIND_DEPTH_STENCIL; else bind = PIPE_BIND_RENDER_TARGET; - if (!screen->is_format_supported(screen, dst->format, dst->target, + if (!screen->is_format_supported(screen, dst_format, dst->target, dst->nr_samples, bind)) { return FALSE; } } if (src) { - if (!screen->is_format_supported(screen, src->format, src->target, + if (src->nr_samples > 1 && !ctx->has_texture_multisample) { + return FALSE; + } + + if (!screen->is_format_supported(screen, src_format, src->target, src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) { return FALSE; } /* Check stencil sampler support for stencil copy. */ - if (util_format_has_stencil(util_format_description(src->format))) { - enum pipe_format stencil_format = - util_format_stencil_only(src->format); - assert(stencil_format != PIPE_FORMAT_NONE); - - if (stencil_format != src->format && - !screen->is_format_supported(screen, stencil_format, src->target, - src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) { - return FALSE; + if (mask & PIPE_MASK_S) { + if (util_format_has_stencil(util_format_description(src_format))) { + enum pipe_format stencil_format = + util_format_stencil_only(src_format); + assert(stencil_format != PIPE_FORMAT_NONE); + + if (stencil_format != src_format && + !screen->is_format_supported(screen, stencil_format, + src->target, src->nr_samples, + PIPE_BIND_SAMPLER_VIEW)) { + return FALSE; + } } } } @@ -1079,24 +1482,45 @@ boolean util_blitter_is_copy_supported(struct blitter_context *blitter, return TRUE; } +boolean util_blitter_is_copy_supported(struct blitter_context *blitter, + const struct pipe_resource *dst, + const struct pipe_resource *src) +{ + return is_blit_generic_supported(blitter, dst, dst->format, + src, src->format, PIPE_MASK_RGBAZS); +} + +boolean util_blitter_is_blit_supported(struct blitter_context *blitter, + const struct pipe_blit_info *info) +{ + return is_blit_generic_supported(blitter, + info->dst.resource, info->dst.format, + info->src.resource, info->src.format, + info->mask); +} + void util_blitter_copy_texture(struct blitter_context *blitter, struct pipe_resource *dst, - unsigned dst_level, unsigned dst_sample_mask, + unsigned dst_level, unsigned dstx, unsigned dsty, unsigned dstz, struct pipe_resource *src, - unsigned src_level, unsigned src_sample, + unsigned src_level, const struct pipe_box *srcbox) { struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; struct pipe_context *pipe = ctx->base.pipe; struct pipe_surface *dst_view, dst_templ; struct pipe_sampler_view src_templ, *src_view; + struct pipe_box dstbox; assert(dst && src); assert(src->target < PIPE_MAX_TEXTURE_TYPES); + u_box_3d(dstx, dsty, dstz, abs(srcbox->width), abs(srcbox->height), + abs(srcbox->depth), &dstbox); + /* Initialize the surface. */ - util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz, srcbox); + util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz); dst_view = pipe->create_surface(pipe, dst, &dst_templ); /* Initialize the sampler view. */ @@ -1104,103 +1528,251 @@ void util_blitter_copy_texture(struct blitter_context *blitter, src_view = pipe->create_sampler_view(pipe, src, &src_templ); /* Copy. */ - util_blitter_copy_texture_view(blitter, dst_view, dst_sample_mask, dstx, - dsty, src_view, src_sample, srcbox, - src->width0, src->height0, PIPE_MASK_RGBAZS); + util_blitter_blit_generic(blitter, dst_view, &dstbox, + src_view, srcbox, src->width0, src->height0, + PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL, + FALSE); pipe_surface_reference(&dst_view, NULL); pipe_sampler_view_reference(&src_view, NULL); } -void util_blitter_copy_texture_view(struct blitter_context *blitter, - struct pipe_surface *dst, - unsigned dst_sample_mask, - unsigned dstx, unsigned dsty, - struct pipe_sampler_view *src, - unsigned src_sample, - const struct pipe_box *srcbox, - unsigned src_width0, unsigned src_height0, - unsigned mask) +static void do_blits(struct blitter_context_priv *ctx, + struct pipe_surface *dst, + const struct pipe_box *dstbox, + struct pipe_sampler_view *src, + unsigned src_width0, + unsigned src_height0, + const struct pipe_box *srcbox, + bool is_zsbuf) { - struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; struct pipe_context *pipe = ctx->base.pipe; - struct pipe_framebuffer_state fb_state; + unsigned src_samples = src->texture->nr_samples; + unsigned dst_samples = dst->texture->nr_samples; enum pipe_texture_target src_target = src->texture->target; - int abs_width = abs(srcbox->width); - int abs_height = abs(srcbox->height); - boolean blit_stencil, blit_depth; - const struct util_format_description *src_desc = - util_format_description(src->format); + struct pipe_framebuffer_state fb_state = {0}; + + /* Initialize framebuffer state. */ + fb_state.width = dst->width; + fb_state.height = dst->height; + fb_state.nr_cbufs = is_zsbuf ? 0 : 1; + + blitter_set_dst_dimensions(ctx, fb_state.width, fb_state.height); + + if ((src_target == PIPE_TEXTURE_1D || + src_target == PIPE_TEXTURE_2D || + src_target == PIPE_TEXTURE_RECT) && + src_samples <= 1) { + /* Draw the quad with the draw_rectangle callback. */ - blit_depth = util_format_has_depth(src_desc) && (mask & PIPE_MASK_Z); - blit_stencil = util_format_has_stencil(src_desc) && (mask & PIPE_MASK_S); + /* Set texture coordinates. - use a pipe color union + * for interface purposes. + * XXX pipe_color_union is a wrong name since we use that to set + * texture coordinates too. + */ + union pipe_color_union coord; + get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y, + srcbox->x+srcbox->width, srcbox->y+srcbox->height, coord.f); + + /* Set framebuffer state. */ + if (is_zsbuf) { + fb_state.zsbuf = dst; + } else { + fb_state.cbufs[0] = dst; + } + pipe->set_framebuffer_state(pipe, &fb_state); + + /* Draw. */ + pipe->set_sample_mask(pipe, ~0); + ctx->base.draw_rectangle(&ctx->base, dstbox->x, dstbox->y, + dstbox->x + dstbox->width, + dstbox->y + dstbox->height, 0, + UTIL_BLITTER_ATTRIB_TEXCOORD, &coord); + } else { + /* Draw the quad with the generic codepath. */ + int dst_z; + for (dst_z = 0; dst_z < dstbox->depth; dst_z++) { + struct pipe_surface *old; + float dst2src_scale = srcbox->depth / (float)dstbox->depth; + + /* Scale Z properly if the blit is scaled. + * + * When downscaling, we want the coordinates centered, so that + * mipmapping works for 3D textures. For example, when generating + * a 4x4x4 level, this wouldn't average the pixels: + * + * src Z: 0 1 2 3 4 5 6 7 + * dst Z: 0 1 2 3 + * + * Because the pixels are not centered below the pixels of the higher + * level. Therefore, we want this: + * src Z: 0 1 2 3 4 5 6 7 + * dst Z: 0 1 2 3 + * + * dst_offset defines the offset needed for centering the pixels and + * it works with any scaling (not just 2x). + */ + float dst_offset = ((srcbox->depth - 1) - + (dstbox->depth - 1) * dst2src_scale) * 0.5; + float src_z = (dst_z + dst_offset) * dst2src_scale; + + /* Set framebuffer state. */ + if (is_zsbuf) { + fb_state.zsbuf = dst; + } else { + fb_state.cbufs[0] = dst; + } + pipe->set_framebuffer_state(pipe, &fb_state); + + /* See if we need to blit a multisample or singlesample buffer. */ + if (src_samples == dst_samples && dst_samples > 1) { + /* MSAA copy. */ + unsigned i, max_sample = dst_samples - 1; + + for (i = 0; i <= max_sample; i++) { + pipe->set_sample_mask(pipe, 1 << i); + blitter_set_texcoords(ctx, src, src_width0, src_height0, + srcbox->z + src_z, + i, srcbox->x, srcbox->y, + srcbox->x + srcbox->width, + srcbox->y + srcbox->height); + blitter_draw(ctx, dstbox->x, dstbox->y, + dstbox->x + dstbox->width, + dstbox->y + dstbox->height, 0, 1); + } + } else { + /* Normal copy, MSAA upsampling, or MSAA resolve. */ + pipe->set_sample_mask(pipe, ~0); + blitter_set_texcoords(ctx, src, src_width0, src_height0, + srcbox->z + src_z, 0, + srcbox->x, srcbox->y, + srcbox->x + srcbox->width, + srcbox->y + srcbox->height); + blitter_draw(ctx, dstbox->x, dstbox->y, + dstbox->x + dstbox->width, + dstbox->y + dstbox->height, 0, 1); + } - /* If you want a fallback for stencil copies, - * use util_blitter_copy_texture. */ - if (blit_stencil && !ctx->has_stencil_export) { - blit_stencil = FALSE; + /* Get the next surface or (if this is the last iteration) + * just unreference the last one. */ + old = dst; + if (dst_z < dstbox->depth-1) { + dst = ctx->base.get_next_surface_layer(ctx->base.pipe, dst); + } + if (dst_z) { + pipe_surface_reference(&old, NULL); + } + } + } +} - if (!blit_depth) - return; +void util_blitter_blit_generic(struct blitter_context *blitter, + struct pipe_surface *dst, + const struct pipe_box *dstbox, + struct pipe_sampler_view *src, + const struct pipe_box *srcbox, + unsigned src_width0, unsigned src_height0, + unsigned mask, unsigned filter, + const struct pipe_scissor_state *scissor, + boolean alpha_blend) +{ + struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; + struct pipe_context *pipe = ctx->base.pipe; + enum pipe_texture_target src_target = src->texture->target; + unsigned src_samples = src->texture->nr_samples; + unsigned dst_samples = dst->texture->nr_samples; + boolean has_depth, has_stencil, has_color; + boolean blit_stencil, blit_depth, blit_color; + void *sampler_state; + const struct util_format_description *src_desc = + util_format_description(src->format); + const struct util_format_description *dst_desc = + util_format_description(dst->format); + + has_color = src_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS && + dst_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS; + has_depth = util_format_has_depth(src_desc) && + util_format_has_depth(dst_desc); + has_stencil = util_format_has_stencil(src_desc) && + util_format_has_stencil(dst_desc); + + blit_color = has_color && (mask & PIPE_MASK_RGBA); + blit_depth = has_depth && (mask & PIPE_MASK_Z); + blit_stencil = has_stencil && (mask & PIPE_MASK_S) && + ctx->has_stencil_export; + + if (!blit_stencil && !blit_depth && !blit_color) { + return; } - /* Sanity checks. */ - if (dst->texture == src->texture && - dst->u.tex.level == src->u.tex.first_level) { - assert(!is_overlap(dstx, dsty, 0, srcbox)); + if (blit_stencil || + (dstbox->width == abs(srcbox->width) && + dstbox->height == abs(srcbox->height))) { + filter = PIPE_TEX_FILTER_NEAREST; } - /* XXX should handle 3d regions */ - assert(srcbox->depth == 1); /* Check whether the states are properly saved. */ - blitter_set_running_flag(ctx); + util_blitter_set_running_flag(blitter); 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 = dst->width; - fb_state.height = dst->height; + blitter_disable_render_cond(ctx); if (blit_depth || blit_stencil) { - pipe->bind_blend_state(pipe, ctx->blend_keep_color); + pipe->bind_blend_state(pipe, ctx->blend[0][0]); if (blit_depth && blit_stencil) { pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil); - pipe->bind_fs_state(pipe, - blitter_get_fs_texfetch_depthstencil(ctx, src->texture)); + ctx->bind_fs_state(pipe, + blitter_get_fs_texfetch_depthstencil(ctx, src_target, + src_samples)); } else if (blit_depth) { pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil); - pipe->bind_fs_state(pipe, - blitter_get_fs_texfetch_depth(ctx, src->texture)); + ctx->bind_fs_state(pipe, + blitter_get_fs_texfetch_depth(ctx, src_target, + src_samples)); } else { /* is_stencil */ pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil); - pipe->bind_fs_state(pipe, - blitter_get_fs_texfetch_stencil(ctx, src->texture)); + ctx->bind_fs_state(pipe, + blitter_get_fs_texfetch_stencil(ctx, src_target, + src_samples)); } - fb_state.nr_cbufs = 0; - fb_state.zsbuf = dst; } else { - pipe->bind_blend_state(pipe, ctx->blend_write_color); + unsigned colormask = mask & PIPE_MASK_RGBA; + + pipe->bind_blend_state(pipe, ctx->blend[colormask][alpha_blend]); pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); - pipe->bind_fs_state(pipe, - blitter_get_fs_texfetch_col(ctx, src->texture)); + ctx->bind_fs_state(pipe, + blitter_get_fs_texfetch_col(ctx, src->format, dst->format, src_target, + src_samples, dst_samples, filter)); + } - fb_state.nr_cbufs = 1; - fb_state.cbufs[0] = dst; - fb_state.zsbuf = 0; + /* Set the linear filter only for scaled color non-MSAA blits. */ + if (filter == PIPE_TEX_FILTER_LINEAR) { + if (src_target == PIPE_TEXTURE_RECT) { + sampler_state = ctx->sampler_state_rect_linear; + } else { + sampler_state = ctx->sampler_state_linear; + } + } else { + if (src_target == PIPE_TEXTURE_RECT) { + sampler_state = ctx->sampler_state_rect; + } else { + sampler_state = ctx->sampler_state; + } } + /* Set samplers. */ if (blit_depth && blit_stencil) { /* Setup two samplers, one for depth and the other one for stencil. */ struct pipe_sampler_view templ; struct pipe_sampler_view *views[2]; - void *samplers[2] = {ctx->sampler_state, ctx->sampler_state}; + void *samplers[2] = {sampler_state, sampler_state}; templ = *src; templ.format = util_format_stencil_only(templ.format); @@ -1209,8 +1781,8 @@ void util_blitter_copy_texture_view(struct blitter_context *blitter, views[0] = src; views[1] = pipe->create_sampler_view(pipe, src->texture, &templ); - pipe->set_fragment_sampler_views(pipe, 2, views); - pipe->bind_fragment_sampler_states(pipe, 2, samplers); + pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 2, views); + pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, 2, samplers); pipe_sampler_view_reference(&views[1], NULL); } else if (blit_stencil) { @@ -1224,54 +1796,170 @@ void util_blitter_copy_texture_view(struct blitter_context *blitter, view = pipe->create_sampler_view(pipe, src->texture, &templ); - pipe->set_fragment_sampler_views(pipe, 1, &view); - pipe->bind_fragment_sampler_states(pipe, 1, &ctx->sampler_state); + pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &view); + pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, + 0, 1, &sampler_state); pipe_sampler_view_reference(&view, NULL); } else { - pipe->set_fragment_sampler_views(pipe, 1, &src); - pipe->bind_fragment_sampler_states(pipe, 1, &ctx->sampler_state); + pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src); + pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, + 0, 1, &sampler_state); } pipe->bind_vertex_elements_state(pipe, ctx->velem_state); - pipe->set_framebuffer_state(pipe, &fb_state); - pipe->set_sample_mask(pipe, dst_sample_mask); + if (scissor) { + pipe->set_scissor_states(pipe, 0, 1, scissor); + } - blitter_set_common_draw_rect_state(ctx); - blitter_set_dst_dimensions(ctx, dst->width, dst->height); + blitter_set_common_draw_rect_state(ctx, scissor != NULL, FALSE); - if ((src_target == PIPE_TEXTURE_1D || - src_target == PIPE_TEXTURE_2D || - src_target == PIPE_TEXTURE_RECT) && - src->texture->nr_samples <= 1) { - /* Draw the quad with the draw_rectangle callback. */ + do_blits(ctx, dst, dstbox, src, src_width0, src_height0, + srcbox, blit_depth || blit_stencil); - /* Set texture coordinates. - use a pipe color union - * for interface purposes. - * XXX pipe_color_union is a wrong name since we use that to set - * texture coordinates too. - */ - union pipe_color_union coord; - get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y, - srcbox->x+srcbox->width, srcbox->y+srcbox->height, coord.f); + util_blitter_restore_vertex_states(blitter); + util_blitter_restore_fragment_states(blitter); + util_blitter_restore_textures(blitter); + util_blitter_restore_fb_state(blitter); + if (scissor) { + pipe->set_scissor_states(pipe, 0, 1, &ctx->base.saved_scissor); + } + util_blitter_restore_render_cond(blitter); + util_blitter_unset_running_flag(blitter); +} - /* Draw. */ - blitter->draw_rectangle(blitter, dstx, dsty, dstx+abs_width, dsty+abs_height, 0, - UTIL_BLITTER_ATTRIB_TEXCOORD, &coord); +void +util_blitter_blit(struct blitter_context *blitter, + const struct pipe_blit_info *info) +{ + struct pipe_resource *dst = info->dst.resource; + struct pipe_resource *src = info->src.resource; + struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; + struct pipe_context *pipe = ctx->base.pipe; + struct pipe_surface *dst_view, dst_templ; + struct pipe_sampler_view src_templ, *src_view; + + /* Initialize the surface. */ + util_blitter_default_dst_texture(&dst_templ, dst, info->dst.level, + info->dst.box.z); + dst_templ.format = info->dst.format; + dst_view = pipe->create_surface(pipe, dst, &dst_templ); + + /* Initialize the sampler view. */ + util_blitter_default_src_texture(&src_templ, src, info->src.level); + src_templ.format = info->src.format; + src_view = pipe->create_sampler_view(pipe, src, &src_templ); + + /* Copy. */ + util_blitter_blit_generic(blitter, dst_view, &info->dst.box, + src_view, &info->src.box, src->width0, src->height0, + info->mask, info->filter, + info->scissor_enable ? &info->scissor : NULL, + info->alpha_blend); + + pipe_surface_reference(&dst_view, NULL); + pipe_sampler_view_reference(&src_view, NULL); +} + +void util_blitter_generate_mipmap(struct blitter_context *blitter, + struct pipe_resource *tex, + enum pipe_format format, + unsigned base_level, unsigned last_level, + unsigned first_layer, unsigned last_layer) +{ + struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; + struct pipe_context *pipe = ctx->base.pipe; + struct pipe_surface dst_templ, *dst_view; + struct pipe_sampler_view src_templ, *src_view; + boolean is_depth; + void *sampler_state; + const struct util_format_description *desc = + util_format_description(format); + unsigned src_level; + + assert(tex->nr_samples <= 1); + assert(!util_format_has_stencil(desc)); + + is_depth = desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS; + + /* Check whether the states are properly saved. */ + util_blitter_set_running_flag(blitter); + blitter_check_saved_vertex_states(ctx); + blitter_check_saved_fragment_states(ctx); + blitter_check_saved_textures(ctx); + blitter_check_saved_fb_state(ctx); + blitter_disable_render_cond(ctx); + + /* Set states. */ + if (is_depth) { + pipe->bind_blend_state(pipe, ctx->blend[0][0]); + pipe->bind_depth_stencil_alpha_state(pipe, + ctx->dsa_write_depth_keep_stencil); + ctx->bind_fs_state(pipe, + blitter_get_fs_texfetch_depth(ctx, tex->target, 1)); } else { - /* Draw the quad with the generic codepath. */ - blitter_set_texcoords(ctx, src, src_width0, src_height0, srcbox->z, - src_sample, - srcbox->x, srcbox->y, - srcbox->x + srcbox->width, srcbox->y + srcbox->height); - blitter_draw(ctx, dstx, dsty, dstx+abs_width, dsty+abs_height, 0); - } - - 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->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]); + pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); + ctx->bind_fs_state(pipe, + blitter_get_fs_texfetch_col(ctx, tex->format, tex->format, tex->target, + 1, 1, PIPE_TEX_FILTER_LINEAR)); + } + + if (tex->target == PIPE_TEXTURE_RECT) { + sampler_state = ctx->sampler_state_rect_linear; + } else { + sampler_state = ctx->sampler_state_linear; + } + pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, + 0, 1, &sampler_state); + + pipe->bind_vertex_elements_state(pipe, ctx->velem_state); + blitter_set_common_draw_rect_state(ctx, FALSE, FALSE); + + for (src_level = base_level; src_level < last_level; src_level++) { + struct pipe_box dstbox = {0}, srcbox = {0}; + unsigned dst_level = src_level + 1; + + dstbox.width = u_minify(tex->width0, dst_level); + dstbox.height = u_minify(tex->height0, dst_level); + + srcbox.width = u_minify(tex->width0, src_level); + srcbox.height = u_minify(tex->height0, src_level); + + if (tex->target == PIPE_TEXTURE_3D) { + dstbox.depth = util_max_layer(tex, dst_level) + 1; + srcbox.depth = util_max_layer(tex, src_level) + 1; + } else { + dstbox.z = srcbox.z = first_layer; + dstbox.depth = srcbox.depth = last_layer - first_layer + 1; + } + + /* Initialize the surface. */ + util_blitter_default_dst_texture(&dst_templ, tex, dst_level, + first_layer); + dst_templ.format = format; + dst_view = pipe->create_surface(pipe, tex, &dst_templ); + + /* Initialize the sampler view. */ + util_blitter_default_src_texture(&src_templ, tex, src_level); + src_templ.format = format; + src_view = pipe->create_sampler_view(pipe, tex, &src_templ); + + pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src_view); + + do_blits(ctx, dst_view, &dstbox, src_view, tex->width0, tex->height0, + &srcbox, is_depth); + + pipe_surface_reference(&dst_view, NULL); + pipe_sampler_view_reference(&src_view, NULL); + } + + util_blitter_restore_vertex_states(blitter); + util_blitter_restore_fragment_states(blitter); + util_blitter_restore_textures(blitter); + util_blitter_restore_fb_state(blitter); + util_blitter_restore_render_cond(blitter); + util_blitter_unset_running_flag(blitter); } /* Clear a region of a color surface to a constant value. */ @@ -1284,21 +1972,23 @@ void util_blitter_clear_render_target(struct blitter_context *blitter, struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; struct pipe_context *pipe = ctx->base.pipe; struct pipe_framebuffer_state fb_state; + unsigned num_layers; assert(dstsurf->texture); if (!dstsurf->texture) return; /* check the saved state */ - blitter_set_running_flag(ctx); + util_blitter_set_running_flag(blitter); blitter_check_saved_vertex_states(ctx); blitter_check_saved_fragment_states(ctx); blitter_check_saved_fb_state(ctx); + blitter_disable_render_cond(ctx); /* bind states */ - pipe->bind_blend_state(pipe, ctx->blend_write_color); + pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]); pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); - pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE)); + bind_fs_write_one_cbuf(ctx); pipe->bind_vertex_elements_state(pipe, ctx->velem_state); /* set a framebuffer state */ @@ -1310,15 +2000,25 @@ void util_blitter_clear_render_target(struct blitter_context *blitter, pipe->set_framebuffer_state(pipe, &fb_state); pipe->set_sample_mask(pipe, ~0); - blitter_set_common_draw_rect_state(ctx); 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_vertex_states(ctx); - blitter_restore_fragment_states(ctx); - blitter_restore_fb_state(ctx); - blitter_unset_running_flag(ctx); + num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1; + if (num_layers > 1 && ctx->has_layered) { + blitter_set_common_draw_rect_state(ctx, FALSE, TRUE); + blitter_set_clear_color(ctx, color); + blitter_draw(ctx, dstx, dsty, dstx+width, dsty+height, 0, num_layers); + } + else { + blitter_set_common_draw_rect_state(ctx, FALSE, FALSE); + blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0, + UTIL_BLITTER_ATTRIB_COLOR, color); + } + + util_blitter_restore_vertex_states(blitter); + util_blitter_restore_fragment_states(blitter); + util_blitter_restore_fb_state(blitter); + util_blitter_restore_render_cond(blitter); + util_blitter_unset_running_flag(blitter); } /* Clear a region of a depth stencil surface. */ @@ -1334,19 +2034,21 @@ void util_blitter_clear_depth_stencil(struct blitter_context *blitter, struct pipe_context *pipe = ctx->base.pipe; struct pipe_framebuffer_state fb_state; struct pipe_stencil_ref sr = { { 0 } }; + unsigned num_layers; assert(dstsurf->texture); if (!dstsurf->texture) return; /* check the saved state */ - blitter_set_running_flag(ctx); + util_blitter_set_running_flag(blitter); blitter_check_saved_vertex_states(ctx); blitter_check_saved_fragment_states(ctx); blitter_check_saved_fb_state(ctx); + blitter_disable_render_cond(ctx); /* bind states */ - pipe->bind_blend_state(pipe, ctx->blend_keep_color); + pipe->bind_blend_state(pipe, ctx->blend[0][0]); if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) { sr.ref_value[0] = stencil & 0xff; pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil); @@ -1364,7 +2066,7 @@ void util_blitter_clear_depth_stencil(struct blitter_context *blitter, /* hmm that should be illegal probably, or make it a no-op somewhere */ pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); - pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE)); + bind_fs_empty(ctx); pipe->bind_vertex_elements_state(pipe, ctx->velem_state); /* set a framebuffer state */ @@ -1376,15 +2078,25 @@ void util_blitter_clear_depth_stencil(struct blitter_context *blitter, pipe->set_framebuffer_state(pipe, &fb_state); pipe->set_sample_mask(pipe, ~0); - blitter_set_common_draw_rect_state(ctx); 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_vertex_states(ctx); - blitter_restore_fragment_states(ctx); - blitter_restore_fb_state(ctx); - blitter_unset_running_flag(ctx); + num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1; + if (num_layers > 1 && ctx->has_layered) { + blitter_set_common_draw_rect_state(ctx, FALSE, TRUE); + blitter_draw(ctx, dstx, dsty, dstx+width, dsty+height, (float) depth, num_layers); + } + else { + blitter_set_common_draw_rect_state(ctx, FALSE, FALSE); + blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, + (float) depth, + UTIL_BLITTER_ATTRIB_NONE, NULL); + } + + util_blitter_restore_vertex_states(blitter); + util_blitter_restore_fragment_states(blitter); + util_blitter_restore_fb_state(blitter); + util_blitter_restore_render_cond(blitter); + util_blitter_unset_running_flag(blitter); } /* draw a rectangle across a region using a custom dsa stage - for r600g */ @@ -1403,15 +2115,20 @@ void util_blitter_custom_depth_stencil(struct blitter_context *blitter, return; /* check the saved state */ - blitter_set_running_flag(ctx); + util_blitter_set_running_flag(blitter); blitter_check_saved_vertex_states(ctx); blitter_check_saved_fragment_states(ctx); blitter_check_saved_fb_state(ctx); + blitter_disable_render_cond(ctx); /* bind states */ - pipe->bind_blend_state(pipe, ctx->blend_write_color); + pipe->bind_blend_state(pipe, cbsurf ? ctx->blend[PIPE_MASK_RGBA][0] : + ctx->blend[0][0]); pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage); - pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE)); + if (cbsurf) + bind_fs_write_one_cbuf(ctx); + else + bind_fs_empty(ctx); pipe->bind_vertex_elements_state(pipe, ctx->velem_state); /* set a framebuffer state */ @@ -1429,15 +2146,16 @@ void util_blitter_custom_depth_stencil(struct blitter_context *blitter, pipe->set_framebuffer_state(pipe, &fb_state); pipe->set_sample_mask(pipe, sample_mask); - blitter_set_common_draw_rect_state(ctx); + blitter_set_common_draw_rect_state(ctx, FALSE, FALSE); 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_vertex_states(ctx); - blitter_restore_fragment_states(ctx); - blitter_restore_fb_state(ctx); - blitter_unset_running_flag(ctx); + util_blitter_restore_vertex_states(blitter); + util_blitter_restore_fragment_states(blitter); + util_blitter_restore_fb_state(blitter); + util_blitter_restore_render_cond(blitter); + util_blitter_unset_running_flag(blitter); } void util_blitter_copy_buffer(struct blitter_context *blitter, @@ -1451,6 +2169,7 @@ void util_blitter_copy_buffer(struct blitter_context *blitter, struct pipe_context *pipe = ctx->base.pipe; struct pipe_vertex_buffer vb; struct pipe_stream_output_target *so_target; + unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0}; if (srcx >= src->width0 || dstx >= dst->width0) { @@ -1476,30 +2195,105 @@ void util_blitter_copy_buffer(struct blitter_context *blitter, return; } - blitter_set_running_flag(ctx); + util_blitter_set_running_flag(blitter); blitter_check_saved_vertex_states(ctx); + blitter_disable_render_cond(ctx); vb.buffer = src; vb.buffer_offset = srcx; vb.stride = 4; - pipe->set_vertex_buffers(pipe, 1, &vb); - pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf); - pipe->bind_vs_state(pipe, ctx->vs_pos_only); + pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb); + pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf[0]); + bind_vs_pos_only(ctx, 1); if (ctx->has_geometry_shader) pipe->bind_gs_state(pipe, NULL); + if (ctx->has_tessellation) { + pipe->bind_tcs_state(pipe, NULL); + pipe->bind_tes_state(pipe, NULL); + } pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state); so_target = pipe->create_stream_output_target(pipe, dst, dstx, size); - pipe->set_stream_output_targets(pipe, 1, &so_target, 0); + pipe->set_stream_output_targets(pipe, 1, &so_target, offsets); util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4); - blitter_restore_vertex_states(ctx); - blitter_unset_running_flag(ctx); + util_blitter_restore_vertex_states(blitter); + util_blitter_restore_render_cond(blitter); + util_blitter_unset_running_flag(blitter); pipe_so_target_reference(&so_target, NULL); } +void util_blitter_clear_buffer(struct blitter_context *blitter, + struct pipe_resource *dst, + unsigned offset, unsigned size, + unsigned num_channels, + const union pipe_color_union *clear_value) +{ + struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; + struct pipe_context *pipe = ctx->base.pipe; + struct pipe_vertex_buffer vb = {0}; + struct pipe_stream_output_target *so_target = NULL; + unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0}; + + assert(num_channels >= 1); + assert(num_channels <= 4); + + /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE! + * + * R600 uses this to initialize texture resources, so width0 might not be + * what you think it is. + */ + + /* Streamout is required. */ + if (!ctx->has_stream_out) { + assert(!"Streamout unsupported in util_blitter_clear_buffer()"); + return; + } + + /* Some alignment is required. */ + if (offset % 4 != 0 || size % 4 != 0) { + assert(!"Bad alignment in util_blitter_clear_buffer()"); + return; + } + + u_upload_data(pipe->stream_uploader, 0, num_channels*4, 4, clear_value, + &vb.buffer_offset, &vb.buffer); + if (!vb.buffer) + goto out; + + vb.stride = 0; + + util_blitter_set_running_flag(blitter); + blitter_check_saved_vertex_states(ctx); + blitter_disable_render_cond(ctx); + + pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb); + pipe->bind_vertex_elements_state(pipe, + ctx->velem_state_readbuf[num_channels-1]); + bind_vs_pos_only(ctx, num_channels); + if (ctx->has_geometry_shader) + pipe->bind_gs_state(pipe, NULL); + if (ctx->has_tessellation) { + pipe->bind_tcs_state(pipe, NULL); + pipe->bind_tes_state(pipe, NULL); + } + pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state); + + so_target = pipe->create_stream_output_target(pipe, dst, offset, size); + pipe->set_stream_output_targets(pipe, 1, &so_target, offsets); + + util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4); + +out: + util_blitter_restore_vertex_states(blitter); + util_blitter_restore_render_cond(blitter); + util_blitter_unset_running_flag(blitter); + pipe_so_target_reference(&so_target, NULL); + pipe_resource_reference(&vb.buffer, NULL); +} + /* probably radeon specific */ void util_blitter_custom_resolve_color(struct blitter_context *blitter, struct pipe_resource *dst, @@ -1508,30 +2302,31 @@ void util_blitter_custom_resolve_color(struct blitter_context *blitter, struct pipe_resource *src, unsigned src_layer, unsigned sample_mask, - void *custom_blend) + void *custom_blend, + enum pipe_format format) { struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; struct pipe_context *pipe = ctx->base.pipe; struct pipe_framebuffer_state fb_state; struct pipe_surface *srcsurf, *dstsurf, surf_tmpl; - blitter_set_running_flag(ctx); + util_blitter_set_running_flag(blitter); blitter_check_saved_vertex_states(ctx); blitter_check_saved_fragment_states(ctx); + blitter_disable_render_cond(ctx); /* bind states */ pipe->bind_blend_state(pipe, custom_blend); pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); pipe->bind_vertex_elements_state(pipe, ctx->velem_state); - pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE)); + bind_fs_write_one_cbuf(ctx); pipe->set_sample_mask(pipe, sample_mask); memset(&surf_tmpl, 0, sizeof(surf_tmpl)); - surf_tmpl.format = dst->format; + surf_tmpl.format = format; surf_tmpl.u.tex.level = dst_level; surf_tmpl.u.tex.first_layer = dst_layer; surf_tmpl.u.tex.last_layer = dst_layer; - surf_tmpl.usage = PIPE_BIND_RENDER_TARGET; dstsurf = pipe->create_surface(pipe, dst, &surf_tmpl); @@ -1550,14 +2345,15 @@ void util_blitter_custom_resolve_color(struct blitter_context *blitter, fb_state.zsbuf = NULL; pipe->set_framebuffer_state(pipe, &fb_state); - blitter_set_common_draw_rect_state(ctx); + blitter_set_common_draw_rect_state(ctx, FALSE, FALSE); blitter_set_dst_dimensions(ctx, src->width0, src->height0); blitter->draw_rectangle(blitter, 0, 0, src->width0, src->height0, 0, 0, NULL); - blitter_restore_fb_state(ctx); - blitter_restore_vertex_states(ctx); - blitter_restore_fragment_states(ctx); - blitter_unset_running_flag(ctx); + util_blitter_restore_fb_state(blitter); + util_blitter_restore_vertex_states(blitter); + util_blitter_restore_fragment_states(blitter); + util_blitter_restore_render_cond(blitter); + util_blitter_unset_running_flag(blitter); pipe_surface_reference(&srcsurf, NULL); pipe_surface_reference(&dstsurf, NULL); @@ -1576,15 +2372,17 @@ void util_blitter_custom_color(struct blitter_context *blitter, return; /* check the saved state */ - blitter_set_running_flag(ctx); + util_blitter_set_running_flag(blitter); blitter_check_saved_vertex_states(ctx); blitter_check_saved_fragment_states(ctx); blitter_check_saved_fb_state(ctx); + blitter_disable_render_cond(ctx); /* bind states */ - pipe->bind_blend_state(pipe, custom_blend); + pipe->bind_blend_state(pipe, custom_blend ? custom_blend + : ctx->blend[PIPE_MASK_RGBA][0]); pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); - pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE)); + bind_fs_write_one_cbuf(ctx); pipe->bind_vertex_elements_state(pipe, ctx->velem_state); pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1); @@ -1597,13 +2395,14 @@ void util_blitter_custom_color(struct blitter_context *blitter, pipe->set_framebuffer_state(pipe, &fb_state); pipe->set_sample_mask(pipe, ~0); - blitter_set_common_draw_rect_state(ctx); + blitter_set_common_draw_rect_state(ctx, FALSE, FALSE); blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height); blitter->draw_rectangle(blitter, 0, 0, dstsurf->width, dstsurf->height, 0, 0, NULL); - blitter_restore_vertex_states(ctx); - blitter_restore_fragment_states(ctx); - blitter_restore_fb_state(ctx); - blitter_unset_running_flag(ctx); + util_blitter_restore_vertex_states(blitter); + util_blitter_restore_fragment_states(blitter); + util_blitter_restore_fb_state(blitter); + util_blitter_restore_render_cond(blitter); + util_blitter_unset_running_flag(blitter); }