etnaviv: update Android build files
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_uniforms.c
1 /*
2 * Copyright (c) 2016 Etnaviv Project
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 NON-INFRINGEMENT. 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 * Authors:
24 * Christian Gmeiner <christian.gmeiner@gmail.com>
25 */
26
27 #include "etnaviv_uniforms.h"
28
29 #include "etnaviv_compiler.h"
30 #include "etnaviv_context.h"
31 #include "etnaviv_util.h"
32 #include "etnaviv_emit.h"
33 #include "pipe/p_defines.h"
34 #include "util/u_math.h"
35
36 static unsigned
37 get_const_idx(const struct etna_context *ctx, bool frag, unsigned samp_id)
38 {
39 if (frag)
40 return samp_id;
41
42 return samp_id + ctx->specs.vertex_sampler_offset;
43 }
44
45 static uint32_t
46 get_texrect_scale(const struct etna_context *ctx, bool frag,
47 enum etna_immediate_contents contents, uint32_t data)
48 {
49 unsigned index = get_const_idx(ctx, frag, data);
50 struct pipe_sampler_view *texture = ctx->sampler_view[index];
51 uint32_t dim;
52
53 if (contents == ETNA_IMMEDIATE_TEXRECT_SCALE_X)
54 dim = texture->texture->width0;
55 else
56 dim = texture->texture->height0;
57
58 return fui(1.0f / dim);
59 }
60
61 void
62 etna_uniforms_write(const struct etna_context *ctx,
63 const struct etna_shader_variant *sobj,
64 struct pipe_constant_buffer *cb)
65 {
66 struct etna_cmd_stream *stream = ctx->stream;
67 const struct etna_shader_uniform_info *uinfo = &sobj->uniforms;
68 bool frag = (sobj == ctx->shader.fs);
69 uint32_t base = frag ? ctx->specs.ps_uniforms_offset : ctx->specs.vs_uniforms_offset;
70
71 if (!uinfo->imm_count)
72 return;
73
74 etna_cmd_stream_reserve(stream, align(uinfo->imm_count + 1, 2));
75 etna_emit_load_state(stream, base >> 2, uinfo->imm_count, 0);
76
77 for (uint32_t i = 0; i < uinfo->imm_count; i++) {
78 uint32_t val = uinfo->imm_data[i];
79
80 switch (uinfo->imm_contents[i]) {
81 case ETNA_IMMEDIATE_CONSTANT:
82 etna_cmd_stream_emit(stream, val);
83 break;
84
85 case ETNA_IMMEDIATE_UNIFORM:
86 assert(cb->user_buffer && val * 4 < cb->buffer_size);
87 etna_cmd_stream_emit(stream, ((uint32_t*) cb->user_buffer)[val]);
88 break;
89
90 case ETNA_IMMEDIATE_TEXRECT_SCALE_X:
91 case ETNA_IMMEDIATE_TEXRECT_SCALE_Y:
92 etna_cmd_stream_emit(stream,
93 get_texrect_scale(ctx, frag, uinfo->imm_contents[i], val));
94 break;
95
96 case ETNA_IMMEDIATE_UBO0_ADDR ... ETNA_IMMEDIATE_UBOMAX_ADDR:
97 assert(uinfo->imm_contents[i] == ETNA_IMMEDIATE_UBO0_ADDR);
98 etna_cmd_stream_reloc(stream, &(struct etna_reloc) {
99 .bo = etna_resource(cb->buffer)->bo,
100 .flags = ETNA_RELOC_READ,
101 .offset = cb->buffer_offset + val,
102 });
103 break;
104
105 case ETNA_IMMEDIATE_UNUSED:
106 etna_cmd_stream_emit(stream, 0);
107 break;
108 }
109 }
110
111 if ((uinfo->imm_count % 2) == 0)
112 etna_cmd_stream_emit(stream, 0);
113 }
114
115 void
116 etna_set_shader_uniforms_dirty_flags(struct etna_shader_variant *sobj)
117 {
118 uint32_t dirty = 0;
119
120 for (uint32_t i = 0; i < sobj->uniforms.imm_count; i++) {
121 switch (sobj->uniforms.imm_contents[i]) {
122 default:
123 break;
124
125 case ETNA_IMMEDIATE_TEXRECT_SCALE_X:
126 case ETNA_IMMEDIATE_TEXRECT_SCALE_Y:
127 dirty |= ETNA_DIRTY_SAMPLER_VIEWS;
128 break;
129 }
130 }
131
132 sobj->uniforms_dirty_bits = dirty;
133 }