Merge branch 'lp-offset-twoside'
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_array.c
1 /*
2 * Copyright (C) 2009-2010 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include "main/bufferobj.h"
28 #include "nouveau_driver.h"
29 #include "nouveau_array.h"
30 #include "nouveau_bufferobj.h"
31
32 static void
33 get_array_extract(struct nouveau_array *a, extract_u_t *extract_u,
34 extract_f_t *extract_f)
35 {
36 #define EXTRACT(in_t, out_t, k) \
37 ({ \
38 auto out_t f(struct nouveau_array *, int, int); \
39 out_t f(struct nouveau_array *a, int i, int j) { \
40 in_t x = ((in_t *)(a->buf + i * a->stride))[j]; \
41 \
42 return (out_t)x / (k); \
43 }; \
44 f; \
45 });
46
47 switch (a->type) {
48 case GL_BYTE:
49 *extract_u = EXTRACT(char, unsigned, 1);
50 *extract_f = EXTRACT(char, float, SCHAR_MAX);
51 break;
52 case GL_UNSIGNED_BYTE:
53 *extract_u = EXTRACT(unsigned char, unsigned, 1);
54 *extract_f = EXTRACT(unsigned char, float, UCHAR_MAX);
55 break;
56 case GL_SHORT:
57 *extract_u = EXTRACT(short, unsigned, 1);
58 *extract_f = EXTRACT(short, float, SHRT_MAX);
59 break;
60 case GL_UNSIGNED_SHORT:
61 *extract_u = EXTRACT(unsigned short, unsigned, 1);
62 *extract_f = EXTRACT(unsigned short, float, USHRT_MAX);
63 break;
64 case GL_INT:
65 *extract_u = EXTRACT(int, unsigned, 1);
66 *extract_f = EXTRACT(int, float, INT_MAX);
67 break;
68 case GL_UNSIGNED_INT:
69 *extract_u = EXTRACT(unsigned int, unsigned, 1);
70 *extract_f = EXTRACT(unsigned int, float, UINT_MAX);
71 break;
72 case GL_FLOAT:
73 *extract_u = EXTRACT(float, unsigned, 1.0 / UINT_MAX);
74 *extract_f = EXTRACT(float, float, 1);
75 break;
76 default:
77 assert(0);
78 }
79 }
80
81 void
82 nouveau_init_array(struct nouveau_array *a, int attr, int stride,
83 int fields, int type, struct gl_buffer_object *obj,
84 const void *ptr, GLboolean map)
85 {
86 a->attr = attr;
87 a->stride = stride;
88 a->fields = fields;
89 a->type = type;
90 a->buf = NULL;
91
92 if (obj) {
93 if (nouveau_bufferobj_hw(obj)) {
94 struct nouveau_bufferobj *nbo =
95 to_nouveau_bufferobj(obj);
96
97 nouveau_bo_ref(nbo->bo, &a->bo);
98 a->offset = (intptr_t)ptr;
99
100 if (map) {
101 nouveau_bo_map(a->bo, NOUVEAU_BO_RD);
102 a->buf = a->bo->map + a->offset;
103 }
104
105 } else {
106 nouveau_bo_ref(NULL, &a->bo);
107 a->offset = 0;
108
109 if (map)
110 a->buf = ADD_POINTERS(
111 nouveau_bufferobj_sys(obj), ptr);
112 }
113 }
114
115 if (a->buf)
116 get_array_extract(a, &a->extract_u, &a->extract_f);
117 }
118
119 void
120 nouveau_deinit_array(struct nouveau_array *a)
121 {
122 if (a->bo) {
123 if (a->bo->map)
124 nouveau_bo_unmap(a->bo);
125 }
126
127 a->buf = NULL;
128 a->fields = 0;
129 }
130
131 void
132 nouveau_cleanup_array(struct nouveau_array *a)
133 {
134 nouveau_deinit_array(a);
135 nouveau_bo_ref(NULL, &a->bo);
136 }