aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/etnaviv/etnaviv_emit.h
blob: b4670eeb8f53227294ccee673d58cb87284ac9d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * Copyright (c) 2012-2015 Etnaviv Project
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sub license,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Wladimir J. van der Laan <laanwj@gmail.com>
 */

#ifndef H_ETNA_EMIT
#define H_ETNA_EMIT

#include "etnaviv_screen.h"
#include "etnaviv_util.h"
#include "hw/cmdstream.xml.h"

struct etna_context;
struct compiled_rs_state;

struct etna_coalesce {
   uint32_t start;
   uint32_t last_reg;
   uint32_t last_fixp;
};

static inline void
etna_emit_load_state(struct etna_cmd_stream *stream, const uint16_t offset,
                     const uint16_t count, const int fixp)
{
   uint32_t v;

   v = VIV_FE_LOAD_STATE_HEADER_OP_LOAD_STATE |
       COND(fixp, VIV_FE_LOAD_STATE_HEADER_FIXP) |
       VIV_FE_LOAD_STATE_HEADER_OFFSET(offset) |
       (VIV_FE_LOAD_STATE_HEADER_COUNT(count) &
        VIV_FE_LOAD_STATE_HEADER_COUNT__MASK);

   etna_cmd_stream_emit(stream, v);
}

static inline void
etna_set_state(struct etna_cmd_stream *stream, uint32_t address, uint32_t value)
{
   etna_cmd_stream_reserve(stream, 2);
   etna_emit_load_state(stream, address >> 2, 1, 0);
   etna_cmd_stream_emit(stream, value);
}

static inline void
etna_set_state_reloc(struct etna_cmd_stream *stream, uint32_t address,
                     const struct etna_reloc *reloc)
{
   etna_cmd_stream_reserve(stream, 2);
   etna_emit_load_state(stream, address >> 2, 1, 0);
   etna_cmd_stream_reloc(stream, reloc);
}

static inline void
etna_set_state_multi(struct etna_cmd_stream *stream, uint32_t base,
                     uint32_t num, const uint32_t *values)
{
   if (num == 0)
      return;

   etna_cmd_stream_reserve(stream, 1 + num + 1); /* 1 extra for potential alignment */
   etna_emit_load_state(stream, base >> 2, num, 0);

   for (uint32_t i = 0; i < num; i++)
      etna_cmd_stream_emit(stream, values[i]);

   /* add potential padding */
   if ((num % 2) == 0)
      etna_cmd_stream_emit(stream, 0);
}

void
etna_stall(struct etna_cmd_stream *stream, uint32_t from, uint32_t to);

static inline void
etna_draw_primitives(struct etna_cmd_stream *stream, uint32_t primitive_type,
                     uint32_t start, uint32_t count)
{
   etna_cmd_stream_reserve(stream, 4);

   etna_cmd_stream_emit(stream, VIV_FE_DRAW_PRIMITIVES_HEADER_OP_DRAW_PRIMITIVES);
   etna_cmd_stream_emit(stream, primitive_type);
   etna_cmd_stream_emit(stream, start);
   etna_cmd_stream_emit(stream, count);
}

static inline void
etna_draw_indexed_primitives(struct etna_cmd_stream *stream,
                             uint32_t primitive_type, uint32_t start,
                             uint32_t count, uint32_t offset)
{
   etna_cmd_stream_reserve(stream, 5 + 1);

   etna_cmd_stream_emit(stream, VIV_FE_DRAW_INDEXED_PRIMITIVES_HEADER_OP_DRAW_INDEXED_PRIMITIVES);
   etna_cmd_stream_emit(stream, primitive_type);
   etna_cmd_stream_emit(stream, start);
   etna_cmd_stream_emit(stream, count);
   etna_cmd_stream_emit(stream, offset);
   etna_cmd_stream_emit(stream, 0);
}

/* important: this takes a vertex count, not a primitive count */
static inline void
etna_draw_instanced(struct etna_cmd_stream *stream,
                    uint32_t indexed, uint32_t primitive_type,
                    uint32_t instance_count,
                    uint32_t vertex_count, uint32_t offset)
{
   etna_cmd_stream_reserve(stream, 3 + 1);
   etna_cmd_stream_emit(stream,
      VIV_FE_DRAW_INSTANCED_HEADER_OP_DRAW_INSTANCED |
      COND(indexed, VIV_FE_DRAW_INSTANCED_HEADER_INDEXED) |
      VIV_FE_DRAW_INSTANCED_HEADER_TYPE(primitive_type) |
      VIV_FE_DRAW_INSTANCED_HEADER_INSTANCE_COUNT_LO(instance_count & 0xffff));
   etna_cmd_stream_emit(stream,
      VIV_FE_DRAW_INSTANCED_COUNT_INSTANCE_COUNT_HI(instance_count >> 16) |
      VIV_FE_DRAW_INSTANCED_COUNT_VERTEX_COUNT(vertex_count));
   etna_cmd_stream_emit(stream,
      VIV_FE_DRAW_INSTANCED_START_INDEX(offset));
   etna_cmd_stream_emit(stream, 0);
}

static inline void
etna_coalesce_start(struct etna_cmd_stream *stream,
                    struct etna_coalesce *coalesce)
{
   coalesce->start = etna_cmd_stream_offset(stream);
   coalesce->last_reg = 0;
   coalesce->last_fixp = 0;
}

static inline void
etna_coalesce_end(struct etna_cmd_stream *stream,
                  struct etna_coalesce *coalesce)
{
   uint32_t end = etna_cmd_stream_offset(stream);
   uint32_t size = end - coalesce->start;

   if (size) {
      uint32_t offset = coalesce->start - 1;
      uint32_t value = etna_cmd_stream_get(stream, offset);

      value |= VIV_FE_LOAD_STATE_HEADER_COUNT(size);
      etna_cmd_stream_set(stream, offset, value);
   }

   /* append needed padding */
   if (end % 2 == 1)
      etna_cmd_stream_emit(stream, 0xdeadbeef);
}

static inline void
check_coalsence(struct etna_cmd_stream *stream, struct etna_coalesce *coalesce,
                uint32_t reg, uint32_t fixp)
{
   if (coalesce->last_reg != 0) {
      if (((coalesce->last_reg + 4) != reg) || (coalesce->last_fixp != fixp)) {
         etna_coalesce_end(stream, coalesce);
         etna_emit_load_state(stream, reg >> 2, 0, fixp);
         coalesce->start = etna_cmd_stream_offset(stream);
      }
   } else {
      etna_emit_load_state(stream, reg >> 2, 0, fixp);
      coalesce->start = etna_cmd_stream_offset(stream);
   }

   coalesce->last_reg = reg;
   coalesce->last_fixp = fixp;
}

static inline void
etna_coalsence_emit(struct etna_cmd_stream *stream,
                    struct etna_coalesce *coalesce, uint32_t reg,
                    uint32_t value)
{
   check_coalsence(stream, coalesce, reg, 0);
   etna_cmd_stream_emit(stream, value);
}

static inline void
etna_coalsence_emit_fixp(struct etna_cmd_stream *stream,
                         struct etna_coalesce *coalesce, uint32_t reg,
                         uint32_t value)
{
   check_coalsence(stream, coalesce, reg, 1);
   etna_cmd_stream_emit(stream, value);
}

static inline void
etna_coalsence_emit_reloc(struct etna_cmd_stream *stream,
                          struct etna_coalesce *coalesce, uint32_t reg,
                          const struct etna_reloc *r)
{
   if (r->bo) {
      check_coalsence(stream, coalesce, reg, 0);
      etna_cmd_stream_reloc(stream, r);
   }
}

void
etna_emit_state(struct etna_context *ctx);

#endif