gallium: decrease the size of pipe_vertex_buffer - 24 -> 16 bytes
[mesa.git] / src / gallium / auxiliary / util / u_helpers.c
1 /**************************************************************************
2 *
3 * Copyright 2012 Marek Olšák <maraeo@gmail.com>
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS AND/OR THEIR SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/u_helpers.h"
29 #include "util/u_inlines.h"
30 #include "util/u_upload_mgr.h"
31 #include <inttypes.h>
32
33 /**
34 * This function is used to copy an array of pipe_vertex_buffer structures,
35 * while properly referencing the pipe_vertex_buffer::buffer member.
36 *
37 * enabled_buffers is updated such that the bits corresponding to the indices
38 * of disabled buffers are set to 0 and the enabled ones are set to 1.
39 *
40 * \sa util_copy_framebuffer_state
41 */
42 void util_set_vertex_buffers_mask(struct pipe_vertex_buffer *dst,
43 uint32_t *enabled_buffers,
44 const struct pipe_vertex_buffer *src,
45 unsigned start_slot, unsigned count)
46 {
47 unsigned i;
48 uint32_t bitmask = 0;
49
50 dst += start_slot;
51
52 if (src) {
53 for (i = 0; i < count; i++) {
54 if (src[i].buffer.resource)
55 bitmask |= 1 << i;
56
57 pipe_vertex_buffer_unreference(&dst[i]);
58
59 if (!src[i].is_user_buffer)
60 pipe_resource_reference(&dst[i].buffer.resource, src[i].buffer.resource);
61 }
62
63 /* Copy over the other members of pipe_vertex_buffer. */
64 memcpy(dst, src, count * sizeof(struct pipe_vertex_buffer));
65
66 *enabled_buffers &= ~(((1ull << count) - 1) << start_slot);
67 *enabled_buffers |= bitmask << start_slot;
68 }
69 else {
70 /* Unreference the buffers. */
71 for (i = 0; i < count; i++)
72 pipe_vertex_buffer_unreference(&dst[i]);
73
74 *enabled_buffers &= ~(((1ull << count) - 1) << start_slot);
75 }
76 }
77
78 /**
79 * Same as util_set_vertex_buffers_mask, but it only returns the number
80 * of bound buffers.
81 */
82 void util_set_vertex_buffers_count(struct pipe_vertex_buffer *dst,
83 unsigned *dst_count,
84 const struct pipe_vertex_buffer *src,
85 unsigned start_slot, unsigned count)
86 {
87 unsigned i;
88 uint32_t enabled_buffers = 0;
89
90 for (i = 0; i < *dst_count; i++) {
91 if (dst[i].buffer.resource)
92 enabled_buffers |= (1ull << i);
93 }
94
95 util_set_vertex_buffers_mask(dst, &enabled_buffers, src, start_slot,
96 count);
97
98 *dst_count = util_last_bit(enabled_buffers);
99 }
100
101
102 void
103 util_set_index_buffer(struct pipe_index_buffer *dst,
104 const struct pipe_index_buffer *src)
105 {
106 if (src) {
107 pipe_resource_reference(&dst->buffer, src->buffer);
108 memcpy(dst, src, sizeof(*dst));
109 }
110 else {
111 pipe_resource_reference(&dst->buffer, NULL);
112 memset(dst, 0, sizeof(*dst));
113 }
114 }
115
116 /**
117 * Given a user index buffer, save the structure to "saved", and upload it.
118 */
119 bool
120 util_save_and_upload_index_buffer(struct pipe_context *pipe,
121 const struct pipe_draw_info *info,
122 const struct pipe_index_buffer *ib,
123 struct pipe_index_buffer *out_saved)
124 {
125 struct pipe_index_buffer new_ib = {0};
126 unsigned start_offset = info->start * ib->index_size;
127
128 u_upload_data(pipe->stream_uploader, start_offset,
129 info->count * ib->index_size, 4,
130 (char*)ib->user_buffer + start_offset,
131 &new_ib.offset, &new_ib.buffer);
132 if (!new_ib.buffer)
133 return false;
134 u_upload_unmap(pipe->stream_uploader);
135
136 new_ib.offset -= start_offset;
137 new_ib.index_size = ib->index_size;
138
139 util_set_index_buffer(out_saved, ib);
140 pipe->set_index_buffer(pipe, &new_ib);
141 pipe_resource_reference(&new_ib.buffer, NULL);
142 return true;
143 }
144
145 struct pipe_query *
146 util_begin_pipestat_query(struct pipe_context *ctx)
147 {
148 struct pipe_query *q =
149 ctx->create_query(ctx, PIPE_QUERY_PIPELINE_STATISTICS, 0);
150 if (!q)
151 return NULL;
152
153 ctx->begin_query(ctx, q);
154 return q;
155 }
156
157 void
158 util_end_pipestat_query(struct pipe_context *ctx, struct pipe_query *q,
159 FILE *f)
160 {
161 static unsigned counter;
162 struct pipe_query_data_pipeline_statistics stats;
163
164 ctx->end_query(ctx, q);
165 ctx->get_query_result(ctx, q, true, (void*)&stats);
166 ctx->destroy_query(ctx, q);
167
168 fprintf(f,
169 "Draw call %u:\n"
170 " ia_vertices = %"PRIu64"\n"
171 " ia_primitives = %"PRIu64"\n"
172 " vs_invocations = %"PRIu64"\n"
173 " gs_invocations = %"PRIu64"\n"
174 " gs_primitives = %"PRIu64"\n"
175 " c_invocations = %"PRIu64"\n"
176 " c_primitives = %"PRIu64"\n"
177 " ps_invocations = %"PRIu64"\n"
178 " hs_invocations = %"PRIu64"\n"
179 " ds_invocations = %"PRIu64"\n"
180 " cs_invocations = %"PRIu64"\n",
181 p_atomic_inc_return(&counter),
182 stats.ia_vertices,
183 stats.ia_primitives,
184 stats.vs_invocations,
185 stats.gs_invocations,
186 stats.gs_primitives,
187 stats.c_invocations,
188 stats.c_primitives,
189 stats.ps_invocations,
190 stats.hs_invocations,
191 stats.ds_invocations,
192 stats.cs_invocations);
193 }