Use inlined uniform() helper function in all the glUniform*() function to
[mesa.git] / src / mesa / shader / shaderobjects_3dlabs.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file shaderobjects_3dlabs.c
27 * shader objects definitions for 3dlabs compiler
28 * \author Michal Krol
29 */
30
31 /* Set this to 1 when we are ready to use 3dlabs' front-end */
32 #define USE_3DLABS_FRONTEND 0
33
34 #include "imports.h"
35 #include "hash.h"
36 #include "macros.h"
37 #include "shaderobjects.h"
38 #include "shaderobjects_3dlabs.h"
39
40 #if USE_3DLABS_FRONTEND
41 #include "slang_mesa.h"
42 #include "Public/ShaderLang.h"
43 #else
44 #include "slang_link.h"
45 #endif
46
47 #if FEATURE_ARB_shader_objects
48
49 struct gl2_unknown_obj
50 {
51 GLuint reference_count;
52 void (*_destructor) (struct gl2_unknown_intf **);
53 };
54
55 struct gl2_unknown_impl
56 {
57 struct gl2_unknown_intf *_vftbl;
58 struct gl2_unknown_obj _obj;
59 };
60
61 static void
62 _unknown_destructor(struct gl2_unknown_intf **intf)
63 {
64 }
65
66 static void
67 _unknown_AddRef(struct gl2_unknown_intf **intf)
68 {
69 struct gl2_unknown_impl *impl = (struct gl2_unknown_impl *) intf;
70
71 impl->_obj.reference_count++;
72 }
73
74 static void
75 _unknown_Release(struct gl2_unknown_intf **intf)
76 {
77 struct gl2_unknown_impl *impl = (struct gl2_unknown_impl *) intf;
78
79 impl->_obj.reference_count--;
80 if (impl->_obj.reference_count == 0) {
81 impl->_obj._destructor(intf);
82 _mesa_free((void *) intf);
83 }
84 }
85
86 static struct gl2_unknown_intf **
87 _unknown_QueryInterface(struct gl2_unknown_intf **intf, enum gl2_uiid uiid)
88 {
89 if (uiid == UIID_UNKNOWN) {
90 (**intf).AddRef(intf);
91 return intf;
92 }
93 return NULL;
94 }
95
96 static struct gl2_unknown_intf _unknown_vftbl = {
97 _unknown_AddRef,
98 _unknown_Release,
99 _unknown_QueryInterface
100 };
101
102 static void
103 _unknown_constructor(struct gl2_unknown_impl *impl)
104 {
105 impl->_vftbl = &_unknown_vftbl;
106 impl->_obj.reference_count = 1;
107 impl->_obj._destructor = _unknown_destructor;
108 }
109
110 struct gl2_unkinner_obj
111 {
112 struct gl2_unknown_intf **unkouter;
113 };
114
115 struct gl2_unkinner_impl
116 {
117 struct gl2_unknown_intf *_vftbl;
118 struct gl2_unkinner_obj _obj;
119 };
120
121 static void
122 _unkinner_destructor(struct gl2_unknown_intf **intf)
123 {
124 }
125
126 static void
127 _unkinner_AddRef(struct gl2_unknown_intf **intf)
128 {
129 struct gl2_unkinner_impl *impl = (struct gl2_unkinner_impl *) intf;
130
131 (**impl->_obj.unkouter).AddRef(impl->_obj.unkouter);
132 }
133
134 static void
135 _unkinner_Release(struct gl2_unknown_intf **intf)
136 {
137 struct gl2_unkinner_impl *impl = (struct gl2_unkinner_impl *) intf;
138
139 (**impl->_obj.unkouter).Release(impl->_obj.unkouter);
140 }
141
142 static struct gl2_unknown_intf **
143 _unkinner_QueryInterface(struct gl2_unknown_intf **intf, enum gl2_uiid uiid)
144 {
145 struct gl2_unkinner_impl *impl = (struct gl2_unkinner_impl *) intf;
146
147 return (**impl->_obj.unkouter).QueryInterface(impl->_obj.unkouter, uiid);
148 }
149
150 static struct gl2_unknown_intf _unkinner_vftbl = {
151 _unkinner_AddRef,
152 _unkinner_Release,
153 _unkinner_QueryInterface
154 };
155
156 static void
157 _unkinner_constructor(struct gl2_unkinner_impl *impl,
158 struct gl2_unknown_intf **outer)
159 {
160 impl->_vftbl = &_unkinner_vftbl;
161 impl->_obj.unkouter = outer;
162 }
163
164 struct gl2_generic_obj
165 {
166 struct gl2_unknown_obj _unknown;
167 GLhandleARB name;
168 GLboolean delete_status;
169 GLcharARB *info_log;
170 };
171
172 struct gl2_generic_impl
173 {
174 struct gl2_generic_intf *_vftbl;
175 struct gl2_generic_obj _obj;
176 };
177
178 static void
179 _generic_destructor(struct gl2_unknown_intf **intf)
180 {
181 GET_CURRENT_CONTEXT(ctx);
182 struct gl2_generic_impl *impl = (struct gl2_generic_impl *) intf;
183
184 _mesa_free((void *) impl->_obj.info_log);
185
186 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
187 _mesa_HashRemove(ctx->Shared->GL2Objects, impl->_obj.name);
188 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
189
190 _unknown_destructor(intf);
191 }
192
193 static struct gl2_unknown_intf **
194 _generic_QueryInterface(struct gl2_unknown_intf **intf, enum gl2_uiid uiid)
195 {
196 if (uiid == UIID_GENERIC) {
197 (**intf).AddRef(intf);
198 return intf;
199 }
200 return _unknown_QueryInterface(intf, uiid);
201 }
202
203 static void
204 _generic_Delete(struct gl2_generic_intf **intf)
205 {
206 struct gl2_generic_impl *impl = (struct gl2_generic_impl *) intf;
207
208 if (impl->_obj.delete_status == GL_FALSE) {
209 impl->_obj.delete_status = GL_TRUE;
210 (**intf)._unknown.Release((struct gl2_unknown_intf **) intf);
211 }
212 }
213
214 static GLhandleARB
215 _generic_GetName(struct gl2_generic_intf **intf)
216 {
217 struct gl2_generic_impl *impl = (struct gl2_generic_impl *) intf;
218
219 return impl->_obj.name;
220 }
221
222 static GLboolean
223 _generic_GetDeleteStatus(struct gl2_generic_intf **intf)
224 {
225 struct gl2_generic_impl *impl = (struct gl2_generic_impl *) intf;
226
227 return impl->_obj.delete_status;
228 }
229
230 static GLvoid
231 _generic_GetInfoLog(struct gl2_generic_intf **intf, GLsizei maxlen,
232 GLcharARB * infolog)
233 {
234 struct gl2_generic_impl *impl = (struct gl2_generic_impl *) (intf);
235
236 if (maxlen > 0) {
237 _mesa_strncpy(infolog, impl->_obj.info_log, maxlen - 1);
238 infolog[maxlen - 1] = '\0';
239 }
240 }
241
242 static GLsizei
243 _generic_GetInfoLogLength(struct gl2_generic_intf **intf)
244 {
245 struct gl2_generic_impl *impl = (struct gl2_generic_impl *) (intf);
246
247 if (impl->_obj.info_log == NULL)
248 return 1;
249 return _mesa_strlen(impl->_obj.info_log) + 1;
250 }
251
252 static struct gl2_generic_intf _generic_vftbl = {
253 {
254 _unknown_AddRef,
255 _unknown_Release,
256 _generic_QueryInterface},
257 _generic_Delete,
258 NULL, /* abstract GetType */
259 _generic_GetName,
260 _generic_GetDeleteStatus,
261 _generic_GetInfoLog,
262 _generic_GetInfoLogLength
263 };
264
265 static void
266 _generic_constructor(struct gl2_generic_impl *impl)
267 {
268 GET_CURRENT_CONTEXT(ctx);
269
270 _unknown_constructor((struct gl2_unknown_impl *) impl);
271 impl->_vftbl = &_generic_vftbl;
272 impl->_obj._unknown._destructor = _generic_destructor;
273 impl->_obj.delete_status = GL_FALSE;
274 impl->_obj.info_log = NULL;
275
276 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
277 impl->_obj.name = _mesa_HashFindFreeKeyBlock(ctx->Shared->GL2Objects, 1);
278 _mesa_HashInsert(ctx->Shared->GL2Objects, impl->_obj.name, (void *) impl);
279 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
280 }
281
282 struct gl2_container_obj
283 {
284 struct gl2_generic_obj _generic;
285 struct gl2_generic_intf ***attached;
286 GLuint attached_count;
287 };
288
289 struct gl2_container_impl
290 {
291 struct gl2_container_intf *_vftbl;
292 struct gl2_container_obj _obj;
293 };
294
295 static void
296 _container_destructor(struct gl2_unknown_intf **intf)
297 {
298 struct gl2_container_impl *impl = (struct gl2_container_impl *) intf;
299 GLuint i;
300
301 for (i = 0; i < impl->_obj.attached_count; i++) {
302 struct gl2_generic_intf **x = impl->_obj.attached[i];
303 (**x)._unknown.Release((struct gl2_unknown_intf **) x);
304 }
305
306 _generic_destructor(intf);
307 }
308
309 static struct gl2_unknown_intf **
310 _container_QueryInterface(struct gl2_unknown_intf **intf, enum gl2_uiid uiid)
311 {
312 if (uiid == UIID_CONTAINER) {
313 (**intf).AddRef(intf);
314 return intf;
315 }
316 return _generic_QueryInterface(intf, uiid);
317 }
318
319 static GLboolean
320 _container_Attach(struct gl2_container_intf **intf,
321 struct gl2_generic_intf **att)
322 {
323 GET_CURRENT_CONTEXT(ctx);
324 struct gl2_container_impl *impl = (struct gl2_container_impl *) intf;
325 GLuint i;
326
327 for (i = 0; i < impl->_obj.attached_count; i++)
328 if (impl->_obj.attached[i] == att) {
329 _mesa_error(ctx, GL_INVALID_OPERATION, "_container_Attach");
330 return GL_FALSE;
331 }
332
333 impl->_obj.attached = (struct gl2_generic_intf ***)
334 _mesa_realloc(impl->_obj.attached,
335 impl->_obj.attached_count * sizeof(*impl->_obj.attached),
336 (impl->_obj.attached_count + 1) * sizeof(*impl->_obj.attached));
337 if (impl->_obj.attached == NULL)
338 return GL_FALSE;
339
340 impl->_obj.attached[impl->_obj.attached_count] = att;
341 impl->_obj.attached_count++;
342 (**att)._unknown.AddRef((struct gl2_unknown_intf **) att);
343 return GL_TRUE;
344 }
345
346 static GLboolean
347 _container_Detach(struct gl2_container_intf **intf,
348 struct gl2_generic_intf **att)
349 {
350 GET_CURRENT_CONTEXT(ctx);
351 struct gl2_container_impl *impl = (struct gl2_container_impl *) intf;
352 GLuint i, j;
353
354 for (i = 0; i < impl->_obj.attached_count; i++)
355 if (impl->_obj.attached[i] == att) {
356 for (j = i; j < impl->_obj.attached_count - 1; j++)
357 impl->_obj.attached[j] = impl->_obj.attached[j + 1];
358 impl->_obj.attached = (struct gl2_generic_intf ***)
359 _mesa_realloc(impl->_obj.attached,
360 impl->_obj.attached_count * sizeof(*impl->_obj.attached),
361 (impl->_obj.attached_count - 1) * sizeof(*impl->_obj.attached));
362 impl->_obj.attached_count--;
363 (**att)._unknown.Release((struct gl2_unknown_intf **) att);
364 return GL_TRUE;
365 }
366
367 _mesa_error(ctx, GL_INVALID_OPERATION, "_container_Detach");
368 return GL_FALSE;
369 }
370
371 static GLsizei
372 _container_GetAttachedCount(struct gl2_container_intf **intf)
373 {
374 struct gl2_container_impl *impl = (struct gl2_container_impl *) intf;
375
376 return impl->_obj.attached_count;
377 }
378
379 static struct gl2_generic_intf **
380 _container_GetAttached(struct gl2_container_intf **intf, GLuint index)
381 {
382 struct gl2_container_impl *impl = (struct gl2_container_impl *) intf;
383
384 (**impl->_obj.attached[index])._unknown.AddRef((struct gl2_unknown_intf **)
385 impl->_obj.attached[index]);
386 return impl->_obj.attached[index];
387 }
388
389 static struct gl2_container_intf _container_vftbl = {
390 {
391 {
392 _unknown_AddRef,
393 _unknown_Release,
394 _container_QueryInterface
395 },
396 _generic_Delete,
397 NULL, /* abstract GetType */
398 _generic_GetName,
399 _generic_GetDeleteStatus,
400 _generic_GetInfoLog,
401 _generic_GetInfoLogLength
402 },
403 _container_Attach,
404 _container_Detach,
405 _container_GetAttachedCount,
406 _container_GetAttached
407 };
408
409 static void
410 _container_constructor(struct gl2_container_impl *impl)
411 {
412 _generic_constructor((struct gl2_generic_impl *) impl);
413 impl->_vftbl = &_container_vftbl;
414 impl->_obj._generic._unknown._destructor = _container_destructor;
415 impl->_obj.attached = NULL;
416 impl->_obj.attached_count = 0;
417 }
418
419 struct gl2_3dlabs_shhandle_obj
420 {
421 struct gl2_unkinner_obj _unknown;
422 #if USE_3DLABS_FRONTEND
423 ShHandle handle;
424 #endif
425 };
426
427 struct gl2_3dlabs_shhandle_impl
428 {
429 struct gl2_3dlabs_shhandle_intf *_vftbl;
430 struct gl2_3dlabs_shhandle_obj _obj;
431 };
432
433 static void
434 _3dlabs_shhandle_destructor(struct gl2_unknown_intf **intf)
435 {
436 #if USE_3DLABS_FRONTEND
437 struct gl2_3dlabs_shhandle_impl *impl =
438 (struct gl2_3dlabs_shhandle_impl *) intf;
439 ShDestruct(impl->_obj.handle);
440 #endif
441 _unkinner_destructor(intf);
442 }
443
444 static GLvoid *
445 _3dlabs_shhandle_GetShHandle(struct gl2_3dlabs_shhandle_intf **intf)
446 {
447 #if USE_3DLABS_FRONTEND
448 struct gl2_3dlabs_shhandle_impl *impl =
449 (struct gl2_3dlabs_shhandle_impl *) intf;
450 return impl->_obj.handle;
451 #else
452 return NULL;
453 #endif
454 }
455
456 static struct gl2_3dlabs_shhandle_intf _3dlabs_shhandle_vftbl = {
457 {
458 _unkinner_AddRef,
459 _unkinner_Release,
460 _unkinner_QueryInterface},
461 _3dlabs_shhandle_GetShHandle
462 };
463
464 static void
465 _3dlabs_shhandle_constructor(struct gl2_3dlabs_shhandle_impl *impl,
466 struct gl2_unknown_intf **outer)
467 {
468 _unkinner_constructor((struct gl2_unkinner_impl *) impl, outer);
469 impl->_vftbl = &_3dlabs_shhandle_vftbl;
470 #if USE_3DLABS_FRONTEND
471 impl->_obj.handle = NULL;
472 #endif
473 }
474
475 struct gl2_shader_obj
476 {
477 struct gl2_generic_obj _generic;
478 struct gl2_3dlabs_shhandle_impl _3dlabs_shhandle;
479 GLboolean compile_status;
480 GLcharARB *source;
481 GLint *offsets;
482 GLsizei offset_count;
483 slang_code_object code;
484 };
485
486 struct gl2_shader_impl
487 {
488 struct gl2_shader_intf *_vftbl;
489 struct gl2_shader_obj _obj;
490 };
491
492 static void
493 _shader_destructor(struct gl2_unknown_intf **intf)
494 {
495 struct gl2_shader_impl *impl = (struct gl2_shader_impl *) intf;
496
497 _mesa_free((void *) impl->_obj.source);
498 _mesa_free((void *) impl->_obj.offsets);
499 _slang_code_object_dtr(&impl->_obj.code);
500 _3dlabs_shhandle_destructor((struct gl2_unknown_intf **) &impl->_obj.
501 _3dlabs_shhandle._vftbl);
502 _generic_destructor(intf);
503 }
504
505 static struct gl2_unknown_intf **
506 _shader_QueryInterface(struct gl2_unknown_intf **intf, enum gl2_uiid uiid)
507 {
508 #if USE_3DLABS_FRONTEND
509 struct gl2_shader_impl *impl = (struct gl2_shader_impl *) intf;
510 #endif
511
512 if (uiid == UIID_SHADER) {
513 (**intf).AddRef(intf);
514 return intf;
515 }
516 #if USE_3DLABS_FRONTEND
517 if (uiid == UIID_3DLABS_SHHANDLE) {
518 (**intf).AddRef(intf);
519 return (struct gl2_unknown_intf **) &impl->_obj._3dlabs_shhandle._vftbl;
520 }
521 #endif
522 return _generic_QueryInterface(intf, uiid);
523 }
524
525 static GLenum
526 _shader_GetType(struct gl2_generic_intf **intf)
527 {
528 return GL_SHADER_OBJECT_ARB;
529 }
530
531 static GLvoid
532 _shader_GetInfoLog(struct gl2_generic_intf **intf, GLsizei maxlen,
533 GLcharARB * infolog)
534 {
535 struct gl2_shader_impl *impl = (struct gl2_shader_impl *) (intf);
536
537 if (maxlen > 0) {
538 if (impl->_obj._generic.info_log != NULL) {
539 GLsizei len = _mesa_strlen(impl->_obj._generic.info_log);
540 if (len > maxlen - 1)
541 len = maxlen - 1;
542 _mesa_memcpy(infolog, impl->_obj._generic.info_log, len);
543 infolog += len;
544 maxlen -= len;
545 }
546 if (impl->_obj.code.machine.infolog != NULL &&
547 impl->_obj.code.machine.infolog->text != NULL) {
548 GLsizei len = _mesa_strlen(impl->_obj.code.machine.infolog->text);
549 if (len > maxlen - 1)
550 len = maxlen - 1;
551 _mesa_memcpy(infolog, impl->_obj.code.machine.infolog->text, len);
552 }
553 infolog[maxlen - 1] = '\0';
554 }
555 }
556
557 static GLsizei
558 _shader_GetInfoLogLength(struct gl2_generic_intf **intf)
559 {
560 struct gl2_shader_impl *impl = (struct gl2_shader_impl *) (intf);
561 GLsizei length = 1;
562
563 if (impl->_obj._generic.info_log != NULL)
564 length += _mesa_strlen(impl->_obj._generic.info_log);
565 if (impl->_obj.code.machine.infolog != NULL &&
566 impl->_obj.code.machine.infolog->text != NULL)
567 length += _mesa_strlen(impl->_obj.code.machine.infolog->text);
568 return length;
569 }
570
571 static GLboolean
572 _shader_GetCompileStatus(struct gl2_shader_intf **intf)
573 {
574 struct gl2_shader_impl *impl = (struct gl2_shader_impl *) intf;
575
576 return impl->_obj.compile_status;
577 }
578
579 static GLvoid
580 _shader_SetSource(struct gl2_shader_intf **intf, GLcharARB * src, GLint * off,
581 GLsizei cnt)
582 {
583 struct gl2_shader_impl *impl = (struct gl2_shader_impl *) intf;
584
585 _mesa_free((void *) impl->_obj.source);
586 impl->_obj.source = src;
587 _mesa_free((void *) impl->_obj.offsets);
588 impl->_obj.offsets = off;
589 impl->_obj.offset_count = cnt;
590 }
591
592 static const GLcharARB *
593 _shader_GetSource(struct gl2_shader_intf **intf)
594 {
595 struct gl2_shader_impl *impl = (struct gl2_shader_impl *) intf;
596
597 return impl->_obj.source;
598 }
599
600 static GLvoid
601 _shader_Compile(struct gl2_shader_intf **intf)
602 {
603 struct gl2_shader_impl *impl = (struct gl2_shader_impl *) intf;
604 #if USE_3DLABS_FRONTEND
605 char **strings;
606 TBuiltInResource res;
607 #else
608 slang_unit_type type;
609 slang_info_log info_log;
610 #endif
611
612 impl->_obj.compile_status = GL_FALSE;
613 _mesa_free((void *) impl->_obj._generic.info_log);
614 impl->_obj._generic.info_log = NULL;
615
616 #if USE_3DLABS_FRONTEND
617 /* 3dlabs compiler expects us to feed it with null-terminated string array,
618 we've got only one big string with offsets, so we must split it; but when
619 there's only one string to deal with, we pass its address directly */
620
621 if (impl->_obj.offset_count <= 1)
622 strings = &impl->_obj.source;
623 else {
624 GLsizei i, offset = 0;
625
626 strings =
627 (char **) _mesa_malloc(impl->_obj.offset_count * sizeof(char *));
628 if (strings == NULL)
629 return;
630
631 for (i = 0; i < impl->_obj.offset_count; i++) {
632 GLsizei size = impl->_obj.offsets[i] - offset;
633
634 strings[i] = (char *) _mesa_malloc((size + 1) * sizeof(char));
635 if (strings[i] == NULL) {
636 GLsizei j;
637
638 for (j = 0; j < i; j++)
639 _mesa_free(strings[j]);
640 _mesa_free(strings);
641 return;
642 }
643
644 _mesa_memcpy(strings[i], impl->_obj.source + offset,
645 size * sizeof(char));
646 strings[i][size] = '\0';
647 offset = impl->_obj.offsets[i];
648 }
649 }
650
651 /* TODO set these fields to some REAL numbers */
652 res.maxLights = 8;
653 res.maxClipPlanes = 6;
654 res.maxTextureUnits = 2;
655 res.maxTextureCoords = 2;
656 res.maxVertexAttribs = 8;
657 res.maxVertexUniformComponents = 64;
658 res.maxVaryingFloats = 8;
659 res.maxVertexTextureImageUnits = 2;
660 res.maxCombinedTextureImageUnits = 2;
661 res.maxTextureImageUnits = 2;
662 res.maxFragmentUniformComponents = 64;
663 res.maxDrawBuffers = 1;
664
665 if (ShCompile
666 (impl->_obj._3dlabs_shhandle._obj.handle, strings,
667 impl->_obj.offset_count, EShOptFull, &res, 0))
668 impl->_obj.compile_status = GL_TRUE;
669 if (impl->_obj.offset_count > 1) {
670 GLsizei i;
671
672 for (i = 0; i < impl->_obj.offset_count; i++)
673 _mesa_free(strings[i]);
674 _mesa_free(strings);
675 }
676
677 impl->_obj._generic.info_log =
678 _mesa_strdup(ShGetInfoLog(impl->_obj._3dlabs_shhandle._obj.handle));
679 #else
680 if (impl->_vftbl->GetSubType(intf) == GL_FRAGMENT_SHADER)
681 type = slang_unit_fragment_shader;
682 else
683 type = slang_unit_vertex_shader;
684 slang_info_log_construct(&info_log);
685 if (_slang_compile(impl->_obj.source, &impl->_obj.code, type, &info_log))
686 impl->_obj.compile_status = GL_TRUE;
687 if (info_log.text != NULL)
688 impl->_obj._generic.info_log = _mesa_strdup(info_log.text);
689 else if (impl->_obj.compile_status)
690 impl->_obj._generic.info_log = _mesa_strdup("Compile OK.\n");
691 else
692 impl->_obj._generic.info_log = _mesa_strdup("Compile failed.\n");
693 slang_info_log_destruct(&info_log);
694 #endif
695 }
696
697 static struct gl2_shader_intf _shader_vftbl = {
698 {
699 {
700 _unknown_AddRef,
701 _unknown_Release,
702 _shader_QueryInterface
703 },
704 _generic_Delete,
705 _shader_GetType,
706 _generic_GetName,
707 _generic_GetDeleteStatus,
708 _shader_GetInfoLog,
709 _shader_GetInfoLogLength
710 },
711 NULL, /* abstract GetSubType */
712 _shader_GetCompileStatus,
713 _shader_SetSource,
714 _shader_GetSource,
715 _shader_Compile
716 };
717
718 static void
719 _shader_constructor(struct gl2_shader_impl *impl)
720 {
721 _generic_constructor((struct gl2_generic_impl *) impl);
722 _3dlabs_shhandle_constructor(&impl->_obj._3dlabs_shhandle,
723 (struct gl2_unknown_intf **)
724 &impl->_vftbl);
725 impl->_vftbl = &_shader_vftbl;
726 impl->_obj._generic._unknown._destructor = _shader_destructor;
727 impl->_obj.compile_status = GL_FALSE;
728 impl->_obj.source = NULL;
729 impl->_obj.offsets = NULL;
730 impl->_obj.offset_count = 0;
731 _slang_code_object_ctr(&impl->_obj.code);
732 }
733
734 struct gl2_program_obj
735 {
736 struct gl2_container_obj _container;
737 GLboolean link_status;
738 GLboolean validate_status;
739 #if USE_3DLABS_FRONTEND
740 ShHandle linker;
741 ShHandle uniforms;
742 #endif
743 slang_program prog;
744 };
745
746 struct gl2_program_impl
747 {
748 struct gl2_program_intf *_vftbl;
749 struct gl2_program_obj _obj;
750 };
751
752 static void
753 _program_destructor(struct gl2_unknown_intf **intf)
754 {
755 struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;
756 #if USE_3DLABS_FRONTEND
757 ShDestruct(impl->_obj.linker);
758 ShDestruct(impl->_obj.uniforms);
759 #endif
760 _container_destructor(intf);
761 _slang_program_dtr(&impl->_obj.prog);
762 }
763
764 static struct gl2_unknown_intf **
765 _program_QueryInterface(struct gl2_unknown_intf **intf, enum gl2_uiid uiid)
766 {
767 if (uiid == UIID_PROGRAM) {
768 (**intf).AddRef(intf);
769 return intf;
770 }
771 return _container_QueryInterface(intf, uiid);
772 }
773
774 static GLenum
775 _program_GetType(struct gl2_generic_intf **intf)
776 {
777 return GL_PROGRAM_OBJECT_ARB;
778 }
779
780 static GLboolean
781 _program_Attach(struct gl2_container_intf **intf,
782 struct gl2_generic_intf **att)
783 {
784 GET_CURRENT_CONTEXT(ctx);
785 struct gl2_unknown_intf **sha;
786
787 sha =
788 (**att)._unknown.QueryInterface((struct gl2_unknown_intf **) att,
789 UIID_SHADER);
790 if (sha == NULL) {
791 _mesa_error(ctx, GL_INVALID_OPERATION, "_program_Attach");
792 return GL_FALSE;
793 }
794
795 (**sha).Release(sha);
796 return _container_Attach(intf, att);
797 }
798
799 static GLboolean
800 _program_GetLinkStatus(struct gl2_program_intf **intf)
801 {
802 struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;
803
804 return impl->_obj.link_status;
805 }
806
807 static GLboolean
808 _program_GetValidateStatus(struct gl2_program_intf **intf)
809 {
810 struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;
811
812 return impl->_obj.validate_status;
813 }
814
815 static GLvoid
816 _program_Link(struct gl2_program_intf **intf)
817 {
818 struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;
819 #if USE_3DLABS_FRONTEND
820 ShHandle *handles;
821 #endif
822 GLuint i, count;
823 slang_code_object *code[2];
824 GLboolean all_compiled = GL_TRUE;
825
826 impl->_obj.link_status = GL_FALSE;
827 _mesa_free((void *) impl->_obj._container._generic.info_log);
828 impl->_obj._container._generic.info_log = NULL;
829 _slang_program_rst(&impl->_obj.prog);
830
831 #if USE_3DLABS_FRONTEND
832 handles =
833 (ShHandle *) _mesa_malloc(impl->_obj._container.attached_count *
834 sizeof(ShHandle));
835 if (handles == NULL)
836 return;
837
838 for (i = 0; i < impl->_obj._container.attached_count; i++) {
839 struct gl2_generic_intf **gen = impl->_obj._container.attached[i];
840 struct gl2_3dlabs_shhandle_intf **sh;
841
842 sh =
843 (struct gl2_3dlabs_shhandle_intf **) (**gen)._unknown.
844 QueryInterface((struct gl2_unknown_intf **) gen,
845 UIID_3DLABS_SHHANDLE);
846 if (sh != NULL) {
847 handles[i] = (**sh).GetShHandle(sh);
848 (**sh)._unknown.Release((struct gl2_unknown_intf **) sh);
849 }
850 else {
851 _mesa_free(handles);
852 return;
853 }
854 }
855
856 if (ShLink(impl->_obj.linker, handles, impl->_obj._container.attached_count,
857 impl->_obj.uniforms, NULL, NULL))
858 impl->_obj.link_status = GL_TRUE;
859
860 impl->_obj._container._generic.info_log =
861 _mesa_strdup(ShGetInfoLog(impl->_obj.linker));
862 #else
863 count = impl->_obj._container.attached_count;
864 if (count > 2)
865 return;
866
867 for (i = 0; i < count; i++) {
868 struct gl2_generic_intf **obj;
869 struct gl2_unknown_intf **unk;
870 struct gl2_shader_impl *sha;
871
872 obj = impl->_obj._container.attached[i];
873 unk =
874 (**obj)._unknown.QueryInterface((struct gl2_unknown_intf **) obj,
875 UIID_SHADER);
876 if (unk == NULL)
877 return;
878 sha = (struct gl2_shader_impl *) unk;
879 code[i] = &sha->_obj.code;
880 all_compiled = all_compiled && sha->_obj.compile_status;
881 (**unk).Release(unk);
882 }
883
884 impl->_obj.link_status = all_compiled;
885 if (!impl->_obj.link_status) {
886 impl->_obj._container._generic.info_log =
887 _mesa_strdup
888 ("Error: One or more shaders has not successfully compiled.\n");
889 return;
890 }
891
892 impl->_obj.link_status = _slang_link(&impl->_obj.prog, code, count);
893 if (!impl->_obj.link_status) {
894 impl->_obj._container._generic.info_log =
895 _mesa_strdup("Link failed.\n");
896 return;
897 }
898
899 impl->_obj._container._generic.info_log = _mesa_strdup("Link OK.\n");
900 #endif
901 }
902
903 static GLvoid
904 _program_Validate(struct gl2_program_intf **intf)
905 {
906 struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;
907
908 impl->_obj.validate_status = GL_FALSE;
909 _mesa_free((void *) impl->_obj._container._generic.info_log);
910 impl->_obj._container._generic.info_log = NULL;
911
912 /* TODO validate */
913 }
914
915 static GLvoid
916 write_common_fixed(slang_program * pro, GLuint index, const GLvoid * src,
917 GLuint off, GLuint size)
918 {
919 GLuint i;
920
921 for (i = 0; i < SLANG_SHADER_MAX; i++) {
922 GLuint addr;
923
924 addr = pro->common_fixed_entries[i][index];
925 if (addr != ~0) {
926 GLubyte *dst;
927
928 dst = (GLubyte *) pro->machines[i]->mem + addr + off * size;
929 _mesa_memcpy(dst, src, size);
930 }
931 }
932 }
933
934 static GLvoid
935 write_common_fixed_mat4(slang_program * pro, GLmatrix * matrix, GLuint off,
936 GLuint i, GLuint ii, GLuint it, GLuint iit)
937 {
938 GLfloat mat[16];
939
940 /* we want inverse matrix */
941 if (!matrix->inv) {
942 /* allocate inverse matrix and make it dirty */
943 _math_matrix_alloc_inv(matrix);
944 _math_matrix_loadf(matrix, matrix->m);
945 }
946 _math_matrix_analyse(matrix);
947
948 write_common_fixed(pro, i, matrix->m, off, 16 * sizeof(GLfloat));
949
950 /* inverse */
951 write_common_fixed(pro, ii, matrix->inv, off, 16 * sizeof(GLfloat));
952
953 /* transpose */
954 _math_transposef(mat, matrix->m);
955 write_common_fixed(pro, it, mat, off, 16 * sizeof(GLfloat));
956
957 /* inverse transpose */
958 _math_transposef(mat, matrix->inv);
959 write_common_fixed(pro, iit, mat, off, 16 * sizeof(GLfloat));
960 }
961
962 static GLvoid
963 write_common_fixed_material(GLcontext * ctx, slang_program * pro, GLuint i,
964 GLuint e, GLuint a, GLuint d, GLuint sp,
965 GLuint sh)
966 {
967 GLfloat v[17];
968
969 COPY_4FV(v, ctx->Light.Material.Attrib[e]);
970 COPY_4FV((v + 4), ctx->Light.Material.Attrib[a]);
971 COPY_4FV((v + 8), ctx->Light.Material.Attrib[d]);
972 COPY_4FV((v + 12), ctx->Light.Material.Attrib[sp]);
973 v[16] = ctx->Light.Material.Attrib[sh][0];
974 write_common_fixed(pro, i, v, 0, 17 * sizeof(GLfloat));
975 }
976
977 static GLvoid
978 write_common_fixed_light_model_product(GLcontext * ctx, slang_program * pro,
979 GLuint i, GLuint e, GLuint a)
980 {
981 GLfloat v[4];
982
983 SCALE_4V(v, ctx->Light.Material.Attrib[a], ctx->Light.Model.Ambient);
984 ACC_4V(v, ctx->Light.Material.Attrib[e]);
985 write_common_fixed(pro, i, v, 0, 4 * sizeof(GLfloat));
986 }
987
988 static GLvoid
989 write_common_fixed_light_product(GLcontext * ctx, slang_program * pro,
990 GLuint off, GLuint i, GLuint a, GLuint d,
991 GLuint s)
992 {
993 GLfloat v[12];
994
995 SCALE_4V(v, ctx->Light.Light[off].Ambient, ctx->Light.Material.Attrib[a]);
996 SCALE_4V((v + 4), ctx->Light.Light[off].Diffuse,
997 ctx->Light.Material.Attrib[d]);
998 SCALE_4V((v + 8), ctx->Light.Light[off].Specular,
999 ctx->Light.Material.Attrib[s]);
1000 write_common_fixed(pro, i, v, off, 12 * sizeof(GLfloat));
1001 }
1002
1003 static GLvoid
1004 _program_UpdateFixedUniforms(struct gl2_program_intf **intf)
1005 {
1006 GET_CURRENT_CONTEXT(ctx);
1007 struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;
1008 slang_program *pro = &impl->_obj.prog;
1009 GLuint i;
1010 GLfloat v[29];
1011 GLfloat *p;
1012
1013 /* MODELVIEW matrix */
1014 write_common_fixed_mat4(pro, ctx->ModelviewMatrixStack.Top, 0,
1015 SLANG_COMMON_FIXED_MODELVIEWMATRIX,
1016 SLANG_COMMON_FIXED_MODELVIEWMATRIXINVERSE,
1017 SLANG_COMMON_FIXED_MODELVIEWMATRIXTRANSPOSE,
1018 SLANG_COMMON_FIXED_MODELVIEWMATRIXINVERSETRANSPOSE);
1019
1020 /* PROJECTION matrix */
1021 write_common_fixed_mat4(pro, ctx->ProjectionMatrixStack.Top, 0,
1022 SLANG_COMMON_FIXED_PROJECTIONMATRIX,
1023 SLANG_COMMON_FIXED_PROJECTIONMATRIXINVERSE,
1024 SLANG_COMMON_FIXED_PROJECTIONMATRIXTRANSPOSE,
1025 SLANG_COMMON_FIXED_PROJECTIONMATRIXINVERSETRANSPOSE);
1026
1027 /* MVP matrix */
1028 write_common_fixed_mat4(pro, &ctx->_ModelProjectMatrix, 0,
1029 SLANG_COMMON_FIXED_MODELVIEWPROJECTIONMATRIX,
1030 SLANG_COMMON_FIXED_MODELVIEWPROJECTIONMATRIXINVERSE,
1031 SLANG_COMMON_FIXED_MODELVIEWPROJECTIONMATRIXTRANSPOSE,
1032 SLANG_COMMON_FIXED_MODELVIEWPROJECTIONMATRIXINVERSETRANSPOSE);
1033
1034 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
1035 /* TEXTURE matrix */
1036 write_common_fixed_mat4(pro, ctx->TextureMatrixStack[i].Top, i,
1037 SLANG_COMMON_FIXED_TEXTUREMATRIX,
1038 SLANG_COMMON_FIXED_TEXTUREMATRIXINVERSE,
1039 SLANG_COMMON_FIXED_TEXTUREMATRIXTRANSPOSE,
1040 SLANG_COMMON_FIXED_TEXTUREMATRIXINVERSETRANSPOSE);
1041
1042 /* EYE_PLANE texture-coordinate generation */
1043 write_common_fixed(pro, SLANG_COMMON_FIXED_EYEPLANES,
1044 ctx->Texture.Unit[i].EyePlaneS, i,
1045 4 * sizeof(GLfloat));
1046 write_common_fixed(pro, SLANG_COMMON_FIXED_EYEPLANET,
1047 ctx->Texture.Unit[i].EyePlaneT, i,
1048 4 * sizeof(GLfloat));
1049 write_common_fixed(pro, SLANG_COMMON_FIXED_EYEPLANER,
1050 ctx->Texture.Unit[i].EyePlaneR, i,
1051 4 * sizeof(GLfloat));
1052 write_common_fixed(pro, SLANG_COMMON_FIXED_EYEPLANEQ,
1053 ctx->Texture.Unit[i].EyePlaneQ, i,
1054 4 * sizeof(GLfloat));
1055
1056 /* OBJECT_PLANE texture-coordinate generation */
1057 write_common_fixed(pro, SLANG_COMMON_FIXED_OBJECTPLANES,
1058 ctx->Texture.Unit[i].ObjectPlaneS, i,
1059 4 * sizeof(GLfloat));
1060 write_common_fixed(pro, SLANG_COMMON_FIXED_OBJECTPLANET,
1061 ctx->Texture.Unit[i].ObjectPlaneT, i,
1062 4 * sizeof(GLfloat));
1063 write_common_fixed(pro, SLANG_COMMON_FIXED_OBJECTPLANER,
1064 ctx->Texture.Unit[i].ObjectPlaneR, i,
1065 4 * sizeof(GLfloat));
1066 write_common_fixed(pro, SLANG_COMMON_FIXED_OBJECTPLANEQ,
1067 ctx->Texture.Unit[i].ObjectPlaneQ, i,
1068 4 * sizeof(GLfloat));
1069 }
1070
1071 /* NORMAL matrix - upper 3x3 inverse transpose of MODELVIEW matrix */
1072 p = ctx->ModelviewMatrixStack.Top->inv;
1073 v[0] = p[0];
1074 v[1] = p[4];
1075 v[2] = p[8];
1076 v[3] = p[1];
1077 v[4] = p[5];
1078 v[5] = p[9];
1079 v[6] = p[2];
1080 v[7] = p[6];
1081 v[8] = p[10];
1082 write_common_fixed(pro, SLANG_COMMON_FIXED_NORMALMATRIX, v, 0,
1083 9 * sizeof(GLfloat));
1084
1085 /* normal scale */
1086 write_common_fixed(pro, SLANG_COMMON_FIXED_NORMALSCALE,
1087 &ctx->_ModelViewInvScale, 0, sizeof(GLfloat));
1088
1089 /* depth range parameters */
1090 v[0] = ctx->Viewport.Near;
1091 v[1] = ctx->Viewport.Far;
1092 v[2] = ctx->Viewport.Far - ctx->Viewport.Near;
1093 write_common_fixed(pro, SLANG_COMMON_FIXED_DEPTHRANGE, v, 0,
1094 3 * sizeof(GLfloat));
1095
1096 /* CLIP_PLANEi */
1097 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
1098 write_common_fixed(pro, SLANG_COMMON_FIXED_CLIPPLANE,
1099 ctx->Transform.EyeUserPlane[i], i,
1100 4 * sizeof(GLfloat));
1101 }
1102
1103 /* point parameters */
1104 v[0] = ctx->Point.Size;
1105 v[1] = ctx->Point.MinSize;
1106 v[2] = ctx->Point.MaxSize;
1107 v[3] = ctx->Point.Threshold;
1108 COPY_3FV((v + 4), ctx->Point.Params);
1109 write_common_fixed(pro, SLANG_COMMON_FIXED_POINT, v, 0,
1110 7 * sizeof(GLfloat));
1111
1112 /* material parameters */
1113 write_common_fixed_material(ctx, pro, SLANG_COMMON_FIXED_FRONTMATERIAL,
1114 MAT_ATTRIB_FRONT_EMISSION,
1115 MAT_ATTRIB_FRONT_AMBIENT,
1116 MAT_ATTRIB_FRONT_DIFFUSE,
1117 MAT_ATTRIB_FRONT_SPECULAR,
1118 MAT_ATTRIB_FRONT_SHININESS);
1119 write_common_fixed_material(ctx, pro, SLANG_COMMON_FIXED_BACKMATERIAL,
1120 MAT_ATTRIB_BACK_EMISSION,
1121 MAT_ATTRIB_BACK_AMBIENT,
1122 MAT_ATTRIB_BACK_DIFFUSE,
1123 MAT_ATTRIB_BACK_SPECULAR,
1124 MAT_ATTRIB_BACK_SHININESS);
1125
1126 for (i = 0; i < ctx->Const.MaxLights; i++) {
1127 /* light source parameters */
1128 COPY_4FV(v, ctx->Light.Light[i].Ambient);
1129 COPY_4FV((v + 4), ctx->Light.Light[i].Diffuse);
1130 COPY_4FV((v + 8), ctx->Light.Light[i].Specular);
1131 COPY_4FV((v + 12), ctx->Light.Light[i].EyePosition);
1132 COPY_2FV((v + 16), ctx->Light.Light[i].EyePosition);
1133 v[18] = ctx->Light.Light[i].EyePosition[2] + 1.0f;
1134 NORMALIZE_3FV((v + 16));
1135 v[19] = 0.0f;
1136 COPY_3V((v + 20), ctx->Light.Light[i].EyeDirection);
1137 v[23] = ctx->Light.Light[i].SpotExponent;
1138 v[24] = ctx->Light.Light[i].SpotCutoff;
1139 v[25] = ctx->Light.Light[i]._CosCutoffNeg;
1140 v[26] = ctx->Light.Light[i].ConstantAttenuation;
1141 v[27] = ctx->Light.Light[i].LinearAttenuation;
1142 v[28] = ctx->Light.Light[i].QuadraticAttenuation;
1143 write_common_fixed(pro, SLANG_COMMON_FIXED_LIGHTSOURCE, v, i,
1144 29 * sizeof(GLfloat));
1145
1146 /* light product */
1147 write_common_fixed_light_product(ctx, pro, i,
1148 SLANG_COMMON_FIXED_FRONTLIGHTPRODUCT,
1149 MAT_ATTRIB_FRONT_AMBIENT,
1150 MAT_ATTRIB_FRONT_DIFFUSE,
1151 MAT_ATTRIB_FRONT_SPECULAR);
1152 write_common_fixed_light_product(ctx, pro, i,
1153 SLANG_COMMON_FIXED_BACKLIGHTPRODUCT,
1154 MAT_ATTRIB_BACK_AMBIENT,
1155 MAT_ATTRIB_BACK_DIFFUSE,
1156 MAT_ATTRIB_BACK_SPECULAR);
1157 }
1158
1159 /* light model parameters */
1160 write_common_fixed(pro, SLANG_COMMON_FIXED_LIGHTMODEL,
1161 ctx->Light.Model.Ambient, 0, 4 * sizeof(GLfloat));
1162
1163 /* light model product */
1164 write_common_fixed_light_model_product(ctx, pro,
1165 SLANG_COMMON_FIXED_FRONTLIGHTMODELPRODUCT,
1166 MAT_ATTRIB_FRONT_EMISSION,
1167 MAT_ATTRIB_FRONT_AMBIENT);
1168 write_common_fixed_light_model_product(ctx, pro,
1169 SLANG_COMMON_FIXED_BACKLIGHTMODELPRODUCT,
1170 MAT_ATTRIB_BACK_EMISSION,
1171 MAT_ATTRIB_BACK_AMBIENT);
1172
1173 /* TEXTURE_ENV_COLOR */
1174 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
1175 write_common_fixed(pro, SLANG_COMMON_FIXED_TEXTUREENVCOLOR,
1176 ctx->Texture.Unit[i].EnvColor, i,
1177 4 * sizeof(GLfloat));
1178 }
1179
1180 /* fog parameters */
1181 COPY_4FV(v, ctx->Fog.Color);
1182 v[4] = ctx->Fog.Density;
1183 v[5] = ctx->Fog.Start;
1184 v[6] = ctx->Fog.End;
1185 v[7] = ctx->Fog._Scale;
1186 write_common_fixed(pro, SLANG_COMMON_FIXED_FOG, v, 0, 8 * sizeof(GLfloat));
1187 }
1188
1189 static GLvoid
1190 _program_UpdateFixedAttrib(struct gl2_program_intf **intf, GLuint index,
1191 GLvoid * data, GLuint offset, GLuint size,
1192 GLboolean write)
1193 {
1194 struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;
1195 slang_program *pro = &impl->_obj.prog;
1196 GLuint addr;
1197
1198 addr = pro->vertex_fixed_entries[index];
1199 if (addr != ~0) {
1200 GLubyte *mem;
1201
1202 mem =
1203 (GLubyte *) pro->machines[SLANG_SHADER_VERTEX]->mem + addr +
1204 offset * size;
1205 if (write)
1206 _mesa_memcpy(mem, data, size);
1207 else
1208 _mesa_memcpy(data, mem, size);
1209 }
1210 }
1211
1212
1213 /**
1214 * Called during fragment shader execution to either load a varying
1215 * register with values, or fetch values from a varying register.
1216 * \param intf the internal program?
1217 * \param index which varying register, one of the SLANG_FRAGMENT_FIXED_*
1218 * values for example.
1219 * \param data source values to load (or dest to write to)
1220 * \param offset indicates a texture unit or generic varying attribute
1221 * \param size number of bytes to copy
1222 * \param write if true, write to the varying register, else store values
1223 * in 'data'
1224 */
1225 static GLvoid
1226 _program_UpdateFixedVarying(struct gl2_program_intf **intf, GLuint index,
1227 GLvoid * data,
1228 GLuint offset, GLuint size, GLboolean write)
1229 {
1230 struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;
1231 slang_program *pro = &impl->_obj.prog;
1232 GLuint addr;
1233
1234 addr = pro->fragment_fixed_entries[index];
1235 if (addr != ~0) {
1236 GLubyte *mem;
1237
1238 mem =
1239 (GLubyte *) pro->machines[SLANG_SHADER_FRAGMENT]->mem + addr +
1240 offset * size;
1241 if (write)
1242 _mesa_memcpy(mem, data, size);
1243 else
1244 _mesa_memcpy(data, mem, size);
1245 }
1246 }
1247
1248 static GLvoid
1249 _program_GetTextureImageUsage(struct gl2_program_intf **intf,
1250 GLbitfield * teximageusage)
1251 {
1252 GET_CURRENT_CONTEXT(ctx);
1253 struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;
1254 slang_program *pro = &impl->_obj.prog;
1255 GLuint i;
1256
1257 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++)
1258 teximageusage[i] = 0;
1259
1260 for (i = 0; i < pro->texture_usage.count; i++) {
1261 GLuint n, addr, j;
1262
1263 n = slang_export_data_quant_elements(pro->texture_usage.table[i].quant);
1264 addr = pro->texture_usage.table[i].frag_address;
1265 for (j = 0; j < n; j++) {
1266 GLubyte *mem;
1267 GLuint image;
1268
1269 mem =
1270 (GLubyte *) pro->machines[SLANG_SHADER_FRAGMENT]->mem + addr +
1271 j * 4;
1272 image = (GLuint) * ((GLfloat *) mem);
1273 if (image >= 0 && image < ctx->Const.MaxTextureImageUnits) {
1274 switch (slang_export_data_quant_type
1275 (pro->texture_usage.table[i].quant)) {
1276 case GL_SAMPLER_1D_ARB:
1277 case GL_SAMPLER_1D_SHADOW_ARB:
1278 teximageusage[image] |= TEXTURE_1D_BIT;
1279 break;
1280 case GL_SAMPLER_2D_ARB:
1281 case GL_SAMPLER_2D_SHADOW_ARB:
1282 teximageusage[image] |= TEXTURE_2D_BIT;
1283 break;
1284 case GL_SAMPLER_3D_ARB:
1285 teximageusage[image] |= TEXTURE_3D_BIT;
1286 break;
1287 case GL_SAMPLER_CUBE_ARB:
1288 teximageusage[image] |= TEXTURE_CUBE_BIT;
1289 break;
1290 }
1291 }
1292 }
1293 }
1294
1295 /* TODO: make sure that for 0<=i<=MaxTextureImageUint bitcount(teximageuint[i])<=0 */
1296 }
1297
1298 static GLboolean
1299 _program_IsShaderPresent(struct gl2_program_intf **intf, GLenum subtype)
1300 {
1301 struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;
1302 slang_program *pro = &impl->_obj.prog;
1303
1304 switch (subtype) {
1305 case GL_VERTEX_SHADER_ARB:
1306 return pro->machines[SLANG_SHADER_VERTEX] != NULL;
1307 case GL_FRAGMENT_SHADER_ARB:
1308 return pro->machines[SLANG_SHADER_FRAGMENT] != NULL;
1309 default:
1310 return GL_FALSE;
1311 }
1312 }
1313
1314 static GLvoid
1315 get_active_variable(slang_active_variable * var, GLsizei maxLength,
1316 GLsizei * length, GLint * size, GLenum * type,
1317 GLchar * name)
1318 {
1319 GLsizei len;
1320
1321 len = _mesa_strlen(var->name);
1322 if (len >= maxLength)
1323 len = maxLength - 1;
1324 if (length != NULL)
1325 *length = len;
1326 *size = slang_export_data_quant_elements(var->quant);
1327 *type = slang_export_data_quant_type(var->quant);
1328 _mesa_memcpy(name, var->name, len);
1329 name[len] = '\0';
1330 }
1331
1332 static GLuint
1333 get_active_variable_max_length(slang_active_variables * vars)
1334 {
1335 GLuint i, len = 0;
1336
1337 for (i = 0; i < vars->count; i++) {
1338 GLuint n = _mesa_strlen(vars->table[i].name);
1339 if (n > len)
1340 len = n;
1341 }
1342 return len;
1343 }
1344
1345 static GLvoid
1346 _program_GetActiveUniform(struct gl2_program_intf **intf, GLuint index,
1347 GLsizei maxLength, GLsizei * length, GLint * size,
1348 GLenum * type, GLchar * name)
1349 {
1350 struct gl2_program_impl *impl = (struct gl2_program_impl *) (intf);
1351 slang_active_variable *u = &impl->_obj.prog.active_uniforms.table[index];
1352
1353 get_active_variable(u, maxLength, length, size, type, name);
1354 }
1355
1356 static GLuint
1357 _program_GetActiveUniformMaxLength(struct gl2_program_intf **intf)
1358 {
1359 struct gl2_program_impl *impl = (struct gl2_program_impl *) (intf);
1360
1361 return get_active_variable_max_length(&impl->_obj.prog.active_uniforms);
1362 }
1363
1364 static GLuint
1365 _program_GetActiveUniformCount(struct gl2_program_intf **intf)
1366 {
1367 struct gl2_program_impl *impl = (struct gl2_program_impl *) (intf);
1368
1369 return impl->_obj.prog.active_uniforms.count;
1370 }
1371
1372 static GLint
1373 _program_GetUniformLocation(struct gl2_program_intf **intf,
1374 const GLchar * name)
1375 {
1376 struct gl2_program_impl *impl = (struct gl2_program_impl *) (intf);
1377 slang_uniform_bindings *bind = &impl->_obj.prog.uniforms;
1378 GLuint i;
1379
1380 for (i = 0; i < bind->count; i++)
1381 if (_mesa_strcmp(bind->table[i].name, name) == 0)
1382 return i;
1383 return -1;
1384 }
1385
1386 /**
1387 * Write a uniform variable into program's memory.
1388 * \return GL_TRUE for success, GL_FALSE if error
1389 */
1390 static GLboolean
1391 _program_WriteUniform(struct gl2_program_intf **intf, GLint loc,
1392 GLsizei count, const GLvoid * data, GLenum type)
1393 {
1394 struct gl2_program_impl *impl = (struct gl2_program_impl *) (intf);
1395 slang_uniform_bindings *uniforms = &impl->_obj.prog.uniforms;
1396 slang_uniform_binding *uniform;
1397 GLuint i;
1398 GLboolean convert_float_to_bool = GL_FALSE;
1399 GLboolean convert_int_to_bool = GL_FALSE;
1400 GLboolean convert_int_to_float = GL_FALSE;
1401 GLboolean types_match = GL_FALSE;
1402
1403 if (loc < 0 || loc >= uniforms->count)
1404 return GL_FALSE;
1405
1406 uniform = &uniforms->table[loc];
1407 /* TODO: check sizes */
1408 if (slang_export_data_quant_struct(uniform->quant))
1409 return GL_FALSE;
1410
1411 switch (slang_export_data_quant_type(uniform->quant)) {
1412 case GL_BOOL_ARB:
1413 types_match = (type == GL_FLOAT) || (type == GL_INT);
1414 if (type == GL_FLOAT)
1415 convert_float_to_bool = GL_TRUE;
1416 else
1417 convert_int_to_bool = GL_TRUE;
1418 break;
1419 case GL_BOOL_VEC2_ARB:
1420 types_match = (type == GL_FLOAT_VEC2_ARB) || (type == GL_INT_VEC2_ARB);
1421 if (type == GL_FLOAT_VEC2_ARB)
1422 convert_float_to_bool = GL_TRUE;
1423 else
1424 convert_int_to_bool = GL_TRUE;
1425 break;
1426 case GL_BOOL_VEC3_ARB:
1427 types_match = (type == GL_FLOAT_VEC3_ARB) || (type == GL_INT_VEC3_ARB);
1428 if (type == GL_FLOAT_VEC3_ARB)
1429 convert_float_to_bool = GL_TRUE;
1430 else
1431 convert_int_to_bool = GL_TRUE;
1432 break;
1433 case GL_BOOL_VEC4_ARB:
1434 types_match = (type == GL_FLOAT_VEC4_ARB) || (type == GL_INT_VEC4_ARB);
1435 if (type == GL_FLOAT_VEC4_ARB)
1436 convert_float_to_bool = GL_TRUE;
1437 else
1438 convert_int_to_bool = GL_TRUE;
1439 break;
1440 case GL_SAMPLER_1D_ARB:
1441 case GL_SAMPLER_2D_ARB:
1442 case GL_SAMPLER_3D_ARB:
1443 case GL_SAMPLER_CUBE_ARB:
1444 case GL_SAMPLER_1D_SHADOW_ARB:
1445 case GL_SAMPLER_2D_SHADOW_ARB:
1446 types_match = (type == GL_INT);
1447 break;
1448 default:
1449 types_match = (type == slang_export_data_quant_type(uniform->quant));
1450 break;
1451 }
1452
1453 if (!types_match)
1454 return GL_FALSE;
1455
1456 switch (type) {
1457 case GL_INT:
1458 case GL_INT_VEC2_ARB:
1459 case GL_INT_VEC3_ARB:
1460 case GL_INT_VEC4_ARB:
1461 convert_int_to_float = GL_TRUE;
1462 break;
1463 }
1464
1465 for (i = 0; i < SLANG_SHADER_MAX; i++) {
1466 if (uniform->address[i] != ~0) {
1467 void *dest
1468 = &impl->_obj.prog.machines[i]->mem[uniform->address[i] / 4];
1469 /* total number of values to copy */
1470 GLuint total
1471 = count * slang_export_data_quant_components(uniform->quant);
1472 GLuint j;
1473 if (convert_float_to_bool) {
1474 const GLfloat *src = (GLfloat *) (data);
1475 GLfloat *dst = (GLfloat *) dest;
1476 for (j = 0; j < total; j++)
1477 dst[j] = src[j] != 0.0f ? 1.0f : 0.0f;
1478 break;
1479 }
1480 else if (convert_int_to_bool) {
1481 const GLint *src = (GLint *) (data);
1482 GLfloat *dst = (GLfloat *) dest;
1483 for (j = 0; j < total; j++)
1484 dst[j] = src[j] ? 1.0f : 0.0f;
1485 break;
1486 }
1487 else if (convert_int_to_float) {
1488 const GLint *src = (GLint *) (data);
1489 GLfloat *dst = (GLfloat *) dest;
1490 for (j = 0; j < total; j++)
1491 dst[j] = (GLfloat) src[j];
1492 break;
1493 }
1494 else {
1495 _mesa_memcpy(dest, data, total * sizeof(GLfloat));
1496 break;
1497 }
1498 break;
1499 }
1500 }
1501 return GL_TRUE;
1502 }
1503
1504 /**
1505 * Read a uniform variable from program's memory.
1506 * \return GL_TRUE for success, GL_FALSE if error
1507 */
1508 static GLboolean
1509 _program_ReadUniform(struct gl2_program_intf **intf, GLint loc,
1510 GLsizei count, GLvoid *data, GLenum type)
1511 {
1512 struct gl2_program_impl *impl = (struct gl2_program_impl *) (intf);
1513 const slang_uniform_bindings *uniforms = &impl->_obj.prog.uniforms;
1514 const slang_uniform_binding *uniform;
1515 GLuint i;
1516 GLboolean convert_bool_to_float = GL_FALSE;
1517 GLboolean convert_bool_to_int = GL_FALSE;
1518 GLboolean convert_float_to_int = GL_FALSE;
1519 GLboolean types_match = GL_FALSE;
1520
1521 if (loc < 0 || loc >= uniforms->count)
1522 return GL_FALSE;
1523
1524 uniform = &uniforms->table[loc];
1525
1526 if (slang_export_data_quant_struct(uniform->quant))
1527 return GL_FALSE;
1528
1529 switch (slang_export_data_quant_type(uniform->quant)) {
1530 case GL_BOOL_ARB:
1531 types_match = (type == GL_FLOAT) || (type == GL_INT);
1532 if (type == GL_FLOAT)
1533 convert_bool_to_float = GL_TRUE;
1534 else
1535 convert_bool_to_int = GL_TRUE;
1536 break;
1537 case GL_BOOL_VEC2_ARB:
1538 types_match = (type == GL_FLOAT_VEC2_ARB) || (type == GL_INT_VEC2_ARB);
1539 if (type == GL_FLOAT_VEC2_ARB)
1540 convert_bool_to_float = GL_TRUE;
1541 else
1542 convert_bool_to_int = GL_TRUE;
1543 break;
1544 case GL_BOOL_VEC3_ARB:
1545 types_match = (type == GL_FLOAT_VEC3_ARB) || (type == GL_INT_VEC3_ARB);
1546 if (type == GL_FLOAT_VEC3_ARB)
1547 convert_bool_to_float = GL_TRUE;
1548 else
1549 convert_bool_to_int = GL_TRUE;
1550 break;
1551 case GL_BOOL_VEC4_ARB:
1552 types_match = (type == GL_FLOAT_VEC4_ARB) || (type == GL_INT_VEC4_ARB);
1553 if (type == GL_FLOAT_VEC4_ARB)
1554 convert_bool_to_float = GL_TRUE;
1555 else
1556 convert_bool_to_int = GL_TRUE;
1557 break;
1558 case GL_SAMPLER_1D_ARB:
1559 case GL_SAMPLER_2D_ARB:
1560 case GL_SAMPLER_3D_ARB:
1561 case GL_SAMPLER_CUBE_ARB:
1562 case GL_SAMPLER_1D_SHADOW_ARB:
1563 case GL_SAMPLER_2D_SHADOW_ARB:
1564 types_match = (type == GL_INT);
1565 break;
1566 default:
1567 /* uniform is a float type */
1568 types_match = (type == GL_FLOAT);
1569 break;
1570 }
1571
1572 if (!types_match)
1573 return GL_FALSE;
1574
1575 switch (type) {
1576 case GL_INT:
1577 case GL_INT_VEC2_ARB:
1578 case GL_INT_VEC3_ARB:
1579 case GL_INT_VEC4_ARB:
1580 convert_float_to_int = GL_TRUE;
1581 break;
1582 }
1583
1584 for (i = 0; i < SLANG_SHADER_MAX; i++) {
1585 if (uniform->address[i] != ~0) {
1586 /* XXX if bools are really implemented as floats, some of this
1587 * could probably be culled out.
1588 */
1589 const void *source
1590 = &impl->_obj.prog.machines[i]->mem[uniform->address[i] / 4];
1591 /* total number of values to copy */
1592 const GLuint total
1593 = count * slang_export_data_quant_components(uniform->quant);
1594 GLuint j;
1595 if (convert_bool_to_float) {
1596 GLfloat *dst = (GLfloat *) (data);
1597 const GLfloat *src = (GLfloat *) source;
1598 for (j = 0; j < total; j++)
1599 dst[j] = src[j] == 0.0 ? 0.0 : 1.0;
1600 }
1601 else if (convert_bool_to_int) {
1602 GLint *dst = (GLint *) (data);
1603 const GLfloat *src = (GLfloat *) source;
1604 for (j = 0; j < total; j++)
1605 dst[j] = src[j] == 0.0 ? 0 : 1;
1606 }
1607 else if (convert_float_to_int) {
1608 GLint *dst = (GLint *) (data);
1609 const GLfloat *src = (GLfloat *) source;
1610 for (j = 0; j < total; j++)
1611 dst[j] = (GLint) src[j];
1612 }
1613 else {
1614 /* no type conversion needed */
1615 _mesa_memcpy(data, source, total * sizeof(GLfloat));
1616 }
1617 break;
1618 } /* if */
1619 } /* for */
1620
1621 return GL_TRUE;
1622 }
1623
1624
1625 static GLvoid
1626 _program_GetActiveAttrib(struct gl2_program_intf **intf, GLuint index,
1627 GLsizei maxLength, GLsizei * length, GLint * size,
1628 GLenum * type, GLchar * name)
1629 {
1630 struct gl2_program_impl *impl = (struct gl2_program_impl *) (intf);
1631 slang_active_variable *a = &impl->_obj.prog.active_attribs.table[index];
1632
1633 get_active_variable(a, maxLength, length, size, type, name);
1634 }
1635
1636 static GLuint
1637 _program_GetActiveAttribMaxLength(struct gl2_program_intf **intf)
1638 {
1639 struct gl2_program_impl *impl = (struct gl2_program_impl *) (intf);
1640
1641 return get_active_variable_max_length(&impl->_obj.prog.active_attribs);
1642 }
1643
1644 static GLuint
1645 _program_GetActiveAttribCount(struct gl2_program_intf **intf)
1646 {
1647 struct gl2_program_impl *impl = (struct gl2_program_impl *) (intf);
1648
1649 return impl->_obj.prog.active_attribs.count;
1650 }
1651
1652 static GLint
1653 _program_GetAttribLocation(struct gl2_program_intf **intf,
1654 const GLchar * name)
1655 {
1656 struct gl2_program_impl *impl = (struct gl2_program_impl *) (intf);
1657 slang_attrib_bindings *attribs = &impl->_obj.prog.attribs;
1658 GLuint i;
1659
1660 for (i = 0; i < attribs->binding_count; i++)
1661 if (_mesa_strcmp(attribs->bindings[i].name, name) == 0)
1662 return attribs->bindings[i].first_slot_index;
1663 return -1;
1664 }
1665
1666 static GLvoid
1667 _program_OverrideAttribBinding(struct gl2_program_intf **intf, GLuint index,
1668 const GLchar * name)
1669 {
1670 GET_CURRENT_CONTEXT(ctx);
1671 struct gl2_program_impl *impl = (struct gl2_program_impl *) (intf);
1672 slang_program *pro = &impl->_obj.prog;
1673
1674 if (!_slang_attrib_overrides_add(&pro->attrib_overrides, index, name))
1675 _mesa_error(ctx, GL_OUT_OF_MEMORY, "_program_OverrideAttribBinding");
1676 }
1677
1678 static GLvoid
1679 _program_WriteAttrib(struct gl2_program_intf **intf, GLuint index,
1680 const GLfloat * value)
1681 {
1682 struct gl2_program_impl *impl = (struct gl2_program_impl *) (intf);
1683 slang_program *pro = &impl->_obj.prog;
1684 slang_attrib_slot *slot = &pro->attribs.slots[index];
1685
1686 /*
1687 * Generic attributes can be allocated in a shader with scalar, vec
1688 * or mat type. For scalar and vec types (specifically float, vec2
1689 * and vec3) this is simple - just ignore the extra components. For
1690 * mat type this is more complicated - the vertex_shader spec
1691 * requires to store every column of a matrix in a separate attrib
1692 * slot. To prvent from overwriting data from neighbouring matrix
1693 * columns, the "fill" information is kept to know how many
1694 * components to copy.
1695 */
1696
1697 if (slot->addr != ~0)
1698 _mesa_memcpy(&pro->machines[SLANG_SHADER_VERTEX]->mem[slot->addr / 4].
1699 _float, value, slot->fill * sizeof(GLfloat));
1700 }
1701
1702 static GLvoid
1703 _program_UpdateVarying(struct gl2_program_intf **intf, GLuint index,
1704 GLfloat * value, GLboolean vert)
1705 {
1706 struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;
1707 slang_program *pro = &impl->_obj.prog;
1708 GLuint addr;
1709
1710 if (index >= pro->varyings.slot_count)
1711 return;
1712 if (vert)
1713 addr = pro->varyings.slots[index].vert_addr / 4;
1714 else
1715 addr = pro->varyings.slots[index].frag_addr / 4;
1716 if (addr != ~0) {
1717 if (vert)
1718 *value = pro->machines[SLANG_SHADER_VERTEX]->mem[addr]._float;
1719 else
1720 pro->machines[SLANG_SHADER_FRAGMENT]->mem[addr]._float = *value;
1721 }
1722 }
1723
1724 static struct gl2_program_intf _program_vftbl = {
1725 {
1726 {
1727 {
1728 _unknown_AddRef,
1729 _unknown_Release,
1730 _program_QueryInterface
1731 },
1732 _generic_Delete,
1733 _program_GetType,
1734 _generic_GetName,
1735 _generic_GetDeleteStatus,
1736 _generic_GetInfoLog,
1737 _generic_GetInfoLogLength
1738 },
1739 _program_Attach,
1740 _container_Detach,
1741 _container_GetAttachedCount,
1742 _container_GetAttached
1743 },
1744 _program_GetLinkStatus,
1745 _program_GetValidateStatus,
1746 _program_Link,
1747 _program_Validate,
1748 _program_UpdateFixedUniforms,
1749 _program_UpdateFixedAttrib,
1750 _program_UpdateFixedVarying,
1751 _program_GetTextureImageUsage,
1752 _program_IsShaderPresent,
1753 _program_GetActiveUniform,
1754 _program_GetActiveUniformMaxLength,
1755 _program_GetActiveUniformCount,
1756 _program_GetUniformLocation,
1757 _program_WriteUniform,
1758 _program_ReadUniform,
1759 _program_GetActiveAttrib,
1760 _program_GetActiveAttribMaxLength,
1761 _program_GetActiveAttribCount,
1762 _program_GetAttribLocation,
1763 _program_OverrideAttribBinding,
1764 _program_WriteAttrib,
1765 _program_UpdateVarying
1766 };
1767
1768 static void
1769 _program_constructor(struct gl2_program_impl *impl)
1770 {
1771 _container_constructor((struct gl2_container_impl *) impl);
1772 impl->_vftbl = &_program_vftbl;
1773 impl->_obj._container._generic._unknown._destructor = _program_destructor;
1774 impl->_obj.link_status = GL_FALSE;
1775 impl->_obj.validate_status = GL_FALSE;
1776 #if USE_3DLABS_FRONTEND
1777 impl->_obj.linker = ShConstructLinker(EShExVertexFragment, 0);
1778 impl->_obj.uniforms = ShConstructUniformMap();
1779 #endif
1780 _slang_program_ctr(&impl->_obj.prog);
1781 }
1782
1783 struct gl2_fragment_shader_obj
1784 {
1785 struct gl2_shader_obj _shader;
1786 };
1787
1788 struct gl2_fragment_shader_impl
1789 {
1790 struct gl2_fragment_shader_intf *_vftbl;
1791 struct gl2_fragment_shader_obj _obj;
1792 };
1793
1794 static void
1795 _fragment_shader_destructor(struct gl2_unknown_intf **intf)
1796 {
1797 struct gl2_fragment_shader_impl *impl =
1798 (struct gl2_fragment_shader_impl *) intf;
1799
1800 (void) impl;
1801 /* TODO free fragment shader data */
1802
1803 _shader_destructor(intf);
1804 }
1805
1806 static struct gl2_unknown_intf **
1807 _fragment_shader_QueryInterface(struct gl2_unknown_intf **intf,
1808 enum gl2_uiid uiid)
1809 {
1810 if (uiid == UIID_FRAGMENT_SHADER) {
1811 (**intf).AddRef(intf);
1812 return intf;
1813 }
1814 return _shader_QueryInterface(intf, uiid);
1815 }
1816
1817 static GLenum
1818 _fragment_shader_GetSubType(struct gl2_shader_intf **intf)
1819 {
1820 return GL_FRAGMENT_SHADER_ARB;
1821 }
1822
1823 static struct gl2_fragment_shader_intf _fragment_shader_vftbl = {
1824 {
1825 {
1826 {
1827 _unknown_AddRef,
1828 _unknown_Release,
1829 _fragment_shader_QueryInterface
1830 },
1831 _generic_Delete,
1832 _shader_GetType,
1833 _generic_GetName,
1834 _generic_GetDeleteStatus,
1835 _shader_GetInfoLog,
1836 _shader_GetInfoLogLength
1837 },
1838 _fragment_shader_GetSubType,
1839 _shader_GetCompileStatus,
1840 _shader_SetSource,
1841 _shader_GetSource,
1842 _shader_Compile
1843 }
1844 };
1845
1846 static void
1847 _fragment_shader_constructor(struct gl2_fragment_shader_impl *impl)
1848 {
1849 _shader_constructor((struct gl2_shader_impl *) impl);
1850 impl->_vftbl = &_fragment_shader_vftbl;
1851 impl->_obj._shader._generic._unknown._destructor =
1852 _fragment_shader_destructor;
1853 #if USE_3DLABS_FRONTEND
1854 impl->_obj._shader._3dlabs_shhandle._obj.handle =
1855 ShConstructCompiler(EShLangFragment, 0);
1856 #endif
1857 }
1858
1859 struct gl2_vertex_shader_obj
1860 {
1861 struct gl2_shader_obj _shader;
1862 };
1863
1864 struct gl2_vertex_shader_impl
1865 {
1866 struct gl2_vertex_shader_intf *_vftbl;
1867 struct gl2_vertex_shader_obj _obj;
1868 };
1869
1870 static void
1871 _vertex_shader_destructor(struct gl2_unknown_intf **intf)
1872 {
1873 struct gl2_vertex_shader_impl *impl =
1874 (struct gl2_vertex_shader_impl *) intf;
1875
1876 (void) impl;
1877 /* TODO free vertex shader data */
1878
1879 _shader_destructor(intf);
1880 }
1881
1882 static struct gl2_unknown_intf **
1883 _vertex_shader_QueryInterface(struct gl2_unknown_intf **intf,
1884 enum gl2_uiid uiid)
1885 {
1886 if (uiid == UIID_VERTEX_SHADER) {
1887 (**intf).AddRef(intf);
1888 return intf;
1889 }
1890 return _shader_QueryInterface(intf, uiid);
1891 }
1892
1893 static GLenum
1894 _vertex_shader_GetSubType(struct gl2_shader_intf **intf)
1895 {
1896 return GL_VERTEX_SHADER_ARB;
1897 }
1898
1899 static struct gl2_vertex_shader_intf _vertex_shader_vftbl = {
1900 {
1901 {
1902 {
1903 _unknown_AddRef,
1904 _unknown_Release,
1905 _vertex_shader_QueryInterface
1906 },
1907 _generic_Delete,
1908 _shader_GetType,
1909 _generic_GetName,
1910 _generic_GetDeleteStatus,
1911 _shader_GetInfoLog,
1912 _shader_GetInfoLogLength
1913 },
1914 _vertex_shader_GetSubType,
1915 _shader_GetCompileStatus,
1916 _shader_SetSource,
1917 _shader_GetSource,
1918 _shader_Compile
1919 }
1920 };
1921
1922 static void
1923 _vertex_shader_constructor(struct gl2_vertex_shader_impl *impl)
1924 {
1925 _shader_constructor((struct gl2_shader_impl *) impl);
1926 impl->_vftbl = &_vertex_shader_vftbl;
1927 impl->_obj._shader._generic._unknown._destructor =
1928 _vertex_shader_destructor;
1929 #if USE_3DLABS_FRONTEND
1930 impl->_obj._shader._3dlabs_shhandle._obj.handle =
1931 ShConstructCompiler(EShLangVertex, 0);
1932 #endif
1933 }
1934
1935 struct gl2_debug_obj
1936 {
1937 struct gl2_generic_obj _generic;
1938 };
1939
1940 struct gl2_debug_impl
1941 {
1942 struct gl2_debug_intf *_vftbl;
1943 struct gl2_debug_obj _obj;
1944 };
1945
1946 static GLvoid
1947 _debug_destructor(struct gl2_unknown_intf **intf)
1948 {
1949 struct gl2_debug_impl *impl = (struct gl2_debug_impl *) (intf);
1950
1951 (void) (impl);
1952 /* TODO */
1953
1954 _generic_destructor(intf);
1955 }
1956
1957 static struct gl2_unknown_intf **
1958 _debug_QueryInterface(struct gl2_unknown_intf **intf, enum gl2_uiid uiid)
1959 {
1960 if (uiid == UIID_DEBUG) {
1961 (**intf).AddRef(intf);
1962 return intf;
1963 }
1964 return _generic_QueryInterface(intf, uiid);
1965 }
1966
1967 static GLenum
1968 _debug_GetType(struct gl2_generic_intf **intf)
1969 {
1970 return /*GL_DEBUG_OBJECT_MESA */ 0;
1971 }
1972
1973 static GLvoid
1974 _debug_ClearDebugLog(struct gl2_debug_intf **intf, GLenum logType,
1975 GLenum shaderType)
1976 {
1977 struct gl2_debug_impl *impl = (struct gl2_debug_impl *) (intf);
1978
1979 (void) (impl);
1980 /* TODO */
1981 }
1982
1983 static GLvoid
1984 _debug_GetDebugLog(struct gl2_debug_intf **intf, GLenum logType,
1985 GLenum shaderType, GLsizei maxLength, GLsizei * length,
1986 GLcharARB * infoLog)
1987 {
1988 struct gl2_debug_impl *impl = (struct gl2_debug_impl *) (intf);
1989
1990 (void) (impl);
1991 /* TODO */
1992 }
1993
1994 static GLsizei
1995 _debug_GetDebugLogLength(struct gl2_debug_intf **intf, GLenum logType,
1996 GLenum shaderType)
1997 {
1998 struct gl2_debug_impl *impl = (struct gl2_debug_impl *) (intf);
1999
2000 (void) (impl);
2001 /* TODO */
2002
2003 return 0;
2004 }
2005
2006 static struct gl2_debug_intf _debug_vftbl = {
2007 {
2008 {
2009 _unknown_AddRef,
2010 _unknown_Release,
2011 _debug_QueryInterface
2012 },
2013 _generic_Delete,
2014 _debug_GetType,
2015 _generic_GetName,
2016 _generic_GetDeleteStatus,
2017 _generic_GetInfoLog,
2018 _generic_GetInfoLogLength
2019 },
2020 _debug_ClearDebugLog,
2021 _debug_GetDebugLog,
2022 _debug_GetDebugLogLength
2023 };
2024
2025 static GLvoid
2026 _debug_constructor(struct gl2_debug_impl *impl)
2027 {
2028 _generic_constructor((struct gl2_generic_impl *) (impl));
2029 impl->_vftbl = &_debug_vftbl;
2030 impl->_obj._generic._unknown._destructor = _debug_destructor;
2031 }
2032
2033 GLhandleARB
2034 _mesa_3dlabs_create_shader_object(GLenum shaderType)
2035 {
2036 switch (shaderType) {
2037 case GL_FRAGMENT_SHADER_ARB:
2038 {
2039 struct gl2_fragment_shader_impl *x =
2040 (struct gl2_fragment_shader_impl *)
2041 _mesa_malloc(sizeof(struct gl2_fragment_shader_impl));
2042
2043 if (x != NULL) {
2044 _fragment_shader_constructor(x);
2045 return x->_obj._shader._generic.name;
2046 }
2047 }
2048 break;
2049 case GL_VERTEX_SHADER_ARB:
2050 {
2051 struct gl2_vertex_shader_impl *x = (struct gl2_vertex_shader_impl *)
2052 _mesa_malloc(sizeof(struct gl2_vertex_shader_impl));
2053
2054 if (x != NULL) {
2055 _vertex_shader_constructor(x);
2056 return x->_obj._shader._generic.name;
2057 }
2058 }
2059 break;
2060 }
2061
2062 return 0;
2063 }
2064
2065 GLhandleARB
2066 _mesa_3dlabs_create_program_object(void)
2067 {
2068 struct gl2_program_impl *x = (struct gl2_program_impl *)
2069 _mesa_malloc(sizeof(struct gl2_program_impl));
2070
2071 if (x != NULL) {
2072 _program_constructor(x);
2073 return x->_obj._container._generic.name;
2074 }
2075
2076 return 0;
2077 }
2078
2079 GLhandleARB
2080 _mesa_3dlabs_create_debug_object(GLvoid)
2081 {
2082 struct gl2_debug_impl *obj;
2083
2084 obj =
2085 (struct gl2_debug_impl *) (_mesa_malloc(sizeof(struct gl2_debug_impl)));
2086 if (obj != NULL) {
2087 _debug_constructor(obj);
2088 return obj->_obj._generic.name;
2089 }
2090 return 0;
2091 }
2092
2093 #include "slang_assemble.h"
2094 #include "slang_execute.h"
2095
2096 int
2097 _slang_fetch_discard(struct gl2_program_intf **pro, GLboolean * val)
2098 {
2099 struct gl2_program_impl *impl;
2100
2101 impl = (struct gl2_program_impl *) pro;
2102 *val =
2103 impl->_obj.prog.machines[SLANG_SHADER_FRAGMENT]->
2104 kill ? GL_TRUE : GL_FALSE;
2105 return 1;
2106 }
2107
2108 static GLvoid
2109 exec_shader(struct gl2_program_intf **pro, GLuint i)
2110 {
2111 struct gl2_program_impl *impl;
2112 slang_program *p;
2113
2114 impl = (struct gl2_program_impl *) pro;
2115 p = &impl->_obj.prog;
2116
2117 slang_machine_init(p->machines[i]);
2118 p->machines[i]->ip = p->code[i][SLANG_COMMON_CODE_MAIN];
2119
2120 _slang_execute2(p->assemblies[i], p->machines[i]);
2121 }
2122
2123 GLvoid
2124 _slang_exec_fragment_shader(struct gl2_program_intf **pro)
2125 {
2126 exec_shader(pro, SLANG_SHADER_FRAGMENT);
2127 }
2128
2129 GLvoid
2130 _slang_exec_vertex_shader(struct gl2_program_intf **pro)
2131 {
2132 exec_shader(pro, SLANG_SHADER_VERTEX);
2133 }
2134
2135 #endif
2136
2137 void
2138 _mesa_init_shaderobjects_3dlabs(GLcontext * ctx)
2139 {
2140 #if USE_3DLABS_FRONTEND
2141 _glslang_3dlabs_InitProcess();
2142 _glslang_3dlabs_ShInitialize();
2143 #endif
2144 }