1 /**************************************************************************
3 * Copyright 2010 Christian König
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 TUNGSTEN GRAPHICS AND/OR ITS 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.
26 **************************************************************************/
29 #include <pipe/p_context.h>
30 #include <pipe/p_screen.h>
31 #include <util/u_memory.h>
32 #include <util/u_inlines.h>
33 #include <util/u_format.h>
34 #include "vl_vertex_buffers.h"
37 /* vertices for a quad covering a block */
38 static const struct quadf const_quad
= {
39 {0.0f
, 1.0f
}, {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}
42 struct pipe_vertex_buffer
43 vl_vb_upload_quads(struct pipe_context
*pipe
, unsigned max_blocks
)
45 struct pipe_vertex_buffer quad
;
46 struct pipe_transfer
*buf_transfer
;
55 quad
.stride
= sizeof(struct vertex2f
);
56 quad
.max_index
= 4 * max_blocks
- 1;
57 quad
.buffer_offset
= 0;
58 quad
.buffer
= pipe_buffer_create
61 PIPE_BIND_VERTEX_BUFFER
,
62 sizeof(struct vertex2f
) * 4 * max_blocks
73 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
77 for ( i
= 0; i
< max_blocks
; ++i
)
78 memcpy(v
+ i
, &const_quad
, sizeof(const_quad
));
80 pipe_buffer_unmap(pipe
, quad
.buffer
, buf_transfer
);
85 struct pipe_vertex_element
86 vl_vb_get_quad_vertex_element()
88 struct pipe_vertex_element element
;
90 /* setup rectangle element */
91 element
.src_offset
= 0;
92 element
.instance_divisor
= 0;
93 element
.vertex_buffer_index
= 0;
94 element
.src_format
= PIPE_FORMAT_R32G32_FLOAT
;
99 struct pipe_vertex_buffer
100 vl_vb_create_buffer(struct pipe_context
*pipe
, unsigned max_blocks
, unsigned stride
)
102 struct pipe_vertex_buffer buf
;
105 buf
.max_index
= 4 * max_blocks
- 1;
106 buf
.buffer_offset
= 0;
107 buf
.buffer
= pipe_buffer_create
110 PIPE_BIND_VERTEX_BUFFER
,
111 stride
* 4 * max_blocks
118 vl_vb_element_helper(struct pipe_vertex_element
* elements
, unsigned num_elements
,
119 unsigned vertex_buffer_index
)
121 unsigned i
, offset
= 0;
123 assert(elements
&& num_elements
);
125 for ( i
= 0; i
< num_elements
; ++i
) {
126 elements
[i
].src_offset
= offset
;
127 elements
[i
].instance_divisor
= 0;
128 elements
[i
].vertex_buffer_index
= vertex_buffer_index
;
129 offset
+= util_format_get_blocksize(elements
[i
].src_format
);
136 vl_vb_init(struct vl_vertex_buffer
*buffer
, unsigned max_blocks
, unsigned num_elements
)
140 buffer
->num_verts
= 0;
141 buffer
->num_elements
= num_elements
;
142 buffer
->buffer
= MALLOC(max_blocks
* num_elements
* sizeof(float) * 4);
143 return buffer
->buffer
!= NULL
;
147 vl_vb_upload(struct vl_vertex_buffer
*buffer
, void *dst
)
153 todo
= buffer
->num_verts
;
154 buffer
->num_verts
= 0;
157 memcpy(dst
, buffer
->buffer
, sizeof(float) * buffer
->num_elements
* todo
);
163 vl_vb_cleanup(struct vl_vertex_buffer
*buffer
)
167 FREE(buffer
->buffer
);