2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
6 Permission is hereby granted, free of charge, to any person obtaining
7 a 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, sublicense, 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
16 portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **********************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
32 #include "main/glheader.h"
33 #include "main/macros.h"
34 #include "main/enums.h"
36 #include "intel_batchbuffer.h"
38 #include "brw_defines.h"
39 #include "brw_context.h"
42 #include "brw_state.h"
45 #include "glsl/ralloc.h"
47 static void compile_gs_prog( struct brw_context
*brw
,
48 struct brw_gs_prog_key
*key
)
50 struct intel_context
*intel
= &brw
->intel
;
51 struct brw_gs_compile c
;
52 const GLuint
*program
;
56 memset(&c
, 0, sizeof(c
));
59 c
.vue_map
= brw
->vs
.prog_data
->vue_map
;
60 c
.nr_regs
= (c
.vue_map
.num_slots
+ 1)/2;
64 /* Begin the compilation:
66 brw_init_compile(brw
, &c
.func
, mem_ctx
);
68 c
.func
.single_program_flow
= 1;
70 /* For some reason the thread is spawned with only 4 channels
73 brw_set_mask_control(&c
.func
, BRW_MASK_DISABLE
);
75 if (intel
->gen
>= 6) {
78 /* On Sandybridge, we use the GS for implementing transform feedback
79 * (called "Stream Out" in the PRM).
81 switch (key
->primitive
) {
82 case _3DPRIM_POINTLIST
:
84 check_edge_flag
= false;
86 case _3DPRIM_LINELIST
:
87 case _3DPRIM_LINESTRIP
:
88 case _3DPRIM_LINELOOP
:
90 check_edge_flag
= false;
94 case _3DPRIM_TRISTRIP
:
95 case _3DPRIM_RECTLIST
:
97 check_edge_flag
= false;
99 case _3DPRIM_QUADLIST
:
100 case _3DPRIM_QUADSTRIP
:
101 case _3DPRIM_POLYGON
:
103 check_edge_flag
= true;
106 assert(!"Unexpected primitive type in Gen6 SOL program.");
109 gen6_sol_program(&c
, key
, num_verts
, check_edge_flag
);
111 /* On Gen4-5, we use the GS to decompose certain types of primitives.
112 * Note that primitives which don't require a GS program have already
113 * been weeded out by now.
115 switch (key
->primitive
) {
116 case _3DPRIM_QUADLIST
:
117 brw_gs_quads( &c
, key
);
119 case _3DPRIM_QUADSTRIP
:
120 brw_gs_quad_strip( &c
, key
);
122 case _3DPRIM_LINELOOP
:
126 ralloc_free(mem_ctx
);
133 program
= brw_get_program(&c
.func
, &program_size
);
135 if (unlikely(INTEL_DEBUG
& DEBUG_GS
)) {
139 for (i
= 0; i
< program_size
/ sizeof(struct brw_instruction
); i
++)
140 brw_disasm(stdout
, &((struct brw_instruction
*)program
)[i
],
145 brw_upload_cache(&brw
->cache
, BRW_GS_PROG
,
146 &c
.key
, sizeof(c
.key
),
147 program
, program_size
,
148 &c
.prog_data
, sizeof(c
.prog_data
),
149 &brw
->gs
.prog_offset
, &brw
->gs
.prog_data
);
150 ralloc_free(mem_ctx
);
153 static void populate_key( struct brw_context
*brw
,
154 struct brw_gs_prog_key
*key
)
156 static const unsigned swizzle_for_offset
[4] = {
157 BRW_SWIZZLE4(0, 1, 2, 3),
158 BRW_SWIZZLE4(1, 2, 3, 3),
159 BRW_SWIZZLE4(2, 3, 3, 3),
160 BRW_SWIZZLE4(3, 3, 3, 3)
163 struct gl_context
*ctx
= &brw
->intel
.ctx
;
164 struct intel_context
*intel
= &brw
->intel
;
166 memset(key
, 0, sizeof(*key
));
168 /* CACHE_NEW_VS_PROG (part of VUE map) */
169 key
->attrs
= brw
->vs
.prog_data
->outputs_written
;
171 /* BRW_NEW_PRIMITIVE */
172 key
->primitive
= brw
->primitive
;
175 key
->pv_first
= (ctx
->Light
.ProvokingVertex
== GL_FIRST_VERTEX_CONVENTION
);
176 if (key
->primitive
== _3DPRIM_QUADLIST
&& ctx
->Light
.ShadeModel
!= GL_FLAT
) {
177 /* Provide consistent primitive order with brw_set_prim's
178 * optimization of single quads to trifans.
180 key
->pv_first
= true;
183 /* CACHE_NEW_VS_PROG (part of VUE map)*/
184 key
->userclip_active
= brw
->vs
.prog_data
->userclip
;
186 if (intel
->gen
>= 7) {
187 /* On Gen7 and later, we don't use GS (yet). */
188 key
->need_gs_prog
= false;
189 } else if (intel
->gen
== 6) {
190 /* On Gen6, GS is used for transform feedback. */
191 /* _NEW_TRANSFORM_FEEDBACK */
192 if (ctx
->TransformFeedback
.CurrentObject
->Active
&&
193 !ctx
->TransformFeedback
.CurrentObject
->Paused
) {
194 const struct gl_shader_program
*shaderprog
=
195 ctx
->Shader
.CurrentVertexProgram
;
196 const struct gl_transform_feedback_info
*linked_xfb_info
=
197 &shaderprog
->LinkedTransformFeedback
;
200 /* Make sure that the VUE slots won't overflow the unsigned chars in
201 * key->transform_feedback_bindings[].
203 STATIC_ASSERT(BRW_VERT_RESULT_MAX
<= 256);
205 /* Make sure that we don't need more binding table entries than we've
206 * set aside for use in transform feedback. (We shouldn't, since we
207 * set aside enough binding table entries to have one per component).
209 assert(linked_xfb_info
->NumOutputs
<= BRW_MAX_SOL_BINDINGS
);
211 key
->need_gs_prog
= true;
212 key
->num_transform_feedback_bindings
= linked_xfb_info
->NumOutputs
;
213 for (i
= 0; i
< key
->num_transform_feedback_bindings
; ++i
) {
214 key
->transform_feedback_bindings
[i
] =
215 linked_xfb_info
->Outputs
[i
].OutputRegister
;
216 key
->transform_feedback_swizzles
[i
] =
217 swizzle_for_offset
[linked_xfb_info
->Outputs
[i
].ComponentOffset
];
220 /* On Gen6, GS is also used for rasterizer discard. */
221 /* _NEW_RASTERIZER_DISCARD */
222 if (ctx
->RasterDiscard
) {
223 key
->need_gs_prog
= true;
224 key
->rasterizer_discard
= true;
227 /* Pre-gen6, GS is used to transform QUADLIST, QUADSTRIP, and LINELOOP
228 * into simpler primitives.
230 key
->need_gs_prog
= (brw
->primitive
== _3DPRIM_QUADLIST
||
231 brw
->primitive
== _3DPRIM_QUADSTRIP
||
232 brw
->primitive
== _3DPRIM_LINELOOP
);
234 /* For testing, the environment variable INTEL_FORCE_GS can be used to
235 * force a GS program to be used, even if it's not necessary.
237 if (getenv("INTEL_FORCE_GS"))
238 key
->need_gs_prog
= true;
241 /* Calculate interpolants for triangle and line rasterization.
244 brw_upload_gs_prog(struct brw_context
*brw
)
246 struct brw_gs_prog_key key
;
249 populate_key(brw
, &key
);
251 if (brw
->gs
.prog_active
!= key
.need_gs_prog
) {
252 brw
->state
.dirty
.cache
|= CACHE_NEW_GS_PROG
;
253 brw
->gs
.prog_active
= key
.need_gs_prog
;
256 if (brw
->gs
.prog_active
) {
257 if (!brw_search_cache(&brw
->cache
, BRW_GS_PROG
,
259 &brw
->gs
.prog_offset
, &brw
->gs
.prog_data
)) {
260 compile_gs_prog( brw
, &key
);
266 const struct brw_tracked_state brw_gs_prog
= {
268 .mesa
= (_NEW_LIGHT
|
269 _NEW_TRANSFORM_FEEDBACK
|
270 _NEW_RASTERIZER_DISCARD
),
271 .brw
= BRW_NEW_PRIMITIVE
,
272 .cache
= CACHE_NEW_VS_PROG
274 .emit
= brw_upload_gs_prog