nouveau: Add support for ARB_sampler_objects
[mesa.git] / src / mesa / drivers / dri / i965 / brw_lower_texture_gradients.cpp
1 /*
2 * Copyright © 2012 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file brw_lower_texture_gradients.cpp
26 */
27
28 #include "glsl/ir.h"
29 #include "glsl/ir_builder.h"
30
31 using namespace ir_builder;
32
33 class lower_texture_grad_visitor : public ir_hierarchical_visitor {
34 public:
35 lower_texture_grad_visitor()
36 {
37 progress = false;
38 }
39
40 ir_visitor_status visit_leave(ir_texture *ir);
41
42
43 bool progress;
44
45 private:
46 void emit(ir_variable *, ir_rvalue *);
47 };
48
49 /**
50 * Emit a variable declaration and an assignment to initialize it.
51 */
52 void
53 lower_texture_grad_visitor::emit(ir_variable *var, ir_rvalue *value)
54 {
55 base_ir->insert_before(var);
56 base_ir->insert_before(assign(var, value));
57 }
58
59 static const glsl_type *
60 txs_type(const glsl_type *type)
61 {
62 unsigned dims;
63 switch (type->sampler_dimensionality) {
64 case GLSL_SAMPLER_DIM_1D:
65 dims = 1;
66 break;
67 case GLSL_SAMPLER_DIM_2D:
68 case GLSL_SAMPLER_DIM_RECT:
69 case GLSL_SAMPLER_DIM_CUBE:
70 dims = 2;
71 break;
72 case GLSL_SAMPLER_DIM_3D:
73 dims = 3;
74 break;
75 default:
76 assert(!"Should not get here: invalid sampler dimensionality");
77 }
78
79 if (type->sampler_array)
80 dims++;
81
82 return glsl_type::get_instance(GLSL_TYPE_INT, dims, 1);
83 }
84
85 ir_visitor_status
86 lower_texture_grad_visitor::visit_leave(ir_texture *ir)
87 {
88 /* Only lower textureGrad with shadow samplers */
89 if (ir->op != ir_txd || !ir->shadow_comparitor)
90 return visit_continue;
91
92 /* Cubes are broken. Avoid assertion failures when swizzling. */
93 if (ir->sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE)
94 return visit_continue;
95
96 void *mem_ctx = ralloc_parent(ir);
97
98 const glsl_type *grad_type = ir->lod_info.grad.dPdx->type;
99
100 /* Use textureSize() to get the width and height of LOD 0; swizzle away
101 * the depth/number of array slices.
102 */
103 ir_texture *txs = new(mem_ctx) ir_texture(ir_txs);
104 txs->set_sampler(ir->sampler->clone(mem_ctx, NULL),
105 txs_type(ir->sampler->type));
106 txs->lod_info.lod = new(mem_ctx) ir_constant(0);
107 ir_variable *size =
108 new(mem_ctx) ir_variable(grad_type, "size", ir_var_temporary);
109 emit(size, expr(ir_unop_i2f,
110 swizzle_for_size(txs, grad_type->vector_elements)));
111
112 /* Scale the gradients by width and height. Effectively, the incoming
113 * gradients are s'(x,y), t'(x,y), and r'(x,y) from equation 3.19 in the
114 * GL 3.0 spec; we want u'(x,y), which is w_t * s'(x,y).
115 */
116 ir_variable *dPdx =
117 new(mem_ctx) ir_variable(grad_type, "dPdx", ir_var_temporary);
118 emit(dPdx, mul(size, ir->lod_info.grad.dPdx));
119
120 ir_variable *dPdy =
121 new(mem_ctx) ir_variable(grad_type, "dPdy", ir_var_temporary);
122 emit(dPdy, mul(size, ir->lod_info.grad.dPdy));
123
124 /* Calculate rho from equation 3.20 of the GL 3.0 specification. */
125 ir_rvalue *rho;
126 if (dPdx->type->is_scalar()) {
127 rho = expr(ir_binop_max, expr(ir_unop_abs, dPdx),
128 expr(ir_unop_abs, dPdy));
129 } else {
130 rho = expr(ir_binop_max, expr(ir_unop_sqrt, dot(dPdx, dPdx)),
131 expr(ir_unop_sqrt, dot(dPdy, dPdy)));
132 }
133
134 /* lambda_base = log2(rho). We're ignoring GL state biases for now. */
135 ir->op = ir_txl;
136 ir->lod_info.lod = expr(ir_unop_log2, rho);
137
138 progress = true;
139 return visit_continue;
140 }
141
142 extern "C" {
143
144 bool
145 brw_lower_texture_gradients(struct exec_list *instructions)
146 {
147 lower_texture_grad_visitor v;
148
149 visit_list_elements(&v, instructions);
150
151 return v.progress;
152 }
153
154 }