Shader/ARB.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 238 SCAR 72.5
CC
37
Cov
0%
load_attrs
182.0
Map
72.0
new
20.0
CRAP 1406.0
1 ############################################################
2 #
3 # OpenGL::Shader::ARB - Copyright 2007 Graphcomp - ALL RIGHTS RESERVED
4 # Author: Bob "grafman" Free - grafman@graphcomp.com
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the same terms as Perl itself.
8 #
9 ############################################################
10  
11 package OpenGL::Shader::ARB;
12  
13 require Exporter;
14  
15 use Carp;
16  
17 use vars qw($VERSION $SHADER_VER $DESCRIPTION @ISA);
18 $VERSION = '1.01';
19  
20 $SHADER_VER = '1.0';
21  
22 $DESCRIPTION = qq
23 {ARBfp1.0 and ARBvp1.0 Assembly};
24  
25 use OpenGL::Shader::Common;
26 @ISA = qw(Exporter OpenGL::Shader::Common);
27  
28 use OpenGL(':all');
29  
30  
31  
32 =head1 NAME
33  
34 OpenGL::Shader::ARB - copyright 2007 Graphcomp - ALL RIGHTS RESERVED
35 Author: Bob "grafman" Free - grafman@graphcomp.com
36  
37 This program is free software; you can redistribute it and/or
38 modify it under the same terms as Perl itself.
39  
40  
41 =head1 DESCRIPTION
42  
43 This is a plug-in module for use with the OpenGL::Shader.
44 While it may be called directly, it will more often be called
45 by the OpenGL::Shader abstraction module.
46  
47 This is a subclass of the OpenGL::Shader::Common module.
48  
49  
50 =head1 SYNOPSIS
51  
52 ##########
53 # Instantiate a shader
54  
55 use OpenGL::Shader;
56 my $shdr = new OpenGL::Shader('ARB');
57  
58 # See docs in OpenGL/Shader/Common.pm
59  
60 =cut
61  
62  
63 # Get Version
64 sub TypeVersion
65 {
66 return undef if (OpenGL::glpCheckExtension('GL_ARB_fragment_program'));
67 return undef if (OpenGL::glpCheckExtension('GL_ARB_vertex_program'));
68  
69 # ARBfp1.0 and ARBvp1.0
70 return $SHADER_VER;
71 }
72  
73  
74 # Get Description
75 sub TypeDescription
76 {
77 return $DESCRIPTION;
78 }
79  
80  
81 # Shader constructor
82 sub new
83 {
84 my $this = shift;
85 my $class = ref($this) || $this;
86  
87 # Check for required OpenGL extensions
88 my $ver = TypeVersion();
89 return undef if (!$ver);
90  
91 my $self = new OpenGL::Shader::Common(@_);
92 return undef if (!$self);
93 bless($self,$class);
94  
95 $self->{type} = 'ARB';
96 $self->{version} = $ver;
97 $self->{description} = TypeDescription();
98  
99 $self->{fragment_const} = GL_FRAGMENT_PROGRAM_ARB;
100 $self->{vertex_const} = GL_VERTEX_PROGRAM_ARB;
101  
102 ($self->{fragment_id},$self->{vertex_id}) = glGenProgramsARB_p(2);
103 return undef if (!$self->{fragment_id} || !$self->{vertex_id});
104  
105 return $self;
106 }
107  
108  
109 # Shader destructor
110 # Must be disabled first
111 sub DESTROY
112 {
113 my($self) = @_;
114 glDeleteProgramsARB_p($self->{fragment_id}) if ($self->{fragment_id});
115 glDeleteProgramsARB_p($self->{vertex_id}) if ($self->{vertex_id});
116 }
117  
118  
119 # Load shader strings
120 sub Load
121 {
122 my($self,$fragment,$vertex) = @_;
123  
124 glBindProgramARB($self->{fragment_const}, $self->{fragment_id});
125 glProgramStringARB_p($self->{fragment_const}, $fragment);
126 $self->{fragment_code} = $fragment;
127 $self->{frag_vars} = {};
128  
129 glBindProgramARB($self->{vertex_const}, $self->{vertex_id});
130 glProgramStringARB_p($self->{vertex_const}, $vertex);
131 $self->{vertex_code} = $vertex;
132 $self->{vert_vars} = {};
133  
134 return '';
135 }
136  
137  
138 # Enable shader
139 sub Enable
140 {
141 my($self) = @_;
142  
143 glEnable($self->{fragment_const});
144 glEnable($self->{vertex_const});
145 }
146  
147  
148 # Disable shader
149 sub Disable
150 {
151 my($self) = @_;
152  
153 glDisable($self->{fragment_const});
154 glDisable($self->{vertex_const});
155 }
156  
157  
158 # Return shader vertex attribute ID
159 sub MapAttr
160 {
161 my($self,$attr) = @_;
162 return undef if ($self->load_attrs());
163 return $self->{attrs}->{$attr};
164 }
165  
166  
167 # Return shader local variable ID
168 sub Map
169 {
170 my($self,$var,$prog) = @_;
171  
172 $self->load_vars($self->{frag_vars},$self->{fragment_code});
173 $self->load_vars($self->{vert_vars},$self->{vertex_code});
174  
175 if ($prog == 1)
176 {
177 return undef if (!$self->{frag_vars});
178 return $self->{frag_vars}->{$var};
179 }
180  
181 if ($prog == 2)
182 {
183 return undef if (!$self->{vert_vars});
184 return $self->{vert_vars}->{$var};
185 }
186  
187 if ($self->{vert_vars})
188 {
189 my $id = $self->{vert_vars}->{$var};
190 return $id if (defined($id));
191 }
192  
193 return undef if (!$self->{frag_vars});
194 return $self->{frag_vars}->{$var};
195 }
196  
197  
198 # Set shader vector
199 sub SetVector
200 {
201 my($self,$var,@values) = @_;
202  
203 my $count = scalar(@values);
204 push(@values,(0) x (4-$count)) if ($count < 4);
205  
206 my $id = $self->Map($var,2);
207 if (defined($id))
208 {
209 glProgramLocalParameter4fARB($self->{vertex_const},$id,@values);
210 return '';
211 }
212  
213 $id = $self->Map($var,1);
214 return 'Unable to map $var' if (!defined($id));
215  
216 glProgramLocalParameter4fARB($self->{fragment_const},$id,@values);
217 return '';
218 }
219  
220  
221 # Set shader 4x4 matrix
222 sub SetMatrix
223 {
224 my($self,$var,$oga) = @_;
225 return 'No oga supplied' if (!$oga);
226  
227 my $id = $self->Map($var,2);
228 if (defined($id))
229 {
230 $self->set_matrix($self->{vertex_const},$id,$oga);
231 return '';
232 }
233  
234 $id = $self->Map($var,1);
235 return 'Unable to map $var' if (!defined($id));
236  
237 $self->set_matrix($self->{fragment_const},$id,$oga);
238 return '';
239 }
240  
241 sub set_matrix
242 {
243 my($self,$const,$id,$oga) = @_;
244 for (my $i=0; $i<4; $i++)
245 {
246 glProgramLocalParameter4fvARB_c($const,$id+$i,$oga->offset($i*4));
247 }
248 }
249  
250  
251 # Parse attribute names
252 sub load_attrs
253 {
254 my($self) = @_;
255 return '' if ($self->{attrs});
256 return 'No vertex program' if (!$self->{vertex_code});
257  
258 my @lines = split('[\r\n]+',$self->{vertex_code});
259 foreach my $line (@lines)
260 {
261 next if ($line !~ m|ATTRIB\s*(\w+)\s*\=\s*vertex\.([^;]+)|);
262 my($var,$map) = ($1,$2);
263  
264 if ($map =~ m|attrib\[(\d+)\]|)
265 {
266 $self->{attrs}->{$var} = $1;
267 }
268 elsif ($map eq 'position')
269 {
270 $self->{attrs}->{$var} = 0;
271 }
272 elsif ($map =~ m|weight|)
273 {
274 $self->{attrs}->{$var} = 1;
275 }
276 elsif ($map eq 'normal')
277 {
278 $self->{attrs}->{$var} = 2;
279 }
280 elsif ($map eq 'color.secondary')
281 {
282 $self->{attrs}->{$var} = 4;
283 }
284 elsif ($map =~ m|color|)
285 {
286 $self->{attrs}->{$var} = 3;
287 }
288 elsif ($map =~ m|fogcoord|)
289 {
290 $self->{attrs}->{$var} = 5;
291 }
292 elsif ($map eq 'texcoord')
293 {
294 $self->{attrs}->{$var} = 8;
295 }
296 elsif ($map =~ m|texcoord\[(\d+)\]|)
297 {
298 $self->{attrs}->{$var} = $1;
299 }
300 }
301 }
302  
303 # Parse variable names
304 sub load_vars
305 {
306 my($self,$href,$code) = @_;
307 return '' if (scalar(%$href));
308 return 'No program' if (!$code);
309  
310 my @lines = split('[\r\n]+',$code);
311 foreach my $line (@lines)
312 {
313 next if ($line !~ m|PARAM\s*([^\[\s]+)[\[\d\]\s]*\=[\{\s]*program\.local\[([0-9]+)|);
314 my($var,$index) = ($1,$2);
315 $href->{$var} = $index;
316 }
317 }
318  
319  
320 1;
321 __END__