2 * Copyright (c) 2011-2013 Luc Verhaegen <libv@skynet.be>
3 * Copyright (c) 2017-2019 Lima Project
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, sub license,
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:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
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 NON-INFRINGEMENT. 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
22 * DEALINGS IN THE SOFTWARE.
26 #include "util/u_memory.h"
27 #include "util/u_inlines.h"
28 #include "util/u_helpers.h"
29 #include "util/u_debug.h"
30 #include "util/u_framebuffer.h"
32 #include "pipe/p_state.h"
34 #include "lima_screen.h"
35 #include "lima_context.h"
36 #include "lima_resource.h"
39 lima_set_framebuffer_state(struct pipe_context
*pctx
,
40 const struct pipe_framebuffer_state
*framebuffer
)
42 struct lima_context
*ctx
= lima_context(pctx
);
44 /* make sure there are always single job in this context */
45 if (lima_debug
& LIMA_DEBUG_SINGLE_JOB
)
48 struct lima_context_framebuffer
*fb
= &ctx
->framebuffer
;
50 util_copy_framebuffer_state(&fb
->base
, framebuffer
);
53 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_FRAMEBUFFER
;
57 lima_set_polygon_stipple(struct pipe_context
*pctx
,
58 const struct pipe_poly_stipple
*stipple
)
64 lima_create_depth_stencil_alpha_state(struct pipe_context
*pctx
,
65 const struct pipe_depth_stencil_alpha_state
*cso
)
67 struct lima_depth_stencil_alpha_state
*so
;
69 so
= CALLOC_STRUCT(lima_depth_stencil_alpha_state
);
79 lima_bind_depth_stencil_alpha_state(struct pipe_context
*pctx
, void *hwcso
)
81 struct lima_context
*ctx
= lima_context(pctx
);
84 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_ZSA
;
88 lima_delete_depth_stencil_alpha_state(struct pipe_context
*pctx
, void *hwcso
)
94 lima_create_rasterizer_state(struct pipe_context
*pctx
,
95 const struct pipe_rasterizer_state
*cso
)
97 struct lima_rasterizer_state
*so
;
99 so
= CALLOC_STRUCT(lima_rasterizer_state
);
109 lima_bind_rasterizer_state(struct pipe_context
*pctx
, void *hwcso
)
111 struct lima_context
*ctx
= lima_context(pctx
);
113 ctx
->rasterizer
= hwcso
;
114 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_RASTERIZER
;
118 lima_delete_rasterizer_state(struct pipe_context
*pctx
, void *hwcso
)
124 lima_create_blend_state(struct pipe_context
*pctx
,
125 const struct pipe_blend_state
*cso
)
127 struct lima_blend_state
*so
;
129 so
= CALLOC_STRUCT(lima_blend_state
);
139 lima_bind_blend_state(struct pipe_context
*pctx
, void *hwcso
)
141 struct lima_context
*ctx
= lima_context(pctx
);
144 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_BLEND
;
148 lima_delete_blend_state(struct pipe_context
*pctx
, void *hwcso
)
154 lima_create_vertex_elements_state(struct pipe_context
*pctx
, unsigned num_elements
,
155 const struct pipe_vertex_element
*elements
)
157 struct lima_vertex_element_state
*so
;
159 so
= CALLOC_STRUCT(lima_vertex_element_state
);
163 memcpy(so
->pipe
, elements
, sizeof(*elements
) * num_elements
);
164 so
->num_elements
= num_elements
;
170 lima_bind_vertex_elements_state(struct pipe_context
*pctx
, void *hwcso
)
172 struct lima_context
*ctx
= lima_context(pctx
);
174 ctx
->vertex_elements
= hwcso
;
175 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_VERTEX_ELEM
;
179 lima_delete_vertex_elements_state(struct pipe_context
*pctx
, void *hwcso
)
185 lima_set_vertex_buffers(struct pipe_context
*pctx
,
186 unsigned start_slot
, unsigned count
,
187 const struct pipe_vertex_buffer
*vb
)
189 struct lima_context
*ctx
= lima_context(pctx
);
190 struct lima_context_vertex_buffer
*so
= &ctx
->vertex_buffers
;
192 util_set_vertex_buffers_mask(so
->vb
, &so
->enabled_mask
,
193 vb
, start_slot
, count
);
194 so
->count
= util_last_bit(so
->enabled_mask
);
196 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_VERTEX_BUFF
;
200 lima_set_viewport_states(struct pipe_context
*pctx
,
202 unsigned num_viewports
,
203 const struct pipe_viewport_state
*viewport
)
205 struct lima_context
*ctx
= lima_context(pctx
);
207 /* reverse calculate the parameter of glViewport */
208 ctx
->viewport
.left
= viewport
->translate
[0] - fabsf(viewport
->scale
[0]);
209 ctx
->viewport
.right
= viewport
->translate
[0] + fabsf(viewport
->scale
[0]);
210 ctx
->viewport
.bottom
= viewport
->translate
[1] - fabsf(viewport
->scale
[1]);
211 ctx
->viewport
.top
= viewport
->translate
[1] + fabsf(viewport
->scale
[1]);
213 /* reverse calculate the parameter of glDepthRange */
215 near
= viewport
->translate
[2] - viewport
->scale
[2];
216 far
= viewport
->translate
[2] + viewport
->scale
[2];
218 ctx
->viewport
.near
= MIN2(near
, far
);
219 ctx
->viewport
.far
= MAX2(near
, far
);
221 ctx
->viewport
.transform
= *viewport
;
222 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_VIEWPORT
;
226 lima_set_scissor_states(struct pipe_context
*pctx
,
228 unsigned num_scissors
,
229 const struct pipe_scissor_state
*scissor
)
231 struct lima_context
*ctx
= lima_context(pctx
);
233 ctx
->scissor
= *scissor
;
234 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_SCISSOR
;
238 lima_set_blend_color(struct pipe_context
*pctx
,
239 const struct pipe_blend_color
*blend_color
)
241 struct lima_context
*ctx
= lima_context(pctx
);
243 ctx
->blend_color
= *blend_color
;
244 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_BLEND_COLOR
;
248 lima_set_stencil_ref(struct pipe_context
*pctx
,
249 const struct pipe_stencil_ref
*stencil_ref
)
251 struct lima_context
*ctx
= lima_context(pctx
);
253 ctx
->stencil_ref
= *stencil_ref
;
254 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_STENCIL_REF
;
258 lima_set_constant_buffer(struct pipe_context
*pctx
,
259 enum pipe_shader_type shader
, uint index
,
260 const struct pipe_constant_buffer
*cb
)
262 struct lima_context
*ctx
= lima_context(pctx
);
263 struct lima_context_constant_buffer
*so
= ctx
->const_buffer
+ shader
;
273 so
->buffer
= cb
->user_buffer
+ cb
->buffer_offset
;
274 so
->size
= cb
->buffer_size
;
278 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_CONST_BUFF
;
283 lima_create_sampler_state(struct pipe_context
*pctx
,
284 const struct pipe_sampler_state
*cso
)
286 struct lima_sampler_state
*so
= CALLOC_STRUCT(lima_sampler_state
);
290 memcpy(so
, cso
, sizeof(*cso
));
296 lima_sampler_state_delete(struct pipe_context
*pctx
, void *sstate
)
302 lima_sampler_states_bind(struct pipe_context
*pctx
,
303 enum pipe_shader_type shader
, unsigned start
,
304 unsigned nr
, void **hwcso
)
306 struct lima_context
*ctx
= lima_context(pctx
);
307 struct lima_texture_stateobj
*lima_tex
= &ctx
->tex_stateobj
;
313 for (i
= 0; i
< nr
; i
++) {
316 lima_tex
->samplers
[i
] = hwcso
[i
];
319 for (; i
< lima_tex
->num_samplers
; i
++) {
320 lima_tex
->samplers
[i
] = NULL
;
323 lima_tex
->num_samplers
= new_nr
;
324 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_TEXTURES
;
327 static struct pipe_sampler_view
*
328 lima_create_sampler_view(struct pipe_context
*pctx
, struct pipe_resource
*prsc
,
329 const struct pipe_sampler_view
*cso
)
331 struct lima_sampler_view
*so
= CALLOC_STRUCT(lima_sampler_view
);
338 pipe_reference(NULL
, &prsc
->reference
);
339 so
->base
.texture
= prsc
;
340 so
->base
.reference
.count
= 1;
341 so
->base
.context
= pctx
;
347 lima_sampler_view_destroy(struct pipe_context
*pctx
,
348 struct pipe_sampler_view
*pview
)
350 struct lima_sampler_view
*view
= lima_sampler_view(pview
);
352 pipe_resource_reference(&pview
->texture
, NULL
);
358 lima_set_sampler_views(struct pipe_context
*pctx
,
359 enum pipe_shader_type shader
,
360 unsigned start
, unsigned nr
,
361 struct pipe_sampler_view
**views
)
363 struct lima_context
*ctx
= lima_context(pctx
);
364 struct lima_texture_stateobj
*lima_tex
= &ctx
->tex_stateobj
;
370 for (i
= 0; i
< nr
; i
++) {
373 pipe_sampler_view_reference(&lima_tex
->textures
[i
], views
[i
]);
376 for (; i
< lima_tex
->num_textures
; i
++) {
377 pipe_sampler_view_reference(&lima_tex
->textures
[i
], NULL
);
380 lima_tex
->num_textures
= new_nr
;
381 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_TEXTURES
;
385 lima_set_sample_mask(struct pipe_context
*pctx
,
386 unsigned sample_mask
)
391 lima_state_init(struct lima_context
*ctx
)
393 ctx
->base
.set_framebuffer_state
= lima_set_framebuffer_state
;
394 ctx
->base
.set_polygon_stipple
= lima_set_polygon_stipple
;
395 ctx
->base
.set_viewport_states
= lima_set_viewport_states
;
396 ctx
->base
.set_scissor_states
= lima_set_scissor_states
;
397 ctx
->base
.set_blend_color
= lima_set_blend_color
;
398 ctx
->base
.set_stencil_ref
= lima_set_stencil_ref
;
400 ctx
->base
.set_vertex_buffers
= lima_set_vertex_buffers
;
401 ctx
->base
.set_constant_buffer
= lima_set_constant_buffer
;
403 ctx
->base
.create_depth_stencil_alpha_state
= lima_create_depth_stencil_alpha_state
;
404 ctx
->base
.bind_depth_stencil_alpha_state
= lima_bind_depth_stencil_alpha_state
;
405 ctx
->base
.delete_depth_stencil_alpha_state
= lima_delete_depth_stencil_alpha_state
;
407 ctx
->base
.create_rasterizer_state
= lima_create_rasterizer_state
;
408 ctx
->base
.bind_rasterizer_state
= lima_bind_rasterizer_state
;
409 ctx
->base
.delete_rasterizer_state
= lima_delete_rasterizer_state
;
411 ctx
->base
.create_blend_state
= lima_create_blend_state
;
412 ctx
->base
.bind_blend_state
= lima_bind_blend_state
;
413 ctx
->base
.delete_blend_state
= lima_delete_blend_state
;
415 ctx
->base
.create_vertex_elements_state
= lima_create_vertex_elements_state
;
416 ctx
->base
.bind_vertex_elements_state
= lima_bind_vertex_elements_state
;
417 ctx
->base
.delete_vertex_elements_state
= lima_delete_vertex_elements_state
;
419 ctx
->base
.create_sampler_state
= lima_create_sampler_state
;
420 ctx
->base
.delete_sampler_state
= lima_sampler_state_delete
;
421 ctx
->base
.bind_sampler_states
= lima_sampler_states_bind
;
423 ctx
->base
.create_sampler_view
= lima_create_sampler_view
;
424 ctx
->base
.sampler_view_destroy
= lima_sampler_view_destroy
;
425 ctx
->base
.set_sampler_views
= lima_set_sampler_views
;
427 ctx
->base
.set_sample_mask
= lima_set_sample_mask
;
431 lima_state_fini(struct lima_context
*ctx
)
433 struct lima_context_vertex_buffer
*so
= &ctx
->vertex_buffers
;
435 util_set_vertex_buffers_mask(so
->vb
, &so
->enabled_mask
, NULL
,
436 0, ARRAY_SIZE(so
->vb
));
438 pipe_surface_reference(&ctx
->framebuffer
.base
.cbufs
[0], NULL
);
439 pipe_surface_reference(&ctx
->framebuffer
.base
.zsbuf
, NULL
);