effect-parser.c 34.9 KB
Newer Older
J
jp9000 已提交
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
/******************************************************************************
    Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include <assert.h>
#include "effect-parser.h"
#include "effect.h"

void ep_free(struct effect_parser *ep)
{
	size_t i;
	for (i = 0; i < ep->params.num; i++)
		ep_param_free(ep->params.array+i);
	for (i = 0; i < ep->structs.num; i++)
		ep_struct_free(ep->structs.array+i);
	for (i = 0; i < ep->funcs.num; i++)
		ep_func_free(ep->funcs.array+i);
	for (i = 0; i < ep->samplers.num; i++)
		ep_sampler_free(ep->samplers.array+i);
	for (i = 0; i < ep->techniques.num; i++)
		ep_technique_free(ep->techniques.array+i);

	ep->cur_pass = NULL;
	cf_parser_free(&ep->cfp);
	da_free(ep->params);
	da_free(ep->structs);
	da_free(ep->funcs);
	da_free(ep->samplers);
	da_free(ep->techniques);
}

static inline struct ep_func *ep_getfunc(struct effect_parser *ep,
		const char *name)
{
	size_t i;
	for (i = 0; i < ep->funcs.num; i++) {
		if (strcmp(name, ep->funcs.array[i].name) == 0)
			return ep->funcs.array+i;
	}

	return NULL;
}

static inline struct ep_struct *ep_getstruct(struct effect_parser *ep,
		const char *name)
{
	size_t i;
	for (i = 0; i < ep->structs.num; i++) {
		if (strcmp(name, ep->structs.array[i].name) == 0)
			return ep->structs.array+i;
	}

	return NULL;
}

static inline struct ep_sampler *ep_getsampler(struct effect_parser *ep,
		const char *name)
{
	size_t i;
	for (i = 0; i < ep->samplers.num; i++) {
		if (strcmp(name, ep->samplers.array[i].name) == 0)
			return ep->samplers.array+i;
	}

	return NULL;
}

static inline struct ep_param *ep_getparam(struct effect_parser *ep,
		const char *name)
{
	size_t i;
	for (i = 0; i < ep->params.num; i++) {
		if (strcmp(name, ep->params.array[i].name) == 0)
			return ep->params.array+i;
	}

	return NULL;
}

static inline struct ep_func *ep_getfunc_strref(struct effect_parser *ep,
		const struct strref *ref)
{
	size_t i;
	for (i = 0; i < ep->funcs.num; i++) {
		if (strref_cmp(ref, ep->funcs.array[i].name) == 0)
			return ep->funcs.array+i;
	}

	return NULL;
}

static inline struct ep_struct *ep_getstruct_strref(struct effect_parser *ep,
		const struct strref *ref)
{
	size_t i;
	for (i = 0; i < ep->structs.num; i++) {
		if (strref_cmp(ref, ep->structs.array[i].name) == 0)
			return ep->structs.array+i;
	}

	return NULL;
}

static inline struct ep_sampler *ep_getsampler_strref(struct effect_parser *ep,
		const struct strref *ref)
{
	size_t i;
	for (i = 0; i < ep->samplers.num; i++) {
		if (strref_cmp(ref, ep->samplers.array[i].name) == 0)
			return ep->samplers.array+i;
	}

	return NULL;
}

static inline struct ep_param *ep_getparam_strref(struct effect_parser *ep,
		const struct strref *ref)
{
	size_t i;
	for (i = 0; i < ep->params.num; i++) {
		if (strref_cmp(ref, ep->params.array[i].name) == 0)
			return ep->params.array+i;
	}

	return NULL;
}

static inline int ep_parse_struct_var(struct effect_parser *ep,
		struct ep_var *var)
{
	int errcode;

	/* -------------------------------------- */
	/* variable type */

	if (!next_valid_token(&ep->cfp)) return PARSE_EOF;

	if (token_is(&ep->cfp, ";")) return PARSE_CONTINUE;
	if (token_is(&ep->cfp, "}")) return PARSE_BREAK;

	errcode = token_is_type(&ep->cfp, CFTOKEN_NAME, "type name", ";");
	if (errcode != PARSE_SUCCESS)
		return errcode;

	copy_token(&ep->cfp, &var->type);

	/* -------------------------------------- */
	/* variable name */

	if (!next_valid_token(&ep->cfp)) return PARSE_EOF;

	if (token_is(&ep->cfp, ";")) return PARSE_UNEXPECTED_CONTINUE;
	if (token_is(&ep->cfp, "}")) return PARSE_UNEXPECTED_BREAK;

	errcode = token_is_type(&ep->cfp, CFTOKEN_NAME, "variable name", ";");
	if (errcode != PARSE_SUCCESS)
		return errcode;

	copy_token(&ep->cfp, &var->name);

	/* -------------------------------------- */
	/* variable mapping if any (POSITION, TEXCOORD, etc) */

	if (!next_valid_token(&ep->cfp)) return PARSE_EOF;

	if (token_is(&ep->cfp, ":")) {
		if (!next_valid_token(&ep->cfp)) return PARSE_EOF;

		if (token_is(&ep->cfp, ";")) return PARSE_UNEXPECTED_CONTINUE;
		if (token_is(&ep->cfp, "}")) return PARSE_UNEXPECTED_BREAK;

		errcode = token_is_type(&ep->cfp, CFTOKEN_NAME,
				"mapping name", ";");
		if (errcode != PARSE_SUCCESS)
			return errcode;

		copy_token(&ep->cfp, &var->mapping);

		if (!next_valid_token(&ep->cfp)) return PARSE_EOF;
	}

	/* -------------------------------------- */

	if (!token_is(&ep->cfp, ";")) {
		if (!go_to_valid_token(&ep->cfp, ";", "}"))
			return PARSE_EOF;
		return PARSE_CONTINUE;
	}

	return PARSE_SUCCESS;
}

static void ep_parse_struct(struct effect_parser *ep)
{
	struct ep_struct eps;
	ep_struct_init(&eps);

	if (next_name(&ep->cfp, &eps.name, "name", ";") != PARSE_SUCCESS)
		goto error;
	if (next_token_should_be(&ep->cfp, "{", ";", NULL) != PARSE_SUCCESS)
		goto error;

	/* get structure variables */
	while (true) {
		bool do_break = false;
		struct ep_var var;

		ep_var_init(&var);

		switch (ep_parse_struct_var(ep, &var)) {

		case PARSE_UNEXPECTED_CONTINUE:
			cf_adderror_syntax_error(&ep->cfp);
		case PARSE_CONTINUE:
			ep_var_free(&var);
			continue;

		case PARSE_UNEXPECTED_BREAK:
			cf_adderror_syntax_error(&ep->cfp);
		case PARSE_BREAK:
			ep_var_free(&var);
			do_break = true;
			break;

		case PARSE_EOF:
			ep_var_free(&var);
			goto error;
		}

		if (do_break)
			break;

		da_push_back(eps.vars, &var);
	}

	if (next_token_should_be(&ep->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
		goto error;

	da_push_back(ep->structs, &eps);
	return;

error:
	ep_struct_free(&eps);
}

static inline int ep_parse_pass_command_call(struct effect_parser *ep,
		struct darray *call)
{
	struct cf_token end_token;
	cf_token_clear(&end_token);

	while (!token_is(&ep->cfp, ";")) {
		if (token_is(&ep->cfp, "}")) {
			cf_adderror_expecting(&ep->cfp, ";");
			return PARSE_CONTINUE;
		}

		darray_push_back(sizeof(struct cf_token), call,
				ep->cfp.cur_token);
		if (!next_valid_token(&ep->cfp)) return PARSE_EOF;
	}

	darray_push_back(sizeof(struct cf_token), call, ep->cfp.cur_token);
	darray_push_back(sizeof(struct cf_token), call, &end_token);
	return PARSE_SUCCESS;
}

static int ep_parse_pass_command(struct effect_parser *ep, struct ep_pass *pass)
{
	struct darray *call; /* struct cf_token */

	if (!next_valid_token(&ep->cfp)) return PARSE_EOF;

	if (token_is(&ep->cfp, "vertex_shader") ||
	    token_is(&ep->cfp, "vertex_program")) {
		call = &pass->vertex_program.da;

	} else if (token_is(&ep->cfp, "pixel_shader") ||
	           token_is(&ep->cfp, "pixel_program")) {
		call = &pass->fragment_program.da;

	} else {
		cf_adderror_syntax_error(&ep->cfp);
		if (!go_to_valid_token(&ep->cfp, ";", "}")) return PARSE_EOF;
		return PARSE_CONTINUE;
	}

	if (next_token_should_be(&ep->cfp, "=", ";", "}") != PARSE_SUCCESS)
		return PARSE_CONTINUE;

	if (!next_valid_token(&ep->cfp)) return PARSE_EOF;
	if (token_is(&ep->cfp, "compile")) {
		cf_adderror(&ep->cfp, "compile keyword not necessary",
				LEVEL_WARNING, NULL, NULL, NULL);
		if (!next_valid_token(&ep->cfp)) return PARSE_EOF;
	}

	return ep_parse_pass_command_call(ep, call);
}

static int ep_parse_pass(struct effect_parser *ep, struct ep_pass *pass)
{
	struct cf_token peek;

	if (!token_is(&ep->cfp, "pass"))
		return PARSE_UNEXPECTED_CONTINUE;

	if (!next_valid_token(&ep->cfp)) return PARSE_EOF;

	if (!token_is(&ep->cfp, "{")) {
		pass->name = bstrdup_n(ep->cfp.cur_token->str.array,
		                        ep->cfp.cur_token->str.len);
		if (!next_valid_token(&ep->cfp)) return PARSE_EOF;
	}

	if (!peek_valid_token(&ep->cfp, &peek)) return PARSE_EOF;

	while (strref_cmp(&peek.str, "}") != 0) {
		int ret = ep_parse_pass_command(ep, pass);
		if (ret < 0 && ret != PARSE_CONTINUE)
			return ret;

		if (!peek_valid_token(&ep->cfp, &peek))
			return PARSE_EOF;
	}

	/* token is '}' */
	next_token(&ep->cfp);

	return PARSE_SUCCESS;
}

static void ep_parse_technique(struct effect_parser *ep)
{
	struct ep_technique ept;
	ep_technique_init(&ept);

	if (next_name(&ep->cfp, &ept.name, "name", ";") != PARSE_SUCCESS)
		goto error;
	if (next_token_should_be(&ep->cfp, "{", ";", NULL) != PARSE_SUCCESS)
		goto error;

	if (!next_valid_token(&ep->cfp))
		goto error;

	while (!token_is(&ep->cfp, "}")) {
		struct ep_pass pass;
		ep_pass_init(&pass);

		switch (ep_parse_pass(ep, &pass)) {
		case PARSE_UNEXPECTED_CONTINUE:
			ep_pass_free(&pass);
			if (!go_to_token(&ep->cfp, "}", NULL))
				goto error;
			continue;
		case PARSE_EOF:
			ep_pass_free(&pass);
			goto error;
		}

		da_push_back(ept.passes, &pass);

		if (!next_valid_token(&ep->cfp))
			goto error;
	}

	/* pass the current token (which is '}') if we reached here */
	next_token(&ep->cfp);

	da_push_back(ep->techniques, &ept);
	return;

error:
	next_token(&ep->cfp);
	ep_technique_free(&ept);
}

static int ep_parse_sampler_state_item(struct effect_parser *ep,
		struct ep_sampler *eps)
{
	int ret;
	char *state = NULL, *value = NULL;

	ret = next_name(&ep->cfp, &state, "state name", ";");
	if (ret != PARSE_SUCCESS) goto fail;

	ret = next_token_should_be(&ep->cfp, "=", ";", NULL);
	if (ret != PARSE_SUCCESS) goto fail;

	ret = next_name(&ep->cfp, &value, "value name", ";");
	if (ret != PARSE_SUCCESS) goto fail;

	ret = next_token_should_be(&ep->cfp, ";", ";", NULL);
	if (ret != PARSE_SUCCESS) goto fail;

	da_push_back(eps->states, &state);
	da_push_back(eps->values, &value);
	return ret;

fail:
	bfree(state);
	bfree(value);
	return ret;
}

static void ep_parse_sampler_state(struct effect_parser *ep)
{
	struct ep_sampler eps;
	struct cf_token peek;
	ep_sampler_init(&eps);

	if (next_name(&ep->cfp, &eps.name, "name", ";") != PARSE_SUCCESS)
		goto error;
	if (next_token_should_be(&ep->cfp, "{", ";", NULL) != PARSE_SUCCESS)
		goto error;

	if (!peek_valid_token(&ep->cfp, &peek))
		goto error;

	while (strref_cmp(&peek.str, "}") != 0) {
		int ret = ep_parse_sampler_state_item(ep, &eps);
		if (ret == PARSE_EOF)
			goto error;

		if (!peek_valid_token(&ep->cfp, &peek))
			goto error;
	}

	if (next_token_should_be(&ep->cfp, "}", ";", NULL) != PARSE_SUCCESS)
		goto error;
	if (next_token_should_be(&ep->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
		goto error;

	da_push_back(ep->samplers, &eps);
	return;

error:
	ep_sampler_free(&eps);
}

static inline int ep_check_for_keyword(struct effect_parser *ep,
		const char *keyword, bool *val)
{
	bool new_val = token_is(&ep->cfp, keyword);
	if (new_val) {
		if (!next_valid_token(&ep->cfp))
			return PARSE_EOF;

		if (new_val && *val)
			cf_adderror(&ep->cfp, "'$1' keyword already specified",
					LEVEL_WARNING, keyword, NULL, NULL);
		*val = new_val;

		return PARSE_CONTINUE;
	}

	return PARSE_SUCCESS;
}

static inline int ep_parse_func_param(struct effect_parser *ep,
		struct ep_func *func, struct ep_var *var)
{
	int errcode;

	if (!next_valid_token(&ep->cfp))
		return PARSE_EOF;

	errcode = ep_check_for_keyword(ep, "uniform", &var->uniform);
	if (errcode == PARSE_EOF)
		return PARSE_EOF;

	errcode = get_name(&ep->cfp, &var->type, "type", ")");
	if (errcode != PARSE_SUCCESS)
		return errcode;

	errcode = next_name(&ep->cfp, &var->name, "name", ")");
	if (errcode != PARSE_SUCCESS)
		return errcode;

	if (!next_valid_token(&ep->cfp))
		return PARSE_EOF;

	if (token_is(&ep->cfp, ":")) {
		errcode = next_name(&ep->cfp, &var->mapping,
				"mapping specifier", ")");
		if (errcode != PARSE_SUCCESS)
			return errcode;

		if (!next_valid_token(&ep->cfp))
			return PARSE_EOF;
	}

	if (ep_getstruct(ep, var->type) != NULL)
		da_push_back(func->struct_deps, &var->type);
	else if (ep_getsampler(ep, var->type) != NULL)
		da_push_back(func->sampler_deps, &var->type);

	return PARSE_SUCCESS;
}

static bool ep_parse_func_params(struct effect_parser *ep, struct ep_func *func)
{
	struct cf_token peek;
	int errcode;

	cf_token_clear(&peek);

	if (!peek_valid_token(&ep->cfp, &peek))
		return false;

	if (*peek.str.array == ')') {
		next_token(&ep->cfp);
		goto exit;
	}

	do {
		struct ep_var var;
		ep_var_init(&var);

		if (!token_is(&ep->cfp, "(") && !token_is(&ep->cfp, ","))
			cf_adderror_syntax_error(&ep->cfp);

		errcode = ep_parse_func_param(ep, func, &var);
		if (errcode != PARSE_SUCCESS) {
			ep_var_free(&var);

			if (errcode == PARSE_CONTINUE)
				goto exit;
			else if (errcode == PARSE_EOF)
				return false;
		}

		da_push_back(func->param_vars, &var);
	} while (!token_is(&ep->cfp, ")"));

exit:
	return true;
}

static inline bool ep_process_struct_dep(struct effect_parser *ep,
		struct ep_func *func)
{
	struct ep_struct *val = ep_getstruct_strref(ep,
			&ep->cfp.cur_token->str);
	if (val)
		da_push_back(func->struct_deps, &val->name);
	return val != NULL;
}

static inline bool ep_process_func_dep(struct effect_parser *ep,
		struct ep_func *func)
{
	struct ep_func *val = ep_getfunc_strref(ep,
			&ep->cfp.cur_token->str);
	if (val)
		da_push_back(func->func_deps, &val->name);
	return val != NULL;
}

static inline bool ep_process_sampler_dep(struct effect_parser *ep,
		struct ep_func *func)
{
	struct ep_sampler *val = ep_getsampler_strref(ep,
			&ep->cfp.cur_token->str);
	if (val)
		da_push_back(func->sampler_deps, &val->name);
	return val != NULL;
}

static inline bool ep_process_param_dep(struct effect_parser *ep,
		struct ep_func *func)
{
	struct ep_param *val = ep_getparam_strref(ep, &ep->cfp.cur_token->str);
	if (val)
		da_push_back(func->param_deps, &val->name);
	return val != NULL;
}

static inline bool ep_parse_func_contents(struct effect_parser *ep,
		struct ep_func *func)
{
	int braces = 1;

	dstr_cat_strref(&func->contents, &ep->cfp.cur_token->str);

	while (braces > 0) {
		if ((ep->cfp.cur_token++)->type == CFTOKEN_NONE)
			return false;

		if (ep->cfp.cur_token->type == CFTOKEN_SPACETAB ||
		    ep->cfp.cur_token->type == CFTOKEN_NEWLINE) {
		} else if (token_is(&ep->cfp, "{")) {
			braces++;
		} else if (token_is(&ep->cfp, "}")) {
			braces--;
		} else if (ep_process_struct_dep(ep, func)  ||
		           ep_process_func_dep(ep, func)    ||
		           ep_process_sampler_dep(ep, func) ||
		           ep_process_param_dep(ep, func)) {
		}

		dstr_cat_strref(&func->contents, &ep->cfp.cur_token->str);
	}

	return true;
}

static void ep_parse_function(struct effect_parser *ep,
		char *type, char *name)
{
	struct ep_func func;
	int errcode;

	ep_func_init(&func, type, name);
	if (ep_getstruct(ep, type))
		da_push_back(func.struct_deps, &func.ret_type);

	if (!ep_parse_func_params(ep, &func))
		goto error;

	if (!next_valid_token(&ep->cfp))
		goto error;

	/* if function is mapped to something, for example COLOR */
	if (token_is(&ep->cfp, ":")) {
		errcode = next_name(&ep->cfp, &func.mapping,
				"mapping specifier", "{");
		if (errcode == PARSE_EOF)
			goto error;
		else if (errcode != PARSE_CONTINUE) {
			if (!next_valid_token(&ep->cfp))
				goto error;
		}
	}

	if (!token_is(&ep->cfp, "{")) {
		cf_adderror_expecting(&ep->cfp, "{");
		goto error;
	}

	if (!ep_parse_func_contents(ep, &func))
		goto error;

	/* it is established that the current token is '}' if we reach this */
	next_token(&ep->cfp);

	da_push_back(ep->funcs, &func);
	return;

error:
	ep_func_free(&func);
}

/* parses "array[count]" */
static bool ep_parse_param_array(struct effect_parser *ep,
		struct ep_param *param)
{
	if (!next_valid_token(&ep->cfp))
		return false;

	if (ep->cfp.cur_token->type != CFTOKEN_NUM ||
	    !valid_int_str(ep->cfp.cur_token->str.array,
		    ep->cfp.cur_token->str.len))
		return false;

	param->array_count = strtol(ep->cfp.cur_token->str.array, NULL, 10);

	if (next_token_should_be(&ep->cfp, "]", ";", NULL) == PARSE_EOF)
		return false;

	if (!next_valid_token(&ep->cfp))
		return false;

	return true;
}

static inline int ep_parse_param_assign_texture(struct effect_parser *ep,
		struct ep_param *param)
{
	int errcode;
	char *str;

	if (!next_valid_token(&ep->cfp))
		return PARSE_EOF;

	errcode = token_is_type(&ep->cfp, CFTOKEN_STRING,
			"texture path string", ";");
	if (errcode != PARSE_SUCCESS)
		return errcode;

	str = cf_literal_to_str(ep->cfp.cur_token->str.array,
	                        ep->cfp.cur_token->str.len);

	if (str) {
		da_copy_array(param->default_val, str, strlen(str) + 1);
		bfree(str);
	}

	return PARSE_SUCCESS;
}

static inline int ep_parse_param_assign_intfloat(struct effect_parser *ep,
		struct ep_param *param, bool is_float)
{
	int errcode;

	if (!next_valid_token(&ep->cfp))
		return PARSE_EOF;

	errcode = token_is_type(&ep->cfp, CFTOKEN_NUM, "numeric value", ";");
	if (errcode != PARSE_SUCCESS)
		return errcode;

	if (is_float) {
		float f = (float)strtod(ep->cfp.cur_token->str.array, NULL);
		da_push_back_array(param->default_val, &f, sizeof(float));
	} else {
		long l = strtol(ep->cfp.cur_token->str.array, NULL, 10);
		da_push_back_array(param->default_val, &l, sizeof(long));
	}

	return PARSE_SUCCESS;
}

/*
 * parses assignment for float1, float2, float3, float4, and any combination
 * for float3x3, float4x4, etc
 */
static inline int ep_parse_param_assign_float_array(struct effect_parser *ep,
		struct ep_param *param)
{
	const char *float_type = param->type+5;
	int float_count = 0, errcode, i;

	/* -------------------------------------------- */

	if (float_type[0] < '1' || float_type[0] > '4')
		cf_adderror(&ep->cfp, "Invalid row count", LEVEL_ERROR,
				NULL, NULL, NULL);

	float_count = float_type[0]-'0';

	if (float_type[1] == 'x') {
		if (float_type[2] < '1' || float_type[2] > '4')
			cf_adderror(&ep->cfp, "Invalid column count",
					LEVEL_ERROR, NULL, NULL, NULL);

		float_count *= float_type[2]-'0';
	}

	/* -------------------------------------------- */

	errcode = next_token_should_be(&ep->cfp, "{", ";", NULL);
	if (errcode != PARSE_SUCCESS) return errcode;

	for (i = 0; i < float_count; i++) {
		char *next = ((i+1) < float_count) ? "," : "}";

		errcode = ep_parse_param_assign_intfloat(ep, param, true);
		if (errcode != PARSE_SUCCESS) return errcode;

		errcode = next_token_should_be(&ep->cfp, next, ";", NULL);
		if (errcode != PARSE_SUCCESS) return errcode;
	}

	return PARSE_SUCCESS;
}

static int ep_parse_param_assignment_val(struct effect_parser *ep,
		struct ep_param *param)
{
	if (param->is_texture)
		return ep_parse_param_assign_texture(ep, param);
	else if (strcmp(param->type, "int") == 0)
		return ep_parse_param_assign_intfloat(ep, param, false);
	else if (strcmp(param->type, "float") == 0)
		return ep_parse_param_assign_intfloat(ep, param, true);
	else if (astrcmp_n(param->type, "float", 5) == 0)
		return ep_parse_param_assign_float_array(ep, param);

	cf_adderror(&ep->cfp, "Invalid type '$1' used for assignment",
			LEVEL_ERROR, param->type, NULL, NULL);

	return PARSE_CONTINUE;
}

static inline bool ep_parse_param_assignment(struct effect_parser *ep,
		struct ep_param *param)
{
	if (ep_parse_param_assignment_val(ep, param) != PARSE_SUCCESS)
		return false;

	if (!next_valid_token(&ep->cfp))
		return false;

	return true;
}

/* static bool ep_parse_param_property(struct effect_parser *ep,
		struct ep_param *param)
{
} */

static void ep_parse_param(struct effect_parser *ep,
		char *type, char *name,
		bool is_property, bool is_const, bool is_uniform)
{
	struct ep_param param;
822
	ep_param_init(&param, type, name, is_property, is_const, is_uniform);
J
jp9000 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969

	if (token_is(&ep->cfp, ";"))
		goto complete;
	if (token_is(&ep->cfp, "[") && !ep_parse_param_array(ep, &param))
		goto error;
	if (token_is(&ep->cfp, "=") && !ep_parse_param_assignment(ep, &param))
		goto error;
	/*if (token_is(&ep->cfp, "<") && !ep_parse_param_property(ep, &param))
		goto error; */
	if (!token_is(&ep->cfp, ";"))
		goto error;

complete:
	da_push_back(ep->params, &param);
	return;

error:
	ep_param_free(&param);
}

static bool ep_get_var_specifiers(struct effect_parser *ep,
		bool *is_property, bool *is_const, bool *is_uniform)
{
	while(true) {
		int errcode;
		errcode = ep_check_for_keyword(ep, "property", is_property);
		if (errcode == PARSE_EOF)
			return false;
		else if (errcode == PARSE_CONTINUE)
			continue;

		errcode = ep_check_for_keyword(ep, "const", is_const);
		if (errcode == PARSE_EOF)
			return false;
		else if (errcode == PARSE_CONTINUE)
			continue;

		errcode = ep_check_for_keyword(ep, "uniform", is_uniform);
		if (errcode == PARSE_EOF)
			return false;
		else if (errcode == PARSE_CONTINUE)
			continue;

		break;
	}

	return true;
}

static inline void report_invalid_func_keyword(struct effect_parser *ep,
		const char *name, bool val)
{
	if (val)
		cf_adderror(&ep->cfp, "'$1' keyword cannot be used with a "
		                      "function", LEVEL_ERROR,
		                      name, NULL, NULL);
}

static void ep_parse_other(struct effect_parser *ep)
{
	bool is_property = false, is_const = false, is_uniform = false;
	char *type = NULL, *name = NULL;

	if (!ep_get_var_specifiers(ep, &is_property, &is_const, &is_uniform))
		goto error;

	if (get_name(&ep->cfp, &type, "type", ";") != PARSE_SUCCESS)
		goto error;
	if (next_name(&ep->cfp, &name, "name", ";") != PARSE_SUCCESS)
		goto error;

	if (!next_valid_token(&ep->cfp))
		goto error;

	if (token_is(&ep->cfp, "(")) {
		report_invalid_func_keyword(ep, "property", is_property);
		report_invalid_func_keyword(ep, "const",    is_const);
		report_invalid_func_keyword(ep, "uniform",  is_uniform);

		ep_parse_function(ep, type, name);
		return;
	} else {
		ep_parse_param(ep, type, name, is_property, is_const,
				is_uniform);
		return;
	}

error:
	bfree(type);
	bfree(name);
}

static bool ep_compile(struct effect_parser *ep);

bool ep_parse(struct effect_parser *ep, effect_t effect,
              const char *effect_string, const char *file)
{
	bool success;

	ep->effect = effect;
	if (!cf_parser_parse(&ep->cfp, effect_string, file))
		return false;

	while (ep->cfp.cur_token && ep->cfp.cur_token->type != CFTOKEN_NONE) {
		if (token_is(&ep->cfp, ";") ||
		    is_whitespace(*ep->cfp.cur_token->str.array)) {
			/* do nothing */
			ep->cfp.cur_token++;

		} else if (token_is(&ep->cfp, "struct")) {
			ep_parse_struct(ep);

		} else if (token_is(&ep->cfp, "technique")) {
			ep_parse_technique(ep);

		} else if (token_is(&ep->cfp, "sampler_state")) {
			ep_parse_sampler_state(ep);

		} else if (token_is(&ep->cfp, "{")) {
			/* add error and pass braces */
			cf_adderror(&ep->cfp, "Unexpected code segment",
					LEVEL_ERROR, NULL, NULL, NULL);
			pass_pair(&ep->cfp, '{', '}');

		} else {
			/* parameters and functions */
			ep_parse_other(ep);
		}
	}

	success = !error_data_has_errors(&ep->cfp.error_list);
	if (success)
		success = ep_compile(ep);

	return success;
}

/* ------------------------------------------------------------------------- */

static inline void ep_write_param(struct dstr *shader, struct ep_param *param,
		struct darray *used_params)
{
	if (param->written)
		return;

	if (param->is_const) {
		dstr_cat(shader, "const ");
970
	} else if (param->is_uniform) {
J
jp9000 已提交
971 972 973
		struct dstr new;
		dstr_init_copy(&new, param->name);
		darray_push_back(sizeof(struct dstr), used_params, &new);
974 975

		dstr_cat(shader, "uniform ");
J
jp9000 已提交
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
	}

	dstr_cat(shader, param->type);
	dstr_cat(shader, " ");
	dstr_cat(shader, param->name);

	if (param->array_count)
		dstr_catf(shader, "[%u]", param->array_count);

	dstr_cat(shader, ";\n");

	param->written = true;
}

static inline void ep_write_func_param_deps(struct effect_parser *ep,
		struct dstr *shader, struct ep_func *func,
		struct darray *used_params)
{
	size_t i;
	for (i = 0; i < func->param_deps.num; i++) {
		const char *name = func->param_deps.array[i];
		struct ep_param *param = ep_getparam(ep, name);
		ep_write_param(shader, param, used_params);
	}

	if (func->param_deps.num)
		dstr_cat(shader, "\n\n");
}

static void ep_write_sampler(struct dstr *shader, struct ep_sampler *sampler)
{
	size_t i;

	if (sampler->written)
		return;

	dstr_cat(shader, "sampler_state ");
	dstr_cat(shader, sampler->name);
	dstr_cat(shader, " {");

	for (i = 0; i <sampler->values.num; i++) {
		dstr_cat(shader, "\n\t");
		dstr_cat(shader, sampler->states.array[i]);
		dstr_cat(shader, " = ");
		dstr_cat(shader, sampler->values.array[i]);
		dstr_cat(shader, ";\n");
	}

	dstr_cat(shader, "\n};\n");
	sampler->written = true;
}

static inline void ep_write_func_sampler_deps(struct effect_parser *ep,
		struct dstr *shader, struct ep_func *func)
{
	size_t i;
	for (i = 0; i < func->sampler_deps.num; i++) {
		const char *name = func->sampler_deps.array[i];

		struct ep_sampler *sampler = ep_getsampler(ep, name);
		ep_write_sampler(shader, sampler);
		dstr_cat(shader, "\n");
	}
}

static inline void ep_write_var(struct dstr *shader, struct ep_var *var)
{
	if (var->uniform)
		dstr_cat(shader, "uniform ");

	dstr_cat(shader, var->type);
	dstr_cat(shader, " ");
	dstr_cat(shader, var->name);

	if (var->mapping) {
		dstr_cat(shader, " : ");
		dstr_cat(shader, var->mapping);
	}
}

static void ep_write_struct(struct dstr *shader, struct ep_struct *st)
{
	size_t i;

	if (st->written)
		return;

	dstr_cat(shader, "struct ");
	dstr_cat(shader, st->name);
	dstr_cat(shader, " {");

	for (i = 0; i < st->vars.num; i++) {
		dstr_cat(shader, "\n\t");
		ep_write_var(shader, st->vars.array+i);
		dstr_cat(shader, ";");
	}

	dstr_cat(shader, "\n};\n");
	st->written = true;
}

static inline void ep_write_func_struct_deps(struct effect_parser *ep,
		struct dstr *shader, struct ep_func *func)
{
	size_t i;
	for (i = 0; i < func->struct_deps.num; i++) {
		const char *name = func->struct_deps.array[i];
		struct ep_struct *st = ep_getstruct(ep, name);

		if (!st->written) {
			ep_write_struct(shader, st);
			dstr_cat(shader, "\n");
			st->written = true;
		}
	}
}

static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
		struct ep_func *func, struct darray *used_params);

static inline void ep_write_func_func_deps(struct effect_parser *ep,
		struct dstr *shader, struct ep_func *func,
		struct darray *used_params)
{
	size_t i;
	for (i = 0; i < func->func_deps.num; i++) {
		const char *name = func->func_deps.array[i];
		struct ep_func *func = ep_getfunc(ep, name);

		if (!func->written) {
			ep_write_func(ep, shader, func, used_params);
			dstr_cat(shader, "\n\n");
		}
	}
}

static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
		struct ep_func *func, struct darray *used_params)
{
	size_t i;

	func->written = true;
	
	ep_write_func_param_deps(ep, shader, func, used_params);
	ep_write_func_sampler_deps(ep, shader, func);
	ep_write_func_struct_deps(ep, shader, func);
	ep_write_func_func_deps(ep, shader, func, used_params);

	/* ------------------------------------ */

	dstr_cat(shader, func->ret_type);
	dstr_cat(shader, " ");
	dstr_cat(shader, func->name);
	dstr_cat(shader, "(");

	for (i = 0; i < func->param_vars.num; i++) {
		struct ep_var *var = func->param_vars.array+i;

		if (i)
			dstr_cat(shader, ", ");
		ep_write_var(shader, var);
	}

	dstr_cat(shader, ")\n");
	dstr_cat_dstr(shader, &func->contents);
	dstr_cat(shader, "\n");
}

/* writes mapped vars used by the call as parameters for main */
static void ep_write_main_params(struct effect_parser *ep,
		struct dstr *shader, struct dstr *param_str,
		struct ep_func *func)
{
	size_t i;
	bool empty_params = dstr_isempty(param_str);

	for (i = 0; i < func->param_vars.num; i++) {
		struct ep_var *var = func->param_vars.array+i;
		struct ep_struct *st = NULL;
		bool mapped = (var->mapping != NULL);

		if (!mapped) {
			st = ep_getstruct(ep, var->type);
			if (st)
				mapped = ep_struct_mapped(st);
		}

		if (mapped) {
			dstr_cat(shader, var->type);
			dstr_cat(shader, " ");
			dstr_cat(shader, var->name);

			if (!st) {
				dstr_cat(shader, " : ");
				dstr_cat(shader, var->mapping);
			}

			if (!dstr_isempty(param_str))
				dstr_cat(param_str, ", ");
			dstr_cat(param_str, var->name);
		}
	}

	if (!empty_params)
		dstr_cat(param_str, ", ");
}

static void ep_write_main(struct effect_parser *ep, struct dstr *shader,
		struct ep_func *func, struct dstr *call_str)
{
	struct dstr param_str;
	struct dstr adjusted_call;

	dstr_init(&param_str);
	dstr_init_copy_dstr(&adjusted_call, call_str);

	dstr_cat(shader, "\n");
	dstr_cat(shader, func->ret_type);
	dstr_cat(shader, " main(");

	ep_write_main_params(ep, shader, &param_str, func);

	dstr_cat(shader, ")");
	if (func->mapping) {
		dstr_cat(shader, " : ");
		dstr_cat(shader, func->mapping);
	}

	dstr_cat(shader, "\n{\n\treturn ");
	dstr_cat_dstr(shader, &adjusted_call);
	dstr_cat(shader, "\n}\n");

	dstr_free(&adjusted_call);
	dstr_free(&param_str);
}

static inline void ep_reset_written(struct effect_parser *ep)
{
	size_t i;
	for (i = 0; i <ep->params.num; i++)
		ep->params.array[i].written = false;
	for (i = 0; i <ep->structs.num; i++)
		ep->structs.array[i].written = false;
	for (i = 0; i <ep->funcs.num; i++)
		ep->funcs.array[i].written = false;
	for (i = 0; i <ep->samplers.num; i++)
		ep->samplers.array[i].written = false;
}

static void ep_makeshaderstring(struct effect_parser *ep,
		struct dstr *shader, struct darray *shader_call,
		struct darray *used_params)
{
	struct cf_token *token = shader_call->array;
	struct cf_token *func_name;
	struct ep_func *func;
	struct dstr call_str;

	dstr_init(&call_str);

	while (token->type != CFTOKEN_NONE && is_whitespace(*token->str.array))
		token++;

	if (token->type == CFTOKEN_NONE ||
	    strref_cmp(&token->str, "NULL") == 0)
		return;

	func_name = token;

	while (token->type != CFTOKEN_NONE) {
		struct ep_param *param = ep_getparam_strref(ep, &token->str);
		if (param)
			ep_write_param(shader, param, used_params);

		dstr_cat_strref(&call_str, &token->str);
		token++;
	}

	func = ep_getfunc_strref(ep, &func_name->str);
	if (!func)
		return;

	ep_write_func(ep, shader, func, used_params);
	ep_write_main(ep, shader, func, &call_str);

	dstr_free(&call_str);

	ep_reset_written(ep);
}

static void ep_compile_param(struct effect_parser *ep, size_t idx)
{
	struct effect_param *param;
	struct ep_param *param_in;

	param = ep->effect->params.array+idx;
	param_in = ep->params.array+idx;
	param_in->param = param;

	param->name = bstrdup(param_in->name);
	param->section = EFFECT_PARAM;
	da_move(param->default_val, param_in->default_val);

	if (strcmp(param_in->type, "bool") == 0)
		param->type = SHADER_PARAM_BOOL;
	else if (strcmp(param_in->type, "float") == 0)
		param->type = SHADER_PARAM_FLOAT;
	else if (strcmp(param_in->type, "int") == 0)
		param->type = SHADER_PARAM_INT;
	else if (strcmp(param_in->type, "float2") == 0)
		param->type = SHADER_PARAM_VEC2;
	else if (strcmp(param_in->type, "float3") == 0)
		param->type = SHADER_PARAM_VEC3;
	else if (strcmp(param_in->type, "float4") == 0)
		param->type = SHADER_PARAM_VEC4;
	else if (strcmp(param_in->type, "float3x3") == 0)
		param->type = SHADER_PARAM_MATRIX3X3;
	else if (strcmp(param_in->type, "float4x4") == 0)
		param->type = SHADER_PARAM_MATRIX4X4;
	else if (param_in->is_texture)
		param->type = SHADER_PARAM_TEXTURE;

	if (strcmp(param_in->name, "ViewProj") == 0)
		ep->effect->view_proj = param;
	else if (strcmp(param_in->name, "World") == 0)
		ep->effect->world = param;
}

static inline void ep_compile_pass_shaderparams(struct effect_parser *ep,
		struct darray *pass_params, struct darray *used_params,
		shader_t shader)
{
	size_t i;
	darray_resize(sizeof(struct pass_shaderparam), pass_params,
			used_params->num);

	for (i = 0; i < pass_params->num; i++) {
		struct dstr *param_name;
		struct pass_shaderparam *param;
1315
		int test;
J
jp9000 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460

		param_name = darray_item(sizeof(struct dstr), used_params, i);
		param = darray_item(sizeof(struct pass_shaderparam),
				pass_params, i);

		param->eparam = effect_getparambyname(ep->effect,
				param_name->array);
		param->sparam = shader_getparambyname(shader,
				param_name->array);
	}
}

static inline bool ep_compile_pass_shader(struct effect_parser *ep,
		struct effect_technique *tech,
		struct effect_pass *pass, struct ep_pass *pass_in,
		size_t pass_idx, enum shader_type type)
{
	struct dstr shader_str;
	struct dstr location;
	struct darray used_params; /* struct dstr */
	struct darray *pass_params; /* struct pass_shaderparam */
	shader_t shader;

	dstr_init(&shader_str);
	darray_init(&used_params);
	dstr_init(&location);

	dstr_copy(&location, ep->cfp.lex.file);
	if (type == SHADER_VERTEX)
		dstr_cat(&location, " (Vertex ");
	else if (type == SHADER_PIXEL)
		dstr_cat(&location, " (Pixel ");
	else if (type == SHADER_GEOMETRY)
		dstr_cat(&location, " (Geometry ");

	dstr_catf(&location, "shader, technique %s, pass %u)", tech->name,
			pass_idx);

	if (type == SHADER_VERTEX) {
		ep_makeshaderstring(ep, &shader_str,
				&pass_in->vertex_program.da, &used_params);

		pass->vertshader = gs_create_vertexshader(shader_str.array,
				location.array, NULL);

		shader = pass->vertshader;
		pass_params = &pass->vertshader_params.da;
	} else if (type == SHADER_PIXEL) {
		ep_makeshaderstring(ep, &shader_str,
				&pass_in->fragment_program.da, &used_params);

		pass->pixelshader = gs_create_pixelshader(shader_str.array,
				location.array, NULL);

		shader = pass->pixelshader;
		pass_params = &pass->pixelshader_params.da;
	}

	blog(LOG_DEBUG, "+++++++++++++++++++++++++++++++++++");
	blog(LOG_DEBUG, "  %s", location.array);
	blog(LOG_DEBUG, "-----------------------------------");
	blog(LOG_DEBUG, "%s", shader_str.array);
	blog(LOG_DEBUG, "+++++++++++++++++++++++++++++++++++");

	if (shader)
		ep_compile_pass_shaderparams(ep, pass_params, &used_params,
				shader);

	dstr_free(&location);
	dstr_array_free(used_params.array, used_params.num);
	darray_free(&used_params);
	dstr_free(&shader_str);

	return shader != NULL;
}

static bool ep_compile_pass(struct effect_parser *ep,
		struct effect_technique *tech,
		struct ep_technique *tech_in,
		size_t idx)
{
	struct effect_pass *pass;
	struct ep_pass *pass_in;
	bool success = true;

	pass = tech->passes.array+idx;
	pass_in = tech_in->passes.array+idx;

	pass->name = bstrdup(pass_in->name);
	pass->section = EFFECT_PASS;

	if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
				SHADER_VERTEX))
		success = false;

	if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
				SHADER_PIXEL))
		success = false;

	return success;
}

static inline bool ep_compile_technique(struct effect_parser *ep, size_t idx)
{
	struct effect_technique *tech;
	struct ep_technique *tech_in;
	bool success = true;
	size_t i;

	tech = ep->effect->techniques.array+idx;
	tech_in = ep->techniques.array+idx;

	tech->name = bstrdup(tech_in->name);
	tech->section = EFFECT_TECHNIQUE;
	tech->effect = ep->effect;

	da_resize(tech->passes, tech_in->passes.num);

	for (i = 0; i < tech->passes.num; i++) {
		if (!ep_compile_pass(ep, tech, tech_in, i))
			success = false;
	}

	return success;
}

static bool ep_compile(struct effect_parser *ep)
{
	bool success = true;
	size_t i;

	assert(ep->effect);

	da_resize(ep->effect->params, ep->params.num);
	da_resize(ep->effect->techniques, ep->techniques.num);

	for (i = 0; i < ep->params.num; i++)
		ep_compile_param(ep, i);
	for (i = 0; i < ep->techniques.num; i++) {
		if (!ep_compile_technique(ep, i))
			success = false;
	}

	return success;
}