1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Mesa 3-D graphics library 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Copyright (C) 1999-2001 Brian Paul All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 12bf215546Sopenharmony_ci * 13bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included 14bf215546Sopenharmony_ci * in all copies or substantial portions of the Software. 15bf215546Sopenharmony_ci * 16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 23bf215546Sopenharmony_ci */ 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_ci/* 26bf215546Sopenharmony_ci * New (3.1) transformation code written by Keith Whitwell. 27bf215546Sopenharmony_ci */ 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#include <stdio.h> 30bf215546Sopenharmony_ci#include <stddef.h> 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci#include "main/glheader.h" 33bf215546Sopenharmony_ci#include "main/macros.h" 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci#include "m_vector.h" 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci#include "util/u_memory.h" 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_ci/** 42bf215546Sopenharmony_ci * Given a vector [count][4] of floats, set all the [][elt] values 43bf215546Sopenharmony_ci * to 0 (if elt = 0, 1, 2) or 1.0 (if elt = 3). 44bf215546Sopenharmony_ci */ 45bf215546Sopenharmony_civoid 46bf215546Sopenharmony_ci_mesa_vector4f_clean_elem( GLvector4f *vec, GLuint count, GLuint elt ) 47bf215546Sopenharmony_ci{ 48bf215546Sopenharmony_ci static const GLubyte elem_bits[4] = { 49bf215546Sopenharmony_ci VEC_DIRTY_0, 50bf215546Sopenharmony_ci VEC_DIRTY_1, 51bf215546Sopenharmony_ci VEC_DIRTY_2, 52bf215546Sopenharmony_ci VEC_DIRTY_3 53bf215546Sopenharmony_ci }; 54bf215546Sopenharmony_ci static const GLfloat clean[4] = { 0, 0, 0, 1 }; 55bf215546Sopenharmony_ci const GLfloat v = clean[elt]; 56bf215546Sopenharmony_ci GLfloat (*data)[4] = (GLfloat (*)[4])vec->start; 57bf215546Sopenharmony_ci GLuint i; 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci for (i = 0; i < count; i++) 60bf215546Sopenharmony_ci data[i][elt] = v; 61bf215546Sopenharmony_ci 62bf215546Sopenharmony_ci vec->flags &= ~elem_bits[elt]; 63bf215546Sopenharmony_ci} 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_cistatic const GLubyte size_bits[5] = { 67bf215546Sopenharmony_ci 0, 68bf215546Sopenharmony_ci VEC_SIZE_1, 69bf215546Sopenharmony_ci VEC_SIZE_2, 70bf215546Sopenharmony_ci VEC_SIZE_3, 71bf215546Sopenharmony_ci VEC_SIZE_4, 72bf215546Sopenharmony_ci}; 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci 75bf215546Sopenharmony_ci/** 76bf215546Sopenharmony_ci * Initialize GLvector objects. 77bf215546Sopenharmony_ci * \param v the vector object to initialize. 78bf215546Sopenharmony_ci * \param flags bitwise-OR of VEC_* flags 79bf215546Sopenharmony_ci * \param storage pointer to storage for the vector's data 80bf215546Sopenharmony_ci */ 81bf215546Sopenharmony_civoid 82bf215546Sopenharmony_ci_mesa_vector4f_init( GLvector4f *v, GLbitfield flags, GLfloat (*storage)[4] ) 83bf215546Sopenharmony_ci{ 84bf215546Sopenharmony_ci STATIC_ASSERT(V4F_DATA == offsetof(GLvector4f, data)); 85bf215546Sopenharmony_ci STATIC_ASSERT(V4F_START == offsetof(GLvector4f, start)); 86bf215546Sopenharmony_ci STATIC_ASSERT(V4F_COUNT == offsetof(GLvector4f, count)); 87bf215546Sopenharmony_ci STATIC_ASSERT(V4F_STRIDE == offsetof(GLvector4f, stride)); 88bf215546Sopenharmony_ci STATIC_ASSERT(V4F_SIZE == offsetof(GLvector4f, size)); 89bf215546Sopenharmony_ci STATIC_ASSERT(V4F_FLAGS == offsetof(GLvector4f, flags)); 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci v->stride = 4 * sizeof(GLfloat); 92bf215546Sopenharmony_ci v->size = 2; /* may change: 2-4 for vertices and 1-4 for texcoords */ 93bf215546Sopenharmony_ci v->data = storage; 94bf215546Sopenharmony_ci v->start = (GLfloat *) storage; 95bf215546Sopenharmony_ci v->count = 0; 96bf215546Sopenharmony_ci v->flags = size_bits[4] | flags; 97bf215546Sopenharmony_ci} 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci/** 101bf215546Sopenharmony_ci * Initialize GLvector objects and allocate storage. 102bf215546Sopenharmony_ci * \param v the vector object 103bf215546Sopenharmony_ci * \param flags bitwise-OR of VEC_* flags 104bf215546Sopenharmony_ci * \param count number of elements to allocate in vector 105bf215546Sopenharmony_ci * \param alignment desired memory alignment for the data (in bytes) 106bf215546Sopenharmony_ci */ 107bf215546Sopenharmony_civoid 108bf215546Sopenharmony_ci_mesa_vector4f_alloc( GLvector4f *v, GLbitfield flags, GLuint count, 109bf215546Sopenharmony_ci GLuint alignment ) 110bf215546Sopenharmony_ci{ 111bf215546Sopenharmony_ci v->stride = 4 * sizeof(GLfloat); 112bf215546Sopenharmony_ci v->size = 2; 113bf215546Sopenharmony_ci v->storage = align_malloc( count * 4 * sizeof(GLfloat), alignment ); 114bf215546Sopenharmony_ci v->storage_count = count; 115bf215546Sopenharmony_ci v->start = (GLfloat *) v->storage; 116bf215546Sopenharmony_ci v->data = (GLfloat (*)[4]) v->storage; 117bf215546Sopenharmony_ci v->count = 0; 118bf215546Sopenharmony_ci v->flags = size_bits[4] | flags | VEC_MALLOC; 119bf215546Sopenharmony_ci} 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci/** 123bf215546Sopenharmony_ci * Vector deallocation. Free whatever memory is pointed to by the 124bf215546Sopenharmony_ci * vector's storage field if the VEC_MALLOC flag is set. 125bf215546Sopenharmony_ci * DO NOT free the GLvector object itself, though. 126bf215546Sopenharmony_ci */ 127bf215546Sopenharmony_civoid 128bf215546Sopenharmony_ci_mesa_vector4f_free( GLvector4f *v ) 129bf215546Sopenharmony_ci{ 130bf215546Sopenharmony_ci if (v->flags & VEC_MALLOC) { 131bf215546Sopenharmony_ci align_free( v->storage ); 132bf215546Sopenharmony_ci v->data = NULL; 133bf215546Sopenharmony_ci v->start = NULL; 134bf215546Sopenharmony_ci v->storage = NULL; 135bf215546Sopenharmony_ci v->flags &= ~VEC_MALLOC; 136bf215546Sopenharmony_ci } 137bf215546Sopenharmony_ci} 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_ci 140bf215546Sopenharmony_ci/** 141bf215546Sopenharmony_ci * For debugging 142bf215546Sopenharmony_ci */ 143bf215546Sopenharmony_civoid 144bf215546Sopenharmony_ci_mesa_vector4f_print( const GLvector4f *v, const GLubyte *cullmask, 145bf215546Sopenharmony_ci GLboolean culling ) 146bf215546Sopenharmony_ci{ 147bf215546Sopenharmony_ci static const GLfloat c[4] = { 0, 0, 0, 1 }; 148bf215546Sopenharmony_ci static const char *templates[5] = { 149bf215546Sopenharmony_ci "%d:\t0, 0, 0, 1\n", 150bf215546Sopenharmony_ci "%d:\t%f, 0, 0, 1\n", 151bf215546Sopenharmony_ci "%d:\t%f, %f, 0, 1\n", 152bf215546Sopenharmony_ci "%d:\t%f, %f, %f, 1\n", 153bf215546Sopenharmony_ci "%d:\t%f, %f, %f, %f\n" 154bf215546Sopenharmony_ci }; 155bf215546Sopenharmony_ci 156bf215546Sopenharmony_ci const char *t = templates[v->size]; 157bf215546Sopenharmony_ci GLfloat *d = (GLfloat *)v->data; 158bf215546Sopenharmony_ci GLuint j, i = 0, count; 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci printf("data-start\n"); 161bf215546Sopenharmony_ci for (; d != v->start; STRIDE_F(d, v->stride), i++) 162bf215546Sopenharmony_ci printf(t, i, d[0], d[1], d[2], d[3]); 163bf215546Sopenharmony_ci 164bf215546Sopenharmony_ci printf("start-count(%u)\n", v->count); 165bf215546Sopenharmony_ci count = i + v->count; 166bf215546Sopenharmony_ci 167bf215546Sopenharmony_ci if (culling) { 168bf215546Sopenharmony_ci for (; i < count; STRIDE_F(d, v->stride), i++) 169bf215546Sopenharmony_ci if (cullmask[i]) 170bf215546Sopenharmony_ci printf(t, i, d[0], d[1], d[2], d[3]); 171bf215546Sopenharmony_ci } 172bf215546Sopenharmony_ci else { 173bf215546Sopenharmony_ci for (; i < count; STRIDE_F(d, v->stride), i++) 174bf215546Sopenharmony_ci printf(t, i, d[0], d[1], d[2], d[3]); 175bf215546Sopenharmony_ci } 176bf215546Sopenharmony_ci 177bf215546Sopenharmony_ci for (j = v->size; j < 4; j++) { 178bf215546Sopenharmony_ci if ((v->flags & (1<<j)) == 0) { 179bf215546Sopenharmony_ci 180bf215546Sopenharmony_ci printf("checking col %u is clean as advertised ", j); 181bf215546Sopenharmony_ci 182bf215546Sopenharmony_ci for (i = 0, d = (GLfloat *) v->data; 183bf215546Sopenharmony_ci i < count && d[j] == c[j]; 184bf215546Sopenharmony_ci i++, STRIDE_F(d, v->stride)) { 185bf215546Sopenharmony_ci /* no-op */ 186bf215546Sopenharmony_ci } 187bf215546Sopenharmony_ci 188bf215546Sopenharmony_ci if (i == count) 189bf215546Sopenharmony_ci printf(" --> ok\n"); 190bf215546Sopenharmony_ci else 191bf215546Sopenharmony_ci printf(" --> Failed at %u ******\n", i); 192bf215546Sopenharmony_ci } 193bf215546Sopenharmony_ci } 194bf215546Sopenharmony_ci} 195