1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "glheader.h"
27 #include "context.h"
28 #include "lines.h"
29 #include "macros.h"
30 #include "mtypes.h"
31
32
33 /**
34 * Set the line width.
35 *
36 * \param width line width in pixels.
37 *
38 * \sa glLineWidth().
39 */
40 static ALWAYS_INLINE void
line_width(struct gl_context * ctx,GLfloat width,bool no_error)41 line_width(struct gl_context *ctx, GLfloat width, bool no_error)
42 {
43 /* If width is unchanged, there can't be an error */
44 if (ctx->Line.Width == width)
45 return;
46
47 if (!no_error && width <= 0.0F) {
48 _mesa_error( ctx, GL_INVALID_VALUE, "glLineWidth" );
49 return;
50 }
51
52 /* Page 407 (page 423 of the PDF) of the OpenGL 3.0 spec says (in the list
53 * of deprecated functionality):
54 *
55 * "Wide lines and line stipple - LineWidth is not deprecated, but
56 * values greater than 1.0 will generate an INVALID_VALUE error;"
57 *
58 * This is one of the very few cases where functionality was deprecated but
59 * *NOT* removed in a later spec. Therefore, we only disallow this in a
60 * forward compatible context.
61 */
62 if (!no_error && ctx->API == API_OPENGL_CORE
63 && ((ctx->Const.ContextFlags & GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT)
64 != 0)
65 && width > 1.0F) {
66 _mesa_error( ctx, GL_INVALID_VALUE, "glLineWidth" );
67 return;
68 }
69
70 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewLineState ? 0 : _NEW_LINE);
71 ctx->NewDriverState |= ctx->DriverFlags.NewLineState;
72 ctx->Line.Width = width;
73
74 if (ctx->Driver.LineWidth)
75 ctx->Driver.LineWidth(ctx, width);
76 }
77
78
79 void GLAPIENTRY
_mesa_LineWidth_no_error(GLfloat width)80 _mesa_LineWidth_no_error(GLfloat width)
81 {
82 GET_CURRENT_CONTEXT(ctx);
83 line_width(ctx, width, true);
84 }
85
86
87 void GLAPIENTRY
_mesa_LineWidth(GLfloat width)88 _mesa_LineWidth(GLfloat width)
89 {
90 GET_CURRENT_CONTEXT(ctx);
91
92 if (MESA_VERBOSE & VERBOSE_API)
93 _mesa_debug(ctx, "glLineWidth %f\n", width);
94
95 line_width(ctx, width, false);
96 }
97
98
99 /**
100 * Set the line stipple pattern.
101 *
102 * \param factor pattern scale factor.
103 * \param pattern bit pattern.
104 *
105 * \sa glLineStipple().
106 *
107 * Updates gl_line_attrib::StippleFactor and gl_line_attrib::StipplePattern. On
108 * change flushes the vertices and notifies the driver via
109 * the dd_function_table::LineStipple callback.
110 */
111 void GLAPIENTRY
_mesa_LineStipple(GLint factor,GLushort pattern)112 _mesa_LineStipple( GLint factor, GLushort pattern )
113 {
114 GET_CURRENT_CONTEXT(ctx);
115
116 if (MESA_VERBOSE & VERBOSE_API)
117 _mesa_debug(ctx, "glLineStipple %d %u\n", factor, pattern);
118
119 factor = CLAMP( factor, 1, 256 );
120
121 if (ctx->Line.StippleFactor == factor &&
122 ctx->Line.StipplePattern == pattern)
123 return;
124
125 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewLineState ? 0 : _NEW_LINE);
126 ctx->NewDriverState |= ctx->DriverFlags.NewLineState;
127 ctx->Line.StippleFactor = factor;
128 ctx->Line.StipplePattern = pattern;
129
130 if (ctx->Driver.LineStipple)
131 ctx->Driver.LineStipple( ctx, factor, pattern );
132 }
133
134
135 /**
136 * Initialize the context line state.
137 *
138 * \param ctx GL context.
139 *
140 * Initializes __struct gl_contextRec::Line and line related constants in
141 * __struct gl_contextRec::Const.
142 */
143 void
_mesa_init_line(struct gl_context * ctx)144 _mesa_init_line( struct gl_context * ctx )
145 {
146 ctx->Line.SmoothFlag = GL_FALSE;
147 ctx->Line.StippleFlag = GL_FALSE;
148 ctx->Line.Width = 1.0;
149 ctx->Line.StipplePattern = 0xffff;
150 ctx->Line.StippleFactor = 1;
151 }
152