1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (c) 2016 Etnaviv Project
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sub license,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
12bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
13bf215546Sopenharmony_ci * of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Christian Gmeiner <christian.gmeiner@gmail.com>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "etnaviv_uniforms.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "etnaviv_compiler.h"
30bf215546Sopenharmony_ci#include "etnaviv_context.h"
31bf215546Sopenharmony_ci#include "etnaviv_util.h"
32bf215546Sopenharmony_ci#include "etnaviv_emit.h"
33bf215546Sopenharmony_ci#include "pipe/p_defines.h"
34bf215546Sopenharmony_ci#include "util/u_math.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cistatic unsigned
37bf215546Sopenharmony_ciget_const_idx(const struct etna_context *ctx, bool frag, unsigned samp_id)
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci   struct etna_screen *screen = ctx->screen;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci   if (frag)
42bf215546Sopenharmony_ci      return samp_id;
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   return samp_id + screen->specs.vertex_sampler_offset;
45bf215546Sopenharmony_ci}
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_cistatic uint32_t
48bf215546Sopenharmony_ciget_texrect_scale(const struct etna_context *ctx, bool frag,
49bf215546Sopenharmony_ci                  enum etna_uniform_contents contents, uint32_t data)
50bf215546Sopenharmony_ci{
51bf215546Sopenharmony_ci   unsigned index = get_const_idx(ctx, frag, data);
52bf215546Sopenharmony_ci   struct pipe_sampler_view *texture = ctx->sampler_view[index];
53bf215546Sopenharmony_ci   uint32_t dim;
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci   if (contents == ETNA_UNIFORM_TEXRECT_SCALE_X)
56bf215546Sopenharmony_ci      dim = texture->texture->width0;
57bf215546Sopenharmony_ci   else
58bf215546Sopenharmony_ci      dim = texture->texture->height0;
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   return fui(1.0f / dim);
61bf215546Sopenharmony_ci}
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_civoid
64bf215546Sopenharmony_cietna_uniforms_write(const struct etna_context *ctx,
65bf215546Sopenharmony_ci                    const struct etna_shader_variant *sobj,
66bf215546Sopenharmony_ci                    struct pipe_constant_buffer *cb)
67bf215546Sopenharmony_ci{
68bf215546Sopenharmony_ci   struct etna_screen *screen = ctx->screen;
69bf215546Sopenharmony_ci   struct etna_cmd_stream *stream = ctx->stream;
70bf215546Sopenharmony_ci   const struct etna_shader_uniform_info *uinfo = &sobj->uniforms;
71bf215546Sopenharmony_ci   bool frag = (sobj == ctx->shader.fs);
72bf215546Sopenharmony_ci   uint32_t base = frag ? screen->specs.ps_uniforms_offset : screen->specs.vs_uniforms_offset;
73bf215546Sopenharmony_ci   unsigned idx;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   if (!uinfo->count)
76bf215546Sopenharmony_ci      return;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci   etna_cmd_stream_reserve(stream, align(uinfo->count + 1, 2));
79bf215546Sopenharmony_ci   etna_emit_load_state(stream, base >> 2, uinfo->count, 0);
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   for (uint32_t i = 0; i < uinfo->count; i++) {
82bf215546Sopenharmony_ci      uint32_t val = uinfo->data[i];
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci      switch (uinfo->contents[i]) {
85bf215546Sopenharmony_ci      case ETNA_UNIFORM_CONSTANT:
86bf215546Sopenharmony_ci         etna_cmd_stream_emit(stream, val);
87bf215546Sopenharmony_ci         break;
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci      case ETNA_UNIFORM_UNIFORM:
90bf215546Sopenharmony_ci         assert(cb->user_buffer && val * 4 < cb->buffer_size);
91bf215546Sopenharmony_ci         etna_cmd_stream_emit(stream, ((uint32_t*) cb->user_buffer)[val]);
92bf215546Sopenharmony_ci         break;
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci      case ETNA_UNIFORM_TEXRECT_SCALE_X:
95bf215546Sopenharmony_ci      case ETNA_UNIFORM_TEXRECT_SCALE_Y:
96bf215546Sopenharmony_ci         etna_cmd_stream_emit(stream,
97bf215546Sopenharmony_ci            get_texrect_scale(ctx, frag, uinfo->contents[i], val));
98bf215546Sopenharmony_ci         break;
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci      case ETNA_UNIFORM_UBO0_ADDR ... ETNA_UNIFORM_UBOMAX_ADDR:
101bf215546Sopenharmony_ci         idx = uinfo->contents[i] - ETNA_UNIFORM_UBO0_ADDR;
102bf215546Sopenharmony_ci         etna_cmd_stream_reloc(stream, &(struct etna_reloc) {
103bf215546Sopenharmony_ci            .bo = etna_resource(cb[idx].buffer)->bo,
104bf215546Sopenharmony_ci            .flags = ETNA_RELOC_READ,
105bf215546Sopenharmony_ci            .offset = cb[idx].buffer_offset + val,
106bf215546Sopenharmony_ci         });
107bf215546Sopenharmony_ci         break;
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci      case ETNA_UNIFORM_UNUSED:
110bf215546Sopenharmony_ci         etna_cmd_stream_emit(stream, 0);
111bf215546Sopenharmony_ci         break;
112bf215546Sopenharmony_ci      }
113bf215546Sopenharmony_ci   }
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   if ((uinfo->count % 2) == 0)
116bf215546Sopenharmony_ci      etna_cmd_stream_emit(stream, 0);
117bf215546Sopenharmony_ci}
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_civoid
120bf215546Sopenharmony_cietna_set_shader_uniforms_dirty_flags(struct etna_shader_variant *sobj)
121bf215546Sopenharmony_ci{
122bf215546Sopenharmony_ci   uint32_t dirty = 0;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   for (uint32_t i = 0; i < sobj->uniforms.count; i++) {
125bf215546Sopenharmony_ci      switch (sobj->uniforms.contents[i]) {
126bf215546Sopenharmony_ci      default:
127bf215546Sopenharmony_ci         break;
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci      case ETNA_UNIFORM_TEXRECT_SCALE_X:
130bf215546Sopenharmony_ci      case ETNA_UNIFORM_TEXRECT_SCALE_Y:
131bf215546Sopenharmony_ci         dirty |= ETNA_DIRTY_SAMPLER_VIEWS;
132bf215546Sopenharmony_ci         break;
133bf215546Sopenharmony_ci      }
134bf215546Sopenharmony_ci   }
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci   sobj->uniforms_dirty_bits = dirty;
137bf215546Sopenharmony_ci}
138