1/*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 *    Chia-I Wu <olv@lunarg.com>
26 */
27
28#ifndef ILO_STATE_VF_H
29#define ILO_STATE_VF_H
30
31#include "genhw/genhw.h"
32
33#include "ilo_core.h"
34#include "ilo_dev.h"
35
36/*
37 * From the Sandy Bridge PRM, volume 2 part 1, page 93:
38 *
39 *     "Up to 34 (DevSNB+) vertex elements are supported."
40 *
41 *     "Up to 33 VBs are supported"
42 *
43 * Reserve two VEs and one VB for internal use.
44 */
45#define ILO_STATE_VF_MAX_ELEMENT_COUNT (34 - 2)
46#define ILO_STATE_VF_MAX_BUFFER_COUNT (33 - 1)
47
48enum ilo_state_vf_dirty_bits {
49   ILO_STATE_VF_3DSTATE_VERTEX_ELEMENTS            = (1 << 0),
50   ILO_STATE_VF_3DSTATE_VF_SGVS                    = (1 << 1),
51   ILO_STATE_VF_3DSTATE_VF_INSTANCING              = (1 << 2),
52   ILO_STATE_VF_3DSTATE_VERTEX_BUFFERS             = (1 << 3),
53   ILO_STATE_VF_3DSTATE_VF                         = (1 << 4),
54   ILO_STATE_VF_3DSTATE_INDEX_BUFFER               = (1 << 5),
55};
56
57/**
58 * Fetch a 128-bit vertex attribute.
59 */
60struct ilo_state_vf_element_info {
61   uint8_t buffer;
62   uint16_t vertex_offset;
63   enum gen_surface_format format;
64
65   uint8_t format_size;
66   uint8_t component_count;
67   bool is_integer;
68
69   /* must be the same for those share the same buffer before Gen8 */
70   bool instancing_enable;
71   uint32_t instancing_step_rate;
72};
73
74/**
75 * VF parameters.
76 */
77struct ilo_state_vf_params_info {
78   enum gen_3dprim_type cv_topology;
79
80   /* prepend an attribute of zeros */
81   bool prepend_zeros;
82
83   /* prepend an attribute of VertexID and/or InstanceID */
84   bool prepend_vertexid;
85   bool prepend_instanceid;
86
87   bool last_element_edge_flag;
88
89   enum gen_index_format cv_index_format;
90   bool cut_index_enable;
91   uint32_t cut_index;
92};
93
94/**
95 * Vertex fetch.
96 */
97struct ilo_state_vf_info {
98   void *data;
99   size_t data_size;
100
101   const struct ilo_state_vf_element_info *elements;
102   uint8_t element_count;
103
104   struct ilo_state_vf_params_info params;
105};
106
107struct ilo_state_vf {
108   uint32_t (*user_ve)[2];
109   uint32_t (*user_instancing)[2];
110   int8_t vb_to_first_elem[ILO_STATE_VF_MAX_BUFFER_COUNT];
111   uint8_t user_ve_count;
112
113   bool edge_flag_supported;
114   uint32_t last_user_ve[2][2];
115
116   /* two VEs are reserved for internal use */
117   uint32_t internal_ve[2][2];
118   uint8_t internal_ve_count;
119
120   uint32_t sgvs[1];
121
122   uint32_t cut[2];
123};
124
125struct ilo_state_vf_delta {
126   uint32_t dirty;
127};
128
129struct ilo_vma;
130
131struct ilo_state_vertex_buffer_info {
132   const struct ilo_vma *vma;
133   uint32_t offset;
134   uint32_t size;
135
136   uint16_t stride;
137
138   /* doubles must be at 64-bit aligned addresses */
139   bool cv_has_double;
140   uint8_t cv_double_vertex_offset_mod_8;
141};
142
143struct ilo_state_vertex_buffer {
144   uint32_t vb[3];
145
146   const struct ilo_vma *vma;
147};
148
149struct ilo_state_index_buffer_info {
150   const struct ilo_vma *vma;
151   uint32_t offset;
152   uint32_t size;
153
154   enum gen_index_format format;
155};
156
157struct ilo_state_index_buffer {
158   uint32_t ib[3];
159
160   const struct ilo_vma *vma;
161};
162
163static inline size_t
164ilo_state_vf_data_size(const struct ilo_dev *dev, uint8_t element_count)
165{
166   const struct ilo_state_vf *vf = NULL;
167   return (sizeof(vf->user_ve[0]) +
168           sizeof(vf->user_instancing[0])) * element_count;
169}
170
171bool
172ilo_state_vf_valid_element_format(const struct ilo_dev *dev,
173                                  enum gen_surface_format format);
174
175bool
176ilo_state_vf_init(struct ilo_state_vf *vf,
177                  const struct ilo_dev *dev,
178                  const struct ilo_state_vf_info *info);
179
180bool
181ilo_state_vf_init_for_rectlist(struct ilo_state_vf *vf,
182                               const struct ilo_dev *dev,
183                               void *data, size_t data_size,
184                               const struct ilo_state_vf_element_info *elements,
185                               uint8_t element_count);
186
187bool
188ilo_state_vf_set_params(struct ilo_state_vf *vf,
189                        const struct ilo_dev *dev,
190                        const struct ilo_state_vf_params_info *params);
191
192/**
193 * Return the number of attributes in the VUE.
194 */
195static inline uint8_t
196ilo_state_vf_get_attr_count(const struct ilo_state_vf *vf)
197{
198   return vf->internal_ve_count + vf->user_ve_count;
199}
200
201void
202ilo_state_vf_full_delta(const struct ilo_state_vf *vf,
203                        const struct ilo_dev *dev,
204                        struct ilo_state_vf_delta *delta);
205
206void
207ilo_state_vf_get_delta(const struct ilo_state_vf *vf,
208                       const struct ilo_dev *dev,
209                       const struct ilo_state_vf *old,
210                       struct ilo_state_vf_delta *delta);
211
212uint32_t
213ilo_state_vertex_buffer_size(const struct ilo_dev *dev, uint32_t size,
214                             uint32_t *alignment);
215
216bool
217ilo_state_vertex_buffer_set_info(struct ilo_state_vertex_buffer *vb,
218                                 const struct ilo_dev *dev,
219                                 const struct ilo_state_vertex_buffer_info *info);
220
221uint32_t
222ilo_state_index_buffer_size(const struct ilo_dev *dev, uint32_t size,
223                            uint32_t *alignment);
224
225bool
226ilo_state_index_buffer_set_info(struct ilo_state_index_buffer *ib,
227                                const struct ilo_dev *dev,
228                                const struct ilo_state_index_buffer_info *info);
229
230#endif /* ILO_STATE_VF_H */
231