Path: blob/21.2-virgl/src/gallium/drivers/asahi/agx_state.h
4570 views
/*1* Copyright 2021 Alyssa Rosenzweig2* Copyright (C) 2019-2021 Collabora, Ltd.3*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* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* 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 AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE.22*/2324#ifndef AGX_STATE_H25#define AGX_STATE_H2627#include "gallium/include/pipe/p_context.h"28#include "gallium/include/pipe/p_state.h"29#include "gallium/include/pipe/p_screen.h"30#include "gallium/auxiliary/util/u_blitter.h"31#include "asahi/lib/agx_pack.h"32#include "asahi/lib/agx_bo.h"33#include "asahi/lib/agx_device.h"34#include "asahi/lib/pool.h"35#include "asahi/compiler/agx_compile.h"36#include "compiler/nir/nir_lower_blend.h"37#include "util/hash_table.h"38#include "util/bitset.h"3940struct agx_streamout_target {41struct pipe_stream_output_target base;42uint32_t offset;43};4445struct agx_streamout {46struct pipe_stream_output_target *targets[PIPE_MAX_SO_BUFFERS];47unsigned num_targets;48};4950static inline struct agx_streamout_target *51agx_so_target(struct pipe_stream_output_target *target)52{53return (struct agx_streamout_target *)target;54}5556struct agx_compiled_shader {57/* Mapped executable memory */58struct agx_bo *bo;5960/* Varying descriptor (TODO: is this the right place?) */61uint64_t varyings;6263/* Metadata returned from the compiler */64struct agx_shader_info info;65};6667struct agx_uncompiled_shader {68struct pipe_shader_state base;69struct nir_shader *nir;70struct hash_table *variants;7172/* Set on VS, passed to FS for linkage */73unsigned base_varying;74};7576struct agx_stage {77struct agx_uncompiled_shader *shader;78uint32_t dirty;7980struct pipe_constant_buffer cb[PIPE_MAX_CONSTANT_BUFFERS];81uint32_t cb_mask;8283/* Need full CSOs for u_blitter */84struct agx_sampler_state *samplers[PIPE_MAX_SAMPLERS];85struct agx_sampler_view *textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];8687unsigned sampler_count, texture_count;88};8990/* Uploaded scissor descriptors */91struct agx_scissors {92struct agx_bo *bo;93unsigned count;94};9596struct agx_batch {97unsigned width, height, nr_cbufs;98struct pipe_surface *cbufs[8];99struct pipe_surface *zsbuf;100101/* PIPE_CLEAR_* bitmask */102uint32_t clear, draw;103104float clear_color[4];105106/* Resource list requirements, represented as a bit set indexed by BO107* handles (GEM handles on Linux, or IOGPU's equivalent on macOS) */108BITSET_WORD bo_list[256];109110struct agx_pool pool, pipeline_pool;111struct agx_bo *encoder;112uint8_t *encoder_current;113114struct agx_scissors scissor;115};116117struct agx_zsa {118struct pipe_depth_stencil_alpha_state base;119struct agx_rasterizer_face_packed front, back;120};121122struct agx_blend {123bool logicop_enable, blend_enable;124125union {126nir_lower_blend_rt rt[8];127unsigned logicop_func;128};129};130131struct asahi_shader_key {132struct agx_shader_key base;133struct agx_blend blend;134unsigned nr_cbufs;135enum pipe_format rt_formats[PIPE_MAX_COLOR_BUFS];136};137138enum agx_dirty {139AGX_DIRTY_VERTEX = BITFIELD_BIT(0),140AGX_DIRTY_VIEWPORT = BITFIELD_BIT(1),141AGX_DIRTY_SCISSOR = BITFIELD_BIT(2),142};143144struct agx_context {145struct pipe_context base;146struct agx_compiled_shader *vs, *fs;147uint32_t dirty;148149struct agx_batch *batch;150151struct pipe_vertex_buffer vertex_buffers[PIPE_MAX_ATTRIBS];152uint32_t vb_mask;153154struct agx_stage stage[PIPE_SHADER_TYPES];155struct agx_attribute *attributes;156struct agx_rasterizer *rast;157struct agx_zsa zs;158struct agx_blend *blend;159struct pipe_blend_color blend_color;160struct pipe_viewport_state viewport;161struct pipe_scissor_state scissor;162struct pipe_stencil_ref stencil_ref;163struct agx_streamout streamout;164uint16_t sample_mask;165struct pipe_framebuffer_state framebuffer;166167struct pipe_query *cond_query;168bool cond_cond;169enum pipe_render_cond_flag cond_mode;170171bool is_noop;172173uint8_t render_target[8][AGX_RENDER_TARGET_LENGTH];174175struct blitter_context *blitter;176};177178static inline struct agx_context *179agx_context(struct pipe_context *pctx)180{181return (struct agx_context *) pctx;182}183184struct agx_rasterizer {185struct pipe_rasterizer_state base;186uint8_t cull[AGX_CULL_LENGTH];187uint8_t line_width;188};189190struct agx_query {191unsigned query;192};193194struct agx_sampler_state {195struct pipe_sampler_state base;196197/* Prepared descriptor */198struct agx_bo *desc;199};200201struct agx_sampler_view {202struct pipe_sampler_view base;203204/* Prepared descriptor */205struct agx_bo *desc;206};207208struct agx_screen {209struct pipe_screen pscreen;210struct agx_device dev;211struct sw_winsys *winsys;212};213214static inline struct agx_screen *215agx_screen(struct pipe_screen *p)216{217return (struct agx_screen *)p;218}219220static inline struct agx_device *221agx_device(struct pipe_screen *p)222{223return &(agx_screen(p)->dev);224}225226/* TODO: UABI, fake for macOS */227#ifndef DRM_FORMAT_MOD_LINEAR228#define DRM_FORMAT_MOD_LINEAR 1229#endif230#define DRM_FORMAT_MOD_APPLE_64X64_MORTON_ORDER (2)231232struct agx_resource {233struct pipe_resource base;234uint64_t modifier;235236/* Hardware backing */237struct agx_bo *bo;238239/* Software backing (XXX) */240struct sw_displaytarget *dt;241unsigned dt_stride;242243BITSET_DECLARE(data_valid, PIPE_MAX_TEXTURE_LEVELS);244245struct {246unsigned offset;247unsigned line_stride;248} slices[PIPE_MAX_TEXTURE_LEVELS];249};250251static inline struct agx_resource *252agx_resource(struct pipe_resource *pctx)253{254return (struct agx_resource *) pctx;255}256257struct agx_transfer {258struct pipe_transfer base;259void *map;260struct {261struct pipe_resource *rsrc;262struct pipe_box box;263} staging;264};265266static inline struct agx_transfer *267agx_transfer(struct pipe_transfer *p)268{269return (struct agx_transfer *)p;270}271272uint64_t273agx_push_location(struct agx_context *ctx, struct agx_push push,274enum pipe_shader_type stage);275276uint64_t277agx_build_clear_pipeline(struct agx_context *ctx, uint32_t code, uint64_t clear_buf);278279uint64_t280agx_build_store_pipeline(struct agx_context *ctx, uint32_t code,281uint64_t render_target);282283uint64_t284agx_build_reload_pipeline(struct agx_context *ctx, uint32_t code, struct pipe_surface *surf);285286/* Add a BO to a batch. This needs to be amortized O(1) since it's called in287* hot paths. To achieve this we model BO lists by bit sets */288289static inline void290agx_batch_add_bo(struct agx_batch *batch, struct agx_bo *bo)291{292if (unlikely(bo->handle > (sizeof(batch->bo_list) * 8)))293unreachable("todo: growable");294295BITSET_SET(batch->bo_list, bo->handle);296}297298/* Blit shaders */299void agx_blit(struct pipe_context *pipe,300const struct pipe_blit_info *info);301302void agx_internal_shaders(struct agx_device *dev);303304#endif305306307