1/**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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 VMWARE 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.
25 *
26 **************************************************************************/
27
28/* Authors:
29 *   Brian Paul
30 */
31
32#include "st_context.h"
33#include "st_sampler_view.h"
34#include "st_texture.h"
35
36#include "util/u_inlines.h"
37#include "util/u_pack_color.h"
38
39
40/**
41 * Update the pixelmap texture with the contents of the R/G/B/A pixel maps.
42 */
43static void
44load_color_map_texture(struct gl_context *ctx, struct pipe_resource *pt)
45{
46   struct st_context *st = st_context(ctx);
47   struct pipe_context *pipe = st->pipe;
48   struct pipe_transfer *transfer;
49   const GLuint rSize = ctx->PixelMaps.RtoR.Size;
50   const GLuint gSize = ctx->PixelMaps.GtoG.Size;
51   const GLuint bSize = ctx->PixelMaps.BtoB.Size;
52   const GLuint aSize = ctx->PixelMaps.AtoA.Size;
53   const uint texSize = pt->width0;
54   uint *dest;
55   uint i, j;
56
57   dest = (uint *) pipe_texture_map(pipe,
58                                     pt, 0, 0, PIPE_MAP_WRITE,
59                                     0, 0, texSize, texSize, &transfer);
60
61   /* Pack four 1D maps into a 2D texture:
62    * R map is placed horizontally, indexed by S, in channel 0
63    * G map is placed vertically, indexed by T, in channel 1
64    * B map is placed horizontally, indexed by S, in channel 2
65    * A map is placed vertically, indexed by T, in channel 3
66    */
67   for (i = 0; i < texSize; i++) {
68      for (j = 0; j < texSize; j++) {
69         union util_color uc;
70         int k = (i * texSize + j);
71         float rgba[4];
72         rgba[0] = ctx->PixelMaps.RtoR.Map[j * rSize / texSize];
73         rgba[1] = ctx->PixelMaps.GtoG.Map[i * gSize / texSize];
74         rgba[2] = ctx->PixelMaps.BtoB.Map[j * bSize / texSize];
75         rgba[3] = ctx->PixelMaps.AtoA.Map[i * aSize / texSize];
76         util_pack_color(rgba, pt->format, &uc);
77         *(dest + k) = uc.ui[0];
78      }
79   }
80
81   pipe_texture_unmap(pipe, transfer);
82}
83
84/**
85 * Upload the pixel transfer color map texture.
86 */
87void
88st_update_pixel_transfer(struct st_context *st)
89{
90   struct gl_context *ctx = st->ctx;
91
92   if (ctx->Pixel.MapColorFlag) {
93      /* create the colormap/texture now if not already done */
94      if (!st->pixel_xfer.pixelmap_texture) {
95         st->pixel_xfer.pixelmap_texture = st_create_color_map_texture(ctx);
96         st->pixel_xfer.pixelmap_sampler_view =
97            st_create_texture_sampler_view(st->pipe,
98                                           st->pixel_xfer.pixelmap_texture);
99      }
100      load_color_map_texture(ctx, st->pixel_xfer.pixelmap_texture);
101   }
102}
103