Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / gallium / drivers / r300 / r300_vbo.c
1 /*
2 * Copyright 2009 Maciej Cencora <m.cencora@gmail.com>
3 *
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
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 AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 /* r300_vbo: Various helpers for emitting vertex buffers. Needs cleanup,
25 * refactoring, etc. */
26
27 #include "r300_vbo.h"
28
29 #include "pipe/p_format.h"
30
31 #include "r300_cs.h"
32 #include "r300_context.h"
33 #include "r300_state_inlines.h"
34 #include "r300_reg.h"
35 #include "r300_winsys.h"
36
37 static INLINE void setup_vertex_attribute(struct r300_vertex_info *vinfo,
38 struct pipe_vertex_element *vert_elem,
39 unsigned attr_num)
40 {
41 uint16_t hw_fmt1, hw_fmt2;
42
43 hw_fmt1 = r300_translate_vertex_data_type(vert_elem->src_format) |
44 (attr_num << R300_DST_VEC_LOC_SHIFT);
45 hw_fmt2 = r300_translate_vertex_data_swizzle(vert_elem->src_format);
46
47 if (attr_num % 2 == 0)
48 {
49 vinfo->vap_prog_stream_cntl[attr_num >> 1] = hw_fmt1;
50 vinfo->vap_prog_stream_cntl_ext[attr_num >> 1] = hw_fmt2;
51 }
52 else
53 {
54 vinfo->vap_prog_stream_cntl[attr_num >> 1] |= hw_fmt1 << 16;
55 vinfo->vap_prog_stream_cntl_ext[attr_num >> 1] |= hw_fmt2 << 16;
56 }
57 }
58
59 static void finish_vertex_attribs_setup(struct r300_vertex_info *vinfo,
60 unsigned attribs_num)
61 {
62 uint32_t last_vec_bit = (attribs_num % 2 == 0) ?
63 (R300_LAST_VEC << 16) : R300_LAST_VEC;
64
65 assert(attribs_num > 0 && attribs_num <= 16);
66 vinfo->vap_prog_stream_cntl[(attribs_num - 1) >> 1] |= last_vec_bit;
67 }
68
69 void setup_vertex_attributes(struct r300_context *r300)
70 {
71 struct pipe_vertex_element *vert_elem;
72 int i;
73
74 for (i = 0; i < r300->vertex_element_count; i++) {
75 vert_elem = &r300->vertex_element[i];
76 setup_vertex_attribute(r300->vertex_info, vert_elem, i);
77 }
78
79 finish_vertex_attribs_setup(r300->vertex_info,
80 r300->vertex_element_count);
81 }
82
83 static INLINE int get_buffer_offset(struct r300_context *r300,
84 unsigned int buf_nr,
85 unsigned int elem_offset)
86 {
87 return r300->vertex_buffer[buf_nr].buffer_offset + elem_offset;
88 }
89 #if 0
90 /* XXX not called at all */
91 static void setup_vertex_buffers(struct r300_context *r300)
92 {
93 struct pipe_vertex_element *vert_elem;
94 int i;
95
96 for (i = 0; i < r300->aos_count; i++)
97 {
98 vert_elem = &r300->vertex_element[i];
99 /* XXX use translate module to convert the data */
100 if (!format_is_supported(vert_elem->src_format,
101 vert_elem->nr_components)) {
102 assert(0);
103 /*
104 struct pipe_buffer *buf;
105 const unsigned int max_index = r300->vertex_buffers[vert_elem->vertex_buffer_index].max_index;
106 buf = pipe_buffer_create(r300->context.screen, 4, usage, vert_elem->nr_components * max_index * sizeof(float));
107 */
108 }
109
110 if (get_buffer_offset(r300,
111 vert_elem->vertex_buffer_index,
112 vert_elem->src_offset) % 4) {
113 /* XXX need to align buffer */
114 assert(0);
115 }
116 }
117 }
118 #endif
119 /* XXX these shouldn't be asserts since we can work around bad indexbufs */
120 void setup_index_buffer(struct r300_context *r300,
121 struct pipe_buffer* indexBuffer,
122 unsigned indexSize)
123 {
124 if (!r300->winsys->add_buffer(r300->winsys, indexBuffer,
125 RADEON_GEM_DOMAIN_GTT, 0)) {
126 assert(0);
127 }
128
129 if (!r300->winsys->validate(r300->winsys)) {
130 assert(0);
131 }
132 }