vc4: Include stdio/stdlib in headers so I don't have to include it per file.
[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 "pipe/p_state.h"
26 #include "util/u_inlines.h"
27 #include "util/u_math.h"
28 #include "util/u_memory.h"
29 #include "util/u_helpers.h"
30
31 #include "vc4_context.h"
32
33 static void *
34 vc4_generic_cso_state_create(const void *src, uint32_t size)
35 {
36 void *dst = calloc(1, size);
37 if (!dst)
38 return NULL;
39 memcpy(dst, src, size);
40 return dst;
41 }
42
43 static void
44 vc4_generic_cso_state_delete(struct pipe_context *pctx, void *hwcso)
45 {
46 free(hwcso);
47 }
48
49 static void
50 vc4_set_blend_color(struct pipe_context *pctx,
51 const struct pipe_blend_color *blend_color)
52 {
53 struct vc4_context *vc4 = vc4_context(pctx);
54 vc4->blend_color = *blend_color;
55 vc4->dirty |= VC4_DIRTY_BLEND_COLOR;
56 }
57
58 static void
59 vc4_set_stencil_ref(struct pipe_context *pctx,
60 const struct pipe_stencil_ref *stencil_ref)
61 {
62 struct vc4_context *vc4 = vc4_context(pctx);
63 vc4->stencil_ref =* stencil_ref;
64 vc4->dirty |= VC4_DIRTY_STENCIL_REF;
65 }
66
67 static void
68 vc4_set_clip_state(struct pipe_context *pctx,
69 const struct pipe_clip_state *clip)
70 {
71 fprintf(stderr, "clip todo\n");
72 }
73
74 static void
75 vc4_set_sample_mask(struct pipe_context *pctx, unsigned sample_mask)
76 {
77 struct vc4_context *vc4 = vc4_context(pctx);
78 vc4->sample_mask = (uint16_t)sample_mask;
79 vc4->dirty |= VC4_DIRTY_SAMPLE_MASK;
80 }
81
82 static void *
83 vc4_create_rasterizer_state(struct pipe_context *pctx,
84 const struct pipe_rasterizer_state *cso)
85 {
86 struct vc4_rasterizer_state *so;
87
88 so = CALLOC_STRUCT(vc4_rasterizer_state);
89 if (!so)
90 return NULL;
91
92 so->base = *cso;
93
94 if (!(cso->cull_face & PIPE_FACE_FRONT))
95 so->config_bits[0] |= VC4_CONFIG_BITS_ENABLE_PRIM_FRONT;
96 if (!(cso->cull_face & PIPE_FACE_BACK))
97 so->config_bits[0] |= VC4_CONFIG_BITS_ENABLE_PRIM_BACK;
98
99 /* XXX: per_vertex */
100 so->point_size = cso->point_size;
101
102 if (cso->front_ccw)
103 so->config_bits[0] |= VC4_CONFIG_BITS_CW_PRIMITIVES;
104
105 if (cso->offset_tri)
106 so->config_bits[0] |= VC4_CONFIG_BITS_ENABLE_DEPTH_OFFSET;
107
108 return so;
109 }
110
111 /* Blend state is baked into shaders. */
112 static void *
113 vc4_create_blend_state(struct pipe_context *pctx,
114 const struct pipe_blend_state *cso)
115 {
116 return vc4_generic_cso_state_create(cso, sizeof(*cso));
117 }
118
119 static void *
120 vc4_create_depth_stencil_alpha_state(struct pipe_context *pctx,
121 const struct pipe_depth_stencil_alpha_state *cso)
122 {
123 struct vc4_depth_stencil_alpha_state *so;
124
125 so = CALLOC_STRUCT(vc4_depth_stencil_alpha_state);
126 if (!so)
127 return NULL;
128
129 so->base = *cso;
130
131 if (cso->depth.enabled) {
132 if (cso->depth.writemask) {
133 so->config_bits[1] |= VC4_CONFIG_BITS_Z_UPDATE;
134 }
135 so->config_bits[1] |= (cso->depth.func <<
136 VC4_CONFIG_BITS_DEPTH_FUNC_SHIFT);
137 } else {
138 so->config_bits[1] |= (PIPE_FUNC_ALWAYS <<
139 VC4_CONFIG_BITS_DEPTH_FUNC_SHIFT);
140 }
141
142 return so;
143 }
144
145 static void
146 vc4_set_polygon_stipple(struct pipe_context *pctx,
147 const struct pipe_poly_stipple *stipple)
148 {
149 struct vc4_context *vc4 = vc4_context(pctx);
150 vc4->stipple = *stipple;
151 vc4->dirty |= VC4_DIRTY_STIPPLE;
152 }
153
154 static void
155 vc4_set_scissor_states(struct pipe_context *pctx,
156 unsigned start_slot,
157 unsigned num_scissors,
158 const struct pipe_scissor_state *scissor)
159 {
160 struct vc4_context *vc4 = vc4_context(pctx);
161
162 vc4->scissor = *scissor;
163 vc4->dirty |= VC4_DIRTY_SCISSOR;
164 }
165
166 static void
167 vc4_set_viewport_states(struct pipe_context *pctx,
168 unsigned start_slot,
169 unsigned num_viewports,
170 const struct pipe_viewport_state *viewport)
171 {
172 struct vc4_context *vc4 = vc4_context(pctx);
173 vc4->viewport = *viewport;
174 vc4->dirty |= VC4_DIRTY_VIEWPORT;
175 }
176
177 static void
178 vc4_set_vertex_buffers(struct pipe_context *pctx,
179 unsigned start_slot, unsigned count,
180 const struct pipe_vertex_buffer *vb)
181 {
182 struct vc4_context *vc4 = vc4_context(pctx);
183 struct vc4_vertexbuf_stateobj *so = &vc4->vertexbuf;
184
185 util_set_vertex_buffers_mask(so->vb, &so->enabled_mask, vb,
186 start_slot, count);
187 so->count = util_last_bit(so->enabled_mask);
188
189 vc4->dirty |= VC4_DIRTY_VTXBUF;
190 }
191
192 static void
193 vc4_set_index_buffer(struct pipe_context *pctx,
194 const struct pipe_index_buffer *ib)
195 {
196 struct vc4_context *vc4 = vc4_context(pctx);
197
198 if (ib) {
199 pipe_resource_reference(&vc4->indexbuf.buffer, ib->buffer);
200 vc4->indexbuf.index_size = ib->index_size;
201 vc4->indexbuf.offset = ib->offset;
202 vc4->indexbuf.user_buffer = ib->user_buffer;
203 } else {
204 pipe_resource_reference(&vc4->indexbuf.buffer, NULL);
205 }
206
207 vc4->dirty |= VC4_DIRTY_INDEXBUF;
208 }
209
210 static void
211 vc4_blend_state_bind(struct pipe_context *pctx, void *hwcso)
212 {
213 struct vc4_context *vc4 = vc4_context(pctx);
214 vc4->blend = hwcso;
215 vc4->dirty |= VC4_DIRTY_BLEND;
216 }
217
218 static void
219 vc4_rasterizer_state_bind(struct pipe_context *pctx, void *hwcso)
220 {
221 struct vc4_context *vc4 = vc4_context(pctx);
222 vc4->rasterizer = hwcso;
223 vc4->dirty |= VC4_DIRTY_RASTERIZER;
224 }
225
226 static void
227 vc4_zsa_state_bind(struct pipe_context *pctx, void *hwcso)
228 {
229 struct vc4_context *vc4 = vc4_context(pctx);
230 vc4->zsa = hwcso;
231 vc4->dirty |= VC4_DIRTY_ZSA;
232 }
233
234 static void *
235 vc4_vertex_state_create(struct pipe_context *pctx, unsigned num_elements,
236 const struct pipe_vertex_element *elements)
237 {
238 struct vc4_vertex_stateobj *so = CALLOC_STRUCT(vc4_vertex_stateobj);
239
240 if (!so)
241 return NULL;
242
243 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
244 so->num_elements = num_elements;
245
246 return so;
247 }
248
249 static void
250 vc4_vertex_state_bind(struct pipe_context *pctx, void *hwcso)
251 {
252 struct vc4_context *vc4 = vc4_context(pctx);
253 vc4->vtx = hwcso;
254 vc4->dirty |= VC4_DIRTY_VTXSTATE;
255 }
256
257 static void
258 vc4_set_constant_buffer(struct pipe_context *pctx, uint shader, uint index,
259 struct pipe_constant_buffer *cb)
260 {
261 struct vc4_context *vc4 = vc4_context(pctx);
262 struct vc4_constbuf_stateobj *so = &vc4->constbuf[shader];
263
264 assert(index == 0);
265
266 /* Note that the state tracker can unbind constant buffers by
267 * passing NULL here.
268 */
269 if (unlikely(!cb)) {
270 so->enabled_mask &= ~(1 << index);
271 so->dirty_mask &= ~(1 << index);
272 return;
273 }
274
275 assert(!cb->buffer);
276 so->cb[index].buffer_offset = cb->buffer_offset;
277 so->cb[index].buffer_size = cb->buffer_size;
278 so->cb[index].user_buffer = cb->user_buffer;
279
280 so->enabled_mask |= 1 << index;
281 so->dirty_mask |= 1 << index;
282 vc4->dirty |= VC4_DIRTY_CONSTBUF;
283 }
284
285 static void
286 vc4_set_framebuffer_state(struct pipe_context *pctx,
287 const struct pipe_framebuffer_state *framebuffer)
288 {
289 struct vc4_context *vc4 = vc4_context(pctx);
290 struct pipe_framebuffer_state *cso = &vc4->framebuffer;
291 unsigned i;
292
293 vc4_flush(pctx);
294
295 for (i = 0; i < framebuffer->nr_cbufs; i++)
296 pipe_surface_reference(&cso->cbufs[i], framebuffer->cbufs[i]);
297 for (; i < vc4->framebuffer.nr_cbufs; i++)
298 pipe_surface_reference(&cso->cbufs[i], NULL);
299
300 cso->nr_cbufs = framebuffer->nr_cbufs;
301
302 cso->width = framebuffer->width;
303 cso->height = framebuffer->height;
304
305 pipe_surface_reference(&cso->zsbuf, framebuffer->zsbuf);
306
307 vc4->dirty |= VC4_DIRTY_FRAMEBUFFER;
308 }
309
310 static struct vc4_texture_stateobj *
311 vc4_get_stage_tex(struct vc4_context *vc4, unsigned shader)
312 {
313 vc4->dirty |= VC4_DIRTY_TEXSTATE;
314
315 switch (shader) {
316 case PIPE_SHADER_FRAGMENT:
317 vc4->dirty |= VC4_DIRTY_FRAGTEX;
318 return &vc4->fragtex;
319 break;
320 case PIPE_SHADER_VERTEX:
321 vc4->dirty |= VC4_DIRTY_VERTTEX;
322 return &vc4->verttex;
323 break;
324 default:
325 fprintf(stderr, "Unknown shader target %d\n", shader);
326 abort();
327 }
328 }
329
330 static void *
331 vc4_create_sampler_state(struct pipe_context *pctx,
332 const struct pipe_sampler_state *cso)
333 {
334 return vc4_generic_cso_state_create(cso, sizeof(*cso));
335 }
336
337 static void
338 vc4_sampler_states_bind(struct pipe_context *pctx,
339 unsigned shader, unsigned start,
340 unsigned nr, void **hwcso)
341 {
342 struct vc4_context *vc4 = vc4_context(pctx);
343 struct vc4_texture_stateobj *stage_tex = vc4_get_stage_tex(vc4, shader);
344
345 assert(start == 0);
346 unsigned i;
347 unsigned new_nr = 0;
348
349 for (i = 0; i < nr; i++) {
350 if (hwcso[i])
351 new_nr = i + 1;
352 stage_tex->samplers[i] = hwcso[i];
353 stage_tex->dirty_samplers |= (1 << i);
354 }
355
356 for (; i < stage_tex->num_samplers; i++) {
357 stage_tex->samplers[i] = NULL;
358 stage_tex->dirty_samplers |= (1 << i);
359 }
360
361 stage_tex->num_samplers = new_nr;
362 }
363
364 static struct pipe_sampler_view *
365 vc4_create_sampler_view(struct pipe_context *pctx, struct pipe_resource *prsc,
366 const struct pipe_sampler_view *cso)
367 {
368 struct pipe_sampler_view *so = malloc(sizeof(*so));
369
370 if (!so)
371 return NULL;
372
373 *so = *cso;
374 pipe_reference(NULL, &prsc->reference);
375 so->texture = prsc;
376 so->reference.count = 1;
377 so->context = pctx;
378
379 return so;
380 }
381
382 static void
383 vc4_sampler_view_destroy(struct pipe_context *pctx,
384 struct pipe_sampler_view *view)
385 {
386 pipe_resource_reference(&view->texture, NULL);
387 free(view);
388 }
389
390 static void
391 vc4_set_sampler_views(struct pipe_context *pctx, unsigned shader,
392 unsigned start, unsigned nr,
393 struct pipe_sampler_view **views)
394 {
395 struct vc4_context *vc4 = vc4_context(pctx);
396 struct vc4_texture_stateobj *stage_tex = vc4_get_stage_tex(vc4, shader);
397 unsigned i;
398 unsigned new_nr = 0;
399
400 assert(start == 0);
401
402 vc4->dirty |= VC4_DIRTY_TEXSTATE;
403
404 for (i = 0; i < nr; i++) {
405 if (views[i])
406 new_nr = i + 1;
407 pipe_sampler_view_reference(&stage_tex->textures[i], views[i]);
408 stage_tex->dirty_samplers |= (1 << i);
409 }
410
411 for (; i < stage_tex->num_textures; i++) {
412 pipe_sampler_view_reference(&stage_tex->textures[i], NULL);
413 stage_tex->dirty_samplers |= (1 << i);
414 }
415
416 stage_tex->num_textures = new_nr;
417 }
418
419 void
420 vc4_state_init(struct pipe_context *pctx)
421 {
422 pctx->set_blend_color = vc4_set_blend_color;
423 pctx->set_stencil_ref = vc4_set_stencil_ref;
424 pctx->set_clip_state = vc4_set_clip_state;
425 pctx->set_sample_mask = vc4_set_sample_mask;
426 pctx->set_constant_buffer = vc4_set_constant_buffer;
427 pctx->set_framebuffer_state = vc4_set_framebuffer_state;
428 pctx->set_polygon_stipple = vc4_set_polygon_stipple;
429 pctx->set_scissor_states = vc4_set_scissor_states;
430 pctx->set_viewport_states = vc4_set_viewport_states;
431
432 pctx->set_vertex_buffers = vc4_set_vertex_buffers;
433 pctx->set_index_buffer = vc4_set_index_buffer;
434
435 pctx->create_blend_state = vc4_create_blend_state;
436 pctx->bind_blend_state = vc4_blend_state_bind;
437 pctx->delete_blend_state = vc4_generic_cso_state_delete;
438
439 pctx->create_rasterizer_state = vc4_create_rasterizer_state;
440 pctx->bind_rasterizer_state = vc4_rasterizer_state_bind;
441 pctx->delete_rasterizer_state = vc4_generic_cso_state_delete;
442
443 pctx->create_depth_stencil_alpha_state = vc4_create_depth_stencil_alpha_state;
444 pctx->bind_depth_stencil_alpha_state = vc4_zsa_state_bind;
445 pctx->delete_depth_stencil_alpha_state = vc4_generic_cso_state_delete;
446
447 pctx->create_vertex_elements_state = vc4_vertex_state_create;
448 pctx->delete_vertex_elements_state = vc4_generic_cso_state_delete;
449 pctx->bind_vertex_elements_state = vc4_vertex_state_bind;
450
451 pctx->create_sampler_state = vc4_create_sampler_state;
452 pctx->delete_sampler_state = vc4_generic_cso_state_delete;
453 pctx->bind_sampler_states = vc4_sampler_states_bind;
454
455 pctx->create_sampler_view = vc4_create_sampler_view;
456 pctx->sampler_view_destroy = vc4_sampler_view_destroy;
457 pctx->set_sampler_views = vc4_set_sampler_views;
458 }