Rename G_NEW_* tokens to SP_NEW_*
[mesa.git] / src / mesa / pipe / softpipe / sp_state_derived.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * 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
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "main/glheader.h"
29 #include "main/macros.h"
30 #include "main/enums.h"
31 #include "shader/program.h"
32
33 #include "vf/vf.h"
34 #include "pipe/draw/draw_context.h"
35 #include "sp_context.h"
36 #include "sp_state.h"
37
38
39 #define EMIT_ATTR( ATTR, FRAG_ATTR, INTERP ) \
40 do { \
41 slot_to_vf_attr[softpipe->nr_attrs] = ATTR; \
42 softpipe->vf_attr_to_slot[ATTR] = softpipe->nr_attrs; \
43 softpipe->fp_attr_to_slot[FRAG_ATTR] = softpipe->nr_attrs; \
44 softpipe->interp[softpipe->nr_attrs] = INTERP; \
45 softpipe->nr_attrs++; \
46 attr_mask |= (1<<ATTR); \
47 } while (0)
48
49
50 static GLuint frag_to_vf[FRAG_ATTRIB_MAX] =
51 {
52 VF_ATTRIB_POS,
53 VF_ATTRIB_COLOR0,
54 VF_ATTRIB_COLOR1,
55 VF_ATTRIB_FOG,
56 VF_ATTRIB_TEX0,
57 VF_ATTRIB_TEX1,
58 VF_ATTRIB_TEX2,
59 VF_ATTRIB_TEX3,
60 VF_ATTRIB_TEX4,
61 VF_ATTRIB_TEX5,
62 VF_ATTRIB_TEX6,
63 VF_ATTRIB_TEX7,
64 };
65
66
67 /* Derived from: fs, setup states.
68 */
69 static void calculate_vertex_layout( struct softpipe_context *softpipe )
70 {
71 struct gl_fragment_program *fp = softpipe->fs.fp;
72 const GLuint inputsRead = fp->Base.InputsRead;
73 GLuint slot_to_vf_attr[VF_ATTRIB_MAX];
74 GLuint attr_mask = 0;
75 GLuint i;
76
77 softpipe->nr_attrs = 0;
78 memset(slot_to_vf_attr, 0, sizeof(slot_to_vf_attr));
79
80 memset(softpipe->fp_attr_to_slot, 0, sizeof(softpipe->fp_attr_to_slot));
81 memset(softpipe->vf_attr_to_slot, 0, sizeof(softpipe->vf_attr_to_slot));
82
83 /* TODO - Figure out if we need to do perspective divide, etc.
84 */
85 EMIT_ATTR(VF_ATTRIB_POS, FRAG_ATTRIB_WPOS, INTERP_LINEAR);
86
87 /* Pull in the rest of the attributes. They are all in float4
88 * format. Future optimizations could be to keep some attributes
89 * as fixed point or ubyte format.
90 */
91 for (i = 1; i < FRAG_ATTRIB_TEX0; i++) {
92 if (inputsRead & (i << i)) {
93 EMIT_ATTR(frag_to_vf[i], i, INTERP_LINEAR);
94 }
95 }
96
97 for (i = FRAG_ATTRIB_TEX0; i < FRAG_ATTRIB_MAX; i++) {
98 if (inputsRead & (i << i)) {
99 EMIT_ATTR(frag_to_vf[i], i, INTERP_PERSPECTIVE);
100 }
101 }
102
103 softpipe->nr_frag_attrs = softpipe->nr_attrs;
104
105 /* Additional attributes required for setup: Just twosided
106 * lighting. Edgeflag is dealt with specially by setting bits in
107 * the vertex header.
108 */
109 if (softpipe->setup.light_twoside) {
110 if (inputsRead & FRAG_BIT_COL0) {
111 EMIT_ATTR(VF_ATTRIB_BFC0, FRAG_ATTRIB_MAX, 0); /* XXX: mark as discarded after setup */
112 }
113
114 if (inputsRead & FRAG_BIT_COL1) {
115 EMIT_ATTR(VF_ATTRIB_BFC1, FRAG_ATTRIB_MAX, 0); /* XXX: discard after setup */
116 }
117 }
118
119 if (attr_mask != softpipe->attr_mask) {
120 softpipe->attr_mask = attr_mask;
121
122 draw_set_vertex_attributes( softpipe->draw,
123 slot_to_vf_attr,
124 softpipe->nr_attrs );
125 }
126 }
127
128
129 /* Hopefully this will remain quite simple, otherwise need to pull in
130 * something like the state tracker mechanism.
131 */
132 void softpipe_update_derived( struct softpipe_context *softpipe )
133 {
134 if (softpipe->dirty & (SP_NEW_SETUP | SP_NEW_FS))
135 calculate_vertex_layout( softpipe );
136
137 if (softpipe->dirty & (SP_NEW_BLEND | SP_NEW_DEPTH_TEST | SP_NEW_ALPHA_TEST | SP_NEW_FS))
138 sp_build_quad_pipeline(softpipe);
139
140 softpipe->dirty = 0;
141 }