Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pipe_stipple.c
4565 views
/**************************************************************************1*2* Copyright 2007 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/* Authors: Keith Whitwell <[email protected]>28*/2930/* Implement line stipple by cutting lines up into smaller lines.31* There are hundreds of ways to implement line stipple, this is one32* choice that should work in all situations, requires no state33* manipulations, but with a penalty in terms of large amounts of34* generated geometry.35*/363738#include "pipe/p_defines.h"39#include "pipe/p_shader_tokens.h"40#include "util/u_math.h"41#include "util/u_memory.h"4243#include "draw/draw_pipe.h"444546/** Subclass of draw_stage */47struct stipple_stage {48struct draw_stage stage;49float counter;50ushort pattern;51ushort factor;52bool rectangular;53};545556static inline struct stipple_stage *57stipple_stage(struct draw_stage *stage)58{59return (struct stipple_stage *) stage;60}616263/**64* Compute interpolated vertex attributes for 'dst' at position 't'65* between 'v0' and 'v1'.66* XXX using linear interpolation for all attribs at this time.67*/68static void69screen_interp(struct draw_context *draw,70struct vertex_header *dst,71float t,72const struct vertex_header *v0,73const struct vertex_header *v1)74{75uint attr;76uint num_outputs = draw_current_shader_outputs(draw);77for (attr = 0; attr < num_outputs; attr++) {78const float *val0 = v0->data[attr];79const float *val1 = v1->data[attr];80float *newv = dst->data[attr];81uint i;82for (i = 0; i < 4; i++) {83newv[i] = val0[i] + t * (val1[i] - val0[i]);84}85}86}878889static void90emit_segment(struct draw_stage *stage, struct prim_header *header,91float t0, float t1)92{93struct vertex_header *v0new = dup_vert(stage, header->v[0], 0);94struct vertex_header *v1new = dup_vert(stage, header->v[1], 1);95struct prim_header newprim = *header;9697if (t0 > 0.0) {98screen_interp(stage->draw, v0new, t0, header->v[0], header->v[1]);99newprim.v[0] = v0new;100}101102if (t1 < 1.0) {103screen_interp(stage->draw, v1new, t1, header->v[0], header->v[1]);104newprim.v[1] = v1new;105}106107stage->next->line(stage->next, &newprim);108}109110111static inline bool112stipple_test(int counter, ushort pattern, ushort factor)113{114int b = (counter / factor) & 0xf;115return !!((1 << b) & pattern);116}117118119static void120stipple_line(struct draw_stage *stage, struct prim_header *header)121{122struct stipple_stage *stipple = stipple_stage(stage);123struct vertex_header *v0 = header->v[0];124struct vertex_header *v1 = header->v[1];125const unsigned pos = draw_current_shader_position_output(stage->draw);126const float *pos0 = v0->data[pos];127const float *pos1 = v1->data[pos];128float start = 0;129bool state = 0;130131float x0 = pos0[0];132float x1 = pos1[0];133float y0 = pos0[1];134float y1 = pos1[1];135136float length;137int i;138int intlength;139140if (header->flags & DRAW_PIPE_RESET_STIPPLE)141stipple->counter = 0;142143if (stipple->rectangular) {144float dx = x1 - x0;145float dy = y1 - y0;146length = sqrtf(dx*dx + dy*dy);147} else {148float dx = x0 > x1 ? x0 - x1 : x1 - x0;149float dy = y0 > y1 ? y0 - y1 : y1 - y0;150length = MAX2(dx, dy);151}152153if (util_is_inf_or_nan(length))154intlength = 0;155else156intlength = ceilf(length);157158/* XXX ToDo: instead of iterating pixel-by-pixel, use a look-up table.159*/160for (i = 0; i < intlength; i++) {161bool result = stipple_test((int)stipple->counter + i,162stipple->pattern, stipple->factor);163if (result != state) {164/* changing from "off" to "on" or vice versa */165if (state) {166/* finishing an "on" segment */167emit_segment(stage, header, start / length, i / length);168}169else {170/* starting an "on" segment */171start = (float)i;172}173state = result;174}175}176177if (state && start < length)178emit_segment(stage, header, start / length, 1.0);179180stipple->counter += length;181}182183184static void185reset_stipple_counter(struct draw_stage *stage)186{187struct stipple_stage *stipple = stipple_stage(stage);188stipple->counter = 0;189stage->next->reset_stipple_counter(stage->next);190}191192static void193stipple_reset_point(struct draw_stage *stage, struct prim_header *header)194{195struct stipple_stage *stipple = stipple_stage(stage);196stipple->counter = 0;197stage->next->point(stage->next, header);198}199200static void201stipple_reset_tri(struct draw_stage *stage, struct prim_header *header)202{203struct stipple_stage *stipple = stipple_stage(stage);204stipple->counter = 0;205stage->next->tri(stage->next, header);206}207208209static void210stipple_first_line(struct draw_stage *stage,211struct prim_header *header)212{213struct stipple_stage *stipple = stipple_stage(stage);214struct draw_context *draw = stage->draw;215216stipple->pattern = draw->rasterizer->line_stipple_pattern;217stipple->factor = draw->rasterizer->line_stipple_factor + 1;218stipple->rectangular = draw->rasterizer->line_rectangular;219220stage->line = stipple_line;221stage->line(stage, header);222}223224225static void226stipple_flush(struct draw_stage *stage, unsigned flags)227{228stage->line = stipple_first_line;229stage->next->flush(stage->next, flags);230}231232233static void234stipple_destroy(struct draw_stage *stage)235{236draw_free_temp_verts(stage);237FREE(stage);238}239240241/**242* Create line stippler stage243*/244struct draw_stage *245draw_stipple_stage(struct draw_context *draw)246{247struct stipple_stage *stipple = CALLOC_STRUCT(stipple_stage);248if (!stipple)249goto fail;250251stipple->stage.draw = draw;252stipple->stage.name = "stipple";253stipple->stage.next = NULL;254stipple->stage.point = stipple_reset_point;255stipple->stage.line = stipple_first_line;256stipple->stage.tri = stipple_reset_tri;257stipple->stage.reset_stipple_counter = reset_stipple_counter;258stipple->stage.flush = stipple_flush;259stipple->stage.destroy = stipple_destroy;260261if (!draw_alloc_temp_verts(&stipple->stage, 2))262goto fail;263264return &stipple->stage;265266fail:267if (stipple)268stipple->stage.destroy(&stipple->stage);269270return NULL;271}272273274