ab1e8be6ee34c884eee75a7713d48a5ad05cce4c
[mesa.git] / src / gallium / drivers / vc4 / vc4_state.c
1 /*
2 * Copyright © 2014 Broadcom
3 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include <stdio.h>
26
27 #include "pipe/p_state.h"
28 #include "util/u_inlines.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/u_helpers.h"
32
33 #include "vc4_context.h"
34
35 static void *
36 vc4_generic_cso_state_create(const void *src, uint32_t size)
37 {
38 void *dst = calloc(1, size);
39 if (!dst)
40 return NULL;
41 memcpy(dst, src, size);
42 return dst;
43 }
44
45 static void
46 vc4_generic_cso_state_delete(struct pipe_context *pctx, void *hwcso)
47 {
48 free(hwcso);
49 }
50
51 static void
52 vc4_set_blend_color(struct pipe_context *pctx,
53 const struct pipe_blend_color *blend_color)
54 {
55 struct vc4_context *vc4 = vc4_context(pctx);
56 vc4->blend_color = *blend_color;
57 vc4->dirty |= VC4_DIRTY_BLEND_COLOR;
58 }
59
60 static void
61 vc4_set_stencil_ref(struct pipe_context *pctx,
62 const struct pipe_stencil_ref *stencil_ref)
63 {
64 struct vc4_context *vc4 = vc4_context(pctx);
65 vc4->stencil_ref =* stencil_ref;
66 vc4->dirty |= VC4_DIRTY_STENCIL_REF;
67 }
68
69 static void
70 vc4_set_clip_state(struct pipe_context *pctx,
71 const struct pipe_clip_state *clip)
72 {
73 fprintf(stderr, "clip todo\n");
74 }
75
76 static void
77 vc4_set_sample_mask(struct pipe_context *pctx, unsigned sample_mask)
78 {
79 struct vc4_context *vc4 = vc4_context(pctx);
80 vc4->sample_mask = (uint16_t)sample_mask;
81 vc4->dirty |= VC4_DIRTY_SAMPLE_MASK;
82 }
83
84 static void *
85 vc4_create_rasterizer_state(struct pipe_context *pctx,
86 const struct pipe_rasterizer_state *cso)
87 {
88 struct vc4_rasterizer_state *so;
89
90 so = CALLOC_STRUCT(vc4_rasterizer_state);
91 if (!so)
92 return NULL;
93
94 so->base = *cso;
95
96 if (!(cso->cull_face & PIPE_FACE_FRONT))
97 so->config_bits[0] |= VC4_CONFIG_BITS_ENABLE_PRIM_FRONT;
98 if (!(cso->cull_face & PIPE_FACE_BACK))
99 so->config_bits[0] |= VC4_CONFIG_BITS_ENABLE_PRIM_BACK;
100
101 /* XXX: per_vertex */
102 so->point_size = cso->point_size;
103
104 if (!cso->front_ccw)
105 so->config_bits[0] |= VC4_CONFIG_BITS_CW_PRIMITIVES;
106
107 if (cso->offset_tri)
108 so->config_bits[0] |= VC4_CONFIG_BITS_ENABLE_DEPTH_OFFSET;
109
110 so->config_bits[2] |= VC4_CONFIG_BITS_EARLY_Z_UPDATE;
111
112 return so;
113 }
114
115 /* Blend state is baked into shaders. */
116 static void *
117 vc4_create_blend_state(struct pipe_context *pctx,
118 const struct pipe_blend_state *cso)
119 {
120 return vc4_generic_cso_state_create(cso, sizeof(*cso));
121 }
122
123 static void *
124 vc4_create_depth_stencil_alpha_state(struct pipe_context *pctx,
125 const struct pipe_depth_stencil_alpha_state *cso)
126 {
127 return vc4_generic_cso_state_create(cso, sizeof(*cso));
128 }
129
130 static void
131 vc4_set_polygon_stipple(struct pipe_context *pctx,
132 const struct pipe_poly_stipple *stipple)
133 {
134 struct vc4_context *vc4 = vc4_context(pctx);
135 vc4->stipple = *stipple;
136 vc4->dirty |= VC4_DIRTY_STIPPLE;
137 }
138
139 static void
140 vc4_set_scissor_states(struct pipe_context *pctx,
141 unsigned start_slot,
142 unsigned num_scissors,
143 const struct pipe_scissor_state *scissor)
144 {
145 struct vc4_context *vc4 = vc4_context(pctx);
146
147 vc4->scissor = *scissor;
148 vc4->dirty |= VC4_DIRTY_SCISSOR;
149 }
150
151 static void
152 vc4_set_viewport_states(struct pipe_context *pctx,
153 unsigned start_slot,
154 unsigned num_viewports,
155 const struct pipe_viewport_state *viewport)
156 {
157 struct vc4_context *vc4 = vc4_context(pctx);
158 vc4->viewport = *viewport;
159 vc4->dirty |= VC4_DIRTY_VIEWPORT;
160 }
161
162 static void
163 vc4_set_vertex_buffers(struct pipe_context *pctx,
164 unsigned start_slot, unsigned count,
165 const struct pipe_vertex_buffer *vb)
166 {
167 struct vc4_context *vc4 = vc4_context(pctx);
168 struct vc4_vertexbuf_stateobj *so = &vc4->vertexbuf;
169
170 util_set_vertex_buffers_mask(so->vb, &so->enabled_mask, vb,
171 start_slot, count);
172 so->count = util_last_bit(so->enabled_mask);
173
174 vc4->dirty |= VC4_DIRTY_VTXBUF;
175 }
176
177 static void
178 vc4_set_index_buffer(struct pipe_context *pctx,
179 const struct pipe_index_buffer *ib)
180 {
181 struct vc4_context *vc4 = vc4_context(pctx);
182
183 if (ib) {
184 pipe_resource_reference(&vc4->indexbuf.buffer, ib->buffer);
185 vc4->indexbuf.index_size = ib->index_size;
186 vc4->indexbuf.offset = ib->offset;
187 vc4->indexbuf.user_buffer = ib->user_buffer;
188 } else {
189 pipe_resource_reference(&vc4->indexbuf.buffer, NULL);
190 }
191
192 vc4->dirty |= VC4_DIRTY_INDEXBUF;
193 }
194
195 static void
196 vc4_blend_state_bind(struct pipe_context *pctx, void *hwcso)
197 {
198 struct vc4_context *vc4 = vc4_context(pctx);
199 vc4->blend = hwcso;
200 vc4->dirty |= VC4_DIRTY_BLEND;
201 }
202
203 static void
204 vc4_rasterizer_state_bind(struct pipe_context *pctx, void *hwcso)
205 {
206 struct vc4_context *vc4 = vc4_context(pctx);
207 vc4->rasterizer = hwcso;
208 vc4->dirty |= VC4_DIRTY_RASTERIZER;
209 }
210
211 static void
212 vc4_zsa_state_bind(struct pipe_context *pctx, void *hwcso)
213 {
214 struct vc4_context *vc4 = vc4_context(pctx);
215 vc4->zsa = hwcso;
216 vc4->dirty |= VC4_DIRTY_ZSA;
217 }
218
219 static void *
220 vc4_vertex_state_create(struct pipe_context *pctx, unsigned num_elements,
221 const struct pipe_vertex_element *elements)
222 {
223 struct vc4_vertex_stateobj *so = CALLOC_STRUCT(vc4_vertex_stateobj);
224
225 if (!so)
226 return NULL;
227
228 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
229 so->num_elements = num_elements;
230
231 return so;
232 }
233
234 static void
235 vc4_vertex_state_bind(struct pipe_context *pctx, void *hwcso)
236 {
237 struct vc4_context *vc4 = vc4_context(pctx);
238 vc4->vtx = hwcso;
239 vc4->dirty |= VC4_DIRTY_VTXSTATE;
240 }
241
242 static void
243 vc4_set_constant_buffer(struct pipe_context *pctx, uint shader, uint index,
244 struct pipe_constant_buffer *cb)
245 {
246 struct vc4_context *vc4 = vc4_context(pctx);
247 struct vc4_constbuf_stateobj *so = &vc4->constbuf[shader];
248
249 assert(index == 0);
250
251 /* Note that the state tracker can unbind constant buffers by
252 * passing NULL here.
253 */
254 if (unlikely(!cb)) {
255 so->enabled_mask &= ~(1 << index);
256 so->dirty_mask &= ~(1 << index);
257 return;
258 }
259
260 assert(!cb->buffer);
261 so->cb[index].buffer_offset = cb->buffer_offset;
262 so->cb[index].buffer_size = cb->buffer_size;
263 so->cb[index].user_buffer = cb->user_buffer;
264
265 so->enabled_mask |= 1 << index;
266 so->dirty_mask |= 1 << index;
267 vc4->dirty |= VC4_DIRTY_CONSTBUF;
268 }
269
270 static void
271 vc4_set_framebuffer_state(struct pipe_context *pctx,
272 const struct pipe_framebuffer_state *framebuffer)
273 {
274 struct vc4_context *vc4 = vc4_context(pctx);
275 struct pipe_framebuffer_state *cso = &vc4->framebuffer;
276 unsigned i;
277
278 vc4_flush(pctx);
279
280 for (i = 0; i < framebuffer->nr_cbufs; i++)
281 pipe_surface_reference(&cso->cbufs[i], framebuffer->cbufs[i]);
282 for (; i < vc4->framebuffer.nr_cbufs; i++)
283 pipe_surface_reference(&cso->cbufs[i], NULL);
284
285 cso->nr_cbufs = framebuffer->nr_cbufs;
286
287 cso->width = framebuffer->width;
288 cso->height = framebuffer->height;
289
290 pipe_surface_reference(&cso->zsbuf, framebuffer->zsbuf);
291
292 vc4->dirty |= VC4_DIRTY_FRAMEBUFFER;
293 }
294
295 static struct vc4_texture_stateobj *
296 vc4_get_stage_tex(struct vc4_context *vc4, unsigned shader)
297 {
298 vc4->dirty |= VC4_DIRTY_TEXSTATE;
299
300 switch (shader) {
301 case PIPE_SHADER_FRAGMENT:
302 vc4->dirty |= VC4_DIRTY_FRAGTEX;
303 return &vc4->fragtex;
304 break;
305 case PIPE_SHADER_VERTEX:
306 vc4->dirty |= VC4_DIRTY_VERTTEX;
307 return &vc4->verttex;
308 break;
309 default:
310 fprintf(stderr, "Unknown shader target %d\n", shader);
311 abort();
312 }
313 }
314
315 static void *
316 vc4_create_sampler_state(struct pipe_context *pctx,
317 const struct pipe_sampler_state *cso)
318 {
319 return vc4_generic_cso_state_create(cso, sizeof(*cso));
320 }
321
322 static void
323 vc4_sampler_states_bind(struct pipe_context *pctx,
324 unsigned shader, unsigned start,
325 unsigned nr, void **hwcso)
326 {
327 struct vc4_context *vc4 = vc4_context(pctx);
328 struct vc4_texture_stateobj *stage_tex = vc4_get_stage_tex(vc4, shader);
329
330 assert(start == 0);
331 unsigned i;
332 unsigned new_nr = 0;
333
334 for (i = 0; i < nr; i++) {
335 if (hwcso[i])
336 new_nr = i + 1;
337 stage_tex->samplers[i] = hwcso[i];
338 stage_tex->dirty_samplers |= (1 << i);
339 }
340
341 for (; i < stage_tex->num_samplers; i++) {
342 stage_tex->samplers[i] = NULL;
343 stage_tex->dirty_samplers |= (1 << i);
344 }
345
346 stage_tex->num_samplers = new_nr;
347 }
348
349 static struct pipe_sampler_view *
350 vc4_create_sampler_view(struct pipe_context *pctx, struct pipe_resource *prsc,
351 const struct pipe_sampler_view *cso)
352 {
353 struct pipe_sampler_view *so = malloc(sizeof(*so));
354
355 if (!so)
356 return NULL;
357
358 *so = *cso;
359 pipe_reference(NULL, &prsc->reference);
360 so->texture = prsc;
361 so->reference.count = 1;
362 so->context = pctx;
363
364 return so;
365 }
366
367 static void
368 vc4_sampler_view_destroy(struct pipe_context *pctx,
369 struct pipe_sampler_view *view)
370 {
371 pipe_resource_reference(&view->texture, NULL);
372 free(view);
373 }
374
375 static void
376 vc4_set_sampler_views(struct pipe_context *pctx, unsigned shader,
377 unsigned start, unsigned nr,
378 struct pipe_sampler_view **views)
379 {
380 struct vc4_context *vc4 = vc4_context(pctx);
381 struct vc4_texture_stateobj *stage_tex = vc4_get_stage_tex(vc4, shader);
382 unsigned i;
383 unsigned new_nr = 0;
384
385 assert(start == 0);
386
387 vc4->dirty |= VC4_DIRTY_TEXSTATE;
388
389 for (i = 0; i < nr; i++) {
390 if (views[i])
391 new_nr = i + 1;
392 pipe_sampler_view_reference(&stage_tex->textures[i], views[i]);
393 stage_tex->dirty_samplers |= (1 << i);
394 }
395
396 for (; i < stage_tex->num_textures; i++) {
397 pipe_sampler_view_reference(&stage_tex->textures[i], NULL);
398 stage_tex->dirty_samplers |= (1 << i);
399 }
400
401 stage_tex->num_textures = new_nr;
402 }
403
404 void
405 vc4_state_init(struct pipe_context *pctx)
406 {
407 pctx->set_blend_color = vc4_set_blend_color;
408 pctx->set_stencil_ref = vc4_set_stencil_ref;
409 pctx->set_clip_state = vc4_set_clip_state;
410 pctx->set_sample_mask = vc4_set_sample_mask;
411 pctx->set_constant_buffer = vc4_set_constant_buffer;
412 pctx->set_framebuffer_state = vc4_set_framebuffer_state;
413 pctx->set_polygon_stipple = vc4_set_polygon_stipple;
414 pctx->set_scissor_states = vc4_set_scissor_states;
415 pctx->set_viewport_states = vc4_set_viewport_states;
416
417 pctx->set_vertex_buffers = vc4_set_vertex_buffers;
418 pctx->set_index_buffer = vc4_set_index_buffer;
419
420 pctx->create_blend_state = vc4_create_blend_state;
421 pctx->bind_blend_state = vc4_blend_state_bind;
422 pctx->delete_blend_state = vc4_generic_cso_state_delete;
423
424 pctx->create_rasterizer_state = vc4_create_rasterizer_state;
425 pctx->bind_rasterizer_state = vc4_rasterizer_state_bind;
426 pctx->delete_rasterizer_state = vc4_generic_cso_state_delete;
427
428 pctx->create_depth_stencil_alpha_state = vc4_create_depth_stencil_alpha_state;
429 pctx->bind_depth_stencil_alpha_state = vc4_zsa_state_bind;
430 pctx->delete_depth_stencil_alpha_state = vc4_generic_cso_state_delete;
431
432 pctx->create_vertex_elements_state = vc4_vertex_state_create;
433 pctx->delete_vertex_elements_state = vc4_generic_cso_state_delete;
434 pctx->bind_vertex_elements_state = vc4_vertex_state_bind;
435
436 pctx->create_sampler_state = vc4_create_sampler_state;
437 pctx->delete_sampler_state = vc4_generic_cso_state_delete;
438 pctx->bind_sampler_states = vc4_sampler_states_bind;
439
440 pctx->create_sampler_view = vc4_create_sampler_view;
441 pctx->sampler_view_destroy = vc4_sampler_view_destroy;
442 pctx->set_sampler_views = vc4_set_sampler_views;
443 }