1b8e80941Smrg/* 2b8e80941Smrg * Copyright (c) 2012-2015 Etnaviv Project 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sub license, 8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the 9b8e80941Smrg * Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the 12b8e80941Smrg * next paragraph) shall be included in all copies or substantial portions 13b8e80941Smrg * of the Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21b8e80941Smrg * DEALINGS IN THE SOFTWARE. 22b8e80941Smrg * 23b8e80941Smrg * Authors: 24b8e80941Smrg * Wladimir J. van der Laan <laanwj@gmail.com> 25b8e80941Smrg * Christian Gmeiner <christian.gmeiner@gmail.com> 26b8e80941Smrg */ 27b8e80941Smrg 28b8e80941Smrg#include "etnaviv_state.h" 29b8e80941Smrg 30b8e80941Smrg#include "hw/common.xml.h" 31b8e80941Smrg 32b8e80941Smrg#include "etnaviv_blend.h" 33b8e80941Smrg#include "etnaviv_clear_blit.h" 34b8e80941Smrg#include "etnaviv_context.h" 35b8e80941Smrg#include "etnaviv_format.h" 36b8e80941Smrg#include "etnaviv_shader.h" 37b8e80941Smrg#include "etnaviv_surface.h" 38b8e80941Smrg#include "etnaviv_translate.h" 39b8e80941Smrg#include "etnaviv_util.h" 40b8e80941Smrg#include "util/u_framebuffer.h" 41b8e80941Smrg#include "util/u_helpers.h" 42b8e80941Smrg#include "util/u_inlines.h" 43b8e80941Smrg#include "util/u_math.h" 44b8e80941Smrg#include "util/u_memory.h" 45b8e80941Smrg 46b8e80941Smrgstatic void 47b8e80941Smrgetna_set_stencil_ref(struct pipe_context *pctx, const struct pipe_stencil_ref *sr) 48b8e80941Smrg{ 49b8e80941Smrg struct etna_context *ctx = etna_context(pctx); 50b8e80941Smrg struct compiled_stencil_ref *cs = &ctx->stencil_ref; 51b8e80941Smrg 52b8e80941Smrg ctx->stencil_ref_s = *sr; 53b8e80941Smrg 54b8e80941Smrg cs->PE_STENCIL_CONFIG = VIVS_PE_STENCIL_CONFIG_REF_FRONT(sr->ref_value[0]); 55b8e80941Smrg /* rest of bits weaved in from depth_stencil_alpha */ 56b8e80941Smrg cs->PE_STENCIL_CONFIG_EXT = 57b8e80941Smrg VIVS_PE_STENCIL_CONFIG_EXT_REF_BACK(sr->ref_value[0]); 58b8e80941Smrg ctx->dirty |= ETNA_DIRTY_STENCIL_REF; 59b8e80941Smrg} 60b8e80941Smrg 61b8e80941Smrgstatic void 62b8e80941Smrgetna_set_clip_state(struct pipe_context *pctx, const struct pipe_clip_state *pcs) 63b8e80941Smrg{ 64b8e80941Smrg /* NOOP */ 65b8e80941Smrg} 66b8e80941Smrg 67b8e80941Smrgstatic void 68b8e80941Smrgetna_set_sample_mask(struct pipe_context *pctx, unsigned sample_mask) 69b8e80941Smrg{ 70b8e80941Smrg struct etna_context *ctx = etna_context(pctx); 71b8e80941Smrg 72b8e80941Smrg ctx->sample_mask = sample_mask; 73b8e80941Smrg ctx->dirty |= ETNA_DIRTY_SAMPLE_MASK; 74b8e80941Smrg} 75b8e80941Smrg 76b8e80941Smrgstatic void 77b8e80941Smrgetna_set_constant_buffer(struct pipe_context *pctx, 78b8e80941Smrg enum pipe_shader_type shader, uint index, 79b8e80941Smrg const struct pipe_constant_buffer *cb) 80b8e80941Smrg{ 81b8e80941Smrg struct etna_context *ctx = etna_context(pctx); 82b8e80941Smrg 83b8e80941Smrg if (unlikely(index > 0)) { 84b8e80941Smrg DBG("Unhandled buffer index %i", index); 85b8e80941Smrg return; 86b8e80941Smrg } 87b8e80941Smrg 88b8e80941Smrg 89b8e80941Smrg util_copy_constant_buffer(&ctx->constant_buffer[shader], cb); 90b8e80941Smrg 91b8e80941Smrg /* Note that the state tracker can unbind constant buffers by 92b8e80941Smrg * passing NULL here. */ 93b8e80941Smrg if (unlikely(!cb || (!cb->buffer && !cb->user_buffer))) 94b8e80941Smrg return; 95b8e80941Smrg 96b8e80941Smrg /* there is no support for ARB_uniform_buffer_object */ 97b8e80941Smrg assert(cb->buffer == NULL && cb->user_buffer != NULL); 98b8e80941Smrg 99b8e80941Smrg ctx->dirty |= ETNA_DIRTY_CONSTBUF; 100b8e80941Smrg} 101b8e80941Smrg 102b8e80941Smrgstatic void 103b8e80941Smrgetna_update_render_resource(struct pipe_context *pctx, struct pipe_resource *pres) 104b8e80941Smrg{ 105b8e80941Smrg struct etna_resource *res = etna_resource(pres); 106b8e80941Smrg 107b8e80941Smrg if (res->texture && etna_resource_older(res, etna_resource(res->texture))) { 108b8e80941Smrg /* The render buffer is older than the texture buffer. Copy it over. */ 109b8e80941Smrg etna_copy_resource(pctx, pres, res->texture, 0, pres->last_level); 110b8e80941Smrg res->seqno = etna_resource(res->texture)->seqno; 111b8e80941Smrg } 112b8e80941Smrg} 113b8e80941Smrg 114b8e80941Smrgstatic void 115b8e80941Smrgetna_set_framebuffer_state(struct pipe_context *pctx, 116b8e80941Smrg const struct pipe_framebuffer_state *sv) 117b8e80941Smrg{ 118b8e80941Smrg struct etna_context *ctx = etna_context(pctx); 119b8e80941Smrg struct compiled_framebuffer_state *cs = &ctx->framebuffer; 120b8e80941Smrg int nr_samples_color = -1; 121b8e80941Smrg int nr_samples_depth = -1; 122b8e80941Smrg 123b8e80941Smrg /* Set up TS as well. Warning: this state is used by both the RS and PE */ 124b8e80941Smrg uint32_t ts_mem_config = 0; 125b8e80941Smrg 126b8e80941Smrg if (sv->nr_cbufs > 0) { /* at least one color buffer? */ 127b8e80941Smrg struct etna_surface *cbuf = etna_surface(sv->cbufs[0]); 128b8e80941Smrg struct etna_resource *res = etna_resource(cbuf->base.texture); 129b8e80941Smrg bool color_supertiled = (res->layout & ETNA_LAYOUT_BIT_SUPER) != 0; 130b8e80941Smrg 131b8e80941Smrg assert(res->layout & ETNA_LAYOUT_BIT_TILE); /* Cannot render to linear surfaces */ 132b8e80941Smrg etna_update_render_resource(pctx, cbuf->base.texture); 133b8e80941Smrg 134b8e80941Smrg cs->PE_COLOR_FORMAT = 135b8e80941Smrg VIVS_PE_COLOR_FORMAT_FORMAT(translate_rs_format(cbuf->base.format)) | 136b8e80941Smrg VIVS_PE_COLOR_FORMAT_COMPONENTS__MASK | 137b8e80941Smrg VIVS_PE_COLOR_FORMAT_OVERWRITE | 138b8e80941Smrg COND(color_supertiled, VIVS_PE_COLOR_FORMAT_SUPER_TILED) | 139b8e80941Smrg COND(color_supertiled && ctx->specs.halti >= 5, VIVS_PE_COLOR_FORMAT_SUPER_TILED_NEW); 140b8e80941Smrg /* VIVS_PE_COLOR_FORMAT_COMPONENTS() and 141b8e80941Smrg * VIVS_PE_COLOR_FORMAT_OVERWRITE comes from blend_state 142b8e80941Smrg * but only if we set the bits above. */ 143b8e80941Smrg /* merged with depth_stencil_alpha */ 144b8e80941Smrg if ((cbuf->surf.offset & 63) || 145b8e80941Smrg (((cbuf->surf.stride * 4) & 63) && cbuf->surf.height > 4)) { 146b8e80941Smrg /* XXX Must make temporary surface here. 147b8e80941Smrg * Need the same mechanism on gc2000 when we want to do mipmap 148b8e80941Smrg * generation by 149b8e80941Smrg * rendering to levels > 1 due to multitiled / tiled conversion. */ 150b8e80941Smrg BUG("Alignment error, trying to render to offset %08x with tile " 151b8e80941Smrg "stride %i", 152b8e80941Smrg cbuf->surf.offset, cbuf->surf.stride * 4); 153b8e80941Smrg } 154b8e80941Smrg 155b8e80941Smrg if (ctx->specs.pixel_pipes == 1) { 156b8e80941Smrg cs->PE_COLOR_ADDR = cbuf->reloc[0]; 157b8e80941Smrg cs->PE_COLOR_ADDR.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE; 158b8e80941Smrg } else { 159b8e80941Smrg /* Rendered textures must always be multi-tiled, or single-buffer mode must be supported */ 160b8e80941Smrg assert((res->layout & ETNA_LAYOUT_BIT_MULTI) || ctx->specs.single_buffer); 161b8e80941Smrg for (int i = 0; i < ctx->specs.pixel_pipes; i++) { 162b8e80941Smrg cs->PE_PIPE_COLOR_ADDR[i] = cbuf->reloc[i]; 163b8e80941Smrg cs->PE_PIPE_COLOR_ADDR[i].flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE; 164b8e80941Smrg } 165b8e80941Smrg } 166b8e80941Smrg cs->PE_COLOR_STRIDE = cbuf->surf.stride; 167b8e80941Smrg 168b8e80941Smrg if (cbuf->surf.ts_size) { 169b8e80941Smrg cs->TS_COLOR_CLEAR_VALUE = cbuf->level->clear_value; 170b8e80941Smrg 171b8e80941Smrg cs->TS_COLOR_STATUS_BASE = cbuf->ts_reloc; 172b8e80941Smrg cs->TS_COLOR_STATUS_BASE.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE; 173b8e80941Smrg 174b8e80941Smrg cs->TS_COLOR_SURFACE_BASE = cbuf->reloc[0]; 175b8e80941Smrg cs->TS_COLOR_SURFACE_BASE.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE; 176b8e80941Smrg } 177b8e80941Smrg 178b8e80941Smrg /* MSAA */ 179b8e80941Smrg if (cbuf->base.texture->nr_samples > 1) 180b8e80941Smrg ts_mem_config |= 181b8e80941Smrg VIVS_TS_MEM_CONFIG_COLOR_COMPRESSION | translate_msaa_format(cbuf->base.format); 182b8e80941Smrg 183b8e80941Smrg nr_samples_color = cbuf->base.texture->nr_samples; 184b8e80941Smrg } else { 185b8e80941Smrg /* Clearing VIVS_PE_COLOR_FORMAT_COMPONENTS__MASK and 186b8e80941Smrg * VIVS_PE_COLOR_FORMAT_OVERWRITE prevents us from overwriting the 187b8e80941Smrg * color target */ 188b8e80941Smrg cs->PE_COLOR_FORMAT = VIVS_PE_COLOR_FORMAT_OVERWRITE; 189b8e80941Smrg cs->PE_COLOR_STRIDE = 0; 190b8e80941Smrg cs->TS_COLOR_STATUS_BASE.bo = NULL; 191b8e80941Smrg cs->TS_COLOR_SURFACE_BASE.bo = NULL; 192b8e80941Smrg 193b8e80941Smrg cs->PE_COLOR_ADDR = ctx->dummy_rt_reloc; 194b8e80941Smrg for (int i = 0; i < ctx->specs.pixel_pipes; i++) 195b8e80941Smrg cs->PE_PIPE_COLOR_ADDR[i] = ctx->dummy_rt_reloc; 196b8e80941Smrg } 197b8e80941Smrg 198b8e80941Smrg if (sv->zsbuf != NULL) { 199b8e80941Smrg struct etna_surface *zsbuf = etna_surface(sv->zsbuf); 200b8e80941Smrg struct etna_resource *res = etna_resource(zsbuf->base.texture); 201b8e80941Smrg 202b8e80941Smrg etna_update_render_resource(pctx, zsbuf->base.texture); 203b8e80941Smrg 204b8e80941Smrg assert(res->layout &ETNA_LAYOUT_BIT_TILE); /* Cannot render to linear surfaces */ 205b8e80941Smrg 206b8e80941Smrg uint32_t depth_format = translate_depth_format(zsbuf->base.format); 207b8e80941Smrg unsigned depth_bits = 208b8e80941Smrg depth_format == VIVS_PE_DEPTH_CONFIG_DEPTH_FORMAT_D16 ? 16 : 24; 209b8e80941Smrg bool depth_supertiled = (res->layout & ETNA_LAYOUT_BIT_SUPER) != 0; 210b8e80941Smrg 211b8e80941Smrg cs->PE_DEPTH_CONFIG = 212b8e80941Smrg depth_format | 213b8e80941Smrg COND(depth_supertiled, VIVS_PE_DEPTH_CONFIG_SUPER_TILED) | 214b8e80941Smrg VIVS_PE_DEPTH_CONFIG_DEPTH_MODE_Z | 215b8e80941Smrg COND(ctx->specs.halti >= 5, VIVS_PE_DEPTH_CONFIG_DISABLE_ZS) /* Needs to be enabled on GC7000, otherwise depth writes hang w/ TS - apparently it does something else now */ 216b8e80941Smrg ; 217b8e80941Smrg /* VIVS_PE_DEPTH_CONFIG_ONLY_DEPTH */ 218b8e80941Smrg /* merged with depth_stencil_alpha */ 219b8e80941Smrg 220b8e80941Smrg if (ctx->specs.pixel_pipes == 1) { 221b8e80941Smrg cs->PE_DEPTH_ADDR = zsbuf->reloc[0]; 222b8e80941Smrg cs->PE_DEPTH_ADDR.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE; 223b8e80941Smrg } else { 224b8e80941Smrg for (int i = 0; i < ctx->specs.pixel_pipes; i++) { 225b8e80941Smrg cs->PE_PIPE_DEPTH_ADDR[i] = zsbuf->reloc[i]; 226b8e80941Smrg cs->PE_PIPE_DEPTH_ADDR[i].flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE; 227b8e80941Smrg } 228b8e80941Smrg } 229b8e80941Smrg 230b8e80941Smrg cs->PE_DEPTH_STRIDE = zsbuf->surf.stride; 231b8e80941Smrg cs->PE_HDEPTH_CONTROL = VIVS_PE_HDEPTH_CONTROL_FORMAT_DISABLED; 232b8e80941Smrg cs->PE_DEPTH_NORMALIZE = fui(exp2f(depth_bits) - 1.0f); 233b8e80941Smrg 234b8e80941Smrg if (zsbuf->surf.ts_size) { 235b8e80941Smrg cs->TS_DEPTH_CLEAR_VALUE = zsbuf->level->clear_value; 236b8e80941Smrg 237b8e80941Smrg cs->TS_DEPTH_STATUS_BASE = zsbuf->ts_reloc; 238b8e80941Smrg cs->TS_DEPTH_STATUS_BASE.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE; 239b8e80941Smrg 240b8e80941Smrg cs->TS_DEPTH_SURFACE_BASE = zsbuf->reloc[0]; 241b8e80941Smrg cs->TS_DEPTH_SURFACE_BASE.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE; 242b8e80941Smrg } 243b8e80941Smrg 244b8e80941Smrg ts_mem_config |= COND(depth_bits == 16, VIVS_TS_MEM_CONFIG_DEPTH_16BPP); 245b8e80941Smrg 246b8e80941Smrg /* MSAA */ 247b8e80941Smrg if (zsbuf->base.texture->nr_samples > 1) 248b8e80941Smrg /* XXX VIVS_TS_MEM_CONFIG_DEPTH_COMPRESSION; 249b8e80941Smrg * Disable without MSAA for now, as it causes corruption in glquake. */ 250b8e80941Smrg ts_mem_config |= VIVS_TS_MEM_CONFIG_DEPTH_COMPRESSION; 251b8e80941Smrg 252b8e80941Smrg nr_samples_depth = zsbuf->base.texture->nr_samples; 253b8e80941Smrg } else { 254b8e80941Smrg cs->PE_DEPTH_CONFIG = VIVS_PE_DEPTH_CONFIG_DEPTH_MODE_NONE; 255b8e80941Smrg cs->PE_DEPTH_ADDR.bo = NULL; 256b8e80941Smrg cs->PE_DEPTH_STRIDE = 0; 257b8e80941Smrg cs->TS_DEPTH_STATUS_BASE.bo = NULL; 258b8e80941Smrg cs->TS_DEPTH_SURFACE_BASE.bo = NULL; 259b8e80941Smrg 260b8e80941Smrg for (int i = 0; i < ETNA_MAX_PIXELPIPES; i++) 261b8e80941Smrg cs->PE_PIPE_DEPTH_ADDR[i].bo = NULL; 262b8e80941Smrg } 263b8e80941Smrg 264b8e80941Smrg /* MSAA setup */ 265b8e80941Smrg if (nr_samples_depth != -1 && nr_samples_color != -1 && 266b8e80941Smrg nr_samples_depth != nr_samples_color) { 267b8e80941Smrg BUG("Number of samples in color and depth texture must match (%i and %i respectively)", 268b8e80941Smrg nr_samples_color, nr_samples_depth); 269b8e80941Smrg } 270b8e80941Smrg 271b8e80941Smrg switch (MAX2(nr_samples_depth, nr_samples_color)) { 272b8e80941Smrg case 0: 273b8e80941Smrg case 1: /* Are 0 and 1 samples allowed? */ 274b8e80941Smrg cs->GL_MULTI_SAMPLE_CONFIG = 275b8e80941Smrg VIVS_GL_MULTI_SAMPLE_CONFIG_MSAA_SAMPLES_NONE; 276b8e80941Smrg cs->msaa_mode = false; 277b8e80941Smrg break; 278b8e80941Smrg case 2: 279b8e80941Smrg cs->GL_MULTI_SAMPLE_CONFIG = VIVS_GL_MULTI_SAMPLE_CONFIG_MSAA_SAMPLES_2X; 280b8e80941Smrg cs->msaa_mode = true; /* Add input to PS */ 281b8e80941Smrg cs->RA_MULTISAMPLE_UNK00E04 = 0x0; 282b8e80941Smrg cs->RA_MULTISAMPLE_UNK00E10[0] = 0x0000aa22; 283b8e80941Smrg cs->RA_CENTROID_TABLE[0] = 0x66aa2288; 284b8e80941Smrg cs->RA_CENTROID_TABLE[1] = 0x88558800; 285b8e80941Smrg cs->RA_CENTROID_TABLE[2] = 0x88881100; 286b8e80941Smrg cs->RA_CENTROID_TABLE[3] = 0x33888800; 287b8e80941Smrg break; 288b8e80941Smrg case 4: 289b8e80941Smrg cs->GL_MULTI_SAMPLE_CONFIG = VIVS_GL_MULTI_SAMPLE_CONFIG_MSAA_SAMPLES_4X; 290b8e80941Smrg cs->msaa_mode = true; /* Add input to PS */ 291b8e80941Smrg cs->RA_MULTISAMPLE_UNK00E04 = 0x0; 292b8e80941Smrg cs->RA_MULTISAMPLE_UNK00E10[0] = 0xeaa26e26; 293b8e80941Smrg cs->RA_MULTISAMPLE_UNK00E10[1] = 0xe6ae622a; 294b8e80941Smrg cs->RA_MULTISAMPLE_UNK00E10[2] = 0xaaa22a22; 295b8e80941Smrg cs->RA_CENTROID_TABLE[0] = 0x4a6e2688; 296b8e80941Smrg cs->RA_CENTROID_TABLE[1] = 0x888888a2; 297b8e80941Smrg cs->RA_CENTROID_TABLE[2] = 0x888888ea; 298b8e80941Smrg cs->RA_CENTROID_TABLE[3] = 0x888888c6; 299b8e80941Smrg cs->RA_CENTROID_TABLE[4] = 0x46622a88; 300b8e80941Smrg cs->RA_CENTROID_TABLE[5] = 0x888888ae; 301b8e80941Smrg cs->RA_CENTROID_TABLE[6] = 0x888888e6; 302b8e80941Smrg cs->RA_CENTROID_TABLE[7] = 0x888888ca; 303b8e80941Smrg cs->RA_CENTROID_TABLE[8] = 0x262a2288; 304b8e80941Smrg cs->RA_CENTROID_TABLE[9] = 0x886688a2; 305b8e80941Smrg cs->RA_CENTROID_TABLE[10] = 0x888866aa; 306b8e80941Smrg cs->RA_CENTROID_TABLE[11] = 0x668888a6; 307b8e80941Smrg break; 308b8e80941Smrg } 309b8e80941Smrg 310b8e80941Smrg /* Scissor setup */ 311b8e80941Smrg cs->SE_SCISSOR_LEFT = 0; /* affected by rasterizer and scissor state as well */ 312b8e80941Smrg cs->SE_SCISSOR_TOP = 0; 313b8e80941Smrg cs->SE_SCISSOR_RIGHT = (sv->width << 16) + ETNA_SE_SCISSOR_MARGIN_RIGHT; 314b8e80941Smrg cs->SE_SCISSOR_BOTTOM = (sv->height << 16) + ETNA_SE_SCISSOR_MARGIN_BOTTOM; 315b8e80941Smrg cs->SE_CLIP_RIGHT = (sv->width << 16) + ETNA_SE_CLIP_MARGIN_RIGHT; 316b8e80941Smrg cs->SE_CLIP_BOTTOM = (sv->height << 16) + ETNA_SE_CLIP_MARGIN_BOTTOM; 317b8e80941Smrg 318b8e80941Smrg cs->TS_MEM_CONFIG = ts_mem_config; 319b8e80941Smrg 320b8e80941Smrg /* Single buffer setup. There is only one switch for this, not a separate 321b8e80941Smrg * one per color buffer / depth buffer. To keep the logic simple always use 322b8e80941Smrg * single buffer when this feature is available. 323b8e80941Smrg */ 324b8e80941Smrg cs->PE_LOGIC_OP = VIVS_PE_LOGIC_OP_SINGLE_BUFFER(ctx->specs.single_buffer ? 3 : 0); 325b8e80941Smrg 326b8e80941Smrg /* keep copy of original structure */ 327b8e80941Smrg util_copy_framebuffer_state(&ctx->framebuffer_s, sv); 328b8e80941Smrg ctx->dirty |= ETNA_DIRTY_FRAMEBUFFER | ETNA_DIRTY_DERIVE_TS; 329b8e80941Smrg} 330b8e80941Smrg 331b8e80941Smrgstatic void 332b8e80941Smrgetna_set_polygon_stipple(struct pipe_context *pctx, 333b8e80941Smrg const struct pipe_poly_stipple *stipple) 334b8e80941Smrg{ 335b8e80941Smrg /* NOP */ 336b8e80941Smrg} 337b8e80941Smrg 338b8e80941Smrgstatic void 339b8e80941Smrgetna_set_scissor_states(struct pipe_context *pctx, unsigned start_slot, 340b8e80941Smrg unsigned num_scissors, const struct pipe_scissor_state *ss) 341b8e80941Smrg{ 342b8e80941Smrg struct etna_context *ctx = etna_context(pctx); 343b8e80941Smrg struct compiled_scissor_state *cs = &ctx->scissor; 344b8e80941Smrg assert(ss->minx <= ss->maxx); 345b8e80941Smrg assert(ss->miny <= ss->maxy); 346b8e80941Smrg 347b8e80941Smrg /* note that this state is only used when rasterizer_state->scissor is on */ 348b8e80941Smrg ctx->scissor_s = *ss; 349b8e80941Smrg cs->SE_SCISSOR_LEFT = (ss->minx << 16); 350b8e80941Smrg cs->SE_SCISSOR_TOP = (ss->miny << 16); 351b8e80941Smrg cs->SE_SCISSOR_RIGHT = (ss->maxx << 16) + ETNA_SE_SCISSOR_MARGIN_RIGHT; 352b8e80941Smrg cs->SE_SCISSOR_BOTTOM = (ss->maxy << 16) + ETNA_SE_SCISSOR_MARGIN_BOTTOM; 353b8e80941Smrg cs->SE_CLIP_RIGHT = (ss->maxx << 16) + ETNA_SE_CLIP_MARGIN_RIGHT; 354b8e80941Smrg cs->SE_CLIP_BOTTOM = (ss->maxy << 16) + ETNA_SE_CLIP_MARGIN_BOTTOM; 355b8e80941Smrg 356b8e80941Smrg ctx->dirty |= ETNA_DIRTY_SCISSOR; 357b8e80941Smrg} 358b8e80941Smrg 359b8e80941Smrgstatic void 360b8e80941Smrgetna_set_viewport_states(struct pipe_context *pctx, unsigned start_slot, 361b8e80941Smrg unsigned num_scissors, const struct pipe_viewport_state *vs) 362b8e80941Smrg{ 363b8e80941Smrg struct etna_context *ctx = etna_context(pctx); 364b8e80941Smrg struct compiled_viewport_state *cs = &ctx->viewport; 365b8e80941Smrg 366b8e80941Smrg ctx->viewport_s = *vs; 367b8e80941Smrg /** 368b8e80941Smrg * For Vivante GPU, viewport z transformation is 0..1 to 0..1 instead of 369b8e80941Smrg * -1..1 to 0..1. 370b8e80941Smrg * scaling and translation to 0..1 already happened, so remove that 371b8e80941Smrg * 372b8e80941Smrg * z' = (z * 2 - 1) * scale + translate 373b8e80941Smrg * = z * (2 * scale) + (translate - scale) 374b8e80941Smrg * 375b8e80941Smrg * scale' = 2 * scale 376b8e80941Smrg * translate' = translate - scale 377b8e80941Smrg */ 378b8e80941Smrg 379b8e80941Smrg /* must be fixp as v4 state deltas assume it is */ 380b8e80941Smrg cs->PA_VIEWPORT_SCALE_X = etna_f32_to_fixp16(vs->scale[0]); 381b8e80941Smrg cs->PA_VIEWPORT_SCALE_Y = etna_f32_to_fixp16(vs->scale[1]); 382b8e80941Smrg cs->PA_VIEWPORT_SCALE_Z = fui(vs->scale[2] * 2.0f); 383b8e80941Smrg cs->PA_VIEWPORT_OFFSET_X = etna_f32_to_fixp16(vs->translate[0]); 384b8e80941Smrg cs->PA_VIEWPORT_OFFSET_Y = etna_f32_to_fixp16(vs->translate[1]); 385b8e80941Smrg cs->PA_VIEWPORT_OFFSET_Z = fui(vs->translate[2] - vs->scale[2]); 386b8e80941Smrg 387b8e80941Smrg /* Compute scissor rectangle (fixp) from viewport. 388b8e80941Smrg * Make sure left is always < right and top always < bottom. 389b8e80941Smrg */ 390b8e80941Smrg cs->SE_SCISSOR_LEFT = etna_f32_to_fixp16(MAX2(vs->translate[0] - fabsf(vs->scale[0]), 0.0f)); 391b8e80941Smrg cs->SE_SCISSOR_TOP = etna_f32_to_fixp16(MAX2(vs->translate[1] - fabsf(vs->scale[1]), 0.0f)); 392b8e80941Smrg uint32_t right_fixp = etna_f32_to_fixp16(MAX2(vs->translate[0] + fabsf(vs->scale[0]), 0.0f)); 393b8e80941Smrg uint32_t bottom_fixp = etna_f32_to_fixp16(MAX2(vs->translate[1] + fabsf(vs->scale[1]), 0.0f)); 394b8e80941Smrg cs->SE_SCISSOR_RIGHT = right_fixp + ETNA_SE_SCISSOR_MARGIN_RIGHT; 395b8e80941Smrg cs->SE_SCISSOR_BOTTOM = bottom_fixp + ETNA_SE_SCISSOR_MARGIN_BOTTOM; 396b8e80941Smrg cs->SE_CLIP_RIGHT = right_fixp + ETNA_SE_CLIP_MARGIN_RIGHT; 397b8e80941Smrg cs->SE_CLIP_BOTTOM = bottom_fixp + ETNA_SE_CLIP_MARGIN_BOTTOM; 398b8e80941Smrg 399b8e80941Smrg cs->PE_DEPTH_NEAR = fui(0.0); /* not affected if depth mode is Z (as in GL) */ 400b8e80941Smrg cs->PE_DEPTH_FAR = fui(1.0); 401b8e80941Smrg ctx->dirty |= ETNA_DIRTY_VIEWPORT; 402b8e80941Smrg} 403b8e80941Smrg 404b8e80941Smrgstatic void 405b8e80941Smrgetna_set_vertex_buffers(struct pipe_context *pctx, unsigned start_slot, 406b8e80941Smrg unsigned num_buffers, const struct pipe_vertex_buffer *vb) 407b8e80941Smrg{ 408b8e80941Smrg struct etna_context *ctx = etna_context(pctx); 409b8e80941Smrg struct etna_vertexbuf_state *so = &ctx->vertex_buffer; 410b8e80941Smrg 411b8e80941Smrg util_set_vertex_buffers_mask(so->vb, &so->enabled_mask, vb, start_slot, num_buffers); 412b8e80941Smrg so->count = util_last_bit(so->enabled_mask); 413b8e80941Smrg 414b8e80941Smrg for (unsigned idx = start_slot; idx < start_slot + num_buffers; ++idx) { 415b8e80941Smrg struct compiled_set_vertex_buffer *cs = &so->cvb[idx]; 416b8e80941Smrg struct pipe_vertex_buffer *vbi = &so->vb[idx]; 417b8e80941Smrg 418b8e80941Smrg assert(!vbi->is_user_buffer); /* XXX support user_buffer using 419b8e80941Smrg etna_usermem_map */ 420b8e80941Smrg 421b8e80941Smrg if (vbi->buffer.resource) { /* GPU buffer */ 422b8e80941Smrg cs->FE_VERTEX_STREAM_BASE_ADDR.bo = etna_resource(vbi->buffer.resource)->bo; 423b8e80941Smrg cs->FE_VERTEX_STREAM_BASE_ADDR.offset = vbi->buffer_offset; 424b8e80941Smrg cs->FE_VERTEX_STREAM_BASE_ADDR.flags = ETNA_RELOC_READ; 425b8e80941Smrg cs->FE_VERTEX_STREAM_CONTROL = 426b8e80941Smrg FE_VERTEX_STREAM_CONTROL_VERTEX_STRIDE(vbi->stride); 427b8e80941Smrg } else { 428b8e80941Smrg cs->FE_VERTEX_STREAM_BASE_ADDR.bo = NULL; 429b8e80941Smrg cs->FE_VERTEX_STREAM_CONTROL = 0; 430b8e80941Smrg } 431b8e80941Smrg } 432b8e80941Smrg 433b8e80941Smrg ctx->dirty |= ETNA_DIRTY_VERTEX_BUFFERS; 434b8e80941Smrg} 435b8e80941Smrg 436b8e80941Smrgstatic void 437b8e80941Smrgetna_blend_state_bind(struct pipe_context *pctx, void *bs) 438b8e80941Smrg{ 439b8e80941Smrg struct etna_context *ctx = etna_context(pctx); 440b8e80941Smrg 441b8e80941Smrg ctx->blend = bs; 442b8e80941Smrg ctx->dirty |= ETNA_DIRTY_BLEND; 443b8e80941Smrg} 444b8e80941Smrg 445b8e80941Smrgstatic void 446b8e80941Smrgetna_blend_state_delete(struct pipe_context *pctx, void *bs) 447b8e80941Smrg{ 448b8e80941Smrg FREE(bs); 449b8e80941Smrg} 450b8e80941Smrg 451b8e80941Smrgstatic void 452b8e80941Smrgetna_rasterizer_state_bind(struct pipe_context *pctx, void *rs) 453b8e80941Smrg{ 454b8e80941Smrg struct etna_context *ctx = etna_context(pctx); 455b8e80941Smrg 456b8e80941Smrg ctx->rasterizer = rs; 457b8e80941Smrg ctx->dirty |= ETNA_DIRTY_RASTERIZER; 458b8e80941Smrg} 459b8e80941Smrg 460b8e80941Smrgstatic void 461b8e80941Smrgetna_rasterizer_state_delete(struct pipe_context *pctx, void *rs) 462b8e80941Smrg{ 463b8e80941Smrg FREE(rs); 464b8e80941Smrg} 465b8e80941Smrg 466b8e80941Smrgstatic void 467b8e80941Smrgetna_zsa_state_bind(struct pipe_context *pctx, void *zs) 468b8e80941Smrg{ 469b8e80941Smrg struct etna_context *ctx = etna_context(pctx); 470b8e80941Smrg 471b8e80941Smrg ctx->zsa = zs; 472b8e80941Smrg ctx->dirty |= ETNA_DIRTY_ZSA; 473b8e80941Smrg} 474b8e80941Smrg 475b8e80941Smrgstatic void 476b8e80941Smrgetna_zsa_state_delete(struct pipe_context *pctx, void *zs) 477b8e80941Smrg{ 478b8e80941Smrg FREE(zs); 479b8e80941Smrg} 480b8e80941Smrg 481b8e80941Smrg/** Create vertex element states, which define a layout for fetching 482b8e80941Smrg * vertices for rendering. 483b8e80941Smrg */ 484b8e80941Smrgstatic void * 485b8e80941Smrgetna_vertex_elements_state_create(struct pipe_context *pctx, 486b8e80941Smrg unsigned num_elements, const struct pipe_vertex_element *elements) 487b8e80941Smrg{ 488b8e80941Smrg struct etna_context *ctx = etna_context(pctx); 489b8e80941Smrg struct compiled_vertex_elements_state *cs = CALLOC_STRUCT(compiled_vertex_elements_state); 490b8e80941Smrg 491b8e80941Smrg if (!cs) 492b8e80941Smrg return NULL; 493b8e80941Smrg 494b8e80941Smrg if (num_elements > ctx->specs.vertex_max_elements) { 495b8e80941Smrg BUG("number of elements (%u) exceeds chip maximum (%u)", num_elements, 496b8e80941Smrg ctx->specs.vertex_max_elements); 497b8e80941Smrg return NULL; 498b8e80941Smrg } 499b8e80941Smrg 500b8e80941Smrg /* XXX could minimize number of consecutive stretches here by sorting, and 501b8e80941Smrg * permuting the inputs in shader or does Mesa do this already? */ 502b8e80941Smrg 503b8e80941Smrg /* Check that vertex element binding is compatible with hardware; thus 504b8e80941Smrg * elements[idx].vertex_buffer_index are < stream_count. If not, the binding 505b8e80941Smrg * uses more streams than is supported, and u_vbuf should have done some 506b8e80941Smrg * reorganization for compatibility. */ 507b8e80941Smrg 508b8e80941Smrg /* TODO: does mesa this for us? */ 509b8e80941Smrg bool incompatible = false; 510b8e80941Smrg for (unsigned idx = 0; idx < num_elements; ++idx) { 511b8e80941Smrg if (elements[idx].vertex_buffer_index >= ctx->specs.stream_count || elements[idx].instance_divisor > 0) 512b8e80941Smrg incompatible = true; 513b8e80941Smrg } 514b8e80941Smrg 515b8e80941Smrg cs->num_elements = num_elements; 516b8e80941Smrg if (incompatible || num_elements == 0) { 517b8e80941Smrg DBG("Error: zero vertex elements, or more vertex buffers used than supported"); 518b8e80941Smrg FREE(cs); 519b8e80941Smrg return NULL; 520b8e80941Smrg } 521b8e80941Smrg 522b8e80941Smrg unsigned start_offset = 0; /* start of current consecutive stretch */ 523b8e80941Smrg bool nonconsecutive = true; /* previous value of nonconsecutive */ 524b8e80941Smrg 525b8e80941Smrg for (unsigned idx = 0; idx < num_elements; ++idx) { 526b8e80941Smrg unsigned element_size = util_format_get_blocksize(elements[idx].src_format); 527b8e80941Smrg unsigned end_offset = elements[idx].src_offset + element_size; 528b8e80941Smrg uint32_t format_type, normalize; 529b8e80941Smrg 530b8e80941Smrg if (nonconsecutive) 531b8e80941Smrg start_offset = elements[idx].src_offset; 532b8e80941Smrg 533b8e80941Smrg /* maximum vertex size is 256 bytes */ 534b8e80941Smrg assert(element_size != 0 && end_offset <= 256); 535b8e80941Smrg 536b8e80941Smrg /* check whether next element is consecutive to this one */ 537b8e80941Smrg nonconsecutive = (idx == (num_elements - 1)) || 538b8e80941Smrg elements[idx + 1].vertex_buffer_index != elements[idx].vertex_buffer_index || 539b8e80941Smrg end_offset != elements[idx + 1].src_offset; 540b8e80941Smrg 541b8e80941Smrg format_type = translate_vertex_format_type(elements[idx].src_format); 542b8e80941Smrg normalize = translate_vertex_format_normalize(elements[idx].src_format); 543b8e80941Smrg 544b8e80941Smrg assert(format_type != ETNA_NO_MATCH); 545b8e80941Smrg assert(normalize != ETNA_NO_MATCH); 546b8e80941Smrg 547b8e80941Smrg if (ctx->specs.halti < 5) { 548b8e80941Smrg cs->FE_VERTEX_ELEMENT_CONFIG[idx] = 549b8e80941Smrg COND(nonconsecutive, VIVS_FE_VERTEX_ELEMENT_CONFIG_NONCONSECUTIVE) | 550b8e80941Smrg format_type | 551b8e80941Smrg VIVS_FE_VERTEX_ELEMENT_CONFIG_NUM(util_format_get_nr_components(elements[idx].src_format)) | 552b8e80941Smrg normalize | VIVS_FE_VERTEX_ELEMENT_CONFIG_ENDIAN(ENDIAN_MODE_NO_SWAP) | 553b8e80941Smrg VIVS_FE_VERTEX_ELEMENT_CONFIG_STREAM(elements[idx].vertex_buffer_index) | 554b8e80941Smrg VIVS_FE_VERTEX_ELEMENT_CONFIG_START(elements[idx].src_offset) | 555b8e80941Smrg VIVS_FE_VERTEX_ELEMENT_CONFIG_END(end_offset - start_offset); 556b8e80941Smrg } else { /* HALTI5 spread vertex attrib config over two registers */ 557b8e80941Smrg cs->NFE_GENERIC_ATTRIB_CONFIG0[idx] = 558b8e80941Smrg format_type | 559b8e80941Smrg VIVS_NFE_GENERIC_ATTRIB_CONFIG0_NUM(util_format_get_nr_components(elements[idx].src_format)) | 560b8e80941Smrg normalize | VIVS_NFE_GENERIC_ATTRIB_CONFIG0_ENDIAN(ENDIAN_MODE_NO_SWAP) | 561b8e80941Smrg VIVS_NFE_GENERIC_ATTRIB_CONFIG0_STREAM(elements[idx].vertex_buffer_index) | 562b8e80941Smrg VIVS_NFE_GENERIC_ATTRIB_CONFIG0_START(elements[idx].src_offset); 563b8e80941Smrg cs->NFE_GENERIC_ATTRIB_CONFIG1[idx] = 564b8e80941Smrg COND(nonconsecutive, VIVS_NFE_GENERIC_ATTRIB_CONFIG1_NONCONSECUTIVE) | 565b8e80941Smrg VIVS_NFE_GENERIC_ATTRIB_CONFIG1_END(end_offset - start_offset); 566b8e80941Smrg } 567b8e80941Smrg cs->NFE_GENERIC_ATTRIB_SCALE[idx] = 0x3f800000; /* 1 for integer, 1.0 for float */ 568b8e80941Smrg } 569b8e80941Smrg 570b8e80941Smrg return cs; 571b8e80941Smrg} 572b8e80941Smrg 573b8e80941Smrgstatic void 574b8e80941Smrgetna_vertex_elements_state_delete(struct pipe_context *pctx, void *ve) 575b8e80941Smrg{ 576b8e80941Smrg FREE(ve); 577b8e80941Smrg} 578b8e80941Smrg 579b8e80941Smrgstatic void 580b8e80941Smrgetna_vertex_elements_state_bind(struct pipe_context *pctx, void *ve) 581b8e80941Smrg{ 582b8e80941Smrg struct etna_context *ctx = etna_context(pctx); 583b8e80941Smrg 584b8e80941Smrg ctx->vertex_elements = ve; 585b8e80941Smrg ctx->dirty |= ETNA_DIRTY_VERTEX_ELEMENTS; 586b8e80941Smrg} 587b8e80941Smrg 588b8e80941Smrgstatic bool 589b8e80941Smrgetna_update_ts_config(struct etna_context *ctx) 590b8e80941Smrg{ 591b8e80941Smrg uint32_t new_ts_config = ctx->framebuffer.TS_MEM_CONFIG; 592b8e80941Smrg 593b8e80941Smrg if (ctx->framebuffer_s.nr_cbufs > 0) { 594b8e80941Smrg struct etna_surface *c_surf = etna_surface(ctx->framebuffer_s.cbufs[0]); 595b8e80941Smrg 596b8e80941Smrg if(c_surf->level->ts_size && c_surf->level->ts_valid) { 597b8e80941Smrg new_ts_config |= VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR; 598b8e80941Smrg } else { 599b8e80941Smrg new_ts_config &= ~VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR; 600b8e80941Smrg } 601b8e80941Smrg } 602b8e80941Smrg 603b8e80941Smrg if (ctx->framebuffer_s.zsbuf) { 604b8e80941Smrg struct etna_surface *zs_surf = etna_surface(ctx->framebuffer_s.zsbuf); 605b8e80941Smrg 606b8e80941Smrg if(zs_surf->level->ts_size && zs_surf->level->ts_valid) { 607b8e80941Smrg new_ts_config |= VIVS_TS_MEM_CONFIG_DEPTH_FAST_CLEAR; 608b8e80941Smrg } else { 609b8e80941Smrg new_ts_config &= ~VIVS_TS_MEM_CONFIG_DEPTH_FAST_CLEAR; 610b8e80941Smrg } 611b8e80941Smrg } 612b8e80941Smrg 613b8e80941Smrg if (new_ts_config != ctx->framebuffer.TS_MEM_CONFIG || 614b8e80941Smrg (ctx->dirty & ETNA_DIRTY_FRAMEBUFFER)) { 615b8e80941Smrg ctx->framebuffer.TS_MEM_CONFIG = new_ts_config; 616b8e80941Smrg ctx->dirty |= ETNA_DIRTY_TS; 617b8e80941Smrg } 618b8e80941Smrg 619b8e80941Smrg ctx->dirty &= ~ETNA_DIRTY_DERIVE_TS; 620b8e80941Smrg 621b8e80941Smrg return true; 622b8e80941Smrg} 623b8e80941Smrg 624b8e80941Smrgstruct etna_state_updater { 625b8e80941Smrg bool (*update)(struct etna_context *ctx); 626b8e80941Smrg uint32_t dirty; 627b8e80941Smrg}; 628b8e80941Smrg 629b8e80941Smrgstatic const struct etna_state_updater etna_state_updates[] = { 630b8e80941Smrg { 631b8e80941Smrg etna_shader_update_vertex, ETNA_DIRTY_SHADER | ETNA_DIRTY_VERTEX_ELEMENTS, 632b8e80941Smrg }, 633b8e80941Smrg { 634b8e80941Smrg etna_shader_link, ETNA_DIRTY_SHADER, 635b8e80941Smrg }, 636b8e80941Smrg { 637b8e80941Smrg etna_update_blend, ETNA_DIRTY_BLEND | ETNA_DIRTY_FRAMEBUFFER 638b8e80941Smrg }, 639b8e80941Smrg { 640b8e80941Smrg etna_update_blend_color, ETNA_DIRTY_BLEND_COLOR | ETNA_DIRTY_FRAMEBUFFER, 641b8e80941Smrg }, 642b8e80941Smrg { 643b8e80941Smrg etna_update_ts_config, ETNA_DIRTY_DERIVE_TS, 644b8e80941Smrg } 645b8e80941Smrg}; 646b8e80941Smrg 647b8e80941Smrgbool 648b8e80941Smrgetna_state_update(struct etna_context *ctx) 649b8e80941Smrg{ 650b8e80941Smrg for (unsigned int i = 0; i < ARRAY_SIZE(etna_state_updates); i++) 651b8e80941Smrg if (ctx->dirty & etna_state_updates[i].dirty) 652b8e80941Smrg if (!etna_state_updates[i].update(ctx)) 653b8e80941Smrg return false; 654b8e80941Smrg 655b8e80941Smrg return true; 656b8e80941Smrg} 657b8e80941Smrg 658b8e80941Smrgvoid 659b8e80941Smrgetna_state_init(struct pipe_context *pctx) 660b8e80941Smrg{ 661b8e80941Smrg pctx->set_blend_color = etna_set_blend_color; 662b8e80941Smrg pctx->set_stencil_ref = etna_set_stencil_ref; 663b8e80941Smrg pctx->set_clip_state = etna_set_clip_state; 664b8e80941Smrg pctx->set_sample_mask = etna_set_sample_mask; 665b8e80941Smrg pctx->set_constant_buffer = etna_set_constant_buffer; 666b8e80941Smrg pctx->set_framebuffer_state = etna_set_framebuffer_state; 667b8e80941Smrg pctx->set_polygon_stipple = etna_set_polygon_stipple; 668b8e80941Smrg pctx->set_scissor_states = etna_set_scissor_states; 669b8e80941Smrg pctx->set_viewport_states = etna_set_viewport_states; 670b8e80941Smrg 671b8e80941Smrg pctx->set_vertex_buffers = etna_set_vertex_buffers; 672b8e80941Smrg 673b8e80941Smrg pctx->bind_blend_state = etna_blend_state_bind; 674b8e80941Smrg pctx->delete_blend_state = etna_blend_state_delete; 675b8e80941Smrg 676b8e80941Smrg pctx->bind_rasterizer_state = etna_rasterizer_state_bind; 677b8e80941Smrg pctx->delete_rasterizer_state = etna_rasterizer_state_delete; 678b8e80941Smrg 679b8e80941Smrg pctx->bind_depth_stencil_alpha_state = etna_zsa_state_bind; 680b8e80941Smrg pctx->delete_depth_stencil_alpha_state = etna_zsa_state_delete; 681b8e80941Smrg 682b8e80941Smrg pctx->create_vertex_elements_state = etna_vertex_elements_state_create; 683b8e80941Smrg pctx->delete_vertex_elements_state = etna_vertex_elements_state_delete; 684b8e80941Smrg pctx->bind_vertex_elements_state = etna_vertex_elements_state_bind; 685b8e80941Smrg} 686