-
Notifications
You must be signed in to change notification settings - Fork 1
/
Shader.pm
153 lines (118 loc) · 2.67 KB
/
Shader.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
############################################################
#
# OpenGL::Shader - Copyright 2007 Graphcomp - ALL RIGHTS RESERVED
# Author: Bob "grafman" Free - [email protected]
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
############################################################
### SEE DOCS IN Shader.pod
package OpenGL::Shader;
require Exporter;
use strict;
use warnings;
use Carp;
use vars qw($VERSION $SHADER_TYPES @ISA);
$VERSION = '1.02';
@ISA = qw(Exporter);
use OpenGL(':all');
# Return hashref of supported shader types
# Must be able to open a hidden GL context.
sub GetTypes
{
return ReturnTypes() if (ref($SHADER_TYPES));
my $dir = __FILE__;
return if ($dir !~ s|\.pm$||);
my @types;
# Grab OpenGL/Shader modules
if (opendir(DIR,$dir))
{
foreach my $type (readdir(DIR))
{
next if ($type !~ s|\.pm$||);
next if ($type eq 'Common');
next if ($type eq 'Objects');
push(@types,$type);
}
closedir(DIR);
}
return if (!@types);
$SHADER_TYPES = {};
foreach my $type (@types)
{
my $info = HasType($type);
next if (!$info);
$SHADER_TYPES->{$type} = $info;
}
return ReturnTypes();
}
sub ReturnTypes
{
return wantarray ? values(%$SHADER_TYPES) : $SHADER_TYPES;
}
# Get shader's version
# Must be able to open a hidden GL context.
sub GetTypeVersion
{
my($type) = @_;
return if (!$type);
my $info = HasType($type);
return if (!$info);
return $info->{version};
}
# Check for engine availability; returns module version
sub HasType
{
my($type,$min_ver,$max_ver) = @_;
return if (!$type);
my $module = GetTypeModule($type);
my($version,$desc);
my $exec = qq
{
use $module;
\$version = $module\::TypeVersion();
\$desc = $module\::TypeDescription();
};
eval($exec);
return if (!$version);
return if ($min_ver && $version lt $min_ver);
return if ($max_ver && $version gt $max_ver);
my $info = {};
$info->{name} = $type;
$info->{module} = $module;
$info->{version} = $version;
$info->{description} = $desc;
return $info;
}
# Constructor wrapper for shader type
sub new
{
my $this = shift;
my $class = ref($this) || $this;
my $self = {};
bless($self,$class);
my @types = @_ ? @_ : ('GLSL','CG','ARB');
foreach my $type (@types)
{
next if (!$type);
my $obj;
my $module = GetTypeModule($type);
my $exec = qq
{
use $module;
\$obj = new $module\();
};
eval($exec);
return $obj if ($obj && !$@);
}
return undef;
}
# Get shader module name
sub GetTypeModule
{
my($type) = @_;
return __PACKAGE__.'::'.uc($type);
}
1;
__END__