1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 VMWARE 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.
26 **************************************************************************/
29 #include "util/u_memory.h"
30 #include "pipe/p_shader_tokens.h"
31 #include "draw/draw_context.h"
32 #include "draw/draw_vertex.h"
33 #include "i915_context.h"
34 #include "i915_state.h"
35 #include "i915_debug.h"
39 static uint
find_mapping(const struct i915_fragment_shader
* fs
, int unit
)
42 for (i
= 0; i
< I915_TEX_UNITS
; i
++)
44 if (fs
->generic_mapping
[i
] == unit
)
47 debug_printf("Mapping not found\n");
53 /***********************************************************************
54 * Determine the hardware vertex layout.
55 * Depends on vertex/fragment shader state.
57 static void calculate_vertex_layout(struct i915_context
*i915
)
59 const struct i915_fragment_shader
*fs
= i915
->fs
;
60 struct vertex_info vinfo
;
61 boolean texCoords
[I915_TEX_UNITS
], colors
[2], fog
, needW
, face
;
65 memset(texCoords
, 0, sizeof(texCoords
));
66 colors
[0] = colors
[1] = fog
= needW
= face
= FALSE
;
67 memset(&vinfo
, 0, sizeof(vinfo
));
69 /* Determine which fragment program inputs are needed. Setup HW vertex
70 * layout below, in the HW-specific attribute order.
72 for (i
= 0; i
< fs
->info
.num_inputs
; i
++) {
73 switch (fs
->info
.input_semantic_name
[i
]) {
74 case TGSI_SEMANTIC_POSITION
:
76 uint unit
= I915_SEMANTIC_POS
;
77 texCoords
[find_mapping(fs
, unit
)] = TRUE
;
80 case TGSI_SEMANTIC_COLOR
:
81 assert(fs
->info
.input_semantic_index
[i
] < 2);
82 colors
[fs
->info
.input_semantic_index
[i
]] = TRUE
;
84 case TGSI_SEMANTIC_GENERIC
:
86 /* texcoords/varyings/other generic */
87 uint unit
= fs
->info
.input_semantic_index
[i
];
89 texCoords
[find_mapping(fs
, unit
)] = TRUE
;
93 case TGSI_SEMANTIC_FOG
:
96 case TGSI_SEMANTIC_FACE
:
100 debug_printf("Unknown input type %d\n", fs
->info
.input_semantic_name
[i
]);
107 src
= draw_find_shader_output(i915
->draw
, TGSI_SEMANTIC_POSITION
, 0);
109 draw_emit_vertex_attr(&vinfo
, EMIT_4F
, src
);
110 vinfo
.hwfmt
[0] |= S4_VFMT_XYZW
;
111 vinfo
.attrib
[0].emit
= EMIT_4F
;
114 draw_emit_vertex_attr(&vinfo
, EMIT_3F
, src
);
115 vinfo
.hwfmt
[0] |= S4_VFMT_XYZ
;
116 vinfo
.attrib
[0].emit
= EMIT_3F
;
119 /* hardware point size */
124 src
= draw_find_shader_output(i915
->draw
, TGSI_SEMANTIC_COLOR
, 0);
125 draw_emit_vertex_attr(&vinfo
, EMIT_4UB_BGRA
, src
);
126 vinfo
.hwfmt
[0] |= S4_VFMT_COLOR
;
129 /* secondary color */
131 src
= draw_find_shader_output(i915
->draw
, TGSI_SEMANTIC_COLOR
, 1);
132 draw_emit_vertex_attr(&vinfo
, EMIT_4UB_BGRA
, src
);
133 vinfo
.hwfmt
[0] |= S4_VFMT_SPEC_FOG
;
136 /* fog coord, not fog blend factor */
138 src
= draw_find_shader_output(i915
->draw
, TGSI_SEMANTIC_FOG
, 0);
139 draw_emit_vertex_attr(&vinfo
, EMIT_1F
, src
);
140 vinfo
.hwfmt
[0] |= S4_VFMT_FOG_PARAM
;
143 /* texcoords/varyings */
144 for (i
= 0; i
< I915_TEX_UNITS
; i
++) {
147 hwtc
= TEXCOORDFMT_4D
;
148 src
= draw_find_shader_output(i915
->draw
, TGSI_SEMANTIC_GENERIC
, fs
->generic_mapping
[i
]);
149 draw_emit_vertex_attr(&vinfo
, EMIT_4F
, src
);
152 hwtc
= TEXCOORDFMT_NOT_PRESENT
;
154 vinfo
.hwfmt
[1] |= hwtc
<< (i
* 4);
157 /* front/back face */
159 uint slot
= find_mapping(fs
, I915_SEMANTIC_FACE
);
160 debug_printf("Front/back face is broken\n");
161 /* XXX Because of limitations in the draw module, currently src will be 0
162 * for SEMANTIC_FACE, so this aliases to POS. We need to fix in the draw
163 * module by adding an extra shader output.
165 src
= draw_find_shader_output(i915
->draw
, TGSI_SEMANTIC_FACE
, 0);
166 draw_emit_vertex_attr(&vinfo
, EMIT_1F
, src
);
167 vinfo
.hwfmt
[1] &= ~(TEXCOORDFMT_NOT_PRESENT
<< (slot
* 4));
168 vinfo
.hwfmt
[1] |= TEXCOORDFMT_1D
<< (slot
* 4);
171 draw_compute_vertex_size(&vinfo
);
173 if (memcmp(&i915
->current
.vertex_info
, &vinfo
, sizeof(vinfo
))) {
174 /* Need to set this flag so that the LIS2/4 registers get set.
175 * It also means the i915_update_immediate() function must be called
176 * after this one, in i915_update_derived().
178 i915
->dirty
|= I915_NEW_VERTEX_FORMAT
;
180 memcpy(&i915
->current
.vertex_info
, &vinfo
, sizeof(vinfo
));
184 struct i915_tracked_state i915_update_vertex_layout
= {
186 calculate_vertex_layout
,
187 I915_NEW_RASTERIZER
| I915_NEW_FS
| I915_NEW_VS
192 /***********************************************************************
194 static struct i915_tracked_state
*atoms
[] = {
195 &i915_update_vertex_layout
,
197 &i915_hw_sampler_views
,
201 &i915_hw_framebuffer
,
202 &i915_hw_dst_buf_vars
,
207 void i915_update_derived(struct i915_context
*i915
)
211 if (I915_DBG_ON(DBG_ATOMS
))
212 i915_dump_dirty(i915
, __FUNCTION__
);
214 for (i
= 0; atoms
[i
]; i
++)
215 if (atoms
[i
]->dirty
& i915
->dirty
)
216 atoms
[i
]->update(i915
);