dark-hammer / src / engine / gfx-shader.c

   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
 822
 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
 970
 971
 972
 973
 974
 975
 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
/***********************************************************************************
 * Copyright (c) 2012, Sepehr Taghdisian
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 *
 ***********************************************************************************/

#include <stdio.h>
#include <stdarg.h>

#include "core/core.h"
#include "core/array.h"
#include "core/file-mgr.h"
#include "mem-ids.h"
#include "engine.h"
#include "app.h"

#include "gfx-shader.h"
#include "gfx-device.h"
#include "gfx-cmdqueue.h"

#define CACHE_SIGN 0x48534843   /* HSHC */
#if defined(_D3D_)
#define CACHE_FILENAME "dh_shaders_d3d.cache"
#elif defined(_GL_)
#define CACHE_FILENAME "dh_shaders_gl.cache"
#endif
#define HSEED 4354

/*************************************************************************************************
 * types
 */
struct shader_cache_item
{
	hash_t vs_hash;
	hash_t ps_hash;
	hash_t gs_hash;
	uint32 defines_hash;
	struct gfx_shader_binary_data bin;
};

struct shader_load_item
{
	char vs_filepath[DH_PATH_MAX];
	char ps_filepath[DH_PATH_MAX];
	char gs_filepath[DH_PATH_MAX];
    char* include_code;
	struct allocator* alloc;
};

struct shader_mgr
{
	bool_t disable_cache;
	struct array cache;	/* item = shader_cache_item */
    struct array shaders;   /* item = gfx_shader* */
	uint32 cur_driver_hash;
	struct shader_load_item load_item;
};

/* column major matrixes for submitting to shader */
struct ALIGN16 mat3f_cm
{
    union   {
        struct {
            fl32 m11, m21, m31, m41;    /* column #1 */
            fl32 m12, m22, m32, m42;    /* column #2 */
            fl32 m13, m23, m33, m43;    /* column #3 */
        };

        struct {
            fl32 col1[4];
            fl32 col2[4];
            fl32 col3[4];
        };

        fl32    f[12];
    };
};

struct ALIGN16 mat4f_cm
{
    union   {
        struct {
            fl32 m11, m21, m31, m41;    /* column #1 */
            fl32 m12, m22, m32, m42;    /* column #2 */
            fl32 m13, m23, m33, m43;    /* column #3 */
            fl32 m14, m24, m34, m44;    /* column #4 */
        };

        struct {
            fl32 col1[4];
            fl32 col2[4];
            fl32 col3[4];
            fl32 col4[4];
        };

        fl32 f[16];
    };
};

/*************************************************************************************************
 * globals
 */
struct shader_mgr g_shader_mgr;

/*************************************************************************************************
 * inlines
 */
INLINE struct mat3f_cm* mat3f_togpu(struct mat3f_cm* rm, const struct mat3f* m)
{
    rm->m11 = m->m11;   rm->m21 = m->m21;   rm->m31 = m->m31;   rm->m41 = m->m41;
    rm->m12 = m->m12;   rm->m22 = m->m22;   rm->m32 = m->m32;   rm->m42 = m->m42;
    rm->m13 = m->m13;   rm->m23 = m->m23;   rm->m33 = m->m33;   rm->m43 = m->m43;
    return rm;
}

INLINE struct mat4f_cm* mat4f_togpu(struct mat4f_cm* rm, const struct mat4f* m)
{
    rm->m11 = m->m11;   rm->m21 = m->m21;   rm->m31 = m->m31;   rm->m41 = m->m41;
    rm->m12 = m->m12;   rm->m22 = m->m22;   rm->m32 = m->m32;   rm->m42 = m->m42;
    rm->m13 = m->m13;   rm->m23 = m->m23;   rm->m33 = m->m33;   rm->m43 = m->m43;
    rm->m14 = m->m14;   rm->m24 = m->m24;   rm->m34 = m->m34;   rm->m44 = m->m44;
    return rm;
}

INLINE const struct gfx_constant_desc* shader_find_constantcb(struct gfx_cblock* cb,
		uint32 name_hash)
{
	struct hashtable_item* item = hashtable_fixed_find(&cb->ctable, name_hash);
	if (item != NULL)
		return &cb->constants[item->value];
	else
		return NULL;
}

#if defined(_GL_)
/* add glsl to the last directory path
 * and add .glsl extenstion to the file
 */
INLINE const char* shader_makepath(char* outpath, const char* inpath)
{
	char filename[64];
	path_getdir(outpath, inpath);
	strcat(outpath, "glsl/");
	strcat(outpath, path_getfullfilename(filename, inpath));
	strcat(outpath, ".glsl");
	return outpath;
}
#elif defined(_D3D_)
/* add hlsl to the last directory path
 * and add .hlsl extenstion to the file
 */
INLINE const char* shader_makepath(char* outpath, const char* inpath)
{
	char filename[64];
	path_getdir(outpath, inpath);
	strcat(outpath, "hlsl/");
	strcat(outpath, path_getfullfilename(filename, inpath));
	strcat(outpath, ".hlsl");
	return outpath;
}
#endif

/*************************************************************************************************
 * forward delarations
 */
/* implemented in platform specific (d3d/gl) gfx-shader.c/cpp */
#ifdef __cplusplus
extern "C" {
#endif
void shader_init_cblocks(struct gfx_shader* shader);
void shader_destroy_cblocks(struct gfx_shader* shader);
void shader_init_samplers(struct gfx_shader* shader);
void shader_destroy_samplers(struct gfx_shader* shader);
void shader_init_constants(struct gfx_shader* shader);
void shader_destroy_constants(struct gfx_shader* shader);
result_t shader_init_metadata(struct gfx_shader* shader);
void shader_destroy_metadata(struct gfx_shader* shader);
#ifdef __cplusplus
}
#endif

/* */
const struct shader_cache_item* shader_find_cache_item(hash_t vs_hash,
		hash_t ps_hash, hash_t gs_hash, uint32 defines_hash);

void shader_add_cache_item(hash_t vs_hash,
		hash_t ps_hash, hash_t gs_hash, uint32 defines_hash,
		const struct gfx_shader_binary_data* bin);

void make_define_string(char* define_str, const struct gfx_shader_define* defines,
		uint32 define_cnt);
struct gfx_shader* shader_load_fromcache(struct allocator* alloc,
		struct allocator* tmp_alloc, const struct shader_cache_item* cache,
        const struct gfx_input_element_binding* bindings, uint32 binding_cnt);
struct gfx_shader* shader_load_fromscratch(struct allocator* alloc,
		struct allocator* tmp_alloc,
		const struct gfx_shader_data* source_data,
		const struct gfx_input_element_binding* bindings, uint32 binding_cnt,
		const struct gfx_shader_define* defines, uint32 define_cnt,
		struct gfx_shader_binary_data* bin);
void shader_unload(struct gfx_shader* shader);
void shader_loadcache();
void shader_saveunloadcache();
void* shader_includesource(struct allocator* alloc, const char* include_code, void* src, 
    uint32 src_size, OUT uint32* outsize);

/*************************************************************************************************/
void gfx_shader_zero()
{
	memset(&g_shader_mgr, 0x00, sizeof(struct shader_mgr));
}

result_t gfx_shader_initmgr(bool_t disable_cache)
{
	log_print(LOG_INFO, "init shader-mgr ...");

	if (IS_FAIL(arr_create(mem_heap(), &g_shader_mgr.shaders,
            sizeof(struct gfx_shader*), 50, 50, MID_GFX)))
	{
		err_printn(__FILE__, __LINE__, RET_OUTOFMEMORY);
		return RET_OUTOFMEMORY;
	}

    /* save driver version string hash */
    const char* driver_info = app_get_gfxdriverstr();
    g_shader_mgr.cur_driver_hash = hashtable_str(driver_info);

    if (!disable_cache)	{
		if (IS_FAIL(arr_create(mem_heap(), &g_shader_mgr.cache,
					sizeof(struct shader_cache_item), 50, 50, MID_GFX)))
		{
			err_printn(__FILE__, __LINE__, RET_OUTOFMEMORY);
			return RET_OUTOFMEMORY;
		}

		/* load cache file */
		log_print(LOG_INFO, "\tloading shader cache ...");
		shader_loadcache();
	}	else	{
		log_print(LOG_INFO, "\tshader cache is disabled (debug mode).");
		g_shader_mgr.disable_cache = TRUE;
	}

	return RET_OK;
}

void gfx_shader_releasemgr()
{
    if (!g_shader_mgr.disable_cache)	{
    	log_print(LOG_INFO, "\tsaving shader cache ...");
    	shader_saveunloadcache();
    	arr_destroy(&g_shader_mgr.cache);
    }

    /* unload, remaining shaders */
    for (uint32 i = 0; i < g_shader_mgr.shaders.item_cnt; i++)  {
        struct gfx_shader* shader = ((struct gfx_shader**)g_shader_mgr.shaders.buffer)[i];
        if (shader != NULL)
            shader_unload(shader);
    }

    arr_destroy(&g_shader_mgr.shaders);
}

struct gfx_cblock* gfx_shader_create_cblockraw(struct allocator* alloc, const char* block_name, 
    const struct gfx_constant_desc* constants, uint32 cnt)
{
    struct gfx_cblock* cb = A_ALLOC(alloc, sizeof(struct gfx_cblock), MID_GFX);
    if (cb == NULL)
        return NULL;
    memset(cb, 0x00, sizeof(struct gfx_cblock));
    cb->alloc = alloc;

    cb->constant_cnt = cnt;
    cb->constants = A_ALLOC(alloc, sizeof(struct gfx_constant_desc)*cnt, MID_GFX);
    if (cb->constants == NULL)  {
        A_FREE(alloc, cb);
        return cb;
    }
    memcpy(cb->constants, constants, sizeof(struct gfx_constant_desc)*cnt);

    /* push constants into ctable */
    if (IS_FAIL(hashtable_fixed_create(alloc, &cb->ctable, cnt, MID_GFX))) {
        gfx_shader_destroy_cblock(cb);
        return NULL;
    }

    for (uint32 i = 0; i < cnt; i++) 
        hashtable_fixed_add(&cb->ctable, hashtable_str(constants[i].name), i);

    /* estimate buffer size, and create cpu_buffer */
    uint32 size = 0;
    for (uint32 i = 0; i < cnt; i++)
        size += constants[i].elem_size;

    cb->cpu_buffer = A_ALLOC(alloc, size, MID_GFX);
    if (cb->cpu_buffer == NULL) {
        gfx_shader_destroy_cblock(cb);
        return NULL;
    }
    memset(cb->cpu_buffer, 0x00, size);
    cb->buffer_size = size;

    return cb;
}


void shader_loadcache()
{
	char cache_filepath[DH_PATH_MAX];
	strcat(util_gettempdir(cache_filepath), CACHE_FILENAME);
	FILE* f = fopen(cache_filepath, "rb");
    if (f == NULL)  {
        strcat(util_getexedir(cache_filepath), CACHE_FILENAME);
        f = fopen(cache_filepath, "rb");
    }

	if (f != NULL)	{
		uint32 sign;
		fread(&sign, sizeof(sign), 1, f);
		if (sign != CACHE_SIGN)	{
			log_print(LOG_WARNING, "loading shader cache failed: invalid file format");
			fclose(f);
			return;
		}

		/* if driver_hash is changed, don't load cache*/
		uint32 driver_hash;
		fread(&driver_hash, sizeof(uint32), 1, f);
		if (driver_hash != g_shader_mgr.cur_driver_hash)	{
			log_print(LOG_INFO, "shader cache: driver/platform changed, cache reset.");
			fclose(f);
			return;
		}

        uint32 item_cnt;
		fread(&item_cnt, sizeof(uint32), 1, f);
		log_printf(LOG_INFO, "\tloading %d item(s) from shader cache ...", item_cnt);
		for (uint32 i = 0; i < item_cnt; i++)	{
			struct shader_cache_item* item = arr_add(&g_shader_mgr.cache);
			ASSERT(item);
			fread(item, sizeof(struct shader_cache_item), 1, f);

			/* read program/shaders data */
#if defined(_GL_)
			if (item->bin.prog_size > 0)	{
				item->bin.prog_data = ALLOC(item->bin.prog_size, MID_GFX);
				ASSERT(item->bin.prog_data);
				fread(item->bin.prog_data, item->bin.prog_size, 1, f);
			}
#elif defined(_D3D_)
			if (item->bin.vs_size > 0)	{
				item->bin.vs_data = ALLOC(item->bin.vs_size, MID_GFX);
				ASSERT(item->bin.vs_data);
				fread(item->bin.vs_data, item->bin.vs_size, 1, f);
			}
			if (item->bin.ps_size > 0)	{
				item->bin.ps_data = ALLOC(item->bin.ps_size, MID_GFX);
				ASSERT(item->bin.ps_data);
				fread(item->bin.ps_data, item->bin.ps_size, 1, f);
			}
			if (item->bin.gs_size > 0)	{
				item->bin.gs_data = ALLOC(item->bin.gs_size, MID_GFX);
				ASSERT(item->bin.gs_data);
				fread(item->bin.gs_data, item->bin.gs_size, 1, f);
			}
#endif
		}

		fclose(f);
	}

}

void shader_saveunloadcache()
{
	/* save cache file */
	uint32 item_cnt = g_shader_mgr.cache.item_cnt;
	if (item_cnt > 0)	{
		char cache_filepath[DH_PATH_MAX];
		strcat(util_gettempdir(cache_filepath), CACHE_FILENAME);
		FILE* f = fopen(cache_filepath, "wb");
		if (f != NULL)	{
            uint32 sign = CACHE_SIGN;
			fwrite(&sign, sizeof(sign), 1, f);
			fwrite(&g_shader_mgr.cur_driver_hash, sizeof(uint32), 1, f);
			fwrite(&item_cnt, sizeof(uint32), 1, f);

			struct shader_cache_item* items = g_shader_mgr.cache.buffer;
			for (uint32 i = 0; i < item_cnt; i++)	{
				fwrite(&items[i], sizeof(struct shader_cache_item), 1, f);

#if defined(_GL_)
				if (items[i].bin.prog_size > 0 && items[i].bin.prog_data != NULL)	{
					fwrite(items[i].bin.prog_data, items[i].bin.prog_size, 1, f);
					FREE(items[i].bin.prog_data);
				}
#elif defined(_D3D_)
				if (items[i].bin.vs_size > 0 && items[i].bin.vs_data != NULL)	{
					fwrite(items[i].bin.vs_data, items[i].bin.vs_size, 1, f);
					FREE(items[i].bin.vs_data);
				}
				if (items[i].bin.ps_size > 0 && items[i].bin.ps_data != NULL)	{
					fwrite(items[i].bin.ps_data, items[i].bin.ps_size, 1, f);
					FREE(items[i].bin.ps_data);
				}
				if (items[i].bin.gs_size > 0 && items[i].bin.gs_data != NULL)	{
					fwrite(items[i].bin.gs_data, items[i].bin.gs_size, 1, f);
					FREE(items[i].bin.gs_data);
				}
#endif
			}

			fclose(f);
		}
	}
}

void gfx_shader_beginload(struct allocator* alloc, const char* vs_filepath, const char* ps_filepath,
		const char* gs_filepath, uint32 include_cnt, ...)
{
	g_shader_mgr.load_item.alloc = alloc;
	ASSERT(g_shader_mgr.load_item.vs_filepath != NULL);

	strcpy(g_shader_mgr.load_item.vs_filepath, vs_filepath);
	if (ps_filepath != NULL)
		strcpy(g_shader_mgr.load_item.ps_filepath, ps_filepath);
	if (gs_filepath != NULL)
		strcpy(g_shader_mgr.load_item.gs_filepath, gs_filepath);

    /* include files */
    char* total_inc = NULL;
    if (include_cnt > 0)    {
        uint32 total_size = 0;  
        char filepath[DH_PATH_MAX];

        va_list args;
        va_start(args, include_cnt);
        for (uint32 i = 0; i < include_cnt; i++)    {
            const char* inc_file = va_arg(args, const char*);
            ASSERT(inc_file);

            /* load include file and append it to include_code */
            file_t f = io_file_openmem(mem_heap(), 
                shader_makepath(filepath, inc_file), FALSE, MID_GFX);
            if (f == NULL)	{
                log_printf(LOG_WARNING, "shader '%s' load failed: "
                    "could not open include file '%s'", vs_filepath, inc_file);
                if (total_inc != NULL)
                    FREE(total_inc);
                return;
            }

            size_t inc_size;
            char* inc_code = (char*)io_file_detachmem(f, &inc_size, NULL);
            ASSERT(inc_size > 0);
            io_file_close(f);

            if (total_inc != NULL)  {
                char* tmp = ALLOC(total_size + inc_size, MID_GFX); 
                ASSERT(tmp);
                memcpy(tmp, total_inc, total_size);
                FREE(total_inc);
                total_inc = tmp;
                memcpy(total_inc + total_size - 1, inc_code, inc_size); 
                total_size += inc_size;
            }   else    {
                total_inc = ALLOC(inc_size + 1, MID_GFX);
                ASSERT(total_inc);
                memcpy(total_inc, inc_code, inc_size);
                total_size += (inc_size + 1);
            }
            total_inc[total_size-1] = 0;   /* close the string */

            FREE(inc_code);
        } /* foreach: include file */
        va_end(args);
    }

    g_shader_mgr.load_item.include_code = total_inc;
}

uint32 gfx_shader_add(const char* alias, uint32 binding_cnt, uint32 define_cnt, ...)
{
	ASSERT(g_shader_mgr.load_item.alloc != NULL);

	struct gfx_input_element_binding bindings[GFX_MAX_VBUFFERS];
	struct gfx_shader_define defines[16];

	va_list args;
	va_start(args, define_cnt);

	/* make bindings */
	ASSERT(binding_cnt < GFX_MAX_VBUFFERS);
	for (uint32 i = 0; i < binding_cnt; i++)	{
		bindings[i].id = (enum gfx_input_element_id)va_arg(args, unsigned int);
		bindings[i].var_name = va_arg(args, const char*);
        bindings[i].offset = -1;
	}

	/* make defines */
	ASSERT(define_cnt < 16);
	for (uint32 i = 0; i < define_cnt; i++)	{
		defines[i].name = va_arg(args, const char*);
		defines[i].value = va_arg(args, const char*);
	}

	va_end(args);

	return gfx_shader_load(alias, g_shader_mgr.load_item.alloc,
			g_shader_mgr.load_item.vs_filepath[0] != 0 ? g_shader_mgr.load_item.vs_filepath : NULL,
			g_shader_mgr.load_item.ps_filepath[0] != 0 ? g_shader_mgr.load_item.ps_filepath : NULL,
			g_shader_mgr.load_item.gs_filepath[0] != 0 ? g_shader_mgr.load_item.gs_filepath : NULL,
			bindings, binding_cnt, defines, define_cnt, g_shader_mgr.load_item.include_code);
}

void gfx_shader_endload()
{
    if (g_shader_mgr.load_item.include_code != NULL)
        FREE(g_shader_mgr.load_item.include_code);

	memset(&g_shader_mgr.load_item, 0x00, sizeof(struct shader_load_item));
}

uint32 gfx_shader_load(const char* alias, struct allocator* alloc,
		const char* vs_filepath, const char* ps_filepath, const char* gs_filepath,
		const struct gfx_input_element_binding* bindings, uint32 binding_cnt,
		OPTIONAL const struct gfx_shader_define* defines, uint32 define_cnt,
        OPTIONAL const char* include_code)
{
	char filepath[DH_PATH_MAX];
	struct gfx_shader_data source_data;
	struct gfx_shader* shader = NULL;
    hash_t vs_hash;	hash_zero(&vs_hash);
    hash_t ps_hash;	hash_zero(&ps_hash);
    hash_t gs_hash;	hash_zero(&gs_hash);
    uint32 defines_hash = 0;
    char define_code[1024];
    const struct shader_cache_item* item;
    uint32 shader_id = 0;

	struct allocator* tmp_alloc = eng_get_framealloc();
	eng_save_framealloc();

	memset(&source_data, 0x00, sizeof(struct gfx_shader_data));

	/* get files and calculate hash values */
	if (vs_filepath != NULL)	{
		file_t f = io_file_openmem(tmp_alloc,
				shader_makepath(filepath, vs_filepath), FALSE, MID_GFX);
		if (f == NULL)	{
			err_printf(__FILE__, __LINE__, "shader '%s' load failed: "
					"could not open vertex-shader '%s'", alias, vs_filepath);
			goto cleanup;
		}
		source_data.vs_source = io_file_detachmem(f, &source_data.vs_size, NULL);
		io_file_close(f);

        if (include_code != NULL)  {
            source_data.vs_source = shader_includesource(tmp_alloc, include_code, 
                source_data.vs_source, source_data.vs_size, (uint32*)&source_data.vs_size);
        }
	}

	if (ps_filepath != NULL)	{
		file_t f = io_file_openmem(tmp_alloc, shader_makepath(filepath, ps_filepath), FALSE, 
            MID_GFX);
		if (f == FILE_NULL)	{
			err_printf(__FILE__, __LINE__, "shader '%s' load failed: "
					"could not open pixel-shader '%s'", alias, ps_filepath);
			goto cleanup;
		}
		source_data.ps_source = io_file_detachmem(f, &source_data.ps_size, NULL);
		io_file_close(f);

        if (include_code != NULL)  {
            source_data.ps_source = shader_includesource(tmp_alloc, include_code, 
                source_data.ps_source, source_data.ps_size, (uint32*)&source_data.ps_size);
        }
	}

	if (gs_filepath != NULL)	{
		file_t f = io_file_openmem(tmp_alloc, shader_makepath(filepath, gs_filepath), FALSE, 
            MID_GFX);
		if (f == FILE_NULL)	{
			err_printf(__FILE__, __LINE__, "shader '%s' load failed: "
					"could not open geometry-shader '%s'", alias, gs_filepath);
			goto cleanup;
		}
		source_data.gs_source = io_file_detachmem(f, &source_data.gs_size, NULL);
		io_file_close(f);

        if (include_code != NULL)  {
            source_data.gs_source = shader_includesource(tmp_alloc, include_code, 
                source_data.gs_source, source_data.gs_size, (uint32*)&source_data.gs_size);
        }
	}

	/* check for shader cache enabled
	 * if not just load and return
	 */
	if (g_shader_mgr.disable_cache)	{
        log_printf(LOG_INFO, "compiling shader '%s' ...", alias);
		shader = shader_load_fromscratch(alloc, tmp_alloc, &source_data,
				bindings, binding_cnt, defines, define_cnt, NULL);
		goto cleanup;
	}

	make_define_string(define_code, defines, define_cnt);
	defines_hash = hashtable_str(define_code);

	if (source_data.vs_source != NULL)
		vs_hash = hash_murmur128(source_data.vs_source, source_data.vs_size, HSEED);
	if (source_data.ps_source != NULL)
		ps_hash = hash_murmur128(source_data.ps_source, source_data.ps_size, HSEED);
	if (source_data.gs_source != NULL)
		gs_hash = hash_murmur128(source_data.gs_source, source_data.gs_size, HSEED);

	item = shader_find_cache_item(vs_hash, ps_hash, gs_hash, defines_hash);
	if (item != NULL)	{
		/* use shader from cache */
		shader = shader_load_fromcache(alloc, tmp_alloc, item, bindings, binding_cnt);
	}	else	{
		struct gfx_shader_binary_data bin_data;
		memset(&bin_data, 0x00, sizeof(bin_data));

		/* create and load shader */
        log_printf(LOG_INFO, "compiling shader '%s' ...", alias);
		shader = shader_load_fromscratch(alloc, tmp_alloc, &source_data,
				bindings, binding_cnt, defines, define_cnt, &bin_data);
		if (shader != NULL)
			shader_add_cache_item(vs_hash, ps_hash, gs_hash, defines_hash, &bin_data);
	}

cleanup:
	if (shader != NULL)	{
        /* save bindings */
        for (uint32 i = 0; i < binding_cnt; i++)
            shader->bindings[i] = bindings[i].offset == -1 ? bindings[i].id : bindings[i].offset;
        shader->binding_cnt = binding_cnt;
        /* */
        shader_init_metadata(shader);
		shader_init_cblocks(shader);
		shader_init_samplers(shader);
		shader_init_constants(shader);

#if defined(_DEBUG_)
		strcpy(shader->name, alias);
#endif
        /* add to shaders and fetch ID */
        struct gfx_shader** pshader = arr_add(&g_shader_mgr.shaders);
        ASSERT(pshader);
        *pshader = shader;
        shader_id = g_shader_mgr.shaders.item_cnt;
	}

	/* cleanup */
	if (source_data.vs_source != NULL)
		A_FREE(tmp_alloc, source_data.vs_source);
	if (source_data.ps_source != NULL)
		A_FREE(tmp_alloc, source_data.ps_source);
	if (source_data.gs_source != NULL)
		A_FREE(tmp_alloc, source_data.gs_source);
	eng_load_framealloc();

    if (shader == NULL)
        err_printf(__FILE__, __LINE__, "gfx-shader load error: loading shader '%s' failed", alias);
	return shader_id;
}

void make_define_string(char* define_str,
		const struct gfx_shader_define* defines, uint32 define_cnt)
{
	char line[128];
	define_str[0] = 0;

	for (uint32 i = 0; i < define_cnt; i++)	{
		sprintf(line, "%s=%s\n", defines[i].name, defines[i].value);
		strcat(define_str, line);
	}
}

/* returns NULL if no hash in database is found */
const struct shader_cache_item* shader_find_cache_item(hash_t vs_hash,
		hash_t ps_hash, hash_t gs_hash, uint32 defines_hash)
{
	uint32 item_cnt = g_shader_mgr.cache.item_cnt;
	struct shader_cache_item* items = g_shader_mgr.cache.buffer;

	for (uint32 i = 0; i < item_cnt; i++)	{
		if (items[i].defines_hash == defines_hash &&
			hash_isequal(items[i].vs_hash, vs_hash) &&
			hash_isequal(items[i].ps_hash, ps_hash) &&
			hash_isequal(items[i].gs_hash, gs_hash))
		{
			return &items[i];
		}
	}

	return NULL;
}

struct gfx_shader* shader_load_fromcache(struct allocator* alloc,
		struct allocator* tmp_alloc, const struct shader_cache_item* cache,
        const struct gfx_input_element_binding* bindings, uint32 binding_cnt)
{
	struct gfx_shader* shader = A_ALLOC(alloc, sizeof(struct gfx_shader), MID_GFX);
	if (shader == NULL)		{
		err_printn(__FILE__, __LINE__, RET_OUTOFMEMORY);
		return NULL;
	}
	memset(shader, 0x00, sizeof(struct gfx_shader));
	shader->alloc = alloc;

#if defined(_GL_)
	shader->prog = gfx_create_program_bin(cache->bin.prog_data, cache->bin.prog_size,
			cache->bin.fmt);
#elif defined(_D3D_)
    shader->prog = gfx_create_program_bin(cache->bin.vs_data, cache->bin.vs_size,
        cache->bin.ps_data, cache->bin.ps_size, cache->bin.gs_data, cache->bin.gs_size, 
        bindings, binding_cnt);

#endif
    if (shader->prog == NULL)	{
        err_printf(__FILE__, __LINE__, "shader load failed: could not create shader program");
        shader_unload(shader);
        return NULL;
    }

	return shader;
}

struct gfx_shader* shader_load_fromscratch(struct allocator* alloc,
		struct allocator* tmp_alloc,
		const struct gfx_shader_data* source_data,
		const struct gfx_input_element_binding* bindings, uint32 binding_cnt,
		const struct gfx_shader_define* defines, uint32 define_cnt,
		struct gfx_shader_binary_data* bin)
{
	struct gfx_shader* shader = A_ALLOC(alloc, sizeof(struct gfx_shader), MID_GFX);
	if (shader == NULL)		{
		err_printn(__FILE__, __LINE__, RET_OUTOFMEMORY);
		return NULL;
	}
	memset(shader, 0x00, sizeof(struct gfx_shader));
	shader->alloc = alloc;

	shader->prog = gfx_create_program(source_data, bindings, binding_cnt, defines, define_cnt, bin);
	if (shader->prog == NULL)	{
		err_printf(__FILE__, __LINE__, "shader load failed: could not create shader program");
		shader_unload(shader);
		return NULL;
	}

	return shader;
}

void gfx_shader_unload(uint32 shader_id)
{
    ASSERT(shader_id <= g_shader_mgr.shaders.item_cnt);
    struct gfx_shader* shader = ((struct gfx_shader**)g_shader_mgr.shaders.buffer)[shader_id-1];
    ASSERT(shader != NULL);

    shader_unload(shader);

    ((struct gfx_shader**)g_shader_mgr.shaders.buffer)[shader_id-1] = NULL;
}

void shader_unload(struct gfx_shader* shader)
{
    struct allocator* alloc = shader->alloc;

    if (shader->prog != NULL)
        gfx_destroy_program(shader->prog);

    shader_destroy_samplers(shader);
    shader_destroy_cblocks(shader);
    shader_destroy_constants(shader);
    shader_destroy_metadata(shader);

    memset(shader, 0x00, sizeof(struct gfx_shader));
    A_FREE(alloc, shader);
}


void gfx_shader_updatecblock(gfx_cmdqueue cmdqueue, struct gfx_cblock* cblock)
{
    ASSERT(cblock->gpu_buffer);
	gfx_buffer_update(cmdqueue, cblock->gpu_buffer, cblock->cpu_buffer, cblock->buffer_size);
}

void shader_add_cache_item(hash_t vs_hash,
		hash_t ps_hash, hash_t gs_hash, uint32 defines_hash,
		const struct gfx_shader_binary_data* bin)
{
	struct shader_cache_item* item = arr_add(&g_shader_mgr.cache);
	ASSERT(item != NULL);
	hash_set(&item->vs_hash, vs_hash);
	hash_set(&item->ps_hash, ps_hash);
	hash_set(&item->gs_hash, gs_hash);
	item->defines_hash = defines_hash;
	memcpy(&item->bin, bin, sizeof(struct gfx_shader_binary_data));
}

void gfx_shader_bind(gfx_cmdqueue cmdqueue, struct gfx_shader* shader)
{
    gfx_program_setbindings(cmdqueue, shader->bindings, shader->binding_cnt);
    gfx_program_set(cmdqueue, shader->prog);
}

/*************************************************************************************************/
bool_t gfx_cb_isvalid(struct gfx_cblock* cb, uint32 name_hash)
{
    return shader_find_constantcb(cb, name_hash) != NULL;
}

void gfx_cb_set4m(struct gfx_cblock* cb, uint32 name_hash, const struct mat4f* m)
{
	struct mat4f_cm mcm;
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_MAT4x4);
	memcpy(cb->cpu_buffer + c->offset, mat4f_togpu(&mcm, m)->f, c->elem_size);
}

void gfx_cb_set3m(struct gfx_cblock* cb, uint32 name_hash, const struct mat3f* m)
{
	struct mat3f_cm mcm;
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_MAT4x3);
	memcpy(cb->cpu_buffer + c->offset, mat3f_togpu(&mcm, m)->f, c->elem_size);
}

void gfx_cb_set4f(struct gfx_cblock* cb, uint32 name_hash, const fl32* fv)
{
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_FLOAT4);

    fl32* buff = (fl32*)(cb->cpu_buffer + c->offset);
    buff[0] = fv[0];
    buff[1] = fv[1];
    buff[2] = fv[2];
    buff[3] = fv[3];
}

void gfx_cb_set3f(struct gfx_cblock* cb, uint32 name_hash, const fl32* fv)
{
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_FLOAT3);
    fl32* buff = (fl32*)(cb->cpu_buffer + c->offset);
    buff[0] = fv[0];
    buff[1] = fv[1];
    buff[2] = fv[2];
}

void gfx_cb_set2f(struct gfx_cblock* cb, uint32 name_hash, const fl32* fv)
{
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_FLOAT2);
    fl32* buff = (fl32*)(cb->cpu_buffer + c->offset);
    buff[0] = fv[0];
    buff[1] = fv[1];
}

void gfx_cb_setf(struct gfx_cblock* cb, uint32 name_hash, fl32 f)
{
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_FLOAT);
	*((fl32*)(cb->cpu_buffer + c->offset)) = f;
}

void gfx_cb_setfv(struct gfx_cblock* cb, uint32 name_hash, const fl32* fv, uint32 cnt)
{
    const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
    ASSERT(c);
    ASSERT(c->type == GFX_CONSTANT_FLOAT);
    for (uint32 i = 0; i < cnt; i++) 
        *((fl32*)(cb->cpu_buffer + c->offset + c->arr_stride*i)) = fv[i];
}

void gfx_cb_set4ivn(struct gfx_cblock* cb, uint32 name_hash, const int32* nv, uint32 cnt)
{
    const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
    ASSERT(c);
    ASSERT(c->type == GFX_CONSTANT_INT4);
    memcpy(cb->cpu_buffer + c->offset, nv, sizeof(int32)*cnt);
}


void gfx_cb_set4i(struct gfx_cblock* cb, uint32 name_hash, const int32* nv)
{
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_INT4);
    int32* buff = (int32*)(cb->cpu_buffer + c->offset);
    buff[0] = nv[0];
    buff[1] = nv[1];
    buff[2] = nv[2];
    buff[3] = nv[3];
}

void gfx_cb_set3i(struct gfx_cblock* cb, uint32 name_hash, const int32* nv)
{
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_INT3);
    int32* buff = (int32*)(cb->cpu_buffer + c->offset);
    buff[0] = nv[0];
    buff[1] = nv[1];
    buff[2] = nv[2];
}

void gfx_cb_set3ui(struct gfx_cblock* cb, uint32 name_hash, const uint32* nv)
{
    const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
    ASSERT(c);
    ASSERT(c->type == GFX_CONSTANT_INT3);
    uint32* buff = (uint32*)(cb->cpu_buffer + c->offset);
    buff[0] = nv[0];
    buff[1] = nv[1];
    buff[2] = nv[2];
}


void gfx_cb_set2i(struct gfx_cblock* cb, uint32 name_hash, const int32* nv)
{
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_INT2);
    int32* buff = (int32*)(cb->cpu_buffer + c->offset);
    buff[0] = nv[0];
    buff[1] = nv[1];
}

void gfx_cb_seti(struct gfx_cblock* cb, uint32 name_hash, int32 n)
{
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_INT);
	*((int32*)(cb->cpu_buffer + c->offset)) = n;
}

void gfx_cb_setiv(struct gfx_cblock* cb, uint32 name_hash, const int32* ns, uint32 cnt)
{
    const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
    ASSERT(c);
    ASSERT(c->type == GFX_CONSTANT_INT);
    cnt = minun(c->arr_size, cnt);
    for (uint32 i = 0; i < cnt; i++)    {
        *((int32*)(cb->cpu_buffer + c->offset + c->arr_stride*i)) = ns[i];
    }
}

void gfx_cb_setui(struct gfx_cblock* cb, uint32 name_hash, uint32 n)
{
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_UINT);
	*((uint32*)(cb->cpu_buffer + c->offset)) = n;
}

void gfx_cb_set3mv(struct gfx_cblock* cb, uint32 name_hash, const struct mat3f* mv, uint32 cnt)
{
	struct mat3f_cm mcm;
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_MAT4x3);
	uint32 mat_cnt = minun(c->arr_size, cnt);
	for (uint32 i = 0; i < mat_cnt; i++)	{
		memcpy(cb->cpu_buffer + c->offset + c->arr_stride*i, mat3f_togpu(&mcm, &mv[i])->f,
				c->elem_size);
	}
}

void gfx_cb_set3mvp(struct gfx_cblock* cb, uint32 name_hash, const struct mat3f** mvp, uint32 cnt)
{
	struct mat3f_cm mcm;
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_MAT4x3);
	uint32 mat_cnt = minun(c->arr_size, cnt);
	for (uint32 i = 0; i < mat_cnt; i++)	{
		memcpy(cb->cpu_buffer + c->offset + c->arr_stride*i, mat3f_togpu(&mcm, mvp[i])->f,
				c->elem_size);
	}
}

void gfx_cb_set4mv(struct gfx_cblock* cb, uint32 name_hash, const struct mat4f* mv, uint32 cnt)
{
	struct mat4f_cm mcm;
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_MAT4x4);
	uint32 mat_cnt = minun(c->arr_size, cnt);
	for (uint32 i = 0; i < mat_cnt; i++)	{
		memcpy(cb->cpu_buffer + c->offset + c->arr_stride*i, mat4f_togpu(&mcm, &mv[i])->f,
				c->elem_size);
	}
}

void gfx_cb_set4fv(struct gfx_cblock* cb, uint32 name_hash, const struct vec4f* vv, uint32 cnt)
{
	const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
	ASSERT(c);
	ASSERT(c->type == GFX_CONSTANT_FLOAT4);
	cnt = minun(c->arr_size, cnt);
	for (uint32 i = 0; i < cnt; i++)	{
        fl32* v = (fl32*)(cb->cpu_buffer + c->offset + c->arr_stride*i);
        v[0] = vv[i].x;
        v[1] = vv[i].y;
        v[2] = vv[i].z;
        v[3] = vv[i].w;
	}
}

void gfx_cb_setp(struct gfx_cblock* cb, uint32 name_hash, const void* sdata, uint32 size)
{
    const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
    ASSERT(c);
    ASSERT(c->type == GFX_CONSTANT_STRUCT);

    uint32 s = minun(size, c->arr_stride*c->arr_size);
    memcpy(cb->cpu_buffer + c->offset, sdata, s);
}

void gfx_cb_setpvi(struct gfx_cblock* cb, uint32 name_hash, const void* sdata, uint32 size, 
    uint32 idx)
{
    const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
    ASSERT(c);
    ASSERT(c->type == GFX_CONSTANT_STRUCT);
    ASSERT(idx < c->arr_size);

    uint32 s = minun(size, c->elem_size);
    memcpy(cb->cpu_buffer + c->offset + c->arr_stride*idx, sdata, s);
}

void gfx_cb_setpv(struct gfx_cblock* cb, uint32 name_hash, const void* sdata, uint32 size, 
    uint32 cnt)
{
    const struct gfx_constant_desc* c = shader_find_constantcb(cb, name_hash);
    ASSERT(c);
    ASSERT(c->type == GFX_CONSTANT_STRUCT);

    uint32 s = minun(size, c->elem_size);
    cnt = minun(cnt, c->arr_size);
    for (uint32 i = 0; i < cnt; i++)
        memcpy(cb->cpu_buffer + c->offset + c->arr_stride*i, (const uint8*)sdata + i*size, s);

}

struct gfx_shader* gfx_shader_get(uint32 shader_id)
{
    ASSERT(shader_id <= g_shader_mgr.shaders.item_cnt && shader_id != 0);
    return ((struct gfx_shader**)g_shader_mgr.shaders.buffer)[shader_id-1];
}

void* shader_includesource(struct allocator* alloc, const char* include_code, void* src, 
    uint32 src_size, OUT uint32* outsize)
{
    ASSERT(include_code);
    uint32 s;
    void* buffer;

    if (include_code[0] != 0)   {
        uint32 incl_size = strlen(include_code);

        s =  incl_size + src_size;
        buffer = A_ALLOC(alloc, s, MID_GFX);
        if (buffer != NULL) {
            memcpy(buffer, include_code, incl_size);
            memcpy((uint8*)buffer + incl_size, src, src_size);
            A_FREE(alloc, src);
        }       
    }   else    {
        buffer = src;
        s = src_size;
    }

    *outsize = s;
    return buffer;
}

/* tbuffers are special kind of cblocks where memory resides in global texture space 
 * we limit the structures to one variable (can be array) only with a fixed given name and size
 * so the implementation is same in d3d/gl because there is no need to query variables from shader
 */
struct gfx_cblock* gfx_shader_create_cblock_tbuffer(struct allocator* alloc, 
    struct gfx_shader* shader, const char* block_name, uint32 tbuff_item_cnt, 
    const char* tbuff_membername, uint32 tbuff_membersize)
{
    struct gfx_cblock* cb = (struct gfx_cblock*)A_ALLOC(alloc, sizeof(struct gfx_cblock), MID_GFX);
    if (cb == NULL) {
        err_printn(__FILE__, __LINE__, RET_OUTOFMEMORY);
        return NULL;
    }
    memset(cb, 0x0, sizeof(struct gfx_cblock));
    cb->alloc = alloc;

    /* name hash */
    cb->name_hash = hashtable_str(block_name);

    /* constants (fixed) */
    cb->constants = A_ALLOC(cb->alloc, sizeof(struct gfx_constant_desc), MID_GFX);
    cb->constant_cnt = 1;
    ASSERT(cb->constants);

    memset(cb->constants, 0x00, sizeof(struct gfx_constant_desc));
    str_safecpy(cb->constants[0].name, sizeof(cb->constants[0].name), tbuff_membername);
    cb->constants[0].type = GFX_CONSTANT_STRUCT;
    cb->constants[0].arr_size = tbuff_item_cnt;
    cb->constants[0].arr_stride = tbuff_membersize;
    cb->constants[0].elem_size = tbuff_membersize;
    cb->constants[0].offset = 0;

    /* push constants into ctable */
    result_t r = hashtable_fixed_create(alloc, &cb->ctable, 1, MID_GFX);
    if (IS_FAIL(r))	{
        gfx_shader_destroy_cblock(cb);
        err_printn(__FILE__, __LINE__, RET_OUTOFMEMORY);
        return NULL;
    }
    hashtable_fixed_add(&cb->ctable, hashtable_str(cb->constants[0].name), 0);

    /* buffers (gpu/cpu) */
    uint32 tbuff_size = tbuff_item_cnt * tbuff_membersize;
    cb->cpu_buffer = A_ALLOC(cb->alloc, tbuff_size, MID_GFX);
    cb->gpu_buffer = gfx_create_buffer(GFX_BUFFER_SHADERTEXTURE, GFX_MEMHINT_DYNAMIC, 
        tbuff_size, NULL);
    if (cb->cpu_buffer == NULL || cb->gpu_buffer == NULL)		{
        gfx_shader_destroy_cblock(cb);
        err_printn(__FILE__, __LINE__, RET_OUTOFMEMORY);
        return NULL;
    }
    cb->buffer_size = tbuff_size;
    memset(cb->cpu_buffer, 0x00, tbuff_size);

    return cb;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.