Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pipe_aapoint.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* AA point stage: AA points are converted to quads and rendered with a29* special fragment shader. Another approach would be to use a texture30* map image of a point, but experiments indicate the quality isn't nearly31* as good as this approach.32*33* Note: this looks a lot like draw_aaline.c but there's actually little34* if any code that can be shared.35*36* Authors: Brian Paul37*/383940#include "pipe/p_context.h"41#include "pipe/p_defines.h"42#include "pipe/p_shader_tokens.h"4344#include "tgsi/tgsi_transform.h"45#include "tgsi/tgsi_dump.h"4647#include "util/u_math.h"48#include "util/u_memory.h"4950#include "draw_context.h"51#include "draw_vs.h"52#include "draw_pipe.h"5354#include "nir.h"55#include "nir/nir_draw_helpers.h"5657/** Approx number of new tokens for instructions in aa_transform_inst() */58#define NUM_NEW_TOKENS 200596061/*62* Enabling NORMALIZE might give _slightly_ better results.63* Basically, it controls whether we compute distance as d=sqrt(x*x+y*y) or64* d=x*x+y*y. Since we're working with a unit circle, the later seems65* close enough and saves some costly instructions.66*/67#define NORMALIZE 0686970/**71* Subclass of pipe_shader_state to carry extra fragment shader info.72*/73struct aapoint_fragment_shader74{75struct pipe_shader_state state;76void *driver_fs; /**< the regular shader */77void *aapoint_fs; /**< the aa point-augmented shader */78int generic_attrib; /**< The generic input attrib/texcoord we'll use */79};808182/**83* Subclass of draw_stage84*/85struct aapoint_stage86{87struct draw_stage stage;8889/** half of pipe_rasterizer_state::point_size */90float radius;9192/** vertex attrib slot containing point size */93int psize_slot;9495/** this is the vertex attrib slot for the new texcoords */96uint tex_slot;9798/** vertex attrib slot containing position */99uint pos_slot;100101/** Currently bound fragment shader */102struct aapoint_fragment_shader *fs;103104/*105* Driver interface/override functions106*/107void * (*driver_create_fs_state)(struct pipe_context *,108const struct pipe_shader_state *);109void (*driver_bind_fs_state)(struct pipe_context *, void *);110void (*driver_delete_fs_state)(struct pipe_context *, void *);111};112113114115/**116* Subclass of tgsi_transform_context, used for transforming the117* user's fragment shader to add the special AA instructions.118*/119struct aa_transform_context {120struct tgsi_transform_context base;121uint tempsUsed; /**< bitmask */122int colorOutput; /**< which output is the primary color */123int maxInput, maxGeneric; /**< max input index found */124int tmp0, colorTemp; /**< temp registers */125};126127128/**129* TGSI declaration transform callback.130* Look for two free temp regs and available input reg for new texcoords.131*/132static void133aa_transform_decl(struct tgsi_transform_context *ctx,134struct tgsi_full_declaration *decl)135{136struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;137138if (decl->Declaration.File == TGSI_FILE_OUTPUT &&139decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&140decl->Semantic.Index == 0) {141aactx->colorOutput = decl->Range.First;142}143else if (decl->Declaration.File == TGSI_FILE_INPUT) {144if ((int) decl->Range.Last > aactx->maxInput)145aactx->maxInput = decl->Range.Last;146if (decl->Semantic.Name == TGSI_SEMANTIC_GENERIC &&147(int) decl->Semantic.Index > aactx->maxGeneric) {148aactx->maxGeneric = decl->Semantic.Index;149}150}151else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {152uint i;153for (i = decl->Range.First;154i <= decl->Range.Last; i++) {155aactx->tempsUsed |= (1 << i);156}157}158159ctx->emit_declaration(ctx, decl);160}161162163/**164* TGSI transform callback.165* Insert new declarations and instructions before first instruction.166*/167static void168aa_transform_prolog(struct tgsi_transform_context *ctx)169{170/* emit our new declarations before the first instruction */171struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;172struct tgsi_full_instruction newInst;173const int texInput = aactx->maxInput + 1;174int tmp0;175uint i;176177/* find two free temp regs */178for (i = 0; i < 32; i++) {179if ((aactx->tempsUsed & (1u << i)) == 0) {180/* found a free temp */181if (aactx->tmp0 < 0)182aactx->tmp0 = i;183else if (aactx->colorTemp < 0)184aactx->colorTemp = i;185else186break;187}188}189190assert(aactx->colorTemp != aactx->tmp0);191192tmp0 = aactx->tmp0;193194/* declare new generic input/texcoord */195tgsi_transform_input_decl(ctx, texInput,196TGSI_SEMANTIC_GENERIC, aactx->maxGeneric + 1,197TGSI_INTERPOLATE_LINEAR);198199/* declare new temp regs */200tgsi_transform_temp_decl(ctx, tmp0);201tgsi_transform_temp_decl(ctx, aactx->colorTemp);202203/*204* Emit code to compute fragment coverage, kill if outside point radius205*206* Temp reg0 usage:207* t0.x = distance of fragment from center point208* t0.y = boolean, is t0.x > 1.0, also misc temp usage209* t0.z = temporary for computing 1/(1-k) value210* t0.w = final coverage value211*/212213/* MUL t0.xy, tex, tex; # compute x^2, y^2 */214tgsi_transform_op2_inst(ctx, TGSI_OPCODE_MUL,215TGSI_FILE_TEMPORARY, tmp0, TGSI_WRITEMASK_XY,216TGSI_FILE_INPUT, texInput,217TGSI_FILE_INPUT, texInput, false);218219/* ADD t0.x, t0.x, t0.y; # x^2 + y^2 */220tgsi_transform_op2_swz_inst(ctx, TGSI_OPCODE_ADD,221TGSI_FILE_TEMPORARY, tmp0, TGSI_WRITEMASK_X,222TGSI_FILE_TEMPORARY, tmp0, TGSI_SWIZZLE_X,223TGSI_FILE_TEMPORARY, tmp0, TGSI_SWIZZLE_Y, false);224225#if NORMALIZE /* OPTIONAL normalization of length */226/* RSQ t0.x, t0.x; */227tgsi_transform_op1_inst(ctx, TGSI_OPCODE_RSQ,228TGSI_FILE_TEMPORARY, tmp0, TGSI_WRITEMASK_X,229TGSI_FILE_TEMPORARY, tmp0);230231/* RCP t0.x, t0.x; */232tgsi_transform_op1_inst(ctx, TGSI_OPCODE_RCP,233TGSI_FILE_TEMPORARY, tmp0, TGSI_WRITEMASK_X,234TGSI_FILE_TEMPORARY, tmp0);235#endif236237/* SGT t0.y, t0.xxxx, tex.wwww; # bool b = d > 1 (NOTE tex.w == 1) */238tgsi_transform_op2_swz_inst(ctx, TGSI_OPCODE_SGT,239TGSI_FILE_TEMPORARY, tmp0, TGSI_WRITEMASK_Y,240TGSI_FILE_TEMPORARY, tmp0, TGSI_SWIZZLE_X,241TGSI_FILE_INPUT, texInput, TGSI_SWIZZLE_W, false);242243/* KILL_IF -tmp0.yyyy; # if -tmp0.y < 0, KILL */244tgsi_transform_kill_inst(ctx, TGSI_FILE_TEMPORARY, tmp0,245TGSI_SWIZZLE_Y, TRUE);246247/* compute coverage factor = (1-d)/(1-k) */248249/* SUB t0.z, tex.w, tex.z; # m = 1 - k */250tgsi_transform_op2_swz_inst(ctx, TGSI_OPCODE_ADD,251TGSI_FILE_TEMPORARY, tmp0, TGSI_WRITEMASK_Z,252TGSI_FILE_INPUT, texInput, TGSI_SWIZZLE_W,253TGSI_FILE_INPUT, texInput, TGSI_SWIZZLE_Z, true);254255/* RCP t0.z, t0.z; # t0.z = 1 / m */256newInst = tgsi_default_full_instruction();257newInst.Instruction.Opcode = TGSI_OPCODE_RCP;258newInst.Instruction.NumDstRegs = 1;259newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;260newInst.Dst[0].Register.Index = tmp0;261newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_Z;262newInst.Instruction.NumSrcRegs = 1;263newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;264newInst.Src[0].Register.Index = tmp0;265newInst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_Z;266ctx->emit_instruction(ctx, &newInst);267268/* SUB t0.y, 1, t0.x; # d = 1 - d */269tgsi_transform_op2_swz_inst(ctx, TGSI_OPCODE_ADD,270TGSI_FILE_TEMPORARY, tmp0, TGSI_WRITEMASK_Y,271TGSI_FILE_INPUT, texInput, TGSI_SWIZZLE_W,272TGSI_FILE_TEMPORARY, tmp0, TGSI_SWIZZLE_X, true);273274/* MUL t0.w, t0.y, t0.z; # coverage = d * m */275tgsi_transform_op2_swz_inst(ctx, TGSI_OPCODE_MUL,276TGSI_FILE_TEMPORARY, tmp0, TGSI_WRITEMASK_W,277TGSI_FILE_TEMPORARY, tmp0, TGSI_SWIZZLE_Y,278TGSI_FILE_TEMPORARY, tmp0, TGSI_SWIZZLE_Z, false);279280/* SLE t0.y, t0.x, tex.z; # bool b = distance <= k */281tgsi_transform_op2_swz_inst(ctx, TGSI_OPCODE_SLE,282TGSI_FILE_TEMPORARY, tmp0, TGSI_WRITEMASK_Y,283TGSI_FILE_TEMPORARY, tmp0, TGSI_SWIZZLE_X,284TGSI_FILE_INPUT, texInput, TGSI_SWIZZLE_Z, false);285286/* CMP t0.w, -t0.y, tex.w, t0.w;287* # if -t0.y < 0 then288* t0.w = 1289* else290* t0.w = t0.w291*/292tgsi_transform_op3_swz_inst(ctx, TGSI_OPCODE_CMP,293TGSI_FILE_TEMPORARY, tmp0, TGSI_WRITEMASK_W,294TGSI_FILE_TEMPORARY, tmp0, TGSI_SWIZZLE_Y, 1,295TGSI_FILE_INPUT, texInput, TGSI_SWIZZLE_W,296TGSI_FILE_TEMPORARY, tmp0, TGSI_SWIZZLE_W);297}298299300/**301* TGSI transform callback.302* Insert new instructions before the END instruction.303*/304static void305aa_transform_epilog(struct tgsi_transform_context *ctx)306{307struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;308309/* add alpha modulation code at tail of program */310311/* MOV result.color.xyz, colorTemp; */312tgsi_transform_op1_inst(ctx, TGSI_OPCODE_MOV,313TGSI_FILE_OUTPUT, aactx->colorOutput,314TGSI_WRITEMASK_XYZ,315TGSI_FILE_TEMPORARY, aactx->colorTemp);316317/* MUL result.color.w, colorTemp, tmp0.w; */318tgsi_transform_op2_inst(ctx, TGSI_OPCODE_MUL,319TGSI_FILE_OUTPUT, aactx->colorOutput,320TGSI_WRITEMASK_W,321TGSI_FILE_TEMPORARY, aactx->colorTemp,322TGSI_FILE_TEMPORARY, aactx->tmp0, false);323}324325326/**327* TGSI transform callback.328* Called per instruction.329* Replace writes to result.color w/ a temp reg.330*/331static void332aa_transform_inst(struct tgsi_transform_context *ctx,333struct tgsi_full_instruction *inst)334{335struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;336unsigned i;337338/* Not an END instruction.339* Look for writes to result.color and replace with colorTemp reg.340*/341for (i = 0; i < inst->Instruction.NumDstRegs; i++) {342struct tgsi_full_dst_register *dst = &inst->Dst[i];343if (dst->Register.File == TGSI_FILE_OUTPUT &&344dst->Register.Index == aactx->colorOutput) {345dst->Register.File = TGSI_FILE_TEMPORARY;346dst->Register.Index = aactx->colorTemp;347}348}349350ctx->emit_instruction(ctx, inst);351}352353354/**355* Generate the frag shader we'll use for drawing AA points.356* This will be the user's shader plus some texture/modulate instructions.357*/358static boolean359generate_aapoint_fs(struct aapoint_stage *aapoint)360{361const struct pipe_shader_state *orig_fs = &aapoint->fs->state;362struct pipe_shader_state aapoint_fs;363struct aa_transform_context transform;364const uint newLen = tgsi_num_tokens(orig_fs->tokens) + NUM_NEW_TOKENS;365struct pipe_context *pipe = aapoint->stage.draw->pipe;366367aapoint_fs = *orig_fs; /* copy to init */368369assert(aapoint_fs.type == PIPE_SHADER_IR_TGSI);370aapoint_fs.tokens = tgsi_alloc_tokens(newLen);371if (aapoint_fs.tokens == NULL)372return FALSE;373374memset(&transform, 0, sizeof(transform));375transform.colorOutput = -1;376transform.maxInput = -1;377transform.maxGeneric = -1;378transform.colorTemp = -1;379transform.tmp0 = -1;380transform.base.prolog = aa_transform_prolog;381transform.base.epilog = aa_transform_epilog;382transform.base.transform_instruction = aa_transform_inst;383transform.base.transform_declaration = aa_transform_decl;384385tgsi_transform_shader(orig_fs->tokens,386(struct tgsi_token *) aapoint_fs.tokens,387newLen, &transform.base);388389#if 0 /* DEBUG */390debug_printf("draw_aapoint, orig shader:\n");391tgsi_dump(orig_fs->tokens, 0);392debug_printf("draw_aapoint, new shader:\n");393tgsi_dump(aapoint_fs.tokens, 0);394#endif395396aapoint->fs->aapoint_fs397= aapoint->driver_create_fs_state(pipe, &aapoint_fs);398if (aapoint->fs->aapoint_fs == NULL)399goto fail;400401aapoint->fs->generic_attrib = transform.maxGeneric + 1;402FREE((void *)aapoint_fs.tokens);403return TRUE;404405fail:406FREE((void *)aapoint_fs.tokens);407return FALSE;408}409410static boolean411generate_aapoint_fs_nir(struct aapoint_stage *aapoint)412{413struct pipe_context *pipe = aapoint->stage.draw->pipe;414const struct pipe_shader_state *orig_fs = &aapoint->fs->state;415struct pipe_shader_state aapoint_fs;416417aapoint_fs = *orig_fs; /* copy to init */418aapoint_fs.ir.nir = nir_shader_clone(NULL, orig_fs->ir.nir);419if (!aapoint_fs.ir.nir)420return FALSE;421422nir_lower_aapoint_fs(aapoint_fs.ir.nir, &aapoint->fs->generic_attrib);423aapoint->fs->aapoint_fs = aapoint->driver_create_fs_state(pipe, &aapoint_fs);424if (aapoint->fs->aapoint_fs == NULL)425goto fail;426427return TRUE;428429fail:430return FALSE;431}432433/**434* When we're about to draw our first AA point in a batch, this function is435* called to tell the driver to bind our modified fragment shader.436*/437static boolean438bind_aapoint_fragment_shader(struct aapoint_stage *aapoint)439{440struct draw_context *draw = aapoint->stage.draw;441struct pipe_context *pipe = draw->pipe;442443if (!aapoint->fs->aapoint_fs) {444if (aapoint->fs->state.type == PIPE_SHADER_IR_NIR) {445if (!generate_aapoint_fs_nir(aapoint))446return FALSE;447} else if (!generate_aapoint_fs(aapoint))448return FALSE;449}450451draw->suspend_flushing = TRUE;452aapoint->driver_bind_fs_state(pipe, aapoint->fs->aapoint_fs);453draw->suspend_flushing = FALSE;454455return TRUE;456}457458459460static inline struct aapoint_stage *461aapoint_stage( struct draw_stage *stage )462{463return (struct aapoint_stage *) stage;464}465466467468469/**470* Draw an AA point by drawing a quad.471*/472static void473aapoint_point(struct draw_stage *stage, struct prim_header *header)474{475const struct aapoint_stage *aapoint = aapoint_stage(stage);476struct prim_header tri;477struct vertex_header *v[4];478const uint tex_slot = aapoint->tex_slot;479const uint pos_slot = aapoint->pos_slot;480float radius, *pos, *tex;481uint i;482float k;483484if (aapoint->psize_slot >= 0) {485radius = 0.5f * header->v[0]->data[aapoint->psize_slot][0];486}487else {488radius = aapoint->radius;489}490491/*492* Note: the texcoords (generic attrib, really) we use are special:493* The S and T components simply vary from -1 to +1.494* The R component is k, below.495* The Q component is 1.0 and will used as a handy constant in the496* fragment shader.497*/498499/*500* k is the threshold distance from the point's center at which501* we begin alpha attenuation (the coverage value).502* Operating within a unit circle, we'll compute the fragment's503* distance 'd' from the center point using the texcoords.504* IF d > 1.0 THEN505* KILL fragment506* ELSE IF d > k THEN507* compute coverage in [0,1] proportional to d in [k, 1].508* ELSE509* coverage = 1.0; // full coverage510* ENDIF511*512* Note: the ELSEIF and ELSE clauses are actually implemented with CMP to513* avoid using IF/ELSE/ENDIF TGSI opcodes.514*/515516#if !NORMALIZE517k = 1.0f / radius;518k = 1.0f - 2.0f * k + k * k;519#else520k = 1.0f - 1.0f / radius;521#endif522523/* allocate/dup new verts */524for (i = 0; i < 4; i++) {525v[i] = dup_vert(stage, header->v[0], i);526}527528/* new verts */529pos = v[0]->data[pos_slot];530pos[0] -= radius;531pos[1] -= radius;532533pos = v[1]->data[pos_slot];534pos[0] += radius;535pos[1] -= radius;536537pos = v[2]->data[pos_slot];538pos[0] += radius;539pos[1] += radius;540541pos = v[3]->data[pos_slot];542pos[0] -= radius;543pos[1] += radius;544545/* new texcoords */546tex = v[0]->data[tex_slot];547ASSIGN_4V(tex, -1, -1, k, 1);548549tex = v[1]->data[tex_slot];550ASSIGN_4V(tex, 1, -1, k, 1);551552tex = v[2]->data[tex_slot];553ASSIGN_4V(tex, 1, 1, k, 1);554555tex = v[3]->data[tex_slot];556ASSIGN_4V(tex, -1, 1, k, 1);557558/* emit 2 tris for the quad strip */559tri.v[0] = v[0];560tri.v[1] = v[1];561tri.v[2] = v[2];562stage->next->tri( stage->next, &tri );563564tri.v[0] = v[0];565tri.v[1] = v[2];566tri.v[2] = v[3];567stage->next->tri( stage->next, &tri );568}569570571static void572aapoint_first_point(struct draw_stage *stage, struct prim_header *header)573{574auto struct aapoint_stage *aapoint = aapoint_stage(stage);575struct draw_context *draw = stage->draw;576struct pipe_context *pipe = draw->pipe;577const struct pipe_rasterizer_state *rast = draw->rasterizer;578void *r;579580assert(draw->rasterizer->point_smooth && !draw->rasterizer->multisample);581582if (draw->rasterizer->point_size <= 2.0)583aapoint->radius = 1.0;584else585aapoint->radius = 0.5f * draw->rasterizer->point_size;586587/*588* Bind (generate) our fragprog.589*/590bind_aapoint_fragment_shader(aapoint);591592draw_aapoint_prepare_outputs(draw, draw->pipeline.aapoint);593594draw->suspend_flushing = TRUE;595596/* Disable triangle culling, stippling, unfilled mode etc. */597r = draw_get_rasterizer_no_cull(draw, rast);598pipe->bind_rasterizer_state(pipe, r);599600draw->suspend_flushing = FALSE;601602/* now really draw first point */603stage->point = aapoint_point;604stage->point(stage, header);605}606607608static void609aapoint_flush(struct draw_stage *stage, unsigned flags)610{611struct draw_context *draw = stage->draw;612struct aapoint_stage *aapoint = aapoint_stage(stage);613struct pipe_context *pipe = draw->pipe;614615stage->point = aapoint_first_point;616stage->next->flush( stage->next, flags );617618/* restore original frag shader */619draw->suspend_flushing = TRUE;620aapoint->driver_bind_fs_state(pipe, aapoint->fs ? aapoint->fs->driver_fs : NULL);621622/* restore original rasterizer state */623if (draw->rast_handle) {624pipe->bind_rasterizer_state(pipe, draw->rast_handle);625}626627draw->suspend_flushing = FALSE;628629draw_remove_extra_vertex_attribs(draw);630}631632633static void634aapoint_reset_stipple_counter(struct draw_stage *stage)635{636stage->next->reset_stipple_counter( stage->next );637}638639640static void641aapoint_destroy(struct draw_stage *stage)642{643struct aapoint_stage* aapoint = aapoint_stage(stage);644struct pipe_context *pipe = stage->draw->pipe;645646draw_free_temp_verts( stage );647648/* restore the old entry points */649pipe->create_fs_state = aapoint->driver_create_fs_state;650pipe->bind_fs_state = aapoint->driver_bind_fs_state;651pipe->delete_fs_state = aapoint->driver_delete_fs_state;652653FREE( stage );654}655656void657draw_aapoint_prepare_outputs(struct draw_context *draw,658struct draw_stage *stage)659{660struct aapoint_stage *aapoint = aapoint_stage(stage);661const struct pipe_rasterizer_state *rast = draw->rasterizer;662663/* update vertex attrib info */664aapoint->pos_slot = draw_current_shader_position_output(draw);665666if (!rast->point_smooth || rast->multisample)667return;668669if (aapoint->fs && aapoint->fs->aapoint_fs) {670/* allocate the extra post-transformed vertex attribute */671aapoint->tex_slot = draw_alloc_extra_vertex_attrib(draw,672TGSI_SEMANTIC_GENERIC,673aapoint->fs->generic_attrib);674assert(aapoint->tex_slot > 0); /* output[0] is vertex pos */675} else676aapoint->tex_slot = -1;677678/* find psize slot in post-transform vertex */679aapoint->psize_slot = -1;680if (draw->rasterizer->point_size_per_vertex) {681const struct tgsi_shader_info *info = draw_get_shader_info(draw);682uint i;683/* find PSIZ vertex output */684for (i = 0; i < info->num_outputs; i++) {685if (info->output_semantic_name[i] == TGSI_SEMANTIC_PSIZE) {686aapoint->psize_slot = i;687break;688}689}690}691}692693static struct aapoint_stage *694draw_aapoint_stage(struct draw_context *draw)695{696struct aapoint_stage *aapoint = CALLOC_STRUCT(aapoint_stage);697if (!aapoint)698goto fail;699700aapoint->stage.draw = draw;701aapoint->stage.name = "aapoint";702aapoint->stage.next = NULL;703aapoint->stage.point = aapoint_first_point;704aapoint->stage.line = draw_pipe_passthrough_line;705aapoint->stage.tri = draw_pipe_passthrough_tri;706aapoint->stage.flush = aapoint_flush;707aapoint->stage.reset_stipple_counter = aapoint_reset_stipple_counter;708aapoint->stage.destroy = aapoint_destroy;709710if (!draw_alloc_temp_verts( &aapoint->stage, 4 ))711goto fail;712713return aapoint;714715fail:716if (aapoint)717aapoint->stage.destroy(&aapoint->stage);718719return NULL;720721}722723724static struct aapoint_stage *725aapoint_stage_from_pipe(struct pipe_context *pipe)726{727struct draw_context *draw = (struct draw_context *) pipe->draw;728return aapoint_stage(draw->pipeline.aapoint);729}730731732/**733* This function overrides the driver's create_fs_state() function and734* will typically be called by the gallium frontend.735*/736static void *737aapoint_create_fs_state(struct pipe_context *pipe,738const struct pipe_shader_state *fs)739{740struct aapoint_stage *aapoint = aapoint_stage_from_pipe(pipe);741struct aapoint_fragment_shader *aafs = CALLOC_STRUCT(aapoint_fragment_shader);742if (!aafs)743return NULL;744745aafs->state.type = fs->type;746if (fs->type == PIPE_SHADER_IR_TGSI)747aafs->state.tokens = tgsi_dup_tokens(fs->tokens);748else749aafs->state.ir.nir = nir_shader_clone(NULL, fs->ir.nir);750/* pass-through */751aafs->driver_fs = aapoint->driver_create_fs_state(pipe, fs);752753return aafs;754}755756757static void758aapoint_bind_fs_state(struct pipe_context *pipe, void *fs)759{760struct aapoint_stage *aapoint = aapoint_stage_from_pipe(pipe);761struct aapoint_fragment_shader *aafs = (struct aapoint_fragment_shader *) fs;762/* save current */763aapoint->fs = aafs;764/* pass-through */765aapoint->driver_bind_fs_state(pipe,766(aafs ? aafs->driver_fs : NULL));767}768769770static void771aapoint_delete_fs_state(struct pipe_context *pipe, void *fs)772{773struct aapoint_stage *aapoint = aapoint_stage_from_pipe(pipe);774struct aapoint_fragment_shader *aafs = (struct aapoint_fragment_shader *) fs;775776/* pass-through */777aapoint->driver_delete_fs_state(pipe, aafs->driver_fs);778779if (aafs->aapoint_fs)780aapoint->driver_delete_fs_state(pipe, aafs->aapoint_fs);781782if (aafs->state.type == PIPE_SHADER_IR_TGSI)783FREE((void*)aafs->state.tokens);784else785ralloc_free(aafs->state.ir.nir);786787FREE(aafs);788}789790791/**792* Called by drivers that want to install this AA point prim stage793* into the draw module's pipeline. This will not be used if the794* hardware has native support for AA points.795*/796boolean797draw_install_aapoint_stage(struct draw_context *draw,798struct pipe_context *pipe)799{800struct aapoint_stage *aapoint;801802pipe->draw = (void *) draw;803804/*805* Create / install AA point drawing / prim stage806*/807aapoint = draw_aapoint_stage( draw );808if (!aapoint)809return FALSE;810811/* save original driver functions */812aapoint->driver_create_fs_state = pipe->create_fs_state;813aapoint->driver_bind_fs_state = pipe->bind_fs_state;814aapoint->driver_delete_fs_state = pipe->delete_fs_state;815816/* override the driver's functions */817pipe->create_fs_state = aapoint_create_fs_state;818pipe->bind_fs_state = aapoint_bind_fs_state;819pipe->delete_fs_state = aapoint_delete_fs_state;820821draw->pipeline.aapoint = &aapoint->stage;822823return TRUE;824}825826827