summaryrefslogtreecommitdiff
path: root/src/glitz_texture.c
blob: 4722c81342c47fa0eb583146fc0d671b4b5e811a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * Copyright © 2004 David Reveman
 * 
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * David Reveman not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * David Reveman makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * DAVID REVEMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL DAVID REVEMAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: David Reveman <davidr@novell.com>
 */

#ifdef HAVE_CONFIG_H
#  include "../config.h"
#endif

#include "glitzint.h"

void
glitz_texture_init (glitz_texture_t *texture,
                    int             width,
                    int             height,
                    glitz_gl_int_t  texture_format,
                    unsigned long   feature_mask,
                    glitz_bool_t    unnormalized)
{
    texture->filter = 0;
    texture->wrap = 0;
    texture->format = texture_format;
    texture->name = 0;

    if (feature_mask & GLITZ_FEATURE_TEXTURE_BORDER_CLAMP_MASK)
    {
        texture->box.x1 = texture->box.y1 = 0;
        texture->box.x2 = texture->width = width;
        texture->box.y2 = texture->height = height;
        texture->flags = GLITZ_TEXTURE_FLAG_REPEATABLE_MASK |
            GLITZ_TEXTURE_FLAG_PADABLE_MASK;
    }
    else
    {
        texture->box.x1 = texture->box.y1 = 1;
        texture->box.x2 = width + 1;
        texture->box.y2 = height + 1;
        texture->width = width + 2;
        texture->height = height + 2;
        texture->flags = 0;
    }
  
    if (!unnormalized &&
        ((feature_mask & GLITZ_FEATURE_TEXTURE_NON_POWER_OF_TWO_MASK) ||
         (POWER_OF_TWO (texture->width) && POWER_OF_TWO (texture->height))))
    {
        texture->target = GLITZ_GL_TEXTURE_2D;
    }
    else
    {
        texture->flags &= ~GLITZ_TEXTURE_FLAG_REPEATABLE_MASK;
      
        if (feature_mask & GLITZ_FEATURE_TEXTURE_RECTANGLE_MASK)
        {
            texture->target = GLITZ_GL_TEXTURE_RECTANGLE;
        }
        else
        {
            texture->target = GLITZ_GL_TEXTURE_2D;
            texture->flags &= ~GLITZ_TEXTURE_FLAG_PADABLE_MASK;
          
            if (!POWER_OF_TWO (texture->width))
                texture->width = glitz_uint_to_power_of_two (texture->width);
          
            if (!POWER_OF_TWO (texture->height))
                texture->height = glitz_uint_to_power_of_two (texture->height);
        }
    }

    if (texture->target == GLITZ_GL_TEXTURE_2D)
    {
        texture->texcoord_width_unit = 1.0f / texture->width;
        texture->texcoord_height_unit = 1.0f / texture->height;   
    }
    else
    {
        texture->texcoord_width_unit = 1.0f;
        texture->texcoord_height_unit = 1.0f;
    }
}

void
glitz_texture_size_check (glitz_gl_proc_address_list_t *gl,
                          glitz_texture_t              *texture,
                          glitz_gl_int_t               max_2d,
                          glitz_gl_int_t               max_rect) {
  glitz_gl_enum_t proxy_target;
  glitz_gl_int_t value, max;

  if (texture->target == GLITZ_GL_TEXTURE_2D) {
    max = max_2d;
    proxy_target = GLITZ_GL_PROXY_TEXTURE_2D;
  } else {
    max = max_rect;
    proxy_target = GLITZ_GL_PROXY_TEXTURE_RECTANGLE;
  }
    
  if (texture->width > max || texture->height > max) {
    texture->flags |= GLITZ_TEXTURE_FLAG_INVALID_SIZE_MASK;
    return;
  }
    
  gl->tex_image_2d (proxy_target, 0,
                    texture->format, texture->width, texture->height,
                    0, GLITZ_GL_RGBA, GLITZ_GL_UNSIGNED_BYTE, NULL);
  gl->get_tex_level_parameter_iv (proxy_target, 0,
                                  GLITZ_GL_TEXTURE_WIDTH, &value);
  if (value != texture->width) {
    texture->flags |= GLITZ_TEXTURE_FLAG_INVALID_SIZE_MASK;
    return;
  }
  
  gl->get_tex_level_parameter_iv (proxy_target, 0,
                                  GLITZ_GL_TEXTURE_HEIGHT, &value);
  if (value != texture->height)
    texture->flags |= GLITZ_TEXTURE_FLAG_INVALID_SIZE_MASK;
}

void
glitz_texture_allocate (glitz_gl_proc_address_list_t *gl,
                        glitz_texture_t              *texture)
{
  char *data = NULL;
  
  if (!texture->name)
    gl->gen_textures (1, &texture->name);
  
  texture->flags |= GLITZ_TEXTURE_FLAG_ALLOCATED_MASK;
  
  glitz_texture_bind (gl, texture);

  if (texture->box.x2 != texture->width ||
      texture->box.y2 != texture->height) {
    data = malloc (texture->width * texture->height);
    if (data)
      memset (data, 0, texture->width * texture->height);

    gl->pixel_store_i (GLITZ_GL_UNPACK_ROW_LENGTH, 0);
    gl->pixel_store_i (GLITZ_GL_UNPACK_ROW_LENGTH, 0);
    gl->pixel_store_i (GLITZ_GL_UNPACK_SKIP_ROWS, 0);
    gl->pixel_store_i (GLITZ_GL_UNPACK_SKIP_PIXELS, 0);
    gl->pixel_store_i (GLITZ_GL_UNPACK_ALIGNMENT, 1);
  }

  gl->tex_image_2d (texture->target, 0, texture->format,
                    texture->width, texture->height, 0,
                    GLITZ_GL_ALPHA, GLITZ_GL_UNSIGNED_BYTE, data);

  glitz_texture_unbind (gl, texture);

  if (data)
    free (data);
}

void 
glitz_texture_fini (glitz_gl_proc_address_list_t *gl,
                    glitz_texture_t              *texture)
{
  if (texture->name)
    gl->delete_textures (1, &texture->name);
}

void
glitz_texture_ensure_filter (glitz_gl_proc_address_list_t *gl,
                             glitz_texture_t              *texture,
                             glitz_gl_enum_t              filter)
{
  if (!texture->name)
    return;
    
  if (texture->filter != filter) {
    gl->tex_parameter_i (texture->target, GLITZ_GL_TEXTURE_MAG_FILTER, filter);
    gl->tex_parameter_i (texture->target, GLITZ_GL_TEXTURE_MIN_FILTER, filter);
    texture->filter = filter;
  }
}

void
glitz_texture_ensure_wrap (glitz_gl_proc_address_list_t *gl,
                           glitz_texture_t              *texture,
                           glitz_gl_enum_t              wrap)
{
  if (!texture->name)
    return;
  
  if (texture->wrap != wrap) {
    gl->tex_parameter_i (texture->target, GLITZ_GL_TEXTURE_WRAP_S, wrap);
    gl->tex_parameter_i (texture->target, GLITZ_GL_TEXTURE_WRAP_T, wrap);
    texture->wrap = wrap;
  }
}

void
glitz_texture_bind (glitz_gl_proc_address_list_t *gl,
                    glitz_texture_t              *texture)
{  
  gl->disable (GLITZ_GL_TEXTURE_RECTANGLE);
  gl->disable (GLITZ_GL_TEXTURE_2D);

  if (!texture->name)
    return;

  gl->enable (texture->target);
  gl->bind_texture (texture->target, texture->name);
}

void
glitz_texture_unbind (glitz_gl_proc_address_list_t *gl,
                      glitz_texture_t              *texture)
{
  gl->bind_texture (texture->target, 0);
  gl->disable (texture->target);
}

void
glitz_texture_copy_drawable (glitz_gl_proc_address_list_t *gl,
                             glitz_texture_t              *texture,
                             glitz_drawable_t             *drawable,
                             int                          x_drawable,
                             int                          y_drawable,
                             int                          width,
                             int                          height,
                             int                          x_texture,
                             int                          y_texture)
{
  gl->copy_tex_sub_image_2d (texture->target, 0,
                             texture->box.x1 + x_texture,
                             texture->box.y2 - y_texture - height,
                             x_drawable,
                             drawable->height - y_drawable - height,
                             width, height);
}

void
glitz_texture_set_tex_gen (glitz_gl_proc_address_list_t *gl,
                           glitz_texture_t              *texture,
                           glitz_geometry_t             *geometry,
                           int                          x_src,
                           int                          y_src,
                           unsigned long                flags,
                           glitz_int_coordinate_t       *coord)
{
    glitz_vec4_t plane;

    if (flags & GLITZ_SURFACE_FLAG_GEN_S_COORDS_MASK)
    {
        plane.v[1] = plane.v[2] = 0.0f;
        
        if (flags & GLITZ_SURFACE_FLAG_EYE_COORDS_MASK)
        {
            plane.v[0] = 1.0f;
            plane.v[3] = -x_src;
        }
        else
        {
            plane.v[0] = texture->texcoord_width_unit;
            
            if (flags & GLITZ_SURFACE_FLAG_TRANSFORM_MASK)
                plane.v[3] = -(x_src) * texture->texcoord_width_unit;
            else
                plane.v[3] = -(x_src - texture->box.x1) *
                    texture->texcoord_width_unit;
        }
        
        gl->tex_gen_i (GLITZ_GL_S, GLITZ_GL_TEXTURE_GEN_MODE,
                       GLITZ_GL_EYE_LINEAR);
        gl->tex_gen_fv (GLITZ_GL_S, GLITZ_GL_EYE_PLANE, plane.v);

        gl->enable (GLITZ_GL_TEXTURE_GEN_S);
    }
    else
        gl->disable (GLITZ_GL_TEXTURE_GEN_S);

    if (flags & GLITZ_SURFACE_FLAG_GEN_T_COORDS_MASK)
    {
        plane.v[0] = plane.v[2] = 0.0f;
        if (flags & GLITZ_SURFACE_FLAG_EYE_COORDS_MASK)
        {
            plane.v[1] = 1.0f;
            plane.v[3] = -y_src;
        }
        else
        {
            plane.v[1] = -texture->texcoord_height_unit;
            
            if (flags & GLITZ_SURFACE_FLAG_TRANSFORM_MASK)
                plane.v[3] = (y_src + texture->box.y2 - texture->box.y1) *
                    texture->texcoord_height_unit;
            else
                plane.v[3] = (y_src + texture->box.y2) *
                    texture->texcoord_height_unit;
        }
  
        gl->tex_gen_i (GLITZ_GL_T, GLITZ_GL_TEXTURE_GEN_MODE,
                       GLITZ_GL_EYE_LINEAR);
        gl->tex_gen_fv (GLITZ_GL_T, GLITZ_GL_EYE_PLANE, plane.v);
        
        gl->enable (GLITZ_GL_TEXTURE_GEN_T);
    }
    else
        gl->disable (GLITZ_GL_TEXTURE_GEN_T);

    if (!(flags & GLITZ_SURFACE_FLAG_GEN_S_COORDS_MASK))
    {
        unsigned char *ptr;

        gl->enable_client_state (GLITZ_GL_TEXTURE_COORD_ARRAY);
        
        ptr = glitz_buffer_bind (geometry->buffer, GLITZ_GL_ARRAY_BUFFER);
        ptr += coord->offset;

        gl->tex_coord_pointer (coord->size,
                               coord->type,
                               geometry->stride,
                               (void *) ptr);
    } else
        gl->disable_client_state (GLITZ_GL_TEXTURE_COORD_ARRAY);
}