Path: blob/21.2-virgl/src/gallium/drivers/lima/lima_state.c
4565 views
/*1* Copyright (c) 2011-2013 Luc Verhaegen <[email protected]>2* Copyright (c) 2017-2019 Lima Project3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sub license,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the12* next paragraph) shall be included in all copies or substantial portions13* of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*23*/2425#include "util/format/u_format.h"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"3132#include "pipe/p_state.h"3334#include "lima_screen.h"35#include "lima_context.h"36#include "lima_format.h"37#include "lima_resource.h"3839static void40lima_set_framebuffer_state(struct pipe_context *pctx,41const struct pipe_framebuffer_state *framebuffer)42{43struct lima_context *ctx = lima_context(pctx);4445/* make sure there are always single job in this context */46if (lima_debug & LIMA_DEBUG_SINGLE_JOB)47lima_flush(ctx);4849struct lima_context_framebuffer *fb = &ctx->framebuffer;5051util_copy_framebuffer_state(&fb->base, framebuffer);5253ctx->job = NULL;54ctx->dirty |= LIMA_CONTEXT_DIRTY_FRAMEBUFFER;55}5657static void58lima_set_polygon_stipple(struct pipe_context *pctx,59const struct pipe_poly_stipple *stipple)60{6162}6364static void *65lima_create_depth_stencil_alpha_state(struct pipe_context *pctx,66const struct pipe_depth_stencil_alpha_state *cso)67{68struct lima_depth_stencil_alpha_state *so;6970so = CALLOC_STRUCT(lima_depth_stencil_alpha_state);71if (!so)72return NULL;7374so->base = *cso;7576return so;77}7879static void80lima_bind_depth_stencil_alpha_state(struct pipe_context *pctx, void *hwcso)81{82struct lima_context *ctx = lima_context(pctx);8384ctx->zsa = hwcso;85ctx->dirty |= LIMA_CONTEXT_DIRTY_ZSA;86}8788static void89lima_delete_depth_stencil_alpha_state(struct pipe_context *pctx, void *hwcso)90{91FREE(hwcso);92}9394static void *95lima_create_rasterizer_state(struct pipe_context *pctx,96const struct pipe_rasterizer_state *cso)97{98struct lima_rasterizer_state *so;99100so = CALLOC_STRUCT(lima_rasterizer_state);101if (!so)102return NULL;103104so->base = *cso;105106return so;107}108109static void110lima_bind_rasterizer_state(struct pipe_context *pctx, void *hwcso)111{112struct lima_context *ctx = lima_context(pctx);113114ctx->rasterizer = hwcso;115ctx->dirty |= LIMA_CONTEXT_DIRTY_RASTERIZER;116}117118static void119lima_delete_rasterizer_state(struct pipe_context *pctx, void *hwcso)120{121FREE(hwcso);122}123124static void *125lima_create_blend_state(struct pipe_context *pctx,126const struct pipe_blend_state *cso)127{128struct lima_blend_state *so;129130so = CALLOC_STRUCT(lima_blend_state);131if (!so)132return NULL;133134so->base = *cso;135136return so;137}138139static void140lima_bind_blend_state(struct pipe_context *pctx, void *hwcso)141{142struct lima_context *ctx = lima_context(pctx);143144ctx->blend = hwcso;145ctx->dirty |= LIMA_CONTEXT_DIRTY_BLEND;146}147148static void149lima_delete_blend_state(struct pipe_context *pctx, void *hwcso)150{151FREE(hwcso);152}153154static void *155lima_create_vertex_elements_state(struct pipe_context *pctx, unsigned num_elements,156const struct pipe_vertex_element *elements)157{158struct lima_vertex_element_state *so;159160so = CALLOC_STRUCT(lima_vertex_element_state);161if (!so)162return NULL;163164memcpy(so->pipe, elements, sizeof(*elements) * num_elements);165so->num_elements = num_elements;166167return so;168}169170static void171lima_bind_vertex_elements_state(struct pipe_context *pctx, void *hwcso)172{173struct lima_context *ctx = lima_context(pctx);174175ctx->vertex_elements = hwcso;176ctx->dirty |= LIMA_CONTEXT_DIRTY_VERTEX_ELEM;177}178179static void180lima_delete_vertex_elements_state(struct pipe_context *pctx, void *hwcso)181{182FREE(hwcso);183}184185static void186lima_set_vertex_buffers(struct pipe_context *pctx,187unsigned start_slot, unsigned count,188unsigned unbind_num_trailing_slots,189bool take_ownership,190const struct pipe_vertex_buffer *vb)191{192struct lima_context *ctx = lima_context(pctx);193struct lima_context_vertex_buffer *so = &ctx->vertex_buffers;194195util_set_vertex_buffers_mask(so->vb, &so->enabled_mask,196vb, start_slot, count,197unbind_num_trailing_slots,198take_ownership);199so->count = util_last_bit(so->enabled_mask);200201ctx->dirty |= LIMA_CONTEXT_DIRTY_VERTEX_BUFF;202}203204static void205lima_set_viewport_states(struct pipe_context *pctx,206unsigned start_slot,207unsigned num_viewports,208const struct pipe_viewport_state *viewport)209{210struct lima_context *ctx = lima_context(pctx);211212/* reverse calculate the parameter of glViewport */213ctx->viewport.left = viewport->translate[0] - fabsf(viewport->scale[0]);214ctx->viewport.right = viewport->translate[0] + fabsf(viewport->scale[0]);215ctx->viewport.bottom = viewport->translate[1] - fabsf(viewport->scale[1]);216ctx->viewport.top = viewport->translate[1] + fabsf(viewport->scale[1]);217218/* reverse calculate the parameter of glDepthRange */219float near, far;220near = viewport->translate[2] - viewport->scale[2];221far = viewport->translate[2] + viewport->scale[2];222223ctx->viewport.near = MIN2(near, far);224ctx->viewport.far = MAX2(near, far);225226ctx->viewport.transform = *viewport;227ctx->dirty |= LIMA_CONTEXT_DIRTY_VIEWPORT;228}229230static void231lima_set_scissor_states(struct pipe_context *pctx,232unsigned start_slot,233unsigned num_scissors,234const struct pipe_scissor_state *scissor)235{236struct lima_context *ctx = lima_context(pctx);237238ctx->scissor = *scissor;239ctx->dirty |= LIMA_CONTEXT_DIRTY_SCISSOR;240}241242static void243lima_set_blend_color(struct pipe_context *pctx,244const struct pipe_blend_color *blend_color)245{246struct lima_context *ctx = lima_context(pctx);247248ctx->blend_color = *blend_color;249ctx->dirty |= LIMA_CONTEXT_DIRTY_BLEND_COLOR;250}251252static void253lima_set_stencil_ref(struct pipe_context *pctx,254const struct pipe_stencil_ref stencil_ref)255{256struct lima_context *ctx = lima_context(pctx);257258ctx->stencil_ref = stencil_ref;259ctx->dirty |= LIMA_CONTEXT_DIRTY_STENCIL_REF;260}261262static void263lima_set_clip_state(struct pipe_context *pctx,264const struct pipe_clip_state *clip)265{266struct lima_context *ctx = lima_context(pctx);267ctx->clip = *clip;268269ctx->dirty |= LIMA_CONTEXT_DIRTY_CLIP;270}271272static void273lima_set_constant_buffer(struct pipe_context *pctx,274enum pipe_shader_type shader, uint index,275bool pass_reference,276const struct pipe_constant_buffer *cb)277{278struct lima_context *ctx = lima_context(pctx);279struct lima_context_constant_buffer *so = ctx->const_buffer + shader;280281assert(index == 0);282283if (unlikely(!cb)) {284so->buffer = NULL;285so->size = 0;286} else {287assert(!cb->buffer);288289so->buffer = cb->user_buffer + cb->buffer_offset;290so->size = cb->buffer_size;291}292293so->dirty = true;294ctx->dirty |= LIMA_CONTEXT_DIRTY_CONST_BUFF;295296}297298static void *299lima_create_sampler_state(struct pipe_context *pctx,300const struct pipe_sampler_state *cso)301{302struct lima_sampler_state *so = CALLOC_STRUCT(lima_sampler_state);303if (!so)304return NULL;305306memcpy(so, cso, sizeof(*cso));307308return so;309}310311static void312lima_sampler_state_delete(struct pipe_context *pctx, void *sstate)313{314free(sstate);315}316317static void318lima_sampler_states_bind(struct pipe_context *pctx,319enum pipe_shader_type shader, unsigned start,320unsigned nr, void **hwcso)321{322struct lima_context *ctx = lima_context(pctx);323struct lima_texture_stateobj *lima_tex = &ctx->tex_stateobj;324unsigned i;325unsigned new_nr = 0;326327assert(start == 0);328329for (i = 0; i < nr; i++) {330if (hwcso[i])331new_nr = i + 1;332lima_tex->samplers[i] = hwcso[i];333}334335for (; i < lima_tex->num_samplers; i++) {336lima_tex->samplers[i] = NULL;337}338339lima_tex->num_samplers = new_nr;340ctx->dirty |= LIMA_CONTEXT_DIRTY_TEXTURES;341}342343static struct pipe_sampler_view *344lima_create_sampler_view(struct pipe_context *pctx, struct pipe_resource *prsc,345const struct pipe_sampler_view *cso)346{347struct lima_sampler_view *so = CALLOC_STRUCT(lima_sampler_view);348349if (!so)350return NULL;351352so->base = *cso;353354pipe_reference(NULL, &prsc->reference);355so->base.texture = prsc;356so->base.reference.count = 1;357so->base.context = pctx;358359uint8_t sampler_swizzle[4] = { cso->swizzle_r, cso->swizzle_g,360cso->swizzle_b, cso->swizzle_a };361const uint8_t *format_swizzle = lima_format_get_texel_swizzle(cso->format);362util_format_compose_swizzles(format_swizzle, sampler_swizzle, so->swizzle);363364return &so->base;365}366367static void368lima_sampler_view_destroy(struct pipe_context *pctx,369struct pipe_sampler_view *pview)370{371struct lima_sampler_view *view = lima_sampler_view(pview);372373pipe_resource_reference(&pview->texture, NULL);374375free(view);376}377378static void379lima_set_sampler_views(struct pipe_context *pctx,380enum pipe_shader_type shader,381unsigned start, unsigned nr,382unsigned unbind_num_trailing_slots,383struct pipe_sampler_view **views)384{385struct lima_context *ctx = lima_context(pctx);386struct lima_texture_stateobj *lima_tex = &ctx->tex_stateobj;387int i;388unsigned new_nr = 0;389390assert(start == 0);391392for (i = 0; i < nr; i++) {393if (views[i])394new_nr = i + 1;395pipe_sampler_view_reference(&lima_tex->textures[i], views[i]);396}397398for (; i < lima_tex->num_textures; i++) {399pipe_sampler_view_reference(&lima_tex->textures[i], NULL);400}401402lima_tex->num_textures = new_nr;403ctx->dirty |= LIMA_CONTEXT_DIRTY_TEXTURES;404}405406static void407lima_set_sample_mask(struct pipe_context *pctx,408unsigned sample_mask)409{410}411412void413lima_state_init(struct lima_context *ctx)414{415ctx->base.set_framebuffer_state = lima_set_framebuffer_state;416ctx->base.set_polygon_stipple = lima_set_polygon_stipple;417ctx->base.set_viewport_states = lima_set_viewport_states;418ctx->base.set_scissor_states = lima_set_scissor_states;419ctx->base.set_blend_color = lima_set_blend_color;420ctx->base.set_stencil_ref = lima_set_stencil_ref;421ctx->base.set_clip_state = lima_set_clip_state;422423ctx->base.set_vertex_buffers = lima_set_vertex_buffers;424ctx->base.set_constant_buffer = lima_set_constant_buffer;425426ctx->base.create_depth_stencil_alpha_state = lima_create_depth_stencil_alpha_state;427ctx->base.bind_depth_stencil_alpha_state = lima_bind_depth_stencil_alpha_state;428ctx->base.delete_depth_stencil_alpha_state = lima_delete_depth_stencil_alpha_state;429430ctx->base.create_rasterizer_state = lima_create_rasterizer_state;431ctx->base.bind_rasterizer_state = lima_bind_rasterizer_state;432ctx->base.delete_rasterizer_state = lima_delete_rasterizer_state;433434ctx->base.create_blend_state = lima_create_blend_state;435ctx->base.bind_blend_state = lima_bind_blend_state;436ctx->base.delete_blend_state = lima_delete_blend_state;437438ctx->base.create_vertex_elements_state = lima_create_vertex_elements_state;439ctx->base.bind_vertex_elements_state = lima_bind_vertex_elements_state;440ctx->base.delete_vertex_elements_state = lima_delete_vertex_elements_state;441442ctx->base.create_sampler_state = lima_create_sampler_state;443ctx->base.delete_sampler_state = lima_sampler_state_delete;444ctx->base.bind_sampler_states = lima_sampler_states_bind;445446ctx->base.create_sampler_view = lima_create_sampler_view;447ctx->base.sampler_view_destroy = lima_sampler_view_destroy;448ctx->base.set_sampler_views = lima_set_sampler_views;449450ctx->base.set_sample_mask = lima_set_sample_mask;451}452453void454lima_state_fini(struct lima_context *ctx)455{456struct lima_context_vertex_buffer *so = &ctx->vertex_buffers;457458util_set_vertex_buffers_mask(so->vb, &so->enabled_mask, NULL,4590, 0, ARRAY_SIZE(so->vb), false);460461pipe_surface_reference(&ctx->framebuffer.base.cbufs[0], NULL);462pipe_surface_reference(&ctx->framebuffer.base.zsbuf, NULL);463}464465466