Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pipe_pstipple.c
4565 views
/**************************************************************************1*2* Copyright 2008 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/**28* Polygon stipple stage: implement polygon stipple with texture map and29* fragment program. The fragment program samples the texture using the30* fragment window coordinate register and does a fragment kill for the31* stipple-failing fragments.32*33* Authors: Brian Paul34*/353637#include "pipe/p_context.h"38#include "pipe/p_defines.h"39#include "pipe/p_shader_tokens.h"40#include "util/u_inlines.h"4142#include "util/format/u_format.h"43#include "util/u_math.h"44#include "util/u_memory.h"45#include "util/u_pstipple.h"46#include "util/u_sampler.h"4748#include "tgsi/tgsi_transform.h"4950#include "draw_context.h"51#include "draw_pipe.h"5253#include "nir.h"54#include "nir/nir_draw_helpers.h"5556/** Approx number of new tokens for instructions in pstip_transform_inst() */57#define NUM_NEW_TOKENS 53585960/**61* Subclass of pipe_shader_state to carry extra fragment shader info.62*/63struct pstip_fragment_shader64{65struct pipe_shader_state state;66void *driver_fs;67void *pstip_fs;68uint sampler_unit;69};707172/**73* Subclass of draw_stage74*/75struct pstip_stage76{77struct draw_stage stage;7879void *sampler_cso;80struct pipe_resource *texture;81struct pipe_sampler_view *sampler_view;82uint num_samplers;83uint num_sampler_views;8485/*86* Currently bound state87*/88struct pstip_fragment_shader *fs;89struct {90void *samplers[PIPE_MAX_SAMPLERS];91struct pipe_sampler_view *sampler_views[PIPE_MAX_SHADER_SAMPLER_VIEWS];92const struct pipe_poly_stipple *stipple;93} state;9495/*96* Driver interface/override functions97*/98void * (*driver_create_fs_state)(struct pipe_context *,99const struct pipe_shader_state *);100void (*driver_bind_fs_state)(struct pipe_context *, void *);101void (*driver_delete_fs_state)(struct pipe_context *, void *);102103void (*driver_bind_sampler_states)(struct pipe_context *,104enum pipe_shader_type,105unsigned, unsigned, void **);106107void (*driver_set_sampler_views)(struct pipe_context *,108enum pipe_shader_type shader,109unsigned start, unsigned count,110unsigned unbind_num_trailing_slots,111struct pipe_sampler_view **);112113void (*driver_set_polygon_stipple)(struct pipe_context *,114const struct pipe_poly_stipple *);115116struct pipe_context *pipe;117};118119120/**121* Generate the frag shader we'll use for doing polygon stipple.122* This will be the user's shader prefixed with a TEX and KIL instruction.123*/124static boolean125generate_pstip_fs(struct pstip_stage *pstip)126{127struct pipe_context *pipe = pstip->pipe;128struct pipe_screen *screen = pipe->screen;129const struct pipe_shader_state *orig_fs = &pstip->fs->state;130/*struct draw_context *draw = pstip->stage.draw;*/131struct pipe_shader_state pstip_fs;132enum tgsi_file_type wincoord_file;133134wincoord_file = screen->get_param(screen, PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL) ?135TGSI_FILE_SYSTEM_VALUE : TGSI_FILE_INPUT;136137pstip_fs = *orig_fs; /* copy to init */138if (orig_fs->type == PIPE_SHADER_IR_TGSI) {139pstip_fs.tokens = util_pstipple_create_fragment_shader(orig_fs->tokens,140&pstip->fs->sampler_unit,1410,142wincoord_file);143if (pstip_fs.tokens == NULL)144return FALSE;145} else {146pstip_fs.ir.nir = nir_shader_clone(NULL, orig_fs->ir.nir);147nir_lower_pstipple_fs(pstip_fs.ir.nir,148&pstip->fs->sampler_unit, 0, wincoord_file == TGSI_FILE_SYSTEM_VALUE);149}150151assert(pstip->fs->sampler_unit < PIPE_MAX_SAMPLERS);152153pstip->fs->pstip_fs = pstip->driver_create_fs_state(pipe, &pstip_fs);154155FREE((void *)pstip_fs.tokens);156157if (!pstip->fs->pstip_fs)158return FALSE;159160return TRUE;161}162163164/**165* When we're about to draw our first stipple polygon in a batch, this function166* is called to tell the driver to bind our modified fragment shader.167*/168static boolean169bind_pstip_fragment_shader(struct pstip_stage *pstip)170{171struct draw_context *draw = pstip->stage.draw;172if (!pstip->fs->pstip_fs &&173!generate_pstip_fs(pstip))174return FALSE;175176draw->suspend_flushing = TRUE;177pstip->driver_bind_fs_state(pstip->pipe, pstip->fs->pstip_fs);178draw->suspend_flushing = FALSE;179return TRUE;180}181182183static inline struct pstip_stage *184pstip_stage( struct draw_stage *stage )185{186return (struct pstip_stage *) stage;187}188189190static void191pstip_first_tri(struct draw_stage *stage, struct prim_header *header)192{193struct pstip_stage *pstip = pstip_stage(stage);194struct pipe_context *pipe = pstip->pipe;195struct draw_context *draw = stage->draw;196uint num_samplers;197uint num_sampler_views;198199assert(stage->draw->rasterizer->poly_stipple_enable);200201/* bind our fragprog */202if (!bind_pstip_fragment_shader(pstip)) {203stage->tri = draw_pipe_passthrough_tri;204stage->tri(stage, header);205return;206}207208/* how many samplers? */209/* we'll use sampler/texture[pstip->sampler_unit] for the stipple */210num_samplers = MAX2(pstip->num_samplers, pstip->fs->sampler_unit + 1);211num_sampler_views = MAX2(pstip->num_sampler_views, num_samplers);212213/* plug in our sampler, texture */214pstip->state.samplers[pstip->fs->sampler_unit] = pstip->sampler_cso;215pipe_sampler_view_reference(&pstip->state.sampler_views[pstip->fs->sampler_unit],216pstip->sampler_view);217218assert(num_samplers <= PIPE_MAX_SAMPLERS);219220draw->suspend_flushing = TRUE;221222pstip->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,223num_samplers, pstip->state.samplers);224225pstip->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,226num_sampler_views, 0, pstip->state.sampler_views);227228draw->suspend_flushing = FALSE;229230/* now really draw first triangle */231stage->tri = draw_pipe_passthrough_tri;232stage->tri(stage, header);233}234235236static void237pstip_flush(struct draw_stage *stage, unsigned flags)238{239struct draw_context *draw = stage->draw;240struct pstip_stage *pstip = pstip_stage(stage);241struct pipe_context *pipe = pstip->pipe;242243stage->tri = pstip_first_tri;244stage->next->flush( stage->next, flags );245246/* restore original frag shader, texture, sampler state */247draw->suspend_flushing = TRUE;248pstip->driver_bind_fs_state(pipe, pstip->fs ? pstip->fs->driver_fs : NULL);249250pstip->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,251pstip->num_samplers,252pstip->state.samplers);253254pstip->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,255pstip->num_sampler_views, 0,256pstip->state.sampler_views);257258draw->suspend_flushing = FALSE;259}260261262static void263pstip_reset_stipple_counter(struct draw_stage *stage)264{265stage->next->reset_stipple_counter( stage->next );266}267268269static void270pstip_destroy(struct draw_stage *stage)271{272struct pstip_stage *pstip = pstip_stage(stage);273uint i;274275for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {276pipe_sampler_view_reference(&pstip->state.sampler_views[i], NULL);277}278279pstip->pipe->delete_sampler_state(pstip->pipe, pstip->sampler_cso);280281pipe_resource_reference(&pstip->texture, NULL);282283if (pstip->sampler_view) {284pipe_sampler_view_reference(&pstip->sampler_view, NULL);285}286287draw_free_temp_verts( stage );288FREE( stage );289}290291292/** Create a new polygon stipple drawing stage object */293static struct pstip_stage *294draw_pstip_stage(struct draw_context *draw, struct pipe_context *pipe)295{296struct pstip_stage *pstip = CALLOC_STRUCT(pstip_stage);297if (!pstip)298goto fail;299300pstip->pipe = pipe;301302pstip->stage.draw = draw;303pstip->stage.name = "pstip";304pstip->stage.next = NULL;305pstip->stage.point = draw_pipe_passthrough_point;306pstip->stage.line = draw_pipe_passthrough_line;307pstip->stage.tri = pstip_first_tri;308pstip->stage.flush = pstip_flush;309pstip->stage.reset_stipple_counter = pstip_reset_stipple_counter;310pstip->stage.destroy = pstip_destroy;311312if (!draw_alloc_temp_verts( &pstip->stage, 8 ))313goto fail;314315return pstip;316317fail:318if (pstip)319pstip->stage.destroy( &pstip->stage );320321return NULL;322}323324325static struct pstip_stage *326pstip_stage_from_pipe(struct pipe_context *pipe)327{328struct draw_context *draw = (struct draw_context *) pipe->draw;329return pstip_stage(draw->pipeline.pstipple);330}331332333/**334* This function overrides the driver's create_fs_state() function and335* will typically be called by the gallium frontend.336*/337static void *338pstip_create_fs_state(struct pipe_context *pipe,339const struct pipe_shader_state *fs)340{341struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);342struct pstip_fragment_shader *pstipfs = CALLOC_STRUCT(pstip_fragment_shader);343344if (pstipfs) {345pstipfs->state.type = fs->type;346if (fs->type == PIPE_SHADER_IR_TGSI)347pstipfs->state.tokens = tgsi_dup_tokens(fs->tokens);348else349pstipfs->state.ir.nir = nir_shader_clone(NULL, fs->ir.nir);350351/* pass-through */352pstipfs->driver_fs = pstip->driver_create_fs_state(pstip->pipe, fs);353}354355return pstipfs;356}357358359static void360pstip_bind_fs_state(struct pipe_context *pipe, void *fs)361{362struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);363struct pstip_fragment_shader *pstipfs = (struct pstip_fragment_shader *) fs;364/* save current */365pstip->fs = pstipfs;366/* pass-through */367pstip->driver_bind_fs_state(pstip->pipe,368(pstipfs ? pstipfs->driver_fs : NULL));369}370371372static void373pstip_delete_fs_state(struct pipe_context *pipe, void *fs)374{375struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);376struct pstip_fragment_shader *pstipfs = (struct pstip_fragment_shader *) fs;377/* pass-through */378pstip->driver_delete_fs_state(pstip->pipe, pstipfs->driver_fs);379380if (pstipfs->pstip_fs)381pstip->driver_delete_fs_state(pstip->pipe, pstipfs->pstip_fs);382383if (pstipfs->state.type == PIPE_SHADER_IR_TGSI)384FREE((void*)pstipfs->state.tokens);385else386ralloc_free(pstipfs->state.ir.nir);387FREE(pstipfs);388}389390391static void392pstip_bind_sampler_states(struct pipe_context *pipe,393enum pipe_shader_type shader,394unsigned start, unsigned num, void **sampler)395{396struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);397uint i;398399assert(start == 0);400401if (shader == PIPE_SHADER_FRAGMENT) {402/* save current */403memcpy(pstip->state.samplers, sampler, num * sizeof(void *));404for (i = num; i < PIPE_MAX_SAMPLERS; i++) {405pstip->state.samplers[i] = NULL;406}407pstip->num_samplers = num;408}409410/* pass-through */411pstip->driver_bind_sampler_states(pstip->pipe, shader, start, num, sampler);412}413414415static void416pstip_set_sampler_views(struct pipe_context *pipe,417enum pipe_shader_type shader,418unsigned start, unsigned num,419unsigned unbind_num_trailing_slots,420struct pipe_sampler_view **views)421{422struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);423uint i;424425if (shader == PIPE_SHADER_FRAGMENT) {426/* save current */427for (i = 0; i < num; i++) {428pipe_sampler_view_reference(&pstip->state.sampler_views[start + i],429views[i]);430}431for (; i < num + unbind_num_trailing_slots; i++) {432pipe_sampler_view_reference(&pstip->state.sampler_views[start + i],433NULL);434}435pstip->num_sampler_views = num;436}437438/* pass-through */439pstip->driver_set_sampler_views(pstip->pipe, shader, start, num,440unbind_num_trailing_slots, views);441}442443444static void445pstip_set_polygon_stipple(struct pipe_context *pipe,446const struct pipe_poly_stipple *stipple)447{448struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);449450/* save current */451pstip->state.stipple = stipple;452453/* pass-through */454pstip->driver_set_polygon_stipple(pstip->pipe, stipple);455456util_pstipple_update_stipple_texture(pstip->pipe, pstip->texture,457pstip->state.stipple->stipple);458}459460461/**462* Called by drivers that want to install this polygon stipple stage463* into the draw module's pipeline. This will not be used if the464* hardware has native support for polygon stipple.465*/466boolean467draw_install_pstipple_stage(struct draw_context *draw,468struct pipe_context *pipe)469{470struct pstip_stage *pstip;471472pipe->draw = (void *) draw;473474/*475* Create / install pgon stipple drawing / prim stage476*/477pstip = draw_pstip_stage( draw, pipe );478if (!pstip)479goto fail;480481draw->pipeline.pstipple = &pstip->stage;482483/* save original driver functions */484pstip->driver_create_fs_state = pipe->create_fs_state;485pstip->driver_bind_fs_state = pipe->bind_fs_state;486pstip->driver_delete_fs_state = pipe->delete_fs_state;487488pstip->driver_bind_sampler_states = pipe->bind_sampler_states;489pstip->driver_set_sampler_views = pipe->set_sampler_views;490pstip->driver_set_polygon_stipple = pipe->set_polygon_stipple;491492/* create special texture, sampler state */493pstip->texture = util_pstipple_create_stipple_texture(pipe, NULL);494if (!pstip->texture)495goto fail;496497pstip->sampler_view = util_pstipple_create_sampler_view(pipe,498pstip->texture);499if (!pstip->sampler_view)500goto fail;501502pstip->sampler_cso = util_pstipple_create_sampler(pipe);503if (!pstip->sampler_cso)504goto fail;505506/* override the driver's functions */507pipe->create_fs_state = pstip_create_fs_state;508pipe->bind_fs_state = pstip_bind_fs_state;509pipe->delete_fs_state = pstip_delete_fs_state;510511pipe->bind_sampler_states = pstip_bind_sampler_states;512pipe->set_sampler_views = pstip_set_sampler_views;513pipe->set_polygon_stipple = pstip_set_polygon_stipple;514515return TRUE;516517fail:518if (pstip)519pstip->stage.destroy( &pstip->stage );520521return FALSE;522}523524525