[m-rev.] diff: remove the need for mogl.type_tables

Julien Fischer juliensf at csse.unimelb.edu.au
Mon Oct 15 16:53:00 AEST 2007


The changes to the Allegro binding are for review by PeterW.

Estimated hours taken: 1
Branches: main

Eliminate the need for the type_tables sub-module in the opengl binding,
by defining more types as foreign enumerations.

This also fixes an off-by-two error in the texture_format_flags array
caused by two constants being left out.  (The fix is to delete the array
since it is unnecessary when the type texture_format/0 is defined as
a foreign enumeration.)

extras/graphics/mercury_opengl/mogl.m:
 	Define some more types as foreign enumerations.

extras/graphics/mercury_opengl/mogl.type_tables.m:
 	Delete this sub-module, it is now redundant.

extras/graphics/mercury_allegro/allegrogl.m:
 	Conform to the above change.

Julien.

Index: mercury_allegro/allegrogl.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/graphics/mercury_allegro/allegrogl.m,v
retrieving revision 1.2
diff -u -r1.2 allegrogl.m
--- mercury_allegro/allegrogl.m	9 May 2007 08:25:25 -0000	1.2
+++ mercury_allegro/allegrogl.m	15 Oct 2007 06:46:12 -0000
@@ -182,8 +182,6 @@

  :- implementation.

-:- import_module mogl.type_tables.
-
  :- import_module int.
  :- import_module list.

@@ -279,38 +277,36 @@

  check_texture_ex(TextureFlags, Bitmap, TextureFormat, CanBeTexture, !IO) :-
      TextureFlagsBits = texture_flags_to_bits(TextureFlags),
-    texture_format_to_int(TextureFormat, TextureFormatInt),
-    check_texture_ex_2(TextureFlagsBits, Bitmap, TextureFormatInt,
+    check_texture_ex_2(TextureFlagsBits, Bitmap, TextureFormat,
          CanBeTexture, !IO).

-:- pred check_texture_ex_2(int::in, bitmap::in, int::in, bool::out,
-    io::di, io::uo) is det.
+:- pred check_texture_ex_2(int::in, bitmap::in, mogl.texture_format::in,
+    bool::out, io::di, io::uo) is det.
  :- pragma foreign_proc("C",
      check_texture_ex_2(TextureFlagsBits::in, Bitmap::in, TextureFormatInt::in,
          CanBeTexture::out, IO0::di, IO::uo),
      [will_not_call_mercury, promise_pure, thread_safe],
  "
      CanBeTexture = allegro_gl_check_texture_ex(TextureFlagsBits, Bitmap,
-        texture_format_flags[TextureFormatInt]) ? MR_YES : MR_NO;
+        (GLenum) TextureFormatInt) ? MR_YES : MR_NO;
      IO = IO0;
  ").

  make_texture_ex(TextureFlags, Bitmap, TextureFormat, MaybeTextureName, !IO) :-
      TextureFlagsBits = texture_flags_to_bits(TextureFlags),
-    texture_format_to_int(TextureFormat, TextureFormatInt),
-    make_texture_ex_2(TextureFlagsBits, Bitmap, TextureFormatInt,
+    make_texture_ex_2(TextureFlagsBits, Bitmap, TextureFormat,
          TextureName, !IO),
      MaybeTextureName = (if TextureName = 0 then no else yes(TextureName)).

-:- pred make_texture_ex_2(int::in, bitmap::in, int::in,
+:- pred make_texture_ex_2(int::in, bitmap::in, mogl.texture_format::in,
      mogl.texture_name::out, io::di, io::uo) is det.
  :- pragma foreign_proc("C",
-    make_texture_ex_2(TextureFlagsBits::in, Bitmap::in, TextureFormatInt::in,
+    make_texture_ex_2(TextureFlagsBits::in, Bitmap::in, TextureFormat::in,
          TextureName::out, IO0::di, IO::uo),
      [will_not_call_mercury, promise_pure, thread_safe],
  "
      TextureName = allegro_gl_make_texture_ex(TextureFlagsBits, Bitmap,
-        texture_format_flags[TextureFormatInt]);
+        (GLenum) TextureFormat);
      IO = IO0;
  ").

@@ -356,12 +352,11 @@
  ").

  convert_allegro_font_ex(Fnt, FontType, Scale, Format, MaybeAGLFont, !IO) :-
-    texture_format_to_int(Format, FormatInt),
      convert_allegro_font_ex_2(Fnt, font_type_to_int(FontType), Scale,
-        FormatInt, MaybeAGLFont, !IO).
+        Format, MaybeAGLFont, !IO).

  :- pred convert_allegro_font_ex_2(font::in, int::in, float::in,
-    int::in, maybe(agl_font)::out, io::di, io::uo) is det.
+    mogl.texture_format::in, maybe(agl_font)::out, io::di, io::uo) is det.
  :- pragma foreign_proc("C",
      convert_allegro_font_ex_2(Fnt::in, FontType::in, Scale::in, Format::in,
          MaybeAGLFont::out, IO0::di, IO::uo),
@@ -369,7 +364,7 @@
  "
      FONT *AGLFont = allegro_gl_convert_allegro_font_ex(Fnt,
          __magl_font_type_flags[FontType], Scale,
-        texture_format_flags[Format]);
+        (GLenum) Format);
      if (AGLFont) {
          MaybeAGLFont = __magl_yes_agl_font(AGLFont);
      } else {
Index: mercury_opengl/mogl.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/graphics/mercury_opengl/mogl.m,v
retrieving revision 1.33
diff -u -r1.33 mogl.m
--- mercury_opengl/mogl.m	13 Oct 2007 13:32:42 -0000	1.33
+++ mercury_opengl/mogl.m	15 Oct 2007 06:37:25 -0000
@@ -29,8 +29,6 @@
  :- module mogl.
  :- interface.

-:- include_module mogl.type_tables.
-
  :- import_module bitmap.
  :- import_module bool.
  :- import_module float.
@@ -561,7 +559,7 @@
  %------------------------------------------------------------------------------%
  %
  % Fog
-%
+%

  :- type fog_parameter
      --->    fog_mode(fog_mode)
@@ -1196,8 +1194,6 @@

  :- implementation.

-:- import_module mogl.type_tables.
-
  :- import_module exception.
  :- import_module require.

@@ -1217,8 +1213,6 @@
      #endif
  ").

-:- pragma foreign_import_module("C", mogl.type_tables).
-
  %------------------------------------------------------------------------------%
  %
  % GL errors
@@ -2425,6 +2419,30 @@
      IO = IO0;
  ").

+:- pragma foreign_enum("C", pixel_format/0, [
+    color_index      - "GL_COLOR_INDEX",
+    stencil_index    - "GL_STENCIL_INDEX",
+    depth_component  - "GL_DEPTH_COMPONENT",
+    red              - "GL_RED",
+    green            - "GL_GREEN",
+    blue             - "GL_BLUE",
+    alpha            - "GL_ALPHA",
+    rgb              - "GL_RGB",
+    rgba             - "GL_RGBA",
+    luminance        - "GL_LUMINANCE",
+    luminance_alpha  - "GL_LUMINANCE_ALPHA"
+]).
+
+:- pragma foreign_enum("C", pixel_type/0, [
+    unsigned_byte    - "GL_UNSIGNED_BYTE",
+    bitmap           - "GL_BITMAP",
+    byte             - "GL_BYTE",
+    unsigned_short   - "GL_UNSIGNED_SHORT",
+    unsigned_int     - "GL_UNSIGNED_INT",
+    int              - "GL_INT",
+    float            - "GL_FLOAT"
+]).
+
  :- type pixels
      --->    pixels(
                  pixel_format :: int,
@@ -2504,9 +2522,46 @@
      proxy_texture_2d - "GL_PROXY_TEXTURE_2D"
  ]).

-:- pragma foreign_decl("C", "
-    extern const GLenum texture_format_flags[];
-").
+:- pragma foreign_enum("C", texture_format/0, [
+    alpha               - "GL_ALPHA",
+    luminance           - "GL_LUMINANCE",
+    luminance_alpha     - "GL_LUMINANCE_ALPHA",
+    intensity           - "GL_INTENSITY",
+    rgb                 - "GL_RGB",
+    rgba                - "GL_RGBA",
+    alpha4              - "GL_ALPHA4",
+    alpha8              - "GL_ALPHA8",
+    alpha12             - "GL_ALPHA12",
+    alpha16             - "GL_ALPHA16",
+    luminance4          - "GL_LUMINANCE4",
+    luminance5          - "GL_LUMINANCE5",
+    luminance8          - "GL_LUMINANCE8",
+    luminance12         - "GL_LUMINANCE12",
+    luminance16         - "GL_LUMINANCE16",
+    luminance4_alpha4   - "GL_LUMINANCE4_ALPHA4",
+    luminance6_alpha2   - "GL_LUMINANCE6_ALPHA2",
+    luminance8_alpha8   - "GL_LUMINANCE8_ALPHA8",
+    luminance12_alpha4  - "GL_LUMINANCE12_ALPHA4", 
+    luminance12_alpha12 - "GL_LUMINANCE12_ALPHA12",
+    luminance16_alpha16 - "GL_LUMINANCE16_ALPHA16",
+    intensity4          - "GL_INTENSITY4",
+    intensity8          - "GL_INTENSITY8",
+    intensity12         - "GL_INTENSITY12",
+    intensity16         - "GL_INTENSITY16",
+    r3_g3_b2            - "GL_R3_G3_B2",
+    rgb4                - "GL_RGB4",
+    rgb5                - "GL_RGB5",
+    rgb10               - "GL_RGB10",
+    rgb12               - "GL_RGB12",
+    rgb16               - "GL_RGB16",
+    rgba2               - "GL_RGBA2",
+    rgba4               - "GL_RGBA4",
+    rgb5_a1             - "GL_RGB5_A1",
+    rgba8               - "GL_RGBA8",
+    rgb10_a2            - "GL_RGB10_A2",
+    rgba12              - "GL_RGBA12",
+    rgba16              - "GL_RGBA16"
+]).

  :- pragma foreign_decl("C", "
      extern const GLenum texture_parameter_flags[];
@@ -2840,46 +2895,31 @@
      IO = IO0;
  ").

-tex_image_1d(Target, Level, InternalFormat, Width, Border,
-        Format, Type, Pixels, !IO) :-
-    texture_format_to_int(InternalFormat, InternalFormatInt),
-    tex_image_1d_2(Target, Level, InternalFormatInt, Width, Border,
-        pixel_format_to_int(Format), pixel_type_to_int(Type), Pixels, !IO).
-
-:- pred tex_image_1d_2(texture_target::in, int::in, int::in, int::in, int::in,
-    int::in, int::in, pixel_data::bitmap_ui, io::di, io::uo) is det.
  :- pragma foreign_proc("C", 
-    tex_image_1d_2(Target::in, Level::in, InternalFormat::in,
-        Width::in, Border::in, Format::in, Type::in,
+    tex_image_1d(Target::in(texture_1d), Level::in, InternalFormat::in,
+        Width::in, Border::in(border),
+        Format::in(pixel_format_non_stencil_or_depth), Type::in,
          Pixels::bitmap_ui, IO0::di, IO::uo),
      [will_not_call_mercury, tabled_for_io, promise_pure,
          does_not_affect_liveness],
  "
      glTexImage1D((GLenum) Target, Level,
-        texture_format_flags[InternalFormat], Width, Border,
-        pixel_format_flags[Format], pixel_type_flags[Type], Pixels->elements);
+        (GLenum) InternalFormat, Width, Border,
+        (GLenum) Format, (GLenum) Type, Pixels->elements);
      IO = IO0;
  ").

-tex_image_2d(Target, Level, InternalFormat, Width, Height, Border,
-        Format, Type, Pixels, !IO) :-
-    texture_format_to_int(InternalFormat, InternalFormatInt),
-    tex_image_2d_2(Target, Level, InternalFormatInt, Width, Height, Border,
-        pixel_format_to_int(Format), pixel_type_to_int(Type), Pixels, !IO).
-
-:- pred tex_image_2d_2(texture_target::in, int::in, int::in, int::in,
-    int::in, int::in, int::in, int::in, pixel_data::bitmap_ui,
-    io::di, io::uo) is det.
  :- pragma foreign_proc("C", 
-    tex_image_2d_2(Target::in, Level::in, InternalFormat::in,
-        Width::in, Height::in, Border::in, Format::in, Type::in,
+    tex_image_2d(Target::in(texture_2d), Level::in, InternalFormat::in,
+        Width::in, Height::in, Border::in(border),
+        Format::in(pixel_format_non_stencil_or_depth), Type::in,
          Pixels::bitmap_ui, IO0::di, IO::uo),
      [will_not_call_mercury, tabled_for_io, promise_pure,
          does_not_affect_liveness],
  "
      glTexImage2D((GLenum) Target, Level,
-        texture_format_flags[InternalFormat], Width, Height, Border,
-        pixel_format_flags[Format], pixel_type_flags[Type], Pixels->elements);
+        (GLenum) InternalFormat, Width, Height, Border,
+        (GLenum) Format, (GLenum)Type, Pixels->elements);
      IO = IO0;
  ").

@@ -2906,40 +2946,27 @@
  %     IO = IO0;
  % ").

-copy_tex_image_1d(Target, Level, InternalFormat, X, Y, Width, Border, !IO) :-
-    texture_format_to_int(InternalFormat, InternalFormatInt),
-    copy_tex_image_1d_2(Target, Level, InternalFormatInt, X, Y, Width,
-        Border, !IO).
-
-:- pred copy_tex_image_1d_2(texture_target::in, int::in, int::in, int::in,
-    int::in, int::in, int::in, io::di, io::uo) is det.
  :- pragma foreign_proc("C", 
-    copy_tex_image_1d_2(Target::in, Level::in, InternalFormat::in,
-        X::in, Y::in, Width::in, Border::in, IO0::di, IO::uo),
+    copy_tex_image_1d(Target::in(bound(texture_1d)), Level::in,
+        InternalFormat::in, X::in, Y::in, Width::in,
+        Border::in(border), IO0::di, IO::uo),
      [will_not_call_mercury, tabled_for_io, promise_pure,
          does_not_affect_liveness],
  "
-    glCopyTexImage1D((GLenum) Target, Level,
-        texture_format_flags[InternalFormat], X, Y, Width, Border);
+    glCopyTexImage1D((GLenum) Target, Level, (GLenum) InternalFormat,
+        X, Y, Width, Border);
      IO = IO0;
  ").

-copy_tex_image_2d(Target, Level, InternalFormat, X, Y, Width, Height, Border,
-        !IO) :-
-    texture_format_to_int(InternalFormat, InternalFormatInt),
-    copy_tex_image_2d_2(Target, Level, InternalFormatInt, X, Y,
-        Width, Height, Border, !IO).
-
-:- pred copy_tex_image_2d_2(texture_target::in, int::in, int::in, int::in,
-    int::in, int::in, int::in, int::in, io::di, io::uo) is det.
  :- pragma foreign_proc("C", 
-    copy_tex_image_2d_2(Target::in, Level::in, InternalFormat::in,
-        X::in, Y::in, Width::in, Height::in, Border::in, IO0::di, IO::uo),
+    copy_tex_image_2d(Target::in(bound(texture_2d)), Level::in,
+        InternalFormat::in, X::in, Y::in, Width::in, Height::in,
+        Border::in(border), IO0::di, IO::uo),
      [will_not_call_mercury, tabled_for_io, promise_pure,
          does_not_affect_liveness],
  "
-    glCopyTexImage2D((GLenum) Target, Level,
-        texture_format_flags[InternalFormat], X, Y, Width, Height, Border);
+    glCopyTexImage2D((GLenum) Target, Level, (GLenum) InternalFormat,
+        X, Y, Width, Height, Border);
      IO = IO0;
  ").

Index: mercury_opengl/mogl.type_tables.m
===================================================================
RCS file: mercury_opengl/mogl.type_tables.m
diff -N mercury_opengl/mogl.type_tables.m
--- mercury_opengl/mogl.type_tables.m	6 Dec 2006 03:49:36 -0000	1.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,194 +0,0 @@
-%-----------------------------------------------------------------------------%
-% vim: ft=mercury ts=4 sw=4 et
-%-----------------------------------------------------------------------------%
-% Copyright (C) 1997, 2003-2006 The University of Melbourne.
-% This file may only be copied under the terms of the GNU Library General
-% Public License - see the file COPYING.LIB in the Mercury distribution.
-%-----------------------------------------------------------------------------%
-% 
-% File: mogl.type_tables.m.
-% Main authors: conway, juliensf.
-%
-% This file exposes some of the <type>_to_int and <type>_flags[] tables.
-% Modules other than mogl can import this module to convert mogl types to
-% their equivalent OpenGL constants.  The interface is subject to change.
-%
-%------------------------------------------------------------------------------%
-
-:- module mogl.type_tables.
-:- interface.
-
-:- func pixel_format_to_int(pixel_format) = int.
-
-:- func pixel_type_to_int(pixel_type) = int.
-
-:- pred texture_format_to_int(texture_format, int).
-:- mode texture_format_to_int(in, out) is det.
-%:- mode texture_format_to_int(out, in) is semidet.
-
-%-----------------------------------------------------------------------------%
-
-:- implementation.
-
-%------------------------------------------------------------------------------%
-
-    % XXX Check that this works on Windows.
-    % We may need to #include <windows.h> to make it work.
-:- pragma foreign_decl("C", "
-    #include <stdio.h>
-    #include <math.h>
-    #include <assert.h>
- 
-    #if defined(__APPLE__) && defined(__MACH__)
-        #include <OpenGL/gl.h>
-    #else
-        #include <GL/gl.h>
-    #endif
-").
-
-%------------------------------------------------------------------------------%
-
-pixel_format_to_int(color_index)     = 0.
-pixel_format_to_int(stencil_index)   = 1.
-pixel_format_to_int(depth_component) = 2.
-pixel_format_to_int(red)             = 3.
-pixel_format_to_int(green)           = 4.
-pixel_format_to_int(blue)            = 5.
-pixel_format_to_int(alpha)           = 6.
-pixel_format_to_int(rgb)             = 7.
-pixel_format_to_int(rgba)            = 8.
-pixel_format_to_int(luminance)       = 9.
-pixel_format_to_int(luminance_alpha) = 10.
-
-:- pragma foreign_decl("C", "
-    extern const GLenum pixel_format_flags[];
-").
-
-:- pragma foreign_code("C", "
-    const GLenum pixel_format_flags[] = {
-        GL_COLOR_INDEX,
-        GL_STENCIL_INDEX,
-        GL_DEPTH_COMPONENT,
-        GL_RED,
-        GL_GREEN,
-        GL_BLUE,
-        GL_ALPHA,
-        GL_RGB,
-        GL_RGBA,
-        GL_LUMINANCE,
-        GL_LUMINANCE_ALPHA
-    };
-").
-
-%------------------------------------------------------------------------------%
-
-pixel_type_to_int(unsigned_byte)  = 0.
-pixel_type_to_int(bitmap)         = 1.
-pixel_type_to_int(byte)           = 2.
-pixel_type_to_int(unsigned_short) = 3.
-pixel_type_to_int(unsigned_int)   = 4.
-pixel_type_to_int(int)            = 5.
-pixel_type_to_int(float)          = 6.
-
-:- pragma foreign_decl("C", "
-    extern const GLenum pixel_type_flags[];
-").
-
-:- pragma foreign_code("C",
-"
-    const GLenum pixel_type_flags[] = {
-        GL_UNSIGNED_BYTE,
-        GL_BITMAP,
-        GL_BYTE,
-        GL_UNSIGNED_SHORT,
-        GL_UNSIGNED_INT,
-        GL_INT,
-        GL_FLOAT
-    };
-").
-
-%------------------------------------------------------------------------------%
-
-:- pragma foreign_code("C", "
-    const GLenum texture_format_flags[] = {
-        GL_ALPHA,
-        GL_LUMINANCE,
-        GL_LUMINANCE_ALPHA,
-        GL_INTENSITY,
-        GL_RGB,
-        GL_RGBA,
-        GL_ALPHA4,
-        GL_ALPHA8,
-        GL_ALPHA12,
-        GL_ALPHA16,
-        GL_LUMINANCE4,
-        GL_LUMINANCE8,
-        GL_LUMINANCE12,
-        GL_LUMINANCE16,
-        GL_LUMINANCE4_ALPHA4,
-        GL_LUMINANCE6_ALPHA2,
-        GL_LUMINANCE8_ALPHA8,
-        GL_LUMINANCE12_ALPHA4,
-        GL_LUMINANCE12_ALPHA12,
-        GL_LUMINANCE16_ALPHA16,
-        GL_INTENSITY4,
-        GL_INTENSITY8,
-        GL_INTENSITY12,
-        GL_INTENSITY16,
-        GL_R3_G3_B2,
-        GL_RGB4,
-        GL_RGB5,
-        GL_RGB10,
-        GL_RGB12,
-        GL_RGB16,
-        GL_RGBA2,
-        GL_RGBA4,
-        GL_RGB5_A1,
-        GL_RGBA8,
-        GL_RGB10_A2,
-        GL_RGBA12,
-        GL_RGBA16
-    };
-").
-
-texture_format_to_int(alpha, 0). 
-texture_format_to_int(luminance, 1).
-texture_format_to_int(luminance_alpha, 2).
-texture_format_to_int(intensity, 3).
-texture_format_to_int(rgb, 4).
-texture_format_to_int(rgba, 5).
-texture_format_to_int(alpha4, 6).
-texture_format_to_int(alpha8, 7).
-texture_format_to_int(alpha12, 8).
-texture_format_to_int(alpha16, 9).
-texture_format_to_int(luminance4, 10).
-texture_format_to_int(luminance8, 11).
-texture_format_to_int(luminance12, 12).
-texture_format_to_int(luminance16, 13).
-texture_format_to_int(luminance4_alpha4, 14).
-texture_format_to_int(luminance6_alpha2, 15).
-texture_format_to_int(luminance8_alpha8, 16).
-texture_format_to_int(luminance12_alpha4, 17).
-texture_format_to_int(luminance12_alpha12, 18).
-texture_format_to_int(luminance16_alpha16, 19).
-texture_format_to_int(intensity4, 20).
-texture_format_to_int(intensity8, 21).
-texture_format_to_int(intensity12, 22).
-texture_format_to_int(intensity16, 23).
-texture_format_to_int(r3_g3_b2, 24).
-texture_format_to_int(rgb4, 25).
-texture_format_to_int(rgb5, 26).
-texture_format_to_int(rgb10, 27).
-texture_format_to_int(rgb12, 28).
-texture_format_to_int(rgb16, 29).
-texture_format_to_int(rgba2, 30).
-texture_format_to_int(rgba4, 31).
-texture_format_to_int(rgb5_a1, 32).
-texture_format_to_int(rgba8, 33).
-texture_format_to_int(rgb10_a2, 34).
-texture_format_to_int(rgba12, 35).
-texture_format_to_int(rgba16, 36).
-
-%------------------------------------------------------------------------------%
-:- end_module mogl.type_tables.
-%------------------------------------------------------------------------------%

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list