broadcom/vc5: Fix pasteo that broke vertex texturing.
[mesa.git] / src / gallium / drivers / vc5 / vc5_emit.c
1 /*
2 * Copyright © 2014-2017 Broadcom
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "util/u_format.h"
25 #include "util/u_half.h"
26 #include "vc5_context.h"
27 #include "broadcom/cle/v3d_packet_v33_pack.h"
28 #include "broadcom/compiler/v3d_compiler.h"
29
30 static uint8_t
31 vc5_factor(enum pipe_blendfactor factor)
32 {
33 /* We may get a bad blendfactor when blending is disabled. */
34 if (factor == 0)
35 return V3D_BLEND_FACTOR_ZERO;
36
37 switch (factor) {
38 case PIPE_BLENDFACTOR_ZERO:
39 return V3D_BLEND_FACTOR_ZERO;
40 case PIPE_BLENDFACTOR_ONE:
41 return V3D_BLEND_FACTOR_ONE;
42 case PIPE_BLENDFACTOR_SRC_COLOR:
43 return V3D_BLEND_FACTOR_SRC_COLOR;
44 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
45 return V3D_BLEND_FACTOR_INV_SRC_COLOR;
46 case PIPE_BLENDFACTOR_DST_COLOR:
47 return V3D_BLEND_FACTOR_DST_COLOR;
48 case PIPE_BLENDFACTOR_INV_DST_COLOR:
49 return V3D_BLEND_FACTOR_INV_DST_COLOR;
50 case PIPE_BLENDFACTOR_SRC_ALPHA:
51 return V3D_BLEND_FACTOR_SRC_ALPHA;
52 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
53 return V3D_BLEND_FACTOR_INV_SRC_ALPHA;
54 case PIPE_BLENDFACTOR_DST_ALPHA:
55 return V3D_BLEND_FACTOR_DST_ALPHA;
56 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
57 return V3D_BLEND_FACTOR_INV_DST_ALPHA;
58 case PIPE_BLENDFACTOR_CONST_COLOR:
59 return V3D_BLEND_FACTOR_CONST_COLOR;
60 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
61 return V3D_BLEND_FACTOR_INV_CONST_COLOR;
62 case PIPE_BLENDFACTOR_CONST_ALPHA:
63 return V3D_BLEND_FACTOR_CONST_ALPHA;
64 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
65 return V3D_BLEND_FACTOR_INV_CONST_ALPHA;
66 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
67 return V3D_BLEND_FACTOR_SRC_ALPHA_SATURATE;
68 default:
69 unreachable("Bad blend factor");
70 }
71 }
72
73 static inline uint16_t
74 swizzled_border_color(struct pipe_sampler_state *sampler,
75 struct vc5_sampler_view *sview,
76 int chan)
77 {
78 const struct util_format_description *desc =
79 util_format_description(sview->base.format);
80 uint8_t swiz = chan;
81
82 /* If we're doing swizzling in the sampler, then only rearrange the
83 * border color for the mismatch between the VC5 texture format and
84 * the PIPE_FORMAT, since GL_ARB_texture_swizzle will be handled by
85 * the sampler's swizzle.
86 *
87 * For swizzling in the shader, we don't do any pre-swizzling of the
88 * border color.
89 */
90 if (vc5_get_tex_return_size(sview->base.format) != 32)
91 swiz = desc->swizzle[swiz];
92
93 switch (swiz) {
94 case PIPE_SWIZZLE_0:
95 return util_float_to_half(0.0);
96 case PIPE_SWIZZLE_1:
97 return util_float_to_half(1.0);
98 default:
99 return util_float_to_half(sampler->border_color.f[swiz]);
100 }
101 }
102
103 static void
104 emit_one_texture(struct vc5_context *vc5, struct vc5_texture_stateobj *stage_tex,
105 int i)
106 {
107 struct vc5_job *job = vc5->job;
108 struct pipe_sampler_state *psampler = stage_tex->samplers[i];
109 struct vc5_sampler_state *sampler = vc5_sampler_state(psampler);
110 struct pipe_sampler_view *psview = stage_tex->textures[i];
111 struct vc5_sampler_view *sview = vc5_sampler_view(psview);
112 struct pipe_resource *prsc = psview->texture;
113 struct vc5_resource *rsc = vc5_resource(prsc);
114
115 stage_tex->texture_state[i].offset =
116 vc5_cl_ensure_space(&job->indirect,
117 cl_packet_length(TEXTURE_SHADER_STATE),
118 32);
119 vc5_bo_set_reference(&stage_tex->texture_state[i].bo,
120 job->indirect.bo);
121
122 struct V3D33_TEXTURE_SHADER_STATE unpacked = {
123 /* XXX */
124 .border_color_red = swizzled_border_color(psampler, sview, 0),
125 .border_color_green = swizzled_border_color(psampler, sview, 1),
126 .border_color_blue = swizzled_border_color(psampler, sview, 2),
127 .border_color_alpha = swizzled_border_color(psampler, sview, 3),
128
129 /* XXX: Disable min/maxlod for txf */
130 .max_level_of_detail = MIN2(MIN2(psampler->max_lod,
131 VC5_MAX_MIP_LEVELS),
132 psview->u.tex.last_level),
133
134 .texture_base_pointer = cl_address(rsc->bo,
135 rsc->slices[0].offset),
136 };
137
138 int min_img_filter = psampler->min_img_filter;
139 int min_mip_filter = psampler->min_mip_filter;
140 int mag_img_filter = psampler->mag_img_filter;
141
142 if (vc5_get_tex_return_size(psview->format) == 32) {
143 min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
144 mag_img_filter = PIPE_TEX_FILTER_NEAREST;
145 mag_img_filter = PIPE_TEX_FILTER_NEAREST;
146 }
147
148 bool min_nearest = (min_img_filter == PIPE_TEX_FILTER_NEAREST);
149 switch (min_mip_filter) {
150 case PIPE_TEX_MIPFILTER_NONE:
151 unpacked.minification_filter = 0 + min_nearest;
152 break;
153 case PIPE_TEX_MIPFILTER_NEAREST:
154 unpacked.minification_filter = 2 + !min_nearest;
155 break;
156 case PIPE_TEX_MIPFILTER_LINEAR:
157 unpacked.minification_filter = 4 + !min_nearest;
158 break;
159 }
160 unpacked.magnification_filter = (mag_img_filter ==
161 PIPE_TEX_FILTER_NEAREST);
162
163 uint8_t packed[cl_packet_length(TEXTURE_SHADER_STATE)];
164 cl_packet_pack(TEXTURE_SHADER_STATE)(&job->indirect, packed, &unpacked);
165
166 for (int i = 0; i < ARRAY_SIZE(packed); i++)
167 packed[i] |= sview->texture_shader_state[i] | sampler->texture_shader_state[i];
168
169 cl_emit_prepacked(&job->indirect, &packed);
170 }
171
172 static void
173 emit_textures(struct vc5_context *vc5, struct vc5_texture_stateobj *stage_tex)
174 {
175 for (int i = 0; i < stage_tex->num_textures; i++)
176 emit_one_texture(vc5, stage_tex, i);
177 }
178
179 void
180 vc5_emit_state(struct pipe_context *pctx)
181 {
182 struct vc5_context *vc5 = vc5_context(pctx);
183 struct vc5_job *job = vc5->job;
184
185 if (vc5->dirty & (VC5_DIRTY_SCISSOR | VC5_DIRTY_VIEWPORT |
186 VC5_DIRTY_RASTERIZER)) {
187 float *vpscale = vc5->viewport.scale;
188 float *vptranslate = vc5->viewport.translate;
189 float vp_minx = -fabsf(vpscale[0]) + vptranslate[0];
190 float vp_maxx = fabsf(vpscale[0]) + vptranslate[0];
191 float vp_miny = -fabsf(vpscale[1]) + vptranslate[1];
192 float vp_maxy = fabsf(vpscale[1]) + vptranslate[1];
193
194 /* Clip to the scissor if it's enabled, but still clip to the
195 * drawable regardless since that controls where the binner
196 * tries to put things.
197 *
198 * Additionally, always clip the rendering to the viewport,
199 * since the hardware does guardband clipping, meaning
200 * primitives would rasterize outside of the view volume.
201 */
202 uint32_t minx, miny, maxx, maxy;
203 if (!vc5->rasterizer->base.scissor) {
204 minx = MAX2(vp_minx, 0);
205 miny = MAX2(vp_miny, 0);
206 maxx = MIN2(vp_maxx, job->draw_width);
207 maxy = MIN2(vp_maxy, job->draw_height);
208 } else {
209 minx = MAX2(vp_minx, vc5->scissor.minx);
210 miny = MAX2(vp_miny, vc5->scissor.miny);
211 maxx = MIN2(vp_maxx, vc5->scissor.maxx);
212 maxy = MIN2(vp_maxy, vc5->scissor.maxy);
213 }
214
215 cl_emit(&job->bcl, CLIP_WINDOW, clip) {
216 clip.clip_window_left_pixel_coordinate = minx;
217 clip.clip_window_bottom_pixel_coordinate = miny;
218 clip.clip_window_height_in_pixels = maxy - miny;
219 clip.clip_window_width_in_pixels = maxx - minx;
220 clip.clip_window_height_in_pixels = maxy - miny;
221 }
222
223 job->draw_min_x = MIN2(job->draw_min_x, minx);
224 job->draw_min_y = MIN2(job->draw_min_y, miny);
225 job->draw_max_x = MAX2(job->draw_max_x, maxx);
226 job->draw_max_y = MAX2(job->draw_max_y, maxy);
227 }
228
229 if (vc5->dirty & (VC5_DIRTY_RASTERIZER |
230 VC5_DIRTY_ZSA |
231 VC5_DIRTY_BLEND |
232 VC5_DIRTY_COMPILED_FS)) {
233 cl_emit(&job->bcl, CONFIGURATION_BITS, config) {
234 config.enable_forward_facing_primitive =
235 !(vc5->rasterizer->base.cull_face &
236 PIPE_FACE_FRONT);
237 config.enable_reverse_facing_primitive =
238 !(vc5->rasterizer->base.cull_face &
239 PIPE_FACE_BACK);
240 /* This seems backwards, but it's what gets the
241 * clipflat test to pass.
242 */
243 config.clockwise_primitives =
244 vc5->rasterizer->base.front_ccw;
245
246 config.enable_depth_offset =
247 vc5->rasterizer->base.offset_tri;
248
249 config.rasterizer_oversample_mode =
250 vc5->rasterizer->base.multisample;
251
252 config.direct3d_provoking_vertex =
253 vc5->rasterizer->base.flatshade_first;
254
255 config.blend_enable = vc5->blend->rt[0].blend_enable;
256
257 config.early_z_updates_enable = true;
258 if (vc5->zsa->base.depth.enabled) {
259 config.z_updates_enable =
260 vc5->zsa->base.depth.writemask;
261 config.early_z_enable =
262 vc5->zsa->early_z_enable;
263 config.depth_test_function =
264 vc5->zsa->base.depth.func;
265 } else {
266 config.depth_test_function = PIPE_FUNC_ALWAYS;
267 }
268 }
269
270 }
271
272 if (vc5->dirty & VC5_DIRTY_RASTERIZER) {
273 cl_emit(&job->bcl, DEPTH_OFFSET, depth) {
274 depth.depth_offset_factor =
275 vc5->rasterizer->offset_factor;
276 depth.depth_offset_units =
277 vc5->rasterizer->offset_units;
278 }
279
280 cl_emit(&job->bcl, POINT_SIZE, point_size) {
281 point_size.point_size = vc5->rasterizer->point_size;
282 }
283
284 cl_emit(&job->bcl, LINE_WIDTH, line_width) {
285 line_width.line_width = vc5->rasterizer->base.line_width;
286 }
287 }
288
289 if (vc5->dirty & VC5_DIRTY_VIEWPORT) {
290 cl_emit(&job->bcl, CLIPPER_XY_SCALING, clip) {
291 clip.viewport_half_width_in_1_256th_of_pixel =
292 vc5->viewport.scale[0] * 256.0f;
293 clip.viewport_half_height_in_1_256th_of_pixel =
294 vc5->viewport.scale[1] * 256.0f;
295 }
296
297 cl_emit(&job->bcl, CLIPPER_Z_SCALE_AND_OFFSET, clip) {
298 clip.viewport_z_offset_zc_to_zs =
299 vc5->viewport.translate[2];
300 clip.viewport_z_scale_zc_to_zs =
301 vc5->viewport.scale[2];
302 }
303 if (0 /* XXX */) {
304 cl_emit(&job->bcl, CLIPPER_Z_MIN_MAX_CLIPPING_PLANES, clip) {
305 clip.minimum_zw = (vc5->viewport.translate[2] -
306 vc5->viewport.scale[2]);
307 clip.maximum_zw = (vc5->viewport.translate[2] +
308 vc5->viewport.scale[2]);
309 }
310 }
311
312 cl_emit(&job->bcl, VIEWPORT_OFFSET, vp) {
313 vp.viewport_centre_x_coordinate =
314 vc5->viewport.translate[0];
315 vp.viewport_centre_y_coordinate =
316 vc5->viewport.translate[1];
317 }
318 }
319
320 if (vc5->dirty & VC5_DIRTY_BLEND) {
321 struct pipe_blend_state *blend = vc5->blend;
322
323 cl_emit(&job->bcl, BLEND_CONFIG, config) {
324 struct pipe_rt_blend_state *rtblend = &blend->rt[0];
325
326 config.colour_blend_mode = rtblend->rgb_func;
327 config.colour_blend_dst_factor =
328 vc5_factor(rtblend->rgb_dst_factor);
329 config.colour_blend_src_factor =
330 vc5_factor(rtblend->rgb_src_factor);
331
332 config.alpha_blend_mode = rtblend->alpha_func;
333 config.alpha_blend_dst_factor =
334 vc5_factor(rtblend->alpha_dst_factor);
335 config.alpha_blend_src_factor =
336 vc5_factor(rtblend->alpha_src_factor);
337 }
338
339 cl_emit(&job->bcl, COLOUR_WRITE_MASKS, mask) {
340 if (blend->independent_blend_enable) {
341 mask.render_target_0_per_colour_component_write_masks =
342 (~blend->rt[0].colormask) & 0xf;
343 mask.render_target_1_per_colour_component_write_masks =
344 (~blend->rt[1].colormask) & 0xf;
345 mask.render_target_2_per_colour_component_write_masks =
346 (~blend->rt[2].colormask) & 0xf;
347 mask.render_target_3_per_colour_component_write_masks =
348 (~blend->rt[3].colormask) & 0xf;
349 } else {
350 uint8_t colormask = (~blend->rt[0].colormask) & 0xf;
351 mask.render_target_0_per_colour_component_write_masks = colormask;
352 mask.render_target_1_per_colour_component_write_masks = colormask;
353 mask.render_target_2_per_colour_component_write_masks = colormask;
354 mask.render_target_3_per_colour_component_write_masks = colormask;
355 }
356 }
357 }
358
359 if (vc5->dirty & VC5_DIRTY_BLEND_COLOR) {
360 cl_emit(&job->bcl, BLEND_CONSTANT_COLOUR, colour) {
361 /* XXX: format-dependent swizzling */
362 colour.red_f16 = vc5->blend_color.hf[2];
363 colour.green_f16 = vc5->blend_color.hf[1];
364 colour.blue_f16 = vc5->blend_color.hf[0];
365 colour.alpha_f16 = vc5->blend_color.hf[3];
366 }
367 }
368
369 if (vc5->dirty & (VC5_DIRTY_ZSA | VC5_DIRTY_STENCIL_REF)) {
370 struct pipe_stencil_state *front = &vc5->zsa->base.stencil[0];
371 struct pipe_stencil_state *back = &vc5->zsa->base.stencil[1];
372
373 cl_emit(&job->bcl, STENCIL_CONFIG, config) {
374 config.front_config = true;
375 config.back_config = !back->enabled;
376
377 config.stencil_write_mask = front->writemask;
378 config.stencil_test_mask = front->valuemask;
379
380 config.stencil_test_function = front->func;
381 config.stencil_pass_op = front->zpass_op;
382 config.depth_test_fail_op = front->zfail_op;
383 config.stencil_test_fail_op = front->fail_op;
384
385 config.stencil_ref_value = vc5->stencil_ref.ref_value[0];
386 }
387
388 if (back->enabled) {
389 cl_emit(&job->bcl, STENCIL_CONFIG, config) {
390 config.front_config = false;
391 config.back_config = true;
392
393 config.stencil_write_mask = back->writemask;
394 config.stencil_test_mask = back->valuemask;
395
396 config.stencil_test_function = back->func;
397 config.stencil_pass_op = back->zpass_op;
398 config.depth_test_fail_op = back->zfail_op;
399 config.stencil_test_fail_op = back->fail_op;
400
401 config.stencil_ref_value =
402 vc5->stencil_ref.ref_value[1];
403 }
404 }
405 }
406
407 if (vc5->dirty & VC5_DIRTY_FRAGTEX)
408 emit_textures(vc5, &vc5->fragtex);
409
410 if (vc5->dirty & VC5_DIRTY_VERTTEX)
411 emit_textures(vc5, &vc5->verttex);
412
413 if (vc5->dirty & VC5_DIRTY_FLAT_SHADE_FLAGS) {
414 /* XXX: Need to handle more than 24 entries. */
415 cl_emit(&job->bcl, FLAT_SHADE_FLAGS, flags) {
416 flags.varying_offset_v0 = 0;
417
418 flags.flat_shade_flags_for_varyings_v024 =
419 vc5->prog.fs->prog_data.fs->flat_shade_flags[0] & 0xfffff;
420
421 if (vc5->rasterizer->base.flatshade) {
422 flags.flat_shade_flags_for_varyings_v024 |=
423 vc5->prog.fs->prog_data.fs->shade_model_flags[0] & 0xfffff;
424 }
425 }
426 }
427
428 if (vc5->dirty & VC5_DIRTY_STREAMOUT) {
429 struct vc5_streamout_stateobj *so = &vc5->streamout;
430
431 if (so->num_targets) {
432 cl_emit(&job->bcl, TRANSFORM_FEEDBACK_ENABLE, tfe) {
433 tfe.number_of_32_bit_output_buffer_address_following =
434 so->num_targets;
435 tfe.number_of_16_bit_output_data_specs_following =
436 vc5->prog.bind_vs->num_tf_specs;
437 };
438
439 for (int i = 0; i < vc5->prog.bind_vs->num_tf_specs; i++) {
440 cl_emit_prepacked(&job->bcl,
441 &vc5->prog.bind_vs->tf_specs[i]);
442 }
443
444 for (int i = 0; i < so->num_targets; i++) {
445 const struct pipe_stream_output_target *target =
446 so->targets[i];
447 struct vc5_resource *rsc =
448 vc5_resource(target->buffer);
449
450 cl_emit(&job->bcl, TRANSFORM_FEEDBACK_OUTPUT_ADDRESS, output) {
451 output.address =
452 cl_address(rsc->bo,
453 target->buffer_offset);
454 };
455
456 vc5_job_add_write_resource(vc5->job,
457 target->buffer);
458 /* XXX: buffer_size? */
459 }
460 } else {
461 /* XXX? */
462 }
463 }
464 }