Home | History | Annotate | Download | only in main
      1 /*
      2  * Mesa 3-D graphics library
      3  *
      4  * Copyright (C) 1999-2008  Brian Paul   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 "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included
     14  * in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 
     25 #include "glheader.h"
     26 #include "imports.h"
     27 #include "api_validate.h"
     28 #include "bufferobj.h"
     29 #include "context.h"
     30 #include "drawpix.h"
     31 #include "enums.h"
     32 #include "feedback.h"
     33 #include "framebuffer.h"
     34 #include "image.h"
     35 #include "pbo.h"
     36 #include "state.h"
     37 #include "dispatch.h"
     38 #include "glformats.h"
     39 #include "fbobject.h"
     40 
     41 
     42 /*
     43  * Execute glDrawPixels
     44  */
     45 void GLAPIENTRY
     46 _mesa_DrawPixels( GLsizei width, GLsizei height,
     47                   GLenum format, GLenum type, const GLvoid *pixels )
     48 {
     49    GLenum err;
     50    GET_CURRENT_CONTEXT(ctx);
     51 
     52    FLUSH_VERTICES(ctx, 0);
     53 
     54    if (MESA_VERBOSE & VERBOSE_API)
     55       _mesa_debug(ctx, "glDrawPixels(%d, %d, %s, %s, %p) // to %s at %d, %d\n",
     56                   width, height,
     57                   _mesa_enum_to_string(format),
     58                   _mesa_enum_to_string(type),
     59                   pixels,
     60                   _mesa_enum_to_string(ctx->DrawBuffer->ColorDrawBuffer[0]),
     61                   IROUND(ctx->Current.RasterPos[0]),
     62                   IROUND(ctx->Current.RasterPos[1]));
     63 
     64 
     65    if (width < 0 || height < 0) {
     66       _mesa_error( ctx, GL_INVALID_VALUE, "glDrawPixels(width or height < 0)" );
     67       return;
     68    }
     69 
     70    /* We're not using the current vertex program, and the driver may install
     71     * its own.  Note: this may dirty some state.
     72     */
     73    _mesa_set_vp_override(ctx, GL_TRUE);
     74 
     75    /* Note: this call does state validation */
     76    if (!_mesa_valid_to_render(ctx, "glDrawPixels")) {
     77       goto end;      /* the error code was recorded */
     78    }
     79 
     80    /* GL 3.0 introduced a new restriction on glDrawPixels() over what was in
     81     * GL_EXT_texture_integer.  From section 3.7.4 ("Rasterization of Pixel
     82     * Rectangles) on page 151 of the GL 3.0 specification:
     83     *
     84     *     "If format contains integer components, as shown in table 3.6, an
     85     *      INVALID OPERATION error is generated."
     86     *
     87     * Since DrawPixels rendering would be merely undefined if not an error (due
     88     * to a lack of defined mapping from integer data to gl_Color fragment shader
     89     * input), NVIDIA's implementation also just returns this error despite
     90     * exposing GL_EXT_texture_integer, just return an error regardless.
     91     */
     92    if (_mesa_is_enum_format_integer(format)) {
     93       _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawPixels(integer format)");
     94       goto end;
     95    }
     96 
     97    err = _mesa_error_check_format_and_type(ctx, format, type);
     98    if (err != GL_NO_ERROR) {
     99       _mesa_error(ctx, err, "glDrawPixels(invalid format %s and/or type %s)",
    100                   _mesa_enum_to_string(format),
    101                   _mesa_enum_to_string(type));
    102       goto end;
    103    }
    104 
    105    /* do special format-related checks */
    106    switch (format) {
    107    case GL_STENCIL_INDEX:
    108    case GL_DEPTH_COMPONENT:
    109    case GL_DEPTH_STENCIL_EXT:
    110       /* these buffers must exist */
    111       if (!_mesa_dest_buffer_exists(ctx, format)) {
    112          _mesa_error(ctx, GL_INVALID_OPERATION,
    113                      "glDrawPixels(missing dest buffer)");
    114          goto end;
    115       }
    116       break;
    117    case GL_COLOR_INDEX:
    118       if (ctx->PixelMaps.ItoR.Size == 0 ||
    119           ctx->PixelMaps.ItoG.Size == 0 ||
    120           ctx->PixelMaps.ItoB.Size == 0) {
    121          _mesa_error(ctx, GL_INVALID_OPERATION,
    122                 "glDrawPixels(drawing color index pixels into RGB buffer)");
    123          goto end;
    124       }
    125       break;
    126    default:
    127       /* for color formats it's not an error if the destination color
    128        * buffer doesn't exist.
    129        */
    130       break;
    131    }
    132 
    133    if (ctx->RasterDiscard) {
    134       goto end;
    135    }
    136 
    137    if (!ctx->Current.RasterPosValid) {
    138       goto end;  /* no-op, not an error */
    139    }
    140 
    141    if (ctx->RenderMode == GL_RENDER) {
    142       if (width > 0 && height > 0) {
    143          /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
    144          GLint x = IROUND(ctx->Current.RasterPos[0]);
    145          GLint y = IROUND(ctx->Current.RasterPos[1]);
    146 
    147          if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
    148             /* unpack from PBO */
    149             if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height,
    150                                            1, format, type, INT_MAX, pixels)) {
    151                _mesa_error(ctx, GL_INVALID_OPERATION,
    152                            "glDrawPixels(invalid PBO access)");
    153                goto end;
    154             }
    155             if (_mesa_check_disallowed_mapping(ctx->Unpack.BufferObj)) {
    156                /* buffer is mapped - that's an error */
    157                _mesa_error(ctx, GL_INVALID_OPERATION,
    158                            "glDrawPixels(PBO is mapped)");
    159                goto end;
    160             }
    161          }
    162 
    163          ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
    164                                 &ctx->Unpack, pixels);
    165       }
    166    }
    167    else if (ctx->RenderMode == GL_FEEDBACK) {
    168       /* Feedback the current raster pos info */
    169       FLUSH_CURRENT( ctx, 0 );
    170       _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
    171       _mesa_feedback_vertex( ctx,
    172                              ctx->Current.RasterPos,
    173                              ctx->Current.RasterColor,
    174                              ctx->Current.RasterTexCoords[0] );
    175    }
    176    else {
    177       assert(ctx->RenderMode == GL_SELECT);
    178       /* Do nothing.  See OpenGL Spec, Appendix B, Corollary 6. */
    179    }
    180 
    181 end:
    182    _mesa_set_vp_override(ctx, GL_FALSE);
    183 
    184    if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
    185       _mesa_flush(ctx);
    186    }
    187 }
    188 
    189 
    190 void GLAPIENTRY
    191 _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
    192                   GLenum type )
    193 {
    194    GET_CURRENT_CONTEXT(ctx);
    195 
    196    FLUSH_VERTICES(ctx, 0);
    197 
    198    if (MESA_VERBOSE & VERBOSE_API)
    199       _mesa_debug(ctx,
    200                   "glCopyPixels(%d, %d, %d, %d, %s) // from %s to %s at %d, %d\n",
    201                   srcx, srcy, width, height,
    202                   _mesa_enum_to_string(type),
    203                   _mesa_enum_to_string(ctx->ReadBuffer->ColorReadBuffer),
    204                   _mesa_enum_to_string(ctx->DrawBuffer->ColorDrawBuffer[0]),
    205                   IROUND(ctx->Current.RasterPos[0]),
    206                   IROUND(ctx->Current.RasterPos[1]));
    207 
    208    if (width < 0 || height < 0) {
    209       _mesa_error(ctx, GL_INVALID_VALUE, "glCopyPixels(width or height < 0)");
    210       return;
    211    }
    212 
    213    /* Note: more detailed 'type' checking is done by the
    214     * _mesa_source/dest_buffer_exists() calls below.  That's where we
    215     * check if the stencil buffer exists, etc.
    216     */
    217    if (type != GL_COLOR &&
    218        type != GL_DEPTH &&
    219        type != GL_STENCIL &&
    220        type != GL_DEPTH_STENCIL) {
    221       _mesa_error(ctx, GL_INVALID_ENUM, "glCopyPixels(type=%s)",
    222                   _mesa_enum_to_string(type));
    223       return;
    224    }
    225 
    226    /* We're not using the current vertex program, and the driver may install
    227     * it's own.  Note: this may dirty some state.
    228     */
    229    _mesa_set_vp_override(ctx, GL_TRUE);
    230 
    231    /* Note: this call does state validation */
    232    if (!_mesa_valid_to_render(ctx, "glCopyPixels")) {
    233       goto end;      /* the error code was recorded */
    234    }
    235 
    236    /* Check read buffer's status (draw buffer was already checked) */
    237    if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
    238       _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
    239                   "glCopyPixels(incomplete framebuffer)" );
    240       goto end;
    241    }
    242 
    243    if (_mesa_is_user_fbo(ctx->ReadBuffer) &&
    244        ctx->ReadBuffer->Visual.samples > 0) {
    245       _mesa_error(ctx, GL_INVALID_OPERATION,
    246 		  "glCopyPixels(multisample FBO)");
    247       goto end;
    248    }
    249 
    250    if (!_mesa_source_buffer_exists(ctx, type) ||
    251        !_mesa_dest_buffer_exists(ctx, type)) {
    252       _mesa_error(ctx, GL_INVALID_OPERATION,
    253                   "glCopyPixels(missing source or dest buffer)");
    254       goto end;
    255    }
    256 
    257    if (ctx->RasterDiscard) {
    258       goto end;
    259    }
    260 
    261    if (!ctx->Current.RasterPosValid || width == 0 || height == 0) {
    262       goto end; /* no-op, not an error */
    263    }
    264 
    265    if (ctx->RenderMode == GL_RENDER) {
    266       /* Round to satisfy conformance tests (matches SGI's OpenGL) */
    267       if (width > 0 && height > 0) {
    268          GLint destx = IROUND(ctx->Current.RasterPos[0]);
    269          GLint desty = IROUND(ctx->Current.RasterPos[1]);
    270          ctx->Driver.CopyPixels( ctx, srcx, srcy, width, height, destx, desty,
    271                                  type );
    272       }
    273    }
    274    else if (ctx->RenderMode == GL_FEEDBACK) {
    275       FLUSH_CURRENT( ctx, 0 );
    276       _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_COPY_PIXEL_TOKEN );
    277       _mesa_feedback_vertex( ctx,
    278                              ctx->Current.RasterPos,
    279                              ctx->Current.RasterColor,
    280                              ctx->Current.RasterTexCoords[0] );
    281    }
    282    else {
    283       assert(ctx->RenderMode == GL_SELECT);
    284       /* Do nothing.  See OpenGL Spec, Appendix B, Corollary 6. */
    285    }
    286 
    287 end:
    288    _mesa_set_vp_override(ctx, GL_FALSE);
    289 
    290    if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
    291       _mesa_flush(ctx);
    292    }
    293 }
    294 
    295 
    296 void GLAPIENTRY
    297 _mesa_Bitmap( GLsizei width, GLsizei height,
    298               GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove,
    299               const GLubyte *bitmap )
    300 {
    301    GET_CURRENT_CONTEXT(ctx);
    302 
    303    FLUSH_VERTICES(ctx, 0);
    304 
    305    if (width < 0 || height < 0) {
    306       _mesa_error( ctx, GL_INVALID_VALUE, "glBitmap(width or height < 0)" );
    307       return;
    308    }
    309 
    310    if (!ctx->Current.RasterPosValid) {
    311       return;    /* do nothing */
    312    }
    313 
    314    /* Note: this call does state validation */
    315    if (!_mesa_valid_to_render(ctx, "glBitmap")) {
    316       /* the error code was recorded */
    317       return;
    318    }
    319 
    320    if (ctx->RasterDiscard)
    321       return;
    322 
    323    if (ctx->RenderMode == GL_RENDER) {
    324       /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
    325       if (width > 0 && height > 0) {
    326          const GLfloat epsilon = 0.0001F;
    327          GLint x = IFLOOR(ctx->Current.RasterPos[0] + epsilon - xorig);
    328          GLint y = IFLOOR(ctx->Current.RasterPos[1] + epsilon - yorig);
    329 
    330          if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
    331             /* unpack from PBO */
    332             if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height,
    333                                            1, GL_COLOR_INDEX, GL_BITMAP,
    334                                            INT_MAX, (const GLvoid *) bitmap)) {
    335                _mesa_error(ctx, GL_INVALID_OPERATION,
    336                            "glBitmap(invalid PBO access)");
    337                return;
    338             }
    339             if (_mesa_check_disallowed_mapping(ctx->Unpack.BufferObj)) {
    340                /* buffer is mapped - that's an error */
    341                _mesa_error(ctx, GL_INVALID_OPERATION,
    342                            "glBitmap(PBO is mapped)");
    343                return;
    344             }
    345          }
    346 
    347          ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
    348       }
    349    }
    350    else if (ctx->RenderMode == GL_FEEDBACK) {
    351       FLUSH_CURRENT(ctx, 0);
    352       _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
    353       _mesa_feedback_vertex( ctx,
    354                              ctx->Current.RasterPos,
    355                              ctx->Current.RasterColor,
    356                              ctx->Current.RasterTexCoords[0] );
    357    }
    358    else {
    359       assert(ctx->RenderMode == GL_SELECT);
    360       /* Do nothing.  See OpenGL Spec, Appendix B, Corollary 6. */
    361    }
    362 
    363    /* update raster position */
    364    ctx->Current.RasterPos[0] += xmove;
    365    ctx->Current.RasterPos[1] += ymove;
    366 
    367    if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
    368       _mesa_flush(ctx);
    369    }
    370 }
    371