Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
  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 TUNGSTEN GRAPHICS 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. /*
  29.  * Generate fragment programs to implement pixel transfer ops, such as
  30.  * scale/bias, colortable, convolution...
  31.  *
  32.  * Authors:
  33.  *   Brian Paul
  34.  */
  35.  
  36. #include "main/imports.h"
  37. #include "main/image.h"
  38. #include "main/macros.h"
  39. #include "program/program.h"
  40. #include "program/prog_cache.h"
  41. #include "program/prog_instruction.h"
  42. #include "program/prog_parameter.h"
  43. #include "program/prog_print.h"
  44.  
  45. #include "st_context.h"
  46. #include "st_format.h"
  47. #include "st_texture.h"
  48.  
  49. #include "pipe/p_screen.h"
  50. #include "pipe/p_context.h"
  51. #include "util/u_inlines.h"
  52. #include "util/u_pack_color.h"
  53.  
  54.  
  55. struct state_key
  56. {
  57.    GLuint scaleAndBias:1;
  58.    GLuint pixelMaps:1;
  59.  
  60. #if 0
  61.    GLfloat Maps[3][256][4];
  62.    int NumMaps;
  63.    GLint NumStages;
  64.    pipeline_stage Stages[STAGE_MAX];
  65.    GLboolean StagesUsed[STAGE_MAX];
  66.    GLfloat Scale1[4], Bias1[4];
  67.    GLfloat Scale2[4], Bias2[4];
  68. #endif
  69. };
  70.  
  71. static void
  72. make_state_key(struct gl_context *ctx,  struct state_key *key)
  73. {
  74.    memset(key, 0, sizeof(*key));
  75.  
  76.    if (ctx->Pixel.RedBias != 0.0 || ctx->Pixel.RedScale != 1.0 ||
  77.        ctx->Pixel.GreenBias != 0.0 || ctx->Pixel.GreenScale != 1.0 ||
  78.        ctx->Pixel.BlueBias != 0.0 || ctx->Pixel.BlueScale != 1.0 ||
  79.        ctx->Pixel.AlphaBias != 0.0 || ctx->Pixel.AlphaScale != 1.0) {
  80.       key->scaleAndBias = 1;
  81.    }
  82.  
  83.    key->pixelMaps = ctx->Pixel.MapColorFlag;
  84. }
  85.  
  86.  
  87. /**
  88.  * Update the pixelmap texture with the contents of the R/G/B/A pixel maps.
  89.  */
  90. static void
  91. load_color_map_texture(struct gl_context *ctx, struct pipe_resource *pt)
  92. {
  93.    struct st_context *st = st_context(ctx);
  94.    struct pipe_context *pipe = st->pipe;
  95.    struct pipe_transfer *transfer;
  96.    const GLuint rSize = ctx->PixelMaps.RtoR.Size;
  97.    const GLuint gSize = ctx->PixelMaps.GtoG.Size;
  98.    const GLuint bSize = ctx->PixelMaps.BtoB.Size;
  99.    const GLuint aSize = ctx->PixelMaps.AtoA.Size;
  100.    const uint texSize = pt->width0;
  101.    uint *dest;
  102.    uint i, j;
  103.  
  104.    dest = (uint *) pipe_transfer_map(pipe,
  105.                                      pt, 0, 0, PIPE_TRANSFER_WRITE,
  106.                                      0, 0, texSize, texSize, &transfer);
  107.  
  108.    /* Pack four 1D maps into a 2D texture:
  109.     * R map is placed horizontally, indexed by S, in channel 0
  110.     * G map is placed vertically, indexed by T, in channel 1
  111.     * B map is placed horizontally, indexed by S, in channel 2
  112.     * A map is placed vertically, indexed by T, in channel 3
  113.     */
  114.    for (i = 0; i < texSize; i++) {
  115.       for (j = 0; j < texSize; j++) {
  116.          union util_color uc;
  117.          int k = (i * texSize + j);
  118.          float rgba[4];
  119.          rgba[0] = ctx->PixelMaps.RtoR.Map[j * rSize / texSize];
  120.          rgba[1] = ctx->PixelMaps.GtoG.Map[i * gSize / texSize];
  121.          rgba[2] = ctx->PixelMaps.BtoB.Map[j * bSize / texSize];
  122.          rgba[3] = ctx->PixelMaps.AtoA.Map[i * aSize / texSize];
  123.          util_pack_color(rgba, pt->format, &uc);
  124.          *(dest + k) = uc.ui;
  125.       }
  126.    }
  127.  
  128.    pipe_transfer_unmap(pipe, transfer);
  129. }
  130.  
  131.  
  132.  
  133. #define MAX_INST 100
  134.  
  135. /**
  136.  * Returns a fragment program which implements the current pixel transfer ops.
  137.  */
  138. static struct gl_fragment_program *
  139. get_pixel_transfer_program(struct gl_context *ctx, const struct state_key *key)
  140. {
  141.    struct st_context *st = st_context(ctx);
  142.    struct prog_instruction inst[MAX_INST];
  143.    struct gl_program_parameter_list *params;
  144.    struct gl_fragment_program *fp;
  145.    GLuint ic = 0;
  146.    const GLuint colorTemp = 0;
  147.  
  148.    fp = (struct gl_fragment_program *)
  149.       ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
  150.    if (!fp)
  151.       return NULL;
  152.  
  153.    params = _mesa_new_parameter_list();
  154.  
  155.    /*
  156.     * Get initial pixel color from the texture.
  157.     * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
  158.     */
  159.    _mesa_init_instructions(inst + ic, 1);
  160.    inst[ic].Opcode = OPCODE_TEX;
  161.    inst[ic].DstReg.File = PROGRAM_TEMPORARY;
  162.    inst[ic].DstReg.Index = colorTemp;
  163.    inst[ic].SrcReg[0].File = PROGRAM_INPUT;
  164.    inst[ic].SrcReg[0].Index = VARYING_SLOT_TEX0;
  165.    inst[ic].TexSrcUnit = 0;
  166.    inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
  167.    ic++;
  168.    fp->Base.InputsRead = BITFIELD64_BIT(VARYING_SLOT_TEX0);
  169.    fp->Base.OutputsWritten = BITFIELD64_BIT(FRAG_RESULT_COLOR);
  170.    fp->Base.SamplersUsed = 0x1;  /* sampler 0 (bit 0) is used */
  171.  
  172.    if (key->scaleAndBias) {
  173.       static const gl_state_index scale_state[STATE_LENGTH] =
  174.          { STATE_INTERNAL, STATE_PT_SCALE, 0, 0, 0 };
  175.       static const gl_state_index bias_state[STATE_LENGTH] =
  176.          { STATE_INTERNAL, STATE_PT_BIAS, 0, 0, 0 };
  177.       GLint scale_p, bias_p;
  178.  
  179.       scale_p = _mesa_add_state_reference(params, scale_state);
  180.       bias_p = _mesa_add_state_reference(params, bias_state);
  181.  
  182.       /* MAD colorTemp, colorTemp, scale, bias; */
  183.       _mesa_init_instructions(inst + ic, 1);
  184.       inst[ic].Opcode = OPCODE_MAD;
  185.       inst[ic].DstReg.File = PROGRAM_TEMPORARY;
  186.       inst[ic].DstReg.Index = colorTemp;
  187.       inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
  188.       inst[ic].SrcReg[0].Index = colorTemp;
  189.       inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
  190.       inst[ic].SrcReg[1].Index = scale_p;
  191.       inst[ic].SrcReg[2].File = PROGRAM_STATE_VAR;
  192.       inst[ic].SrcReg[2].Index = bias_p;
  193.       ic++;
  194.    }
  195.  
  196.    if (key->pixelMaps) {
  197.       const GLuint temp = 1;
  198.  
  199.       /* create the colormap/texture now if not already done */
  200.       if (!st->pixel_xfer.pixelmap_texture) {
  201.          st->pixel_xfer.pixelmap_texture = st_create_color_map_texture(ctx);
  202.          st->pixel_xfer.pixelmap_sampler_view =
  203.             st_create_texture_sampler_view(st->pipe,
  204.                                            st->pixel_xfer.pixelmap_texture);
  205.       }
  206.  
  207.       /* with a little effort, we can do four pixel map look-ups with
  208.        * two TEX instructions:
  209.        */
  210.  
  211.       /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
  212.       _mesa_init_instructions(inst + ic, 1);
  213.       inst[ic].Opcode = OPCODE_TEX;
  214.       inst[ic].DstReg.File = PROGRAM_TEMPORARY;
  215.       inst[ic].DstReg.Index = temp;
  216.       inst[ic].DstReg.WriteMask = WRITEMASK_XY; /* write R,G */
  217.       inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
  218.       inst[ic].SrcReg[0].Index = colorTemp;
  219.       inst[ic].TexSrcUnit = 1;
  220.       inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
  221.       ic++;
  222.  
  223.       /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
  224.       _mesa_init_instructions(inst + ic, 1);
  225.       inst[ic].Opcode = OPCODE_TEX;
  226.       inst[ic].DstReg.File = PROGRAM_TEMPORARY;
  227.       inst[ic].DstReg.Index = temp;
  228.       inst[ic].DstReg.WriteMask = WRITEMASK_ZW; /* write B,A */
  229.       inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
  230.       inst[ic].SrcReg[0].Index = colorTemp;
  231.       inst[ic].SrcReg[0].Swizzle = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W,
  232.                                                  SWIZZLE_Z, SWIZZLE_W);
  233.       inst[ic].TexSrcUnit = 1;
  234.       inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
  235.       ic++;
  236.  
  237.       /* MOV colorTemp, temp; */
  238.       _mesa_init_instructions(inst + ic, 1);
  239.       inst[ic].Opcode = OPCODE_MOV;
  240.       inst[ic].DstReg.File = PROGRAM_TEMPORARY;
  241.       inst[ic].DstReg.Index = colorTemp;
  242.       inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
  243.       inst[ic].SrcReg[0].Index = temp;
  244.       ic++;
  245.  
  246.       fp->Base.SamplersUsed |= (1 << 1);  /* sampler 1 is used */
  247.    }
  248.  
  249.    /* Modify last instruction's dst reg to write to result.color */
  250.    {
  251.       struct prog_instruction *last = &inst[ic - 1];
  252.       last->DstReg.File = PROGRAM_OUTPUT;
  253.       last->DstReg.Index = FRAG_RESULT_COLOR;
  254.    }
  255.  
  256.    /* END; */
  257.    _mesa_init_instructions(inst + ic, 1);
  258.    inst[ic].Opcode = OPCODE_END;
  259.    ic++;
  260.  
  261.    assert(ic <= MAX_INST);
  262.  
  263.  
  264.    fp->Base.Instructions = _mesa_alloc_instructions(ic);
  265.    if (!fp->Base.Instructions) {
  266.       _mesa_error(ctx, GL_OUT_OF_MEMORY,
  267.                   "generating pixel transfer program");
  268.       _mesa_free_parameter_list(params);
  269.       return NULL;
  270.    }
  271.  
  272.    _mesa_copy_instructions(fp->Base.Instructions, inst, ic);
  273.    fp->Base.NumInstructions = ic;
  274.    fp->Base.Parameters = params;
  275.  
  276. #if 0
  277.    printf("========= pixel transfer prog\n");
  278.    _mesa_print_program(&fp->Base);
  279.    _mesa_print_parameter_list(fp->Base.Parameters);
  280. #endif
  281.  
  282.    return fp;
  283. }
  284.  
  285.  
  286.  
  287. /**
  288.  * Update st->pixel_xfer.program in response to new pixel-transfer state.
  289.  */
  290. static void
  291. update_pixel_transfer(struct st_context *st)
  292. {
  293.    struct gl_context *ctx = st->ctx;
  294.    struct state_key key;
  295.    struct gl_fragment_program *fp;
  296.  
  297.    make_state_key(st->ctx, &key);
  298.  
  299.    fp = (struct gl_fragment_program *)
  300.       _mesa_search_program_cache(st->pixel_xfer.cache, &key, sizeof(key));
  301.    if (!fp) {
  302.       fp = get_pixel_transfer_program(st->ctx, &key);
  303.       _mesa_program_cache_insert(st->ctx, st->pixel_xfer.cache,
  304.                                  &key, sizeof(key), &fp->Base);
  305.    }
  306.  
  307.    if (ctx->Pixel.MapColorFlag) {
  308.       load_color_map_texture(ctx, st->pixel_xfer.pixelmap_texture);
  309.    }
  310.    st->pixel_xfer.pixelmap_enabled = ctx->Pixel.MapColorFlag;
  311.  
  312.    st->pixel_xfer.program = (struct st_fragment_program *) fp;
  313. }
  314.  
  315.  
  316.  
  317. const struct st_tracked_state st_update_pixel_transfer = {
  318.    "st_update_pixel_transfer",                          /* name */
  319.    {                                                    /* dirty */
  320.       _NEW_PIXEL,                                       /* mesa */
  321.       0,                                                /* st */
  322.    },
  323.    update_pixel_transfer                                /* update */
  324. };
  325.