2 * Copyright (c) 2016 Etnaviv Project
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:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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.
24 * Christian Gmeiner <christian.gmeiner@gmail.com>
27 #include "etnaviv_uniforms.h"
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"
37 get_const_idx(const struct etna_context
*ctx
, bool frag
, unsigned samp_id
)
39 struct etna_screen
*screen
= ctx
->screen
;
44 return samp_id
+ screen
->specs
.vertex_sampler_offset
;
48 get_texrect_scale(const struct etna_context
*ctx
, bool frag
,
49 enum etna_immediate_contents contents
, uint32_t data
)
51 unsigned index
= get_const_idx(ctx
, frag
, data
);
52 struct pipe_sampler_view
*texture
= ctx
->sampler_view
[index
];
55 if (contents
== ETNA_IMMEDIATE_TEXRECT_SCALE_X
)
56 dim
= texture
->texture
->width0
;
58 dim
= texture
->texture
->height0
;
60 return fui(1.0f
/ dim
);
64 etna_uniforms_write(const struct etna_context
*ctx
,
65 const struct etna_shader_variant
*sobj
,
66 struct pipe_constant_buffer
*cb
)
68 struct etna_screen
*screen
= ctx
->screen
;
69 struct etna_cmd_stream
*stream
= ctx
->stream
;
70 const struct etna_shader_uniform_info
*uinfo
= &sobj
->uniforms
;
71 bool frag
= (sobj
== ctx
->shader
.fs
);
72 uint32_t base
= frag
? screen
->specs
.ps_uniforms_offset
: screen
->specs
.vs_uniforms_offset
;
75 if (!uinfo
->imm_count
)
78 etna_cmd_stream_reserve(stream
, align(uinfo
->imm_count
+ 1, 2));
79 etna_emit_load_state(stream
, base
>> 2, uinfo
->imm_count
, 0);
81 for (uint32_t i
= 0; i
< uinfo
->imm_count
; i
++) {
82 uint32_t val
= uinfo
->imm_data
[i
];
84 switch (uinfo
->imm_contents
[i
]) {
85 case ETNA_IMMEDIATE_CONSTANT
:
86 etna_cmd_stream_emit(stream
, val
);
89 case ETNA_IMMEDIATE_UNIFORM
:
90 assert(cb
->user_buffer
&& val
* 4 < cb
->buffer_size
);
91 etna_cmd_stream_emit(stream
, ((uint32_t*) cb
->user_buffer
)[val
]);
94 case ETNA_IMMEDIATE_TEXRECT_SCALE_X
:
95 case ETNA_IMMEDIATE_TEXRECT_SCALE_Y
:
96 etna_cmd_stream_emit(stream
,
97 get_texrect_scale(ctx
, frag
, uinfo
->imm_contents
[i
], val
));
100 case ETNA_IMMEDIATE_UBO0_ADDR
... ETNA_IMMEDIATE_UBOMAX_ADDR
:
101 idx
= uinfo
->imm_contents
[i
] - ETNA_IMMEDIATE_UBO0_ADDR
;
102 etna_cmd_stream_reloc(stream
, &(struct etna_reloc
) {
103 .bo
= etna_resource(cb
[idx
].buffer
)->bo
,
104 .flags
= ETNA_RELOC_READ
,
105 .offset
= cb
[idx
].buffer_offset
+ val
,
109 case ETNA_IMMEDIATE_UNUSED
:
110 etna_cmd_stream_emit(stream
, 0);
115 if ((uinfo
->imm_count
% 2) == 0)
116 etna_cmd_stream_emit(stream
, 0);
120 etna_set_shader_uniforms_dirty_flags(struct etna_shader_variant
*sobj
)
124 for (uint32_t i
= 0; i
< sobj
->uniforms
.imm_count
; i
++) {
125 switch (sobj
->uniforms
.imm_contents
[i
]) {
129 case ETNA_IMMEDIATE_TEXRECT_SCALE_X
:
130 case ETNA_IMMEDIATE_TEXRECT_SCALE_Y
:
131 dirty
|= ETNA_DIRTY_SAMPLER_VIEWS
;
136 sobj
->uniforms_dirty_bits
= dirty
;