Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / 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
32 /**
33 * This function is used to copy an array of pipe_vertex_buffer structures,
34 * while properly referencing the pipe_vertex_buffer::buffer member.
35 *
36 * enabled_buffers is updated such that the bits corresponding to the indices
37 * of disabled buffers are set to 0 and the enabled ones are set to 1.
38 *
39 * \sa util_copy_framebuffer_state
40 */
41 void util_set_vertex_buffers_mask(struct pipe_vertex_buffer *dst,
42 uint32_t *enabled_buffers,
43 const struct pipe_vertex_buffer *src,
44 unsigned start_slot, unsigned count)
45 {
46 unsigned i;
47 uint32_t bitmask = 0;
48
49 dst += start_slot;
50
51 if (src) {
52 for (i = 0; i < count; i++) {
53 if (src[i].buffer || src[i].user_buffer) {
54 bitmask |= 1 << i;
55 }
56 pipe_resource_reference(&dst[i].buffer, src[i].buffer);
57 }
58
59 /* Copy over the other members of pipe_vertex_buffer. */
60 memcpy(dst, src, count * sizeof(struct pipe_vertex_buffer));
61
62 *enabled_buffers &= ~(((1ull << count) - 1) << start_slot);
63 *enabled_buffers |= bitmask << start_slot;
64 }
65 else {
66 /* Unreference the buffers. */
67 for (i = 0; i < count; i++) {
68 pipe_resource_reference(&dst[i].buffer, NULL);
69 dst[i].user_buffer = NULL;
70 }
71
72 *enabled_buffers &= ~(((1ull << count) - 1) << start_slot);
73 }
74 }
75
76 /**
77 * Same as util_set_vertex_buffers_mask, but it only returns the number
78 * of bound buffers.
79 */
80 void util_set_vertex_buffers_count(struct pipe_vertex_buffer *dst,
81 unsigned *dst_count,
82 const struct pipe_vertex_buffer *src,
83 unsigned start_slot, unsigned count)
84 {
85 unsigned i;
86 uint32_t enabled_buffers = 0;
87
88 for (i = 0; i < *dst_count; i++) {
89 if (dst[i].buffer || dst[i].user_buffer)
90 enabled_buffers |= (1ull << i);
91 }
92
93 util_set_vertex_buffers_mask(dst, &enabled_buffers, src, start_slot,
94 count);
95
96 *dst_count = util_last_bit(enabled_buffers);
97 }
98
99
100 void
101 util_set_index_buffer(struct pipe_index_buffer *dst,
102 const struct pipe_index_buffer *src)
103 {
104 if (src) {
105 pipe_resource_reference(&dst->buffer, src->buffer);
106 memcpy(dst, src, sizeof(*dst));
107 }
108 else {
109 pipe_resource_reference(&dst->buffer, NULL);
110 memset(dst, 0, sizeof(*dst));
111 }
112 }
113
114 /**
115 * Given a user index buffer, save the structure to "saved", and upload it.
116 */
117 bool
118 util_save_and_upload_index_buffer(struct pipe_context *pipe,
119 const struct pipe_draw_info *info,
120 const struct pipe_index_buffer *ib,
121 struct pipe_index_buffer *out_saved)
122 {
123 struct pipe_index_buffer new_ib = {0};
124 unsigned start_offset = info->start * ib->index_size;
125
126 u_upload_data(pipe->stream_uploader, start_offset,
127 info->count * ib->index_size, 4,
128 (char*)ib->user_buffer + start_offset,
129 &new_ib.offset, &new_ib.buffer);
130 if (!new_ib.buffer)
131 return false;
132 u_upload_unmap(pipe->stream_uploader);
133
134 new_ib.offset -= start_offset;
135 new_ib.index_size = ib->index_size;
136
137 util_set_index_buffer(out_saved, ib);
138 pipe->set_index_buffer(pipe, &new_ib);
139 pipe_resource_reference(&new_ib.buffer, NULL);
140 return true;
141 }