-
Notifications
You must be signed in to change notification settings - Fork 2
/
texture.cpp
705 lines (538 loc) · 19.3 KB
/
texture.cpp
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
#include "texture.hpp"
#include "clstate.h"
#include <iostream>
#include <math.h>
//#include "texture_manager.hpp"
#include "engine.hpp"
#include "texture_context.hpp"
std::string col2cachename(const vec4f& col)
{
return std::string("CACHE") + std::to_string(col.x()) + std::to_string(col.y()) + std::to_string(col.z()) + std::to_string(col.w());
}
template<typename T>
static std::string to_str(T i)
{
std::ostringstream convert;
convert << i;
return convert.str();
}
texture::texture()
{
std::function<void (texture*)> func;
func = std::bind(texture_load, std::placeholders::_1);
is_active = false;
is_loaded = false;
is_unique = false;
force_load = false;
has_mipmaps = false;
cacheable = true;
set_load_func(func);
id = -1;
type = 0;
gpu_id = 0;
}
cl_uint texture::get_largest_dimension() const
{
if(!is_loaded)
{
lg::log("tried to find dimension of non loaded texture");
return 1;
//exit(32323);
}
int larg = c_image.getSize().x > c_image.getSize().y ? c_image.getSize().x : c_image.getSize().y;
int next_up = pow(2, ceilf(log2(larg)));
//printf("%i l %i nu\n", larg, next_up);
return next_up;
}
cl_uint texture::get_largest_num(int num) const
{
if(num == 0)
return get_largest_dimension();
if(!has_mipmaps)
{
lg::log("fatal error, mipmaps not created");
exit(3434);
}
else
return mipmaps[num-1].getSize().x;
}
sf::Image& texture::get_texture_level(int num)
{
if(num == 0)
return c_image;
if(!has_mipmaps)
{
lg::log("fatal error, mipmaps not created for texture level");
exit(3434);
}
else
return mipmaps[num-1];
}
cl_uint texture::get_active_id()
{
lg::log("err, get_active_id is deprecated");
/*for(int i=0; i<texture_manager::active_textures.size(); i++)
{
if(texture_manager::texture_by_id(texture_manager::active_textures[i])->id == id)
{
return i;
}
}
std::cout << "warning, could not find texture in active textures" << std::endl;*/
return -1;
}
void texture::activate()
{
lg::log("warning, activating a texture is deprecated");
//texture_manager::activate_texture(id);
}
void texture::inactivate()
{
//texture_manager::inactivate_texture(id);
}
void texture::set_unique()
{
is_unique = true;
//cacheable = false;
}
void texture::set_texture_location(const std::string& loc)
{
texture_location = loc;
}
void texture::set_location(const std::string& loc)
{
texture_location = loc;
}
void texture::set_cache_name(const std::string& name)
{
cache_name = name;
}
void texture::set_create_colour(sf::Color col, int w, int h)
{
set_load_func(std::bind(texture_make_blank, std::placeholders::_1, w, h, col));
}
void texture::set_load_from_other_texture(texture* tex)
{
if(tex == nullptr)
{
lg::log("RUH ROH, BAD TEXTURE FOUND IN SET LOAD FROM OTHER TEXTURE");
return;
}
set_load_func(std::bind(texture_load_from_other, std::placeholders::_1, tex));
}
bool texture::exists()
{
throw std::runtime_error("exists is deprecated\n");
//return texture_manager::exists_by_location(texture_location);
}
bool exists_by_id(texture* tex)
{
throw std::runtime_error("exists by idis deprecated\n");
//return texture_manager::texture_by_id(tex->id) != nullptr;
}
void texture::push()
{
lg::log("warning, pushing is now an error");
throw std::runtime_error("pushed texture\n");
/*if(!exists() || is_unique)
{
id = texture_manager::add_texture(*this);
}
else
{
id = texture_manager::texture_id_by_location(texture_location);
texture* tex = texture_manager::texture_by_id(id);
if(id == -1 || tex)
{
if(id == -1 || tex->is_unique)
{
id = texture_manager::add_texture(*this);
}
}
}*/
}
void texture::load()
{
fp(this);
}
void texture::unload()
{
is_loaded = false;
}
///create mipmap from level, where 0 = base texture, 1 = first level etc up to 4
void gen_miplevel(texture& tex, int level)
{
int size = tex.get_largest_num(level);
int newsize = size >> 1;
const sf::Image& base = level == 0 ? tex.c_image : tex.mipmaps[level-1];
sf::Image& mip = tex.mipmaps[level];
mip.create(newsize, newsize);
const float gauss[3][3] = {{1, 2, 1}, {2, 4, 2}, {1, 2, 1}};
for(int j=0; j<newsize; j++)
{
for(int i=0; i<newsize; i++)
{
/*sf::Color p4[4];
p4[0]=base.getPixel(i*2, j*2);
p4[1]=base.getPixel(i*2+1, j*2);
p4[2]=base.getPixel(i*2, j*2+1);
p4[3]=base.getPixel(i*2+1, j*2+1);*/
float xa = 0, ya = 0, za = 0, wa = 0;
int num = 0;
for(int y=-1; y<2; y++)
for(int x=-1; x<2; x++)
{
if(x + i*2 < 0 || x + i*2 >= size || y + j*2 < 0 || y + j*2 >= size)
continue;
sf::Color c = base.getPixel(i*2 + x, j*2 + y);
xa += c.r * gauss[y+1][x+1];
ya += c.g * gauss[y+1][x+1];
za += c.b * gauss[y+1][x+1];
wa += c.a * gauss[y+1][x+1];
num += gauss[y+1][x+1];
}
xa /= num;
ya /= num;
za /= num;
wa /= num;
/*sf::Color ret;
ret.r=(p4[0].r + p4[1].r + p4[2].r + p4[3].r)/4.0f;
ret.g=(p4[0].g + p4[1].g + p4[2].g + p4[3].g)/4.0f;
ret.b=(p4[0].b + p4[1].b + p4[2].b + p4[3].b)/4.0f;*/
sf::Color ret(xa, ya, za, wa);
mip.setPixel(i, j, ret);
}
}
}
///generate mipmaps if necessary
void texture::generate_mipmaps()
{
lg::log("generating cpu side mipmaps is deprecated");
if(!has_mipmaps)
{
has_mipmaps = true;
for(int i=0; i<MIP_LEVELS; i++)
{
std::string mip_loc = texture_location + to_str(i) + ".png";
FILE* pFile = fopen(mip_loc.c_str(), "r");
if(pFile != nullptr)
fclose(pFile);
///file does not exist, generate and cache
if(pFile == nullptr || !cacheable)
{
lg::log("generated mipmap");
gen_miplevel(*this, i);
const sf::Image& img = mipmaps[i];
if(cacheable)
img.saveToFile(mip_loc);
}
else
{
lg::log("loaded cached mipmap");
sf::Image& img = mipmaps[i];
img.loadFromFile(mip_loc);
}
}
}
}
void async_cleanup(cl_event event, cl_int event_command_exec_status, void* user_data)
{
if(event_command_exec_status == CL_COMPLETE)
{
delete ((sf::Texture*)user_data);
lg::log("Async cleanup texture texture.cpp");
}
}
void texture::update_me_to_gpu(texture_context_data& gpu_dat, compute::command_queue cqueue)
{
///swap this for a clenqueuewrite
///itll be significantly more efficient
///will also likely fix the issues that I'm seeing with the swordfight man
///as well as removing gl interop overhead
///win/win!
/*sf::Texture* tex = new sf::Texture();
tex->loadFromImage(c_image);
///?
auto event = update_gpu_texture(*tex, gpu_dat, true, cqueue);
update_gpu_mipmaps(gpu_dat, cqueue);
///this is to avoid what is potentially a bug in the amd driver relating to opengl shared object lifetimes
clSetEventCallback(event.get(), CL_COMPLETE, async_cleanup, tex);*/
compute::image_format format_opengl(CL_RGBA, CL_UNORM_INT8);
compute::image2d buf = compute::image2d(cl::context, CL_MEM_READ_ONLY, format_opengl, c_image.getSize().x, c_image.getSize().y, 0, nullptr);
void* write_buf = (void*)c_image.getPixelsPtr();
size_t origin[3] = {0,0,0};
size_t region[3] = {c_image.getSize().x, c_image.getSize().y, 1};
cl_event no_gl_write;
clEnqueueWriteImage(cqueue.get(), buf.get(), CL_FALSE, origin, region, 0, 0, write_buf, 0, nullptr, &no_gl_write);
no_gl_write_event = compute::event(no_gl_write);
has_event = true;
auto event = update_internal(buf.get(), gpu_dat, true, cqueue, false, {no_gl_write_event});
update_gpu_mipmaps(gpu_dat, cqueue);
}
compute::event texture::update_internal(cl_mem mem, texture_context_data& gpu_dat, cl_int flip, compute::command_queue cqueue, bool acquire, std::vector<compute::event> events)
{
cl_int err = CL_SUCCESS;
if(acquire)
err = clEnqueueAcquireGLObjects(cqueue.get(), 1, &mem, 0, nullptr, nullptr);
if(err != CL_SUCCESS)
{
lg::log("Error acquiring gl objects in update_internal (texture.cpp)", err);
throw std::runtime_error("Error acquire/release");
}
//printf("gpu %i %i\n", gpu_id & 0x0000FFFF, (gpu_id >> 16) & 0x0000FFFF);
arg_list args;
args.push_back(&mem);
args.push_back(&gpu_id); ///what's my gpu id?
args.push_back(&gpu_dat.mipmap_start); ///what's my gpu id?
args.push_back(&gpu_dat.g_texture_nums);
args.push_back(&gpu_dat.g_texture_sizes);
args.push_back(&gpu_dat.g_texture_array);
args.push_back(&flip);
compute::event ev1;
ev1 = run_kernel_with_string("update_gpu_tex", {(int)c_image.getSize().x, (int)c_image.getSize().y}, {16, 16}, 2, args, cqueue, events);
cl_event clevent;
if(acquire)
clEnqueueReleaseGLObjects(cqueue.get(), 1, &mem, 0, nullptr, &clevent);
if(acquire)
return compute::event(clevent);
else
return ev1;
}
compute::event texture::update_gpu_texture(const sf::Texture& tex, texture_context_data& gpu_dat, cl_int flip, compute::command_queue cqueue)
{
if(id == -1)
return compute::event();
GLint opengl_id;
/**sf::Texture::bind( &tex );
glGetIntegerv( GL_TEXTURE_BINDING_2D, &opengl_id );
sf::Texture::bind(nullptr);*/
///WHY DIDN'T I KNOW ABOUT THIS BEFORE
///equivalent to the above
opengl_id = tex.getNativeHandle();
cl_int err;
cl_mem gl_mem = clCreateFromGLTexture2D(cl::context.get(), CL_MEM_READ_ONLY,
GL_TEXTURE_2D, 0, (GLuint)opengl_id, &err);
if(err != CL_SUCCESS)
{
lg::log("Error in clcreatefromgltexture2d in update_gpu_texture ", err);
throw std::runtime_error("why");
}
auto event = update_internal(gl_mem, gpu_dat, flip, cqueue, true);
clReleaseMemObject(gl_mem);
return event;
}
compute::event texture::update_gpu_texture_nogl(compute::image2d buf, texture_context_data& gpu_dat, cl_int flip, compute::command_queue cqueue, std::vector<compute::event> events)
{
if(!is_loaded)
{
lg::log("Tried to write unloaded texture to gpu in update_gpu_texture_nogl");
return compute::event();
}
return update_internal(buf.get(), gpu_dat, flip, cqueue, false, events);
}
void texture::update_gpu_texture_col(cl_float4 col, texture_context_data& gpu_dat)
{
if(id == -1)
return;
arg_list args;
args.push_back(&col);
args.push_back(&gpu_id); ///what's my gpu id?
args.push_back(&gpu_dat.mipmap_start); ///what's my gpu id?
args.push_back(&gpu_dat.g_texture_nums);
args.push_back(&gpu_dat.g_texture_sizes);
args.push_back(&gpu_dat.g_texture_array);
//printf("%i %i\n", c_image.getSize().x, c_image.getSize().y);
//printf("gpuid %i\n", gpu_id);
run_kernel_with_string("update_gpu_tex_colour", {(int)c_image.getSize().x, (int)c_image.getSize().y}, {16, 16}, 2, args);
}
void texture::update_gpu_mipmaps(texture_context_data& gpu_dat, compute::command_queue cqueue)
{
arg_list margs;
margs.push_back(&gpu_id);
margs.push_back(&gpu_dat.mipmap_start);
margs.push_back(&gpu_dat.g_texture_nums);
margs.push_back(&gpu_dat.g_texture_sizes);
margs.push_back(&gpu_dat.g_texture_array);
margs.push_back(&gpu_dat.g_texture_array);
run_kernel_with_string("generate_mips", {(int)c_image.getSize().x, (int)c_image.getSize().y}, {16, 16}, 2, margs, cqueue);
for(int i=0; i<MIP_LEVELS-1; i++)
{
cl_uint mip = i;
arg_list rargs;
///make gpu id part of texture context?
rargs.push_back(&gpu_id);
rargs.push_back(&mip);
rargs.push_back(&gpu_dat.mipmap_start);
rargs.push_back(&gpu_dat.g_texture_nums);
rargs.push_back(&gpu_dat.g_texture_sizes);
rargs.push_back(&gpu_dat.g_texture_array);
rargs.push_back(&gpu_dat.g_texture_array);
run_kernel_with_string("generate_mip_mips", {(int)c_image.getSize().x, (int)c_image.getSize().y}, {16, 16}, 2, rargs, cqueue);
}
}
void texture::update_gpu_mipmaps_aggressive(texture_context_data& gpu_dat, compute::command_queue cqueue)
{
arg_list margs;
margs.push_back(&gpu_id);
margs.push_back(&gpu_dat.mipmap_start);
margs.push_back(&gpu_dat.g_texture_nums);
margs.push_back(&gpu_dat.g_texture_sizes);
margs.push_back(&gpu_dat.g_texture_array);
margs.push_back(&gpu_dat.g_texture_array);
run_kernel_with_string("generate_mips_aggressive", {(int)c_image.getSize().x, (int)c_image.getSize().y}, {16, 16}, 2, margs, cqueue);
for(int i=0; i<MIP_LEVELS-1; i++)
{
cl_uint mip = i;
arg_list rargs;
///make gpu id part of texture context?
rargs.push_back(&gpu_id);
rargs.push_back(&mip);
rargs.push_back(&gpu_dat.mipmap_start);
rargs.push_back(&gpu_dat.g_texture_nums);
rargs.push_back(&gpu_dat.g_texture_sizes);
rargs.push_back(&gpu_dat.g_texture_array);
rargs.push_back(&gpu_dat.g_texture_array);
run_kernel_with_string("generate_mip_mips_aggressive", {(int)c_image.getSize().x, (int)c_image.getSize().y}, {16, 16}, 2, rargs, cqueue);
}
}
void texture::update_random_lines(cl_int num, cl_float4 col, cl_float2 pos, cl_float2 dir, texture_context_data& gpu_dat)
{
if(id == -1)
return;
arg_list args;
args.push_back(&num);
args.push_back(&pos);
args.push_back(&dir);
args.push_back(&col);
args.push_back(&gpu_id); ///what's my gpu id?
args.push_back(&gpu_dat.mipmap_start); ///what's my gpu id?
args.push_back(&gpu_dat.g_texture_nums);
args.push_back(&gpu_dat.g_texture_sizes);
args.push_back(&gpu_dat.g_texture_array);
run_kernel_with_string("procedural_crack", {num}, {128}, 1, args);
}
void async_cleanup_mono(cl_event event, cl_int event_command_exec_status, void* user_data)
{
if(event_command_exec_status == CL_COMPLETE)
{
free((uint8_t*)user_data);
//lg::log("Async cleanup texture texture.cpp");
}
}
void texture::update_gpu_texture_mono(texture_context_data& gpu_dat, uint8_t* buffer_dat, uint32_t len, int width, int height, bool flip)
{
cl_event event;
compute::buffer temporary_gpu_dat = compute::buffer(cl::context, len, CL_MEM_READ_WRITE, nullptr);
clEnqueueWriteBuffer(cl::cqueue.get(), temporary_gpu_dat.get(), CL_FALSE, 0, len, buffer_dat, 0, nullptr, &event);
clSetEventCallback(event, CL_COMPLETE, async_cleanup_mono, buffer_dat);
cl_int stride = len / height;
cl_int cflip = flip;
cl_int2 dim = {width, height};
arg_list args;
args.push_back(&temporary_gpu_dat);
args.push_back(&stride);
args.push_back(&dim);
args.push_back(&gpu_id);
args.push_back(&gpu_dat.g_texture_nums);
args.push_back(&gpu_dat.g_texture_sizes);
args.push_back(&gpu_dat.g_texture_array);
args.push_back(&cflip);
compute::event ev(event, false);
run_kernel_with_string("generate_from_raw", {width, height}, {16, 16}, 2, args, cl::cqueue, {ev});
//update_gpu_mipmaps(gpu_dat, cl::cqueue);
//clReleaseEvent(event);
}
void texture::update_gpu_texture_threshold_split(texture_context_data& gpu_dat, cl_float4 threshold, cl_float4 replacement, cl_int side, cl_float2 pos, float angle, float scale)
{
cl_int2 dim = {c_image.getSize().x, c_image.getSize().y};
/*pos.s[0] += scale * dim.s[0]/2.f;
pos.s[1] += scale * dim.s[1]/2.f;
pos.s[0] /= scale;
pos.s[1] /= scale;
pos.s[0] = dim.s[0] - pos.s[0];
pos.s[1] = dim.s[1] - pos.s[1];
pos.s[0] /= 200.f;
pos.s[1] /= 200.f;
pos.s[0] *= dim.s[0];
pos.s[1] *= dim.s[1];*/
float fside = side == 0 ? 1 : -1;
pos.s[0] -= 40 * fside;
pos.s[0] /= scale;
pos.s[1] /= scale;
pos.s[0] += 100;
pos.s[1] += 100;
pos.s[0] /= 200;
pos.s[1] /= 200;
pos.s[0] *= dim.s[0];
pos.s[1] *= dim.s[1];
pos.s[0] = dim.s[0] - pos.s[0];
arg_list args;
args.push_back(&gpu_id);
args.push_back(&gpu_dat.g_texture_nums);
args.push_back(&gpu_dat.g_texture_sizes);
args.push_back(&gpu_dat.g_texture_array);
args.push_back(&gpu_dat.g_texture_array);
args.push_back(&threshold);
args.push_back(&replacement);
args.push_back(&side);
//args.push_back(&dim);
args.push_back(&pos);
args.push_back(&angle);
run_kernel_with_string("texture_threshold_split", {c_image.getSize().x, c_image.getSize().y}, {16, 16}, 2, args, cl::cqueue);
}
void texture_load(texture* tex)
{
tex->c_image.loadFromFile(tex->texture_location);
tex->is_loaded = true;
if(tex->get_largest_dimension() > max_tex_size)
{
lg::log("Error, texture larger than max texture size @", __LINE__, " @", __FILE__);
///error? set isloaded to false? return bad id or throw?
}
}
void texture_make_blank(texture* tex, int w, int h, sf::Color col)
{
tex->c_image.create(w, h, col);
tex->is_loaded = true;
if(tex->get_largest_dimension() > max_tex_size)
{
lg::log("Error, texture larger than max texture size @", __LINE__, " @", __FILE__);
///error? set isloaded to false? return bad id or throw?
}
}
void texture_load_from_image(texture* tex, sf::Image& img)
{
tex->c_image = img;
tex->is_loaded = true;
if(tex->get_largest_dimension() > max_tex_size)
{
lg::log("Error, texture larger than max texture size @", __LINE__, " @", __FILE__);
///error? set isloaded to false? return bad id or throw?
}
}
void texture_load_from_other(texture* tex, texture* other)
{
if(!other->is_loaded)
{
lg::log("Other texture is not loaded @", __LINE__, " @", __FILE__);
return;
}
tex->c_image = other->c_image;
tex->is_loaded = true;
if(tex->get_largest_dimension() > max_tex_size)
{
lg::log("Error, texture larger than max texture size @", __LINE__, " @", __FILE__);
///error? set isloaded to false? return bad id or throw?
}
}
void texture::set_load_func(std::function<void (texture*)> func)
{
fp = func;
}
texture::~texture()
{
if(has_event)
{
clWaitForEvents(1, &no_gl_write_event.get());
}
}