1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2007 VMware, Inc.
4bf215546Sopenharmony_ci * 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
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci/* Authors:
29bf215546Sopenharmony_ci *   Brian Paul
30bf215546Sopenharmony_ci */
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "st_context.h"
33bf215546Sopenharmony_ci#include "st_sampler_view.h"
34bf215546Sopenharmony_ci#include "st_texture.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci#include "util/u_inlines.h"
37bf215546Sopenharmony_ci#include "util/u_pack_color.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci/**
41bf215546Sopenharmony_ci * Update the pixelmap texture with the contents of the R/G/B/A pixel maps.
42bf215546Sopenharmony_ci */
43bf215546Sopenharmony_cistatic void
44bf215546Sopenharmony_ciload_color_map_texture(struct gl_context *ctx, struct pipe_resource *pt)
45bf215546Sopenharmony_ci{
46bf215546Sopenharmony_ci   struct st_context *st = st_context(ctx);
47bf215546Sopenharmony_ci   struct pipe_context *pipe = st->pipe;
48bf215546Sopenharmony_ci   struct pipe_transfer *transfer;
49bf215546Sopenharmony_ci   const GLuint rSize = ctx->PixelMaps.RtoR.Size;
50bf215546Sopenharmony_ci   const GLuint gSize = ctx->PixelMaps.GtoG.Size;
51bf215546Sopenharmony_ci   const GLuint bSize = ctx->PixelMaps.BtoB.Size;
52bf215546Sopenharmony_ci   const GLuint aSize = ctx->PixelMaps.AtoA.Size;
53bf215546Sopenharmony_ci   const uint texSize = pt->width0;
54bf215546Sopenharmony_ci   uint *dest;
55bf215546Sopenharmony_ci   uint i, j;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   dest = (uint *) pipe_texture_map(pipe,
58bf215546Sopenharmony_ci                                     pt, 0, 0, PIPE_MAP_WRITE,
59bf215546Sopenharmony_ci                                     0, 0, texSize, texSize, &transfer);
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   /* Pack four 1D maps into a 2D texture:
62bf215546Sopenharmony_ci    * R map is placed horizontally, indexed by S, in channel 0
63bf215546Sopenharmony_ci    * G map is placed vertically, indexed by T, in channel 1
64bf215546Sopenharmony_ci    * B map is placed horizontally, indexed by S, in channel 2
65bf215546Sopenharmony_ci    * A map is placed vertically, indexed by T, in channel 3
66bf215546Sopenharmony_ci    */
67bf215546Sopenharmony_ci   for (i = 0; i < texSize; i++) {
68bf215546Sopenharmony_ci      for (j = 0; j < texSize; j++) {
69bf215546Sopenharmony_ci         union util_color uc;
70bf215546Sopenharmony_ci         int k = (i * texSize + j);
71bf215546Sopenharmony_ci         float rgba[4];
72bf215546Sopenharmony_ci         rgba[0] = ctx->PixelMaps.RtoR.Map[j * rSize / texSize];
73bf215546Sopenharmony_ci         rgba[1] = ctx->PixelMaps.GtoG.Map[i * gSize / texSize];
74bf215546Sopenharmony_ci         rgba[2] = ctx->PixelMaps.BtoB.Map[j * bSize / texSize];
75bf215546Sopenharmony_ci         rgba[3] = ctx->PixelMaps.AtoA.Map[i * aSize / texSize];
76bf215546Sopenharmony_ci         util_pack_color(rgba, pt->format, &uc);
77bf215546Sopenharmony_ci         *(dest + k) = uc.ui[0];
78bf215546Sopenharmony_ci      }
79bf215546Sopenharmony_ci   }
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   pipe_texture_unmap(pipe, transfer);
82bf215546Sopenharmony_ci}
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci/**
85bf215546Sopenharmony_ci * Upload the pixel transfer color map texture.
86bf215546Sopenharmony_ci */
87bf215546Sopenharmony_civoid
88bf215546Sopenharmony_cist_update_pixel_transfer(struct st_context *st)
89bf215546Sopenharmony_ci{
90bf215546Sopenharmony_ci   struct gl_context *ctx = st->ctx;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   if (ctx->Pixel.MapColorFlag) {
93bf215546Sopenharmony_ci      /* create the colormap/texture now if not already done */
94bf215546Sopenharmony_ci      if (!st->pixel_xfer.pixelmap_texture) {
95bf215546Sopenharmony_ci         st->pixel_xfer.pixelmap_texture = st_create_color_map_texture(ctx);
96bf215546Sopenharmony_ci         st->pixel_xfer.pixelmap_sampler_view =
97bf215546Sopenharmony_ci            st_create_texture_sampler_view(st->pipe,
98bf215546Sopenharmony_ci                                           st->pixel_xfer.pixelmap_texture);
99bf215546Sopenharmony_ci      }
100bf215546Sopenharmony_ci      load_color_map_texture(ctx, st->pixel_xfer.pixelmap_texture);
101bf215546Sopenharmony_ci   }
102bf215546Sopenharmony_ci}
103