• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2015 Red Hat
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include "nir.h"
25 #include "nir_builder.h"
26 
27 /* Lower glBitmap().
28  *
29  * This is based on the logic in st_get_bitmap_shader() in TGSI compiler.
30  * From st_cb_bitmap.c:
31  *
32  *    glBitmaps are drawn as textured quads.  The user's bitmap pattern
33  *    is stored in a texture image.  An alpha8 texture format is used.
34  *    The fragment shader samples a bit (texel) from the texture, then
35  *    discards the fragment if the bit is off.
36  *
37  *    Note that we actually store the inverse image of the bitmap to
38  *    simplify the fragment program.  An "on" bit gets stored as texel=0x0
39  *    and an "off" bit is stored as texel=0xff.  Then we kill the
40  *    fragment if the negated texel value is less than zero.
41  *
42  * Note that the texture format will be, according to what driver supports,
43  * in order of preference (with swizzle):
44  *
45  *    I8_UNORM - .xxxx
46  *    A8_UNORM - .000x
47  *    L8_UNORM - .xxx1
48  *
49  * If L8_UNORM, options->swizzle_xxxx is true.  Otherwise we can just use
50  * the .w comp.
51  */
52 
53 static void
lower_bitmap(nir_shader * shader,nir_builder * b,const nir_lower_bitmap_options * options)54 lower_bitmap(nir_shader *shader, nir_builder *b,
55              const nir_lower_bitmap_options *options)
56 {
57    nir_def *texcoord;
58    nir_tex_instr *tex;
59    nir_def *cond;
60 
61    if (shader->info.io_lowered) {
62       nir_def *baryc =
63          nir_load_barycentric_pixel(b, 32, .interp_mode = INTERP_MODE_SMOOTH);
64       texcoord = nir_load_interpolated_input(b, 4, 32, baryc, nir_imm_int(b, 0),
65                                              .io_semantics.location = VARYING_SLOT_TEX0);
66    } else {
67       nir_variable *var =
68          nir_get_variable_with_location(shader, nir_var_shader_in,
69                                         VARYING_SLOT_TEX0, glsl_vec4_type());
70       texcoord = nir_load_var(b, var);
71    }
72 
73    const struct glsl_type *sampler2D =
74       glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT);
75 
76    nir_variable *tex_var =
77       nir_variable_create(shader, nir_var_uniform, sampler2D, "bitmap_tex");
78    tex_var->data.binding = options->sampler;
79    tex_var->data.explicit_binding = true;
80    tex_var->data.how_declared = nir_var_hidden;
81 
82    nir_deref_instr *tex_deref = nir_build_deref_var(b, tex_var);
83 
84    tex = nir_tex_instr_create(shader, 3);
85    tex->op = nir_texop_tex;
86    tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
87    tex->coord_components = 2;
88    tex->dest_type = nir_type_float32;
89    tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_deref,
90                                      &tex_deref->def);
91    tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_sampler_deref,
92                                      &tex_deref->def);
93    tex->src[2] = nir_tex_src_for_ssa(nir_tex_src_coord,
94                                      nir_trim_vector(b, texcoord, tex->coord_components));
95 
96    nir_def_init(&tex->instr, &tex->def, 4, 32);
97    nir_builder_instr_insert(b, &tex->instr);
98 
99    /* kill if tex != 0.0.. take .x or .w channel according to format: */
100    cond = nir_fneu_imm(b, nir_channel(b, &tex->def, options->swizzle_xxxx ? 0 : 3),
101                        0.0);
102 
103    nir_discard_if(b, cond);
104 
105    shader->info.fs.uses_discard = true;
106 }
107 
108 static void
lower_bitmap_impl(nir_function_impl * impl,const nir_lower_bitmap_options * options)109 lower_bitmap_impl(nir_function_impl *impl,
110                   const nir_lower_bitmap_options *options)
111 {
112    nir_builder b = nir_builder_at(nir_before_impl(impl));
113 
114    lower_bitmap(impl->function->shader, &b, options);
115 
116    nir_metadata_preserve(impl, nir_metadata_control_flow);
117 }
118 
119 bool
nir_lower_bitmap(nir_shader * shader,const nir_lower_bitmap_options * options)120 nir_lower_bitmap(nir_shader *shader,
121                  const nir_lower_bitmap_options *options)
122 {
123    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
124 
125    lower_bitmap_impl(nir_shader_get_entrypoint(shader), options);
126    return true;
127 }
128