Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pipe_wide_line.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#include "pipe/p_context.h"31#include "pipe/p_defines.h"32#include "pipe/p_shader_tokens.h"33#include "util/u_math.h"34#include "util/u_memory.h"35#include "draw_private.h"36#include "draw_pipe.h"373839struct wideline_stage {40struct draw_stage stage;41};424344/**45* Draw a wide line by drawing a quad (two triangles).46*/47static void wideline_line( struct draw_stage *stage,48struct prim_header *header )49{50const unsigned pos = draw_current_shader_position_output(stage->draw);51const float half_width = 0.5f * stage->draw->rasterizer->line_width;5253struct prim_header tri;5455struct vertex_header *v0 = dup_vert(stage, header->v[0], 0);56struct vertex_header *v1 = dup_vert(stage, header->v[0], 1);57struct vertex_header *v2 = dup_vert(stage, header->v[1], 2);58struct vertex_header *v3 = dup_vert(stage, header->v[1], 3);5960float *pos0 = v0->data[pos];61float *pos1 = v1->data[pos];62float *pos2 = v2->data[pos];63float *pos3 = v3->data[pos];6465const float dx = fabsf(pos0[0] - pos2[0]);66const float dy = fabsf(pos0[1] - pos2[1]);6768const boolean half_pixel_center =69stage->draw->rasterizer->half_pixel_center;7071/* small tweak to meet GL specification */72const float bias = half_pixel_center ? 0.125f : 0.0f;7374/*75* Draw wide line as a quad (two tris) by "stretching" the line along76* X or Y.77* We need to tweak coords in several ways to be conformant here.78*/7980if (dx > dy) {81/* x-major line */82pos0[1] = pos0[1] - half_width - bias;83pos1[1] = pos1[1] + half_width - bias;84pos2[1] = pos2[1] - half_width - bias;85pos3[1] = pos3[1] + half_width - bias;86if (half_pixel_center) {87if (pos0[0] < pos2[0]) {88/* left to right line */89pos0[0] -= 0.5f;90pos1[0] -= 0.5f;91pos2[0] -= 0.5f;92pos3[0] -= 0.5f;93}94else {95/* right to left line */96pos0[0] += 0.5f;97pos1[0] += 0.5f;98pos2[0] += 0.5f;99pos3[0] += 0.5f;100}101}102}103else {104/* y-major line */105pos0[0] = pos0[0] - half_width + bias;106pos1[0] = pos1[0] + half_width + bias;107pos2[0] = pos2[0] - half_width + bias;108pos3[0] = pos3[0] + half_width + bias;109if (half_pixel_center) {110if (pos0[1] < pos2[1]) {111/* top to bottom line */112pos0[1] -= 0.5f;113pos1[1] -= 0.5f;114pos2[1] -= 0.5f;115pos3[1] -= 0.5f;116}117else {118/* bottom to top line */119pos0[1] += 0.5f;120pos1[1] += 0.5f;121pos2[1] += 0.5f;122pos3[1] += 0.5f;123}124}125}126127tri.det = header->det; /* only the sign matters */128tri.v[0] = v0;129tri.v[1] = v2;130tri.v[2] = v3;131stage->next->tri( stage->next, &tri );132133tri.v[0] = v0;134tri.v[1] = v3;135tri.v[2] = v1;136stage->next->tri( stage->next, &tri );137}138139140static void wideline_first_line( struct draw_stage *stage,141struct prim_header *header )142{143struct draw_context *draw = stage->draw;144struct pipe_context *pipe = draw->pipe;145const struct pipe_rasterizer_state *rast = draw->rasterizer;146void *r;147148/* Disable triangle culling, stippling, unfilled mode etc. */149r = draw_get_rasterizer_no_cull(draw, rast);150draw->suspend_flushing = TRUE;151pipe->bind_rasterizer_state(pipe, r);152draw->suspend_flushing = FALSE;153154stage->line = wideline_line;155156wideline_line(stage, header);157}158159160static void wideline_flush( struct draw_stage *stage, unsigned flags )161{162struct draw_context *draw = stage->draw;163struct pipe_context *pipe = draw->pipe;164165stage->line = wideline_first_line;166stage->next->flush( stage->next, flags );167168/* restore original rasterizer state */169if (draw->rast_handle) {170draw->suspend_flushing = TRUE;171pipe->bind_rasterizer_state(pipe, draw->rast_handle);172draw->suspend_flushing = FALSE;173}174}175176177static void wideline_reset_stipple_counter( struct draw_stage *stage )178{179stage->next->reset_stipple_counter( stage->next );180}181182183static void wideline_destroy( struct draw_stage *stage )184{185draw_free_temp_verts( stage );186FREE( stage );187}188189190struct draw_stage *draw_wide_line_stage( struct draw_context *draw )191{192struct wideline_stage *wide = CALLOC_STRUCT(wideline_stage);193if (!wide)194goto fail;195196wide->stage.draw = draw;197wide->stage.name = "wide-line";198wide->stage.next = NULL;199wide->stage.point = draw_pipe_passthrough_point;200wide->stage.line = wideline_first_line;201wide->stage.tri = draw_pipe_passthrough_tri;202wide->stage.flush = wideline_flush;203wide->stage.reset_stipple_counter = wideline_reset_stipple_counter;204wide->stage.destroy = wideline_destroy;205206if (!draw_alloc_temp_verts( &wide->stage, 4 ))207goto fail;208209return &wide->stage;210211fail:212if (wide)213wide->stage.destroy( &wide->stage );214215return NULL;216}217218219