Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 2009  VMware, Inc.  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.  
  26. /**
  27.  * \file viewport.c
  28.  * glViewport and glDepthRange functions.
  29.  */
  30.  
  31.  
  32. #include "context.h"
  33. #include "enums.h"
  34. #include "macros.h"
  35. #include "mtypes.h"
  36. #include "viewport.h"
  37.  
  38. static void
  39. set_viewport_no_notify(struct gl_context *ctx, unsigned idx,
  40.                        GLfloat x, GLfloat y,
  41.                        GLfloat width, GLfloat height)
  42. {
  43.    /* clamp width and height to the implementation dependent range */
  44.    width  = MIN2(width, (GLfloat) ctx->Const.MaxViewportWidth);
  45.    height = MIN2(height, (GLfloat) ctx->Const.MaxViewportHeight);
  46.  
  47.    /* The GL_ARB_viewport_array spec says:
  48.     *
  49.     *     "The location of the viewport's bottom-left corner, given by (x,y),
  50.     *     are clamped to be within the implementation-dependent viewport
  51.     *     bounds range.  The viewport bounds range [min, max] tuple may be
  52.     *     determined by calling GetFloatv with the symbolic constant
  53.     *     VIEWPORT_BOUNDS_RANGE (see section 6.1)."
  54.     */
  55.    if (ctx->Extensions.ARB_viewport_array) {
  56.       x = CLAMP(x,
  57.                 ctx->Const.ViewportBounds.Min, ctx->Const.ViewportBounds.Max);
  58.       y = CLAMP(y,
  59.                 ctx->Const.ViewportBounds.Min, ctx->Const.ViewportBounds.Max);
  60.    }
  61.  
  62.    if (ctx->ViewportArray[idx].X == x &&
  63.        ctx->ViewportArray[idx].Width == width &&
  64.        ctx->ViewportArray[idx].Y == y &&
  65.        ctx->ViewportArray[idx].Height == height)
  66.       return;
  67.  
  68.    ctx->ViewportArray[idx].X = x;
  69.    ctx->ViewportArray[idx].Width = width;
  70.    ctx->ViewportArray[idx].Y = y;
  71.    ctx->ViewportArray[idx].Height = height;
  72.    ctx->NewState |= _NEW_VIEWPORT;
  73. }
  74.  
  75. struct gl_viewport_inputs {
  76.    GLfloat X, Y;                /**< position */
  77.    GLfloat Width, Height;       /**< size */
  78. };
  79.  
  80. struct gl_depthrange_inputs {
  81.    GLdouble Near, Far;          /**< Depth buffer range */
  82. };
  83.  
  84. /**
  85.  * Set the viewport.
  86.  * \sa Called via glViewport() or display list execution.
  87.  *
  88.  * Flushes the vertices and calls _mesa_set_viewport() with the given
  89.  * parameters.
  90.  */
  91. void GLAPIENTRY
  92. _mesa_Viewport(GLint x, GLint y, GLsizei width, GLsizei height)
  93. {
  94.    unsigned i;
  95.    GET_CURRENT_CONTEXT(ctx);
  96.    FLUSH_VERTICES(ctx, 0);
  97.  
  98.    if (MESA_VERBOSE & VERBOSE_API)
  99.       _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
  100.  
  101.    if (width < 0 || height < 0) {
  102.       _mesa_error(ctx,  GL_INVALID_VALUE,
  103.                    "glViewport(%d, %d, %d, %d)", x, y, width, height);
  104.       return;
  105.    }
  106.  
  107.    /* The GL_ARB_viewport_array spec says:
  108.     *
  109.     *     "Viewport sets the parameters for all viewports to the same values
  110.     *     and is equivalent (assuming no errors are generated) to:
  111.     *
  112.     *     for (uint i = 0; i < MAX_VIEWPORTS; i++)
  113.     *         ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
  114.     *
  115.     * Set all of the viewports supported by the implementation, but only
  116.     * signal the driver once at the end.
  117.     */
  118.    for (i = 0; i < ctx->Const.MaxViewports; i++)
  119.       set_viewport_no_notify(ctx, i, x, y, width, height);
  120.  
  121.    if (ctx->Driver.Viewport) {
  122.       /* Many drivers will use this call to check for window size changes
  123.        * and reallocate the z/stencil/accum/etc buffers if needed.
  124.        */
  125.       ctx->Driver.Viewport(ctx);
  126.    }
  127. }
  128.  
  129.  
  130. /**
  131.  * Set new viewport parameters and update derived state.
  132.  * Usually called from _mesa_Viewport().
  133.  *
  134.  * \param ctx GL context.
  135.  * \param idx    Index of the viewport to be updated.
  136.  * \param x, y coordinates of the lower left corner of the viewport rectangle.
  137.  * \param width width of the viewport rectangle.
  138.  * \param height height of the viewport rectangle.
  139.  */
  140. void
  141. _mesa_set_viewport(struct gl_context *ctx, unsigned idx, GLfloat x, GLfloat y,
  142.                     GLfloat width, GLfloat height)
  143. {
  144.    set_viewport_no_notify(ctx, idx, x, y, width, height);
  145.  
  146.    if (ctx->Driver.Viewport) {
  147.       /* Many drivers will use this call to check for window size changes
  148.        * and reallocate the z/stencil/accum/etc buffers if needed.
  149.        */
  150.       ctx->Driver.Viewport(ctx);
  151.    }
  152. }
  153.  
  154. void GLAPIENTRY
  155. _mesa_ViewportArrayv(GLuint first, GLsizei count, const GLfloat *v)
  156. {
  157.    int i;
  158.    const struct gl_viewport_inputs *const p = (struct gl_viewport_inputs *) v;
  159.    GET_CURRENT_CONTEXT(ctx);
  160.  
  161.    if (MESA_VERBOSE & VERBOSE_API)
  162.       _mesa_debug(ctx, "glViewportArrayv %d %d\n", first, count);
  163.  
  164.    if ((first + count) > ctx->Const.MaxViewports) {
  165.       _mesa_error(ctx, GL_INVALID_VALUE,
  166.                   "glViewportArrayv: first (%d) + count (%d) > MaxViewports "
  167.                   "(%d)",
  168.                   first, count, ctx->Const.MaxViewports);
  169.       return;
  170.    }
  171.  
  172.    /* Verify width & height */
  173.    for (i = 0; i < count; i++) {
  174.       if (p[i].Width < 0 || p[i].Height < 0) {
  175.          _mesa_error(ctx, GL_INVALID_VALUE,
  176.                      "glViewportArrayv: index (%d) width or height < 0 "
  177.                      "(%f, %f)",
  178.                      i + first, p[i].Width, p[i].Height);
  179.          return;
  180.       }
  181.    }
  182.  
  183.    for (i = 0; i < count; i++)
  184.       set_viewport_no_notify(ctx, i + first,
  185.                              p[i].X, p[i].Y,
  186.                              p[i].Width, p[i].Height);
  187.  
  188.    if (ctx->Driver.Viewport)
  189.       ctx->Driver.Viewport(ctx);
  190. }
  191.  
  192. static void
  193. ViewportIndexedf(GLuint index, GLfloat x, GLfloat y,
  194.                  GLfloat w, GLfloat h, const char *function)
  195. {
  196.    GET_CURRENT_CONTEXT(ctx);
  197.  
  198.    if (MESA_VERBOSE & VERBOSE_API)
  199.       _mesa_debug(ctx, "%s(%d, %f, %f, %f, %f)\n",
  200.                   function, index, x, y, w, h);
  201.  
  202.    if (index >= ctx->Const.MaxViewports) {
  203.       _mesa_error(ctx, GL_INVALID_VALUE,
  204.                   "%s: index (%d) >= MaxViewports (%d)",
  205.                   function, index, ctx->Const.MaxViewports);
  206.       return;
  207.    }
  208.  
  209.    /* Verify width & height */
  210.    if (w < 0 || h < 0) {
  211.       _mesa_error(ctx, GL_INVALID_VALUE,
  212.                   "%s: index (%d) width or height < 0 (%f, %f)",
  213.                   function, index, w, h);
  214.       return;
  215.    }
  216.  
  217.    _mesa_set_viewport(ctx, index, x, y, w, h);
  218. }
  219.  
  220. void GLAPIENTRY
  221. _mesa_ViewportIndexedf(GLuint index, GLfloat x, GLfloat y,
  222.                        GLfloat w, GLfloat h)
  223. {
  224.    ViewportIndexedf(index, x, y, w, h, "glViewportIndexedf");
  225. }
  226.  
  227. void GLAPIENTRY
  228. _mesa_ViewportIndexedfv(GLuint index, const GLfloat *v)
  229. {
  230.    ViewportIndexedf(index, v[0], v[1], v[2], v[3], "glViewportIndexedfv");
  231. }
  232.  
  233. static void
  234. set_depth_range_no_notify(struct gl_context *ctx, unsigned idx,
  235.                           GLclampd nearval, GLclampd farval)
  236. {
  237.    if (ctx->ViewportArray[idx].Near == nearval &&
  238.        ctx->ViewportArray[idx].Far == farval)
  239.       return;
  240.  
  241.    ctx->ViewportArray[idx].Near = CLAMP(nearval, 0.0, 1.0);
  242.    ctx->ViewportArray[idx].Far = CLAMP(farval, 0.0, 1.0);
  243.    ctx->NewState |= _NEW_VIEWPORT;
  244. }
  245.  
  246. void
  247. _mesa_set_depth_range(struct gl_context *ctx, unsigned idx,
  248.                       GLclampd nearval, GLclampd farval)
  249. {
  250.    set_depth_range_no_notify(ctx, idx, nearval, farval);
  251.  
  252.    if (ctx->Driver.DepthRange)
  253.       ctx->Driver.DepthRange(ctx);
  254. }
  255.  
  256. /**
  257.  * Called by glDepthRange
  258.  *
  259.  * \param nearval  specifies the Z buffer value which should correspond to
  260.  *                 the near clip plane
  261.  * \param farval  specifies the Z buffer value which should correspond to
  262.  *                the far clip plane
  263.  */
  264. void GLAPIENTRY
  265. _mesa_DepthRange(GLclampd nearval, GLclampd farval)
  266. {
  267.    unsigned i;
  268.    GET_CURRENT_CONTEXT(ctx);
  269.  
  270.    FLUSH_VERTICES(ctx, 0);
  271.  
  272.    if (MESA_VERBOSE&VERBOSE_API)
  273.       _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
  274.  
  275.    /* The GL_ARB_viewport_array spec says:
  276.     *
  277.     *     "DepthRange sets the depth range for all viewports to the same
  278.     *     values and is equivalent (assuming no errors are generated) to:
  279.     *
  280.     *     for (uint i = 0; i < MAX_VIEWPORTS; i++)
  281.     *         DepthRangeIndexed(i, n, f);"
  282.     *
  283.     * Set the depth range for all of the viewports supported by the
  284.     * implementation, but only signal the driver once at the end.
  285.     */
  286.    for (i = 0; i < ctx->Const.MaxViewports; i++)
  287.       set_depth_range_no_notify(ctx, i, nearval, farval);
  288.  
  289.    if (ctx->Driver.DepthRange) {
  290.       ctx->Driver.DepthRange(ctx);
  291.    }
  292. }
  293.  
  294. void GLAPIENTRY
  295. _mesa_DepthRangef(GLclampf nearval, GLclampf farval)
  296. {
  297.    _mesa_DepthRange(nearval, farval);
  298. }
  299.  
  300. /**
  301.  * Update a range DepthRange values
  302.  *
  303.  * \param first   starting array index
  304.  * \param count   count of DepthRange items to update
  305.  * \param v       pointer to memory containing
  306.  *                GLclampd near and far clip-plane values
  307.  */
  308. void GLAPIENTRY
  309. _mesa_DepthRangeArrayv(GLuint first, GLsizei count, const GLclampd *v)
  310. {
  311.    int i;
  312.    const struct gl_depthrange_inputs *const p =
  313.       (struct gl_depthrange_inputs *) v;
  314.    GET_CURRENT_CONTEXT(ctx);
  315.  
  316.    if (MESA_VERBOSE & VERBOSE_API)
  317.       _mesa_debug(ctx, "glDepthRangeArrayv %d %d\n", first, count);
  318.  
  319.    if ((first + count) > ctx->Const.MaxViewports) {
  320.       _mesa_error(ctx, GL_INVALID_VALUE,
  321.                   "glDepthRangev: first (%d) + count (%d) >= MaxViewports (%d)",
  322.                   first, count, ctx->Const.MaxViewports);
  323.       return;
  324.    }
  325.  
  326.    for (i = 0; i < count; i++)
  327.       set_depth_range_no_notify(ctx, i + first, p[i].Near, p[i].Far);
  328.  
  329.    if (ctx->Driver.DepthRange)
  330.       ctx->Driver.DepthRange(ctx);
  331. }
  332.  
  333. /**
  334.  * Update a single DepthRange
  335.  *
  336.  * \param index    array index to update
  337.  * \param nearval  specifies the Z buffer value which should correspond to
  338.  *                 the near clip plane
  339.  * \param farval   specifies the Z buffer value which should correspond to
  340.  *                 the far clip plane
  341.  */
  342. void GLAPIENTRY
  343. _mesa_DepthRangeIndexed(GLuint index, GLclampd nearval, GLclampd farval)
  344. {
  345.    GET_CURRENT_CONTEXT(ctx);
  346.  
  347.    if (MESA_VERBOSE & VERBOSE_API)
  348.       _mesa_debug(ctx, "glDepthRangeIndexed(%d, %f, %f)\n",
  349.                   index, nearval, farval);
  350.  
  351.    if (index >= ctx->Const.MaxViewports) {
  352.       _mesa_error(ctx, GL_INVALID_VALUE,
  353.                   "glDepthRangeIndexed: index (%d) >= MaxViewports (%d)",
  354.                   index, ctx->Const.MaxViewports);
  355.       return;
  356.    }
  357.  
  358.    _mesa_set_depth_range(ctx, index, nearval, farval);
  359. }
  360.  
  361. /**
  362.  * Initialize the context viewport attribute group.
  363.  * \param ctx  the GL context.
  364.  */
  365. void _mesa_init_viewport(struct gl_context *ctx)
  366. {
  367.    unsigned i;
  368.  
  369.    ctx->Transform.ClipOrigin = GL_LOWER_LEFT;
  370.    ctx->Transform.ClipDepthMode = GL_NEGATIVE_ONE_TO_ONE;
  371.  
  372.    /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
  373.     * so just initialize all of them.
  374.     */
  375.    for (i = 0; i < MAX_VIEWPORTS; i++) {
  376.       /* Viewport group */
  377.       ctx->ViewportArray[i].X = 0;
  378.       ctx->ViewportArray[i].Y = 0;
  379.       ctx->ViewportArray[i].Width = 0;
  380.       ctx->ViewportArray[i].Height = 0;
  381.       ctx->ViewportArray[i].Near = 0.0;
  382.       ctx->ViewportArray[i].Far = 1.0;
  383.    }
  384. }
  385.  
  386.  
  387. extern void GLAPIENTRY
  388. _mesa_ClipControl(GLenum origin, GLenum depth)
  389. {
  390.    GET_CURRENT_CONTEXT(ctx);
  391.  
  392.    if (MESA_VERBOSE&VERBOSE_API)
  393.       _mesa_debug(ctx, "glClipControl(%s, %s)\n",
  394.                   _mesa_lookup_enum_by_nr(origin),
  395.                   _mesa_lookup_enum_by_nr(depth));
  396.  
  397.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  398.  
  399.    if (!ctx->Extensions.ARB_clip_control) {
  400.       _mesa_error(ctx, GL_INVALID_OPERATION, "glClipControl");
  401.       return;
  402.    }
  403.  
  404.    if (origin != GL_LOWER_LEFT && origin != GL_UPPER_LEFT) {
  405.       _mesa_error(ctx, GL_INVALID_ENUM, "glClipControl");
  406.       return;
  407.    }
  408.  
  409.    if (depth != GL_NEGATIVE_ONE_TO_ONE && depth != GL_ZERO_TO_ONE) {
  410.       _mesa_error(ctx, GL_INVALID_ENUM, "glClipControl");
  411.       return;
  412.    }
  413.  
  414.    if (ctx->Transform.ClipOrigin == origin &&
  415.        ctx->Transform.ClipDepthMode == depth)
  416.       return;
  417.  
  418.    /* Affects transform state and the viewport transform */
  419.    FLUSH_VERTICES(ctx, _NEW_TRANSFORM | _NEW_VIEWPORT);
  420.  
  421.    if (ctx->Transform.ClipOrigin != origin) {
  422.       ctx->Transform.ClipOrigin = origin;
  423.  
  424.       /* Affects the winding order of the front face. */
  425.       ctx->NewState |= _NEW_POLYGON;
  426.  
  427.       if (ctx->Driver.FrontFace)
  428.          ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
  429.    }
  430.  
  431.    if (ctx->Transform.ClipDepthMode != depth) {
  432.       ctx->Transform.ClipDepthMode = depth;
  433.  
  434.       if (ctx->Driver.DepthRange)
  435.          ctx->Driver.DepthRange(ctx);
  436.    }
  437. }
  438.  
  439. /**
  440.  * Computes the scaling and the translation part of the
  441.  * viewport transform matrix of the \param i-th viewport
  442.  * and writes that into \param scale and \param translate.
  443.  */
  444. void
  445. _mesa_get_viewport_xform(struct gl_context *ctx, unsigned i,
  446.                          double scale[3], double translate[3])
  447. {
  448.    double x = ctx->ViewportArray[i].X;
  449.    double y = ctx->ViewportArray[i].Y;
  450.    double half_width = 0.5*ctx->ViewportArray[i].Width;
  451.    double half_height = 0.5*ctx->ViewportArray[i].Height;
  452.    double n = ctx->ViewportArray[i].Near;
  453.    double f = ctx->ViewportArray[i].Far;
  454.  
  455.    scale[0] = half_width;
  456.    translate[0] = half_width + x;
  457.    if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT) {
  458.       scale[1] = -half_height;
  459.       translate[1] = half_height - y;
  460.    } else {
  461.       scale[1] = half_height;
  462.       translate[1] = half_height + y;
  463.    }
  464.    if (ctx->Transform.ClipDepthMode == GL_NEGATIVE_ONE_TO_ONE) {
  465.       scale[2] = 0.5*(f - n);
  466.       translate[2] = 0.5*(n + f);
  467.    } else {
  468.       scale[2] = f - n;
  469.       translate[2] = n;
  470.    }
  471. }
  472.