1 /*
2  Copyright (C) Intel Corp.  2006.  All Rights Reserved.
3  Intel funded Tungsten Graphics to
4  develop this 3D driver.
5 
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:
13 
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.
17 
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.
25 
26  **********************************************************************/
27  /*
28   * Authors:
29   *   Keith Whitwell <keithw@vmware.com>
30   */
31 
32 #include "compiler/nir/nir.h"
33 #include "main/macros.h"
34 #include "main/mtypes.h"
35 #include "main/enums.h"
36 #include "main/fbobject.h"
37 #include "main/state.h"
38 
39 #include "brw_batch.h"
40 
41 #include "brw_defines.h"
42 #include "brw_context.h"
43 #include "brw_util.h"
44 #include "brw_state.h"
45 #include "compiler/brw_eu.h"
46 
47 #include "util/ralloc.h"
48 
49 static void
compile_sf_prog(struct brw_context * brw,struct brw_sf_prog_key * key)50 compile_sf_prog(struct brw_context *brw, struct brw_sf_prog_key *key)
51 {
52    const unsigned *program;
53    void *mem_ctx;
54    unsigned program_size;
55 
56    mem_ctx = ralloc_context(NULL);
57 
58    struct brw_sf_prog_data prog_data;
59    program = brw_compile_sf(brw->screen->compiler, mem_ctx, key, &prog_data,
60                             &brw->vue_map_geom_out, &program_size);
61 
62    brw_upload_cache(&brw->cache, BRW_CACHE_SF_PROG,
63                     key, sizeof(*key),
64                     program, program_size,
65                     &prog_data, sizeof(prog_data),
66                     &brw->sf.prog_offset, &brw->sf.prog_data);
67    ralloc_free(mem_ctx);
68 }
69 
70 /* Calculate interpolants for triangle and line rasterization.
71  */
72 void
brw_upload_sf_prog(struct brw_context * brw)73 brw_upload_sf_prog(struct brw_context *brw)
74 {
75    struct gl_context *ctx = &brw->ctx;
76    struct brw_sf_prog_key key;
77 
78    if (!brw_state_dirty(brw,
79                         _NEW_BUFFERS |
80                         _NEW_HINT |
81                         _NEW_LIGHT |
82                         _NEW_POINT |
83                         _NEW_POLYGON |
84                         _NEW_PROGRAM |
85                         _NEW_TRANSFORM,
86                         BRW_NEW_BLORP |
87                         BRW_NEW_FS_PROG_DATA |
88                         BRW_NEW_REDUCED_PRIMITIVE |
89                         BRW_NEW_VUE_MAP_GEOM_OUT))
90       return;
91 
92    /* _NEW_BUFFERS */
93    bool flip_y = ctx->DrawBuffer->FlipY;
94 
95    memset(&key, 0, sizeof(key));
96 
97    /* Populate the key, noting state dependencies:
98     */
99    /* BRW_NEW_VUE_MAP_GEOM_OUT */
100    key.attrs = brw->vue_map_geom_out.slots_valid;
101 
102    /* BRW_NEW_REDUCED_PRIMITIVE */
103    switch (brw->reduced_primitive) {
104    case GL_TRIANGLES:
105       /* NOTE: We just use the edgeflag attribute as an indicator that
106        * unfilled triangles are active.  We don't actually do the
107        * edgeflag testing here, it is already done in the clip
108        * program.
109        */
110       if (key.attrs & BITFIELD64_BIT(VARYING_SLOT_EDGE))
111          key.primitive = BRW_SF_PRIM_UNFILLED_TRIS;
112       else
113          key.primitive = BRW_SF_PRIM_TRIANGLES;
114       break;
115    case GL_LINES:
116       key.primitive = BRW_SF_PRIM_LINES;
117       break;
118    case GL_POINTS:
119       key.primitive = BRW_SF_PRIM_POINTS;
120       break;
121    }
122 
123    /* _NEW_TRANSFORM */
124    key.userclip_active = (ctx->Transform.ClipPlanesEnabled != 0);
125 
126    /* _NEW_POINT */
127    key.do_point_sprite = ctx->Point.PointSprite;
128    if (key.do_point_sprite) {
129       key.point_sprite_coord_replace = ctx->Point.CoordReplace & 0xff;
130    }
131    if (brw->programs[MESA_SHADER_FRAGMENT]->info.inputs_read &
132        BITFIELD64_BIT(VARYING_SLOT_PNTC)) {
133       key.do_point_coord = 1;
134    }
135 
136    /*
137     * Window coordinates in a FBO are inverted, which means point
138     * sprite origin must be inverted, too.
139     */
140    if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) == flip_y)
141       key.sprite_origin_lower_left = true;
142 
143    /* BRW_NEW_FS_PROG_DATA */
144    const struct brw_wm_prog_data *wm_prog_data =
145       brw_wm_prog_data(brw->wm.base.prog_data);
146    if (wm_prog_data) {
147       key.contains_flat_varying = wm_prog_data->contains_flat_varying;
148 
149       STATIC_ASSERT(sizeof(key.interp_mode) ==
150                     sizeof(wm_prog_data->interp_mode));
151       memcpy(key.interp_mode, wm_prog_data->interp_mode,
152              sizeof(key.interp_mode));
153    }
154 
155    /* _NEW_LIGHT | _NEW_PROGRAM */
156    key.do_twoside_color = _mesa_vertex_program_two_side_enabled(ctx);
157 
158    /* _NEW_POLYGON */
159    if (key.do_twoside_color) {
160       /* If we're rendering to a FBO, we have to invert the polygon
161        * face orientation, just as we invert the viewport in
162        * sf_unit_create_from_key().
163        */
164       key.frontface_ccw = brw->polygon_front_bit != flip_y;
165    }
166 
167    if (!brw_search_cache(&brw->cache, BRW_CACHE_SF_PROG, &key, sizeof(key),
168                          &brw->sf.prog_offset, &brw->sf.prog_data, true)) {
169       compile_sf_prog( brw, &key );
170    }
171 }
172