Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Mesa 3-D graphics library
  3.  * Version:  7.5
  4.  *
  5.  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  6.  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
  7.  *
  8.  * Permission is hereby granted, free of charge, to any person obtaining a
  9.  * copy of this software and associated documentation files (the "Software"),
  10.  * to deal in the Software without restriction, including without limitation
  11.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12.  * and/or sell copies of the Software, and to permit persons to whom the
  13.  * Software is furnished to do so, subject to the following conditions:
  14.  *
  15.  * The above copyright notice and this permission notice shall be included
  16.  * in all copies or substantial portions 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 MERCHANTABILITY,
  20.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  21.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  22.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24.  */
  25.  
  26. /**
  27.  * \file feedback.c
  28.  * Selection and feedback modes functions.
  29.  */
  30.  
  31.  
  32. #include "glheader.h"
  33. #include "colormac.h"
  34. #include "context.h"
  35. #include "enums.h"
  36. #include "feedback.h"
  37. #include "macros.h"
  38. #include "mtypes.h"
  39. #include "main/dispatch.h"
  40.  
  41.  
  42. #if FEATURE_feedback
  43.  
  44.  
  45. #define FB_3D           0x01
  46. #define FB_4D           0x02
  47. #define FB_COLOR        0x04
  48. #define FB_TEXTURE      0X08
  49.  
  50.  
  51.  
  52. static void GLAPIENTRY
  53. _mesa_FeedbackBuffer( GLsizei size, GLenum type, GLfloat *buffer )
  54. {
  55.    GET_CURRENT_CONTEXT(ctx);
  56.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  57.  
  58.    if (ctx->RenderMode==GL_FEEDBACK) {
  59.       _mesa_error( ctx, GL_INVALID_OPERATION, "glFeedbackBuffer" );
  60.       return;
  61.    }
  62.    if (size<0) {
  63.       _mesa_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(size<0)" );
  64.       return;
  65.    }
  66.    if (!buffer) {
  67.       _mesa_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(buffer==NULL)" );
  68.       ctx->Feedback.BufferSize = 0;
  69.       return;
  70.    }
  71.  
  72.    switch (type) {
  73.       case GL_2D:
  74.          ctx->Feedback._Mask = 0;
  75.          break;
  76.       case GL_3D:
  77.          ctx->Feedback._Mask = FB_3D;
  78.          break;
  79.       case GL_3D_COLOR:
  80.          ctx->Feedback._Mask = (FB_3D | FB_COLOR);
  81.          break;
  82.       case GL_3D_COLOR_TEXTURE:
  83.          ctx->Feedback._Mask = (FB_3D | FB_COLOR | FB_TEXTURE);
  84.          break;
  85.       case GL_4D_COLOR_TEXTURE:
  86.          ctx->Feedback._Mask = (FB_3D | FB_4D | FB_COLOR | FB_TEXTURE);
  87.          break;
  88.       default:
  89.          _mesa_error( ctx, GL_INVALID_ENUM, "glFeedbackBuffer" );
  90.          return;
  91.    }
  92.  
  93.    FLUSH_VERTICES(ctx, _NEW_RENDERMODE); /* Always flush */
  94.    ctx->Feedback.Type = type;
  95.    ctx->Feedback.BufferSize = size;
  96.    ctx->Feedback.Buffer = buffer;
  97.    ctx->Feedback.Count = 0;                   /* Becaues of this. */
  98. }
  99.  
  100.  
  101. static void GLAPIENTRY
  102. _mesa_PassThrough( GLfloat token )
  103. {
  104.    GET_CURRENT_CONTEXT(ctx);
  105.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  106.  
  107.    if (ctx->RenderMode==GL_FEEDBACK) {
  108.       FLUSH_VERTICES(ctx, 0);
  109.       _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_PASS_THROUGH_TOKEN );
  110.       _mesa_feedback_token( ctx, token );
  111.    }
  112. }
  113.  
  114.  
  115. /**
  116.  * Put a vertex into the feedback buffer.
  117.  */
  118. void
  119. _mesa_feedback_vertex(struct gl_context *ctx,
  120.                       const GLfloat win[4],
  121.                       const GLfloat color[4],
  122.                       const GLfloat texcoord[4])
  123. {
  124.    _mesa_feedback_token( ctx, win[0] );
  125.    _mesa_feedback_token( ctx, win[1] );
  126.    if (ctx->Feedback._Mask & FB_3D) {
  127.       _mesa_feedback_token( ctx, win[2] );
  128.    }
  129.    if (ctx->Feedback._Mask & FB_4D) {
  130.       _mesa_feedback_token( ctx, win[3] );
  131.    }
  132.    if (ctx->Feedback._Mask & FB_COLOR) {
  133.       _mesa_feedback_token( ctx, color[0] );
  134.       _mesa_feedback_token( ctx, color[1] );
  135.       _mesa_feedback_token( ctx, color[2] );
  136.       _mesa_feedback_token( ctx, color[3] );
  137.    }
  138.    if (ctx->Feedback._Mask & FB_TEXTURE) {
  139.       _mesa_feedback_token( ctx, texcoord[0] );
  140.       _mesa_feedback_token( ctx, texcoord[1] );
  141.       _mesa_feedback_token( ctx, texcoord[2] );
  142.       _mesa_feedback_token( ctx, texcoord[3] );
  143.    }
  144. }
  145.  
  146.  
  147. /**********************************************************************/
  148. /** \name Selection */
  149. /*@{*/
  150.  
  151. /**
  152.  * Establish a buffer for selection mode values.
  153.  *
  154.  * \param size buffer size.
  155.  * \param buffer buffer.
  156.  *
  157.  * \sa glSelectBuffer().
  158.  *
  159.  * \note this function can't be put in a display list.
  160.  *
  161.  * Verifies we're not in selection mode, flushes the vertices and initialize
  162.  * the fields in __struct gl_contextRec::Select with the given buffer.
  163.  */
  164. static void GLAPIENTRY
  165. _mesa_SelectBuffer( GLsizei size, GLuint *buffer )
  166. {
  167.    GET_CURRENT_CONTEXT(ctx);
  168.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  169.  
  170.    if (ctx->RenderMode==GL_SELECT) {
  171.       _mesa_error( ctx, GL_INVALID_OPERATION, "glSelectBuffer" );
  172.       return;                   /* KW: added return */
  173.    }
  174.  
  175.    FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
  176.    ctx->Select.Buffer = buffer;
  177.    ctx->Select.BufferSize = size;
  178.    ctx->Select.BufferCount = 0;
  179.    ctx->Select.HitFlag = GL_FALSE;
  180.    ctx->Select.HitMinZ = 1.0;
  181.    ctx->Select.HitMaxZ = 0.0;
  182. }
  183.  
  184.  
  185. /**
  186.  * Write a value of a record into the selection buffer.
  187.  *
  188.  * \param ctx GL context.
  189.  * \param value value.
  190.  *
  191.  * Verifies there is free space in the buffer to write the value and
  192.  * increments the pointer.
  193.  */
  194. static INLINE void
  195. write_record(struct gl_context *ctx, GLuint value)
  196. {
  197.    if (ctx->Select.BufferCount < ctx->Select.BufferSize) {
  198.       ctx->Select.Buffer[ctx->Select.BufferCount] = value;
  199.    }
  200.    ctx->Select.BufferCount++;
  201. }
  202.  
  203.  
  204. /**
  205.  * Update the hit flag and the maximum and minimum depth values.
  206.  *
  207.  * \param ctx GL context.
  208.  * \param z depth.
  209.  *
  210.  * Sets gl_selection::HitFlag and updates gl_selection::HitMinZ and
  211.  * gl_selection::HitMaxZ.
  212.  */
  213. void
  214. _mesa_update_hitflag(struct gl_context *ctx, GLfloat z)
  215. {
  216.    ctx->Select.HitFlag = GL_TRUE;
  217.    if (z < ctx->Select.HitMinZ) {
  218.       ctx->Select.HitMinZ = z;
  219.    }
  220.    if (z > ctx->Select.HitMaxZ) {
  221.       ctx->Select.HitMaxZ = z;
  222.    }
  223. }
  224.  
  225.  
  226. /**
  227.  * Write the hit record.
  228.  *
  229.  * \param ctx GL context.
  230.  *
  231.  * Write the hit record, i.e., the number of names in the stack, the minimum and
  232.  * maximum depth values and the number of names in the name stack at the time
  233.  * of the event. Resets the hit flag.
  234.  *
  235.  * \sa gl_selection.
  236.  */
  237. static void
  238. write_hit_record(struct gl_context *ctx)
  239. {
  240.    GLuint i;
  241.    GLuint zmin, zmax, zscale = (~0u);
  242.  
  243.    /* HitMinZ and HitMaxZ are in [0,1].  Multiply these values by */
  244.    /* 2^32-1 and round to nearest unsigned integer. */
  245.  
  246.    assert( ctx != NULL ); /* this line magically fixes a SunOS 5.x/gcc bug */
  247.    zmin = (GLuint) ((GLfloat) zscale * ctx->Select.HitMinZ);
  248.    zmax = (GLuint) ((GLfloat) zscale * ctx->Select.HitMaxZ);
  249.  
  250.    write_record( ctx, ctx->Select.NameStackDepth );
  251.    write_record( ctx, zmin );
  252.    write_record( ctx, zmax );
  253.    for (i = 0; i < ctx->Select.NameStackDepth; i++) {
  254.       write_record( ctx, ctx->Select.NameStack[i] );
  255.    }
  256.  
  257.    ctx->Select.Hits++;
  258.    ctx->Select.HitFlag = GL_FALSE;
  259.    ctx->Select.HitMinZ = 1.0;
  260.    ctx->Select.HitMaxZ = -1.0;
  261. }
  262.  
  263.  
  264. /**
  265.  * Initialize the name stack.
  266.  *
  267.  * Verifies we are in select mode and resets the name stack depth and resets
  268.  * the hit record data in gl_selection. Marks new render mode in
  269.  * __struct gl_contextRec::NewState.
  270.  */
  271. static void GLAPIENTRY
  272. _mesa_InitNames( void )
  273. {
  274.    GET_CURRENT_CONTEXT(ctx);
  275.    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
  276.  
  277.    /* Record the hit before the HitFlag is wiped out again. */
  278.    if (ctx->RenderMode == GL_SELECT) {
  279.       if (ctx->Select.HitFlag) {
  280.          write_hit_record( ctx );
  281.       }
  282.    }
  283.    ctx->Select.NameStackDepth = 0;
  284.    ctx->Select.HitFlag = GL_FALSE;
  285.    ctx->Select.HitMinZ = 1.0;
  286.    ctx->Select.HitMaxZ = 0.0;
  287.    ctx->NewState |= _NEW_RENDERMODE;
  288. }
  289.  
  290.  
  291. /**
  292.  * Load the top-most name of the name stack.
  293.  *
  294.  * \param name name.
  295.  *
  296.  * Verifies we are in selection mode and that the name stack is not empty.
  297.  * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
  298.  * and replace the top-most name in the stack.
  299.  *
  300.  * sa __struct gl_contextRec::Select.
  301.  */
  302. static void GLAPIENTRY
  303. _mesa_LoadName( GLuint name )
  304. {
  305.    GET_CURRENT_CONTEXT(ctx);
  306.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  307.  
  308.    if (ctx->RenderMode != GL_SELECT) {
  309.       return;
  310.    }
  311.    if (ctx->Select.NameStackDepth == 0) {
  312.       _mesa_error( ctx, GL_INVALID_OPERATION, "glLoadName" );
  313.       return;
  314.    }
  315.  
  316.    FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
  317.  
  318.    if (ctx->Select.HitFlag) {
  319.       write_hit_record( ctx );
  320.    }
  321.    if (ctx->Select.NameStackDepth < MAX_NAME_STACK_DEPTH) {
  322.       ctx->Select.NameStack[ctx->Select.NameStackDepth-1] = name;
  323.    }
  324.    else {
  325.       ctx->Select.NameStack[MAX_NAME_STACK_DEPTH-1] = name;
  326.    }
  327. }
  328.  
  329.  
  330. /**
  331.  * Push a name into the name stack.
  332.  *
  333.  * \param name name.
  334.  *
  335.  * Verifies we are in selection mode and that the name stack is not full.
  336.  * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
  337.  * and adds the name to the top of the name stack.
  338.  *
  339.  * sa __struct gl_contextRec::Select.
  340.  */
  341. static void GLAPIENTRY
  342. _mesa_PushName( GLuint name )
  343. {
  344.    GET_CURRENT_CONTEXT(ctx);
  345.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  346.  
  347.    if (ctx->RenderMode != GL_SELECT) {
  348.       return;
  349.    }
  350.  
  351.    FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
  352.    if (ctx->Select.HitFlag) {
  353.       write_hit_record( ctx );
  354.    }
  355.    if (ctx->Select.NameStackDepth >= MAX_NAME_STACK_DEPTH) {
  356.       _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushName" );
  357.    }
  358.    else
  359.       ctx->Select.NameStack[ctx->Select.NameStackDepth++] = name;
  360. }
  361.  
  362.  
  363. /**
  364.  * Pop a name into the name stack.
  365.  *
  366.  * Verifies we are in selection mode and that the name stack is not empty.
  367.  * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
  368.  * and removes top-most name in the name stack.
  369.  *
  370.  * sa __struct gl_contextRec::Select.
  371.  */
  372. static void GLAPIENTRY
  373. _mesa_PopName( void )
  374. {
  375.    GET_CURRENT_CONTEXT(ctx);
  376.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  377.  
  378.    if (ctx->RenderMode != GL_SELECT) {
  379.       return;
  380.    }
  381.  
  382.    FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
  383.    if (ctx->Select.HitFlag) {
  384.       write_hit_record( ctx );
  385.    }
  386.    if (ctx->Select.NameStackDepth == 0) {
  387.       _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopName" );
  388.    }
  389.    else
  390.       ctx->Select.NameStackDepth--;
  391. }
  392.  
  393. /*@}*/
  394.  
  395.  
  396. /**********************************************************************/
  397. /** \name Render Mode */
  398. /*@{*/
  399.  
  400. /**
  401.  * Set rasterization mode.
  402.  *
  403.  * \param mode rasterization mode.
  404.  *
  405.  * \note this function can't be put in a display list.
  406.  *
  407.  * \sa glRenderMode().
  408.  *
  409.  * Flushes the vertices and do the necessary cleanup according to the previous
  410.  * rasterization mode, such as writing the hit record or resent the select
  411.  * buffer index when exiting the select mode. Updates
  412.  * __struct gl_contextRec::RenderMode and notifies the driver via the
  413.  * dd_function_table::RenderMode callback.
  414.  */
  415. static GLint GLAPIENTRY
  416. _mesa_RenderMode( GLenum mode )
  417. {
  418.    GET_CURRENT_CONTEXT(ctx);
  419.    GLint result;
  420.    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
  421.  
  422.    if (MESA_VERBOSE & VERBOSE_API)
  423.       _mesa_debug(ctx, "glRenderMode %s\n", _mesa_lookup_enum_by_nr(mode));
  424.  
  425.    FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
  426.  
  427.    switch (ctx->RenderMode) {
  428.       case GL_RENDER:
  429.          result = 0;
  430.          break;
  431.       case GL_SELECT:
  432.          if (ctx->Select.HitFlag) {
  433.             write_hit_record( ctx );
  434.          }
  435.          if (ctx->Select.BufferCount > ctx->Select.BufferSize) {
  436.             /* overflow */
  437. #ifdef DEBUG
  438.             _mesa_warning(ctx, "Feedback buffer overflow");
  439. #endif
  440.             result = -1;
  441.          }
  442.          else {
  443.             result = ctx->Select.Hits;
  444.          }
  445.          ctx->Select.BufferCount = 0;
  446.          ctx->Select.Hits = 0;
  447.          ctx->Select.NameStackDepth = 0;
  448.          break;
  449. #if _HAVE_FULL_GL
  450.       case GL_FEEDBACK:
  451.          if (ctx->Feedback.Count > ctx->Feedback.BufferSize) {
  452.             /* overflow */
  453.             result = -1;
  454.          }
  455.          else {
  456.             result = ctx->Feedback.Count;
  457.          }
  458.          ctx->Feedback.Count = 0;
  459.          break;
  460. #endif
  461.       default:
  462.          _mesa_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
  463.          return 0;
  464.    }
  465.  
  466.    switch (mode) {
  467.       case GL_RENDER:
  468.          break;
  469.       case GL_SELECT:
  470.          if (ctx->Select.BufferSize==0) {
  471.             /* haven't called glSelectBuffer yet */
  472.             _mesa_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
  473.          }
  474.          break;
  475. #if _HAVE_FULL_GL
  476.       case GL_FEEDBACK:
  477.          if (ctx->Feedback.BufferSize==0) {
  478.             /* haven't called glFeedbackBuffer yet */
  479.             _mesa_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
  480.          }
  481.          break;
  482. #endif
  483.       default:
  484.          _mesa_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
  485.          return 0;
  486.    }
  487.  
  488.    ctx->RenderMode = mode;
  489.    if (ctx->Driver.RenderMode)
  490.       ctx->Driver.RenderMode( ctx, mode );
  491.  
  492.    return result;
  493. }
  494.  
  495. /*@}*/
  496.  
  497.  
  498. void
  499. _mesa_init_feedback_dispatch(struct _glapi_table *disp)
  500. {
  501.    SET_InitNames(disp, _mesa_InitNames);
  502.    SET_FeedbackBuffer(disp, _mesa_FeedbackBuffer);
  503.    SET_LoadName(disp, _mesa_LoadName);
  504.    SET_PassThrough(disp, _mesa_PassThrough);
  505.    SET_PopName(disp, _mesa_PopName);
  506.    SET_PushName(disp, _mesa_PushName);
  507.    SET_SelectBuffer(disp, _mesa_SelectBuffer);
  508.    SET_RenderMode(disp, _mesa_RenderMode);
  509. }
  510.  
  511.  
  512. #endif /* FEATURE_feedback */
  513.  
  514.  
  515. /**********************************************************************/
  516. /** \name Initialization */
  517. /*@{*/
  518.  
  519. /**
  520.  * Initialize context feedback data.
  521.  */
  522. void _mesa_init_feedback( struct gl_context * ctx )
  523. {
  524.    /* Feedback */
  525.    ctx->Feedback.Type = GL_2D;   /* TODO: verify */
  526.    ctx->Feedback.Buffer = NULL;
  527.    ctx->Feedback.BufferSize = 0;
  528.    ctx->Feedback.Count = 0;
  529.  
  530.    /* Selection/picking */
  531.    ctx->Select.Buffer = NULL;
  532.    ctx->Select.BufferSize = 0;
  533.    ctx->Select.BufferCount = 0;
  534.    ctx->Select.Hits = 0;
  535.    ctx->Select.NameStackDepth = 0;
  536.  
  537.    /* Miscellaneous */
  538.    ctx->RenderMode = GL_RENDER;
  539. }
  540.  
  541. /*@}*/
  542.