FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SFML/src/SFML/Graphics/Texture.cpp at master · githubhy/SFML · GitHub
githubhy
/
SFML
Public
forked from
SFML/SFML
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SFML
/
src
/
SFML
/
Graphics
/
Texture.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
995 lines (797 loc) · 33.1 KB
Breadcrumbs
SFML
/
src
/
SFML
/
Graphics
/
Texture.cpp
Copy path
File metadata and controls
995 lines (797 loc) · 33.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
//////////////////////////////////////////////////////////
//
//
SFML - Simple and Fast Multimedia Library
//
Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org)
//
//
This software is provided 'as-is', without any express or implied warranty.
//
In no event will the authors be held liable for any damages arising from the use of this software.
//
//
Permission is granted to anyone to use this software for any purpose,
//
including commercial applications, and to alter it and redistribute it freely,
//
subject to the following restrictions:
//
//
1. The origin of this software must not be misrepresented;
//
you must not claim that you wrote the original software.
//
If you use this software in a product, an acknowledgment
//
in the product documentation would be appreciated but is not required.
//
//
2. Altered source versions must be plainly marked as such,
//
and must not be misrepresented as being the original software.
//
//
3. This notice may not be removed or altered from any source distribution.
//
//
//////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////
//
Headers
//
//////////////////////////////////////////////////////////
#
include
<
SFML/Graphics/GLCheck.hpp
>
#
include
<
SFML/Graphics/GLExtensions.hpp
>
#
include
<
SFML/Graphics/Image.hpp
>
#
include
<
SFML/Graphics/Texture.hpp
>
#
include
<
SFML/Graphics/TextureSaver.hpp
>
#
include
<
SFML/Window/Context.hpp
>
#
include
<
SFML/Window/Window.hpp
>
#
include
<
SFML/System/Err.hpp
>
#
include
<
algorithm
>
#
include
<
array
>
#
include
<
atomic
>
#
include
<
ostream
>
#
include
<
utility
>
#
include
<
cassert
>
#
include
<
cstring
>
namespace
{
//
A nested named namespace is used here to allow unity builds of SFML.
namespace
TextureImpl
{
//
Thread-safe unique identifier generator,
//
is used for states cache (see RenderTarget)
std::
uint64_t
getUniqueId
()
noexcept
{
static
std::atomic<std::
uint64_t
>
id
(
1
);
//
start at 1, zero is "no texture"
return
id.
fetch_add
(
1
);
}
}
//
namespace TextureImpl
}
//
namespace
namespace
sf
{
//
//////////////////////////////////////////////////////////
Texture::Texture
(
const
Vector2u& size,
const
Vector2u& actualSize,
unsigned
int
texture,
bool
sRgb
) :
m_size
(size),
m_actualSize
(actualSize),
m_texture
(texture),
m_sRgb
(
sRgb
),
m_cacheId
(TextureImpl::getUniqueId())
{
}
//
//////////////////////////////////////////////////////////
Texture::Texture
(
const
Texture& copy) :
GlResource
(copy),
m_isSmooth
(copy.m_isSmooth),
m_sRgb
(copy.m_sRgb),
m_isRepeated
(copy.m_isRepeated),
m_cacheId
(TextureImpl::getUniqueId())
{
if
(
auto
texture =
create
(copy.
getSize
(), copy.
isSrgb
()))
{
*
this
=
std::move
(*texture);
update
(copy);
}
else
{
err
() <<
"
Failed to copy texture, failed to create new texture
"
<< std::endl;
}
}
//
//////////////////////////////////////////////////////////
Texture::~Texture
()
{
//
Destroy the OpenGL texture
if
(m_texture)
{
const
TransientContextLock lock;
const
GLuint texture = m_texture;
glCheck
(
glDeleteTextures
(
1
, &texture));
}
}
//
//////////////////////////////////////////////////////////
Texture::Texture
(Texture&& right)
noexcept
:
m_size
(std::exchange(right.m_size, {})),
m_actualSize
(std::exchange(right.m_actualSize, {})),
m_texture
(std::exchange(right.m_texture,
0
)),
m_isSmooth
(std::exchange(right.m_isSmooth,
false
)),
m_sRgb
(std::exchange(right.m_sRgb,
false
)),
m_isRepeated
(std::exchange(right.m_isRepeated,
false
)),
m_pixelsFlipped
(std::exchange(right.m_pixelsFlipped,
false
)),
m_fboAttachment
(std::exchange(right.m_fboAttachment,
false
)),
m_hasMipmap
(std::exchange(right.m_hasMipmap,
false
)),
m_cacheId
(std::exchange(right.m_cacheId,
0
))
{
}
//
//////////////////////////////////////////////////////////
Texture& Texture::
operator
=(Texture&& right)
noexcept
{
//
Catch self-moving.
if
(&right ==
this
)
{
return
*
this
;
}
//
Destroy the OpenGL texture
if
(m_texture)
{
const
TransientContextLock lock;
const
GLuint texture = m_texture;
glCheck
(
glDeleteTextures
(
1
, &texture));
}
//
Move old to new.
m_size =
std::exchange
(right.
m_size
, {});
m_actualSize =
std::exchange
(right.
m_actualSize
, {});
m_texture =
std::exchange
(right.
m_texture
,
0
);
m_isSmooth =
std::exchange
(right.
m_isSmooth
,
false
);
m_sRgb =
std::exchange
(right.
m_sRgb
,
false
);
m_isRepeated =
std::exchange
(right.
m_isRepeated
,
false
);
m_pixelsFlipped =
std::exchange
(right.
m_pixelsFlipped
,
false
);
m_fboAttachment =
std::exchange
(right.
m_fboAttachment
,
false
);
m_hasMipmap =
std::exchange
(right.
m_hasMipmap
,
false
);
m_cacheId =
std::exchange
(right.
m_cacheId
,
0
);
return
*
this
;
}
//
//////////////////////////////////////////////////////////
std::optional<Texture>
Texture::create
(
const
Vector2u& size,
bool
sRgb
)
{
//
Check if texture parameters are valid before creating it
if
((size.
x
==
0
) || (size.
y
==
0
))
{
err
() <<
"
Failed to create texture, invalid size (
"
<< size.
x
<<
"
x
"
<< size.
y
<<
"
)
"
<< std::endl;
return
std::
nullopt
;
}
const
TransientContextLock lock;
//
Make sure that extensions are initialized
priv::ensureExtensionsInit
();
//
Compute the internal texture dimensions depending on NPOT textures support
const
Vector2u
actualSize
(
getValidSize
(size.
x
),
getValidSize
(size.
y
));
//
Check the maximum texture size
const
unsigned
int
maxSize =
getMaximumSize
();
if
((actualSize.
x
> maxSize) || (actualSize.
y
> maxSize))
{
err
() <<
"
Failed to create texture, its internal size is too high
"
<<
"
(
"
<< actualSize.
x
<<
"
x
"
<< actualSize.
y
<<
"
,
"
<<
"
maximum is
"
<< maxSize <<
"
x
"
<< maxSize <<
"
)
"
<< std::endl;
return
std::
nullopt
;
}
//
Create the OpenGL texture
GLuint glTexture =
0
;
glCheck
(
glGenTextures
(
1
, &glTexture));
assert
(glTexture);
//
All the validity checks passed, we can store the new texture settings
Texture
texture
(size, actualSize, glTexture,
sRgb
);
//
Make sure that the current texture binding will be preserved
const
priv::TextureSaver save;
static
const
bool
textureEdgeClamp = GLEXT_texture_edge_clamp ||
GLEXT_GL_VERSION_1_2
||
Context::isExtensionAvailable
(
"
GL_EXT_texture_edge_clamp
"
);
if
(!textureEdgeClamp)
{
static
bool
warned =
false
;
if
(!warned)
{
err
() <<
"
OpenGL extension SGIS_texture_edge_clamp unavailable
"
<<
'
\n
'
<<
"
Artifacts may occur along texture edges
"
<<
'
\n
'
<<
"
Ensure that hardware acceleration is enabled if available
"
<< std::endl;
warned =
true
;
}
}
static
const
bool
textureSrgb = GLEXT_texture_sRGB;
if
(texture.
m_sRgb
&& !textureSrgb)
{
static
bool
warned =
false
;
if
(!warned)
{
#
ifndef
SFML_OPENGL_ES
err
() <<
"
OpenGL extension EXT_texture_sRGB unavailable
"
<<
'
\n
'
;
#
else
err
() <<
"
OpenGL ES extension EXT_sRGB unavailable
"
<<
'
\n
'
;
#
endif
err
() <<
"
Automatic sRGB to linear conversion disabled
"
<< std::endl;
warned =
true
;
}
texture.
m_sRgb
=
false
;
}
#
ifndef
SFML_OPENGL_ES
const
GLint textureWrapParam = textureEdgeClamp ?
GLEXT_GL_CLAMP_TO_EDGE
:
GLEXT_GL_CLAMP
;
#
else
const
GLint textureWrapParam =
GLEXT_GL_CLAMP_TO_EDGE
;
#
endif
//
Initialize the texture
glCheck
(
glBindTexture
(
GL_TEXTURE_2D
, texture.
m_texture
));
glCheck
(
glTexImage2D
(
GL_TEXTURE_2D
,
0
,
(texture.
m_sRgb
?
GLEXT_GL_SRGB8_ALPHA8
:
GL_RGBA
),
static_cast
<GLsizei>(texture.
m_actualSize
.
x
),
static_cast
<GLsizei>(texture.
m_actualSize
.
y
),
0
,
GL_RGBA
,
GL_UNSIGNED_BYTE
,
nullptr
));
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_WRAP_S
, textureWrapParam));
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_WRAP_T
, textureWrapParam));
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MAG_FILTER
,
GL_NEAREST
));
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MIN_FILTER
,
GL_NEAREST
));
texture.
m_cacheId
=
TextureImpl::getUniqueId
();
texture.
m_hasMipmap
=
false
;
return
texture;
}
//
//////////////////////////////////////////////////////////
std::optional<Texture>
Texture::loadFromFile
(
const
std::filesystem::path& filename,
bool
sRgb
,
const
IntRect& area)
{
if
(
const
auto
image =
sf::Image::loadFromFile
(filename))
return
loadFromImage
(*image,
sRgb
, area);
err
() <<
"
Failed to load texture from file
"
<< std::endl;
return
std::
nullopt
;
}
//
//////////////////////////////////////////////////////////
std::optional<Texture>
Texture::loadFromMemory
(
const
void
* data, std::
size_t
size,
bool
sRgb
,
const
IntRect& area)
{
if
(
const
auto
image =
sf::Image::loadFromMemory
(data, size))
return
loadFromImage
(*image,
sRgb
, area);
err
() <<
"
Failed to load texture from memory
"
<< std::endl;
return
std::
nullopt
;
}
//
//////////////////////////////////////////////////////////
std::optional<Texture>
Texture::loadFromStream
(InputStream& stream,
bool
sRgb
,
const
IntRect& area)
{
if
(
const
auto
image =
sf::Image::loadFromStream
(stream))
return
loadFromImage
(*image,
sRgb
, area);
err
() <<
"
Failed to load texture from stream
"
<< std::endl;
return
std::
nullopt
;
}
//
//////////////////////////////////////////////////////////
std::optional<Texture>
Texture::loadFromImage
(
const
Image& image,
bool
sRgb
,
const
IntRect& area)
{
//
Retrieve the image size
const
auto
size =
Vector2i
(image.
getSize
());
//
Load the entire image if the source area is either empty or contains the whole image
if
(area.
size
.
x
==
0
|| (area.
size
.
y
==
0
) ||
((area.
position
.
x
<=
0
) && (area.
position
.
y
<=
0
) && (area.
size
.
x
>= size.
x
) && (area.
size
.
y
>= size.
y
)))
{
//
Load the entire image
if
(
auto
texture =
sf::Texture::create
(image.
getSize
(),
sRgb
))
{
texture->
update
(image);
return
texture;
}
else
{
//
Error message generated in called function.
return
std::
nullopt
;
}
}
else
{
//
Load a sub-area of the image
//
Adjust the rectangle to the size of the image
IntRect rectangle = area;
rectangle.
position
.
x
=
std::max
(rectangle.
position
.
x
,
0
);
rectangle.
position
.
y
=
std::max
(rectangle.
position
.
y
,
0
);
rectangle.
size
.
x
=
std::min
(rectangle.
size
.
x
, size.
x
- rectangle.
position
.
x
);
rectangle.
size
.
y
=
std::min
(rectangle.
size
.
y
, size.
y
- rectangle.
position
.
y
);
//
Create the texture and upload the pixels
if
(
auto
texture =
sf::Texture::create
(
Vector2u
(rectangle.
size
),
sRgb
))
{
const
TransientContextLock lock;
//
Make sure that the current texture binding will be preserved
const
priv::TextureSaver save;
//
Copy the pixels to the texture, row by row
const
std::
uint8_t
* pixels = image.
getPixelsPtr
() +
4
* (rectangle.
position
.
x
+ (size.
x
* rectangle.
position
.
y
));
glCheck
(
glBindTexture
(
GL_TEXTURE_2D
, texture->
m_texture
));
for
(
int
i =
0
; i < rectangle.
size
.
y
; ++i)
{
glCheck
(
glTexSubImage2D
(
GL_TEXTURE_2D
,
0
,
0
, i, rectangle.
size
.
x
,
1
,
GL_RGBA
,
GL_UNSIGNED_BYTE
, pixels));
pixels +=
4
* size.
x
;
}
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MIN_FILTER
,
GL_NEAREST
));
texture->
m_hasMipmap
=
false
;
//
Force an OpenGL flush, so that the texture will appear updated
//
in all contexts immediately (solves problems in multi-threaded apps)
glCheck
(
glFlush
());
return
texture;
}
else
{
//
Error message generated in called function.
return
std::
nullopt
;
}
}
}
//
//////////////////////////////////////////////////////////
Vector2u
Texture::getSize
()
const
{
return
m_size;
}
//
//////////////////////////////////////////////////////////
Image
Texture::copyToImage
()
const
{
//
Easy case: empty texture
assert
(m_texture &&
"
Texture::copyToImage Cannot copy empty texture to image
"
);
const
TransientContextLock lock;
//
Make sure that the current texture binding will be preserved
const
priv::TextureSaver save;
//
Create an array of pixels
std::vector<std::
uint8_t
>
pixels
(m_size.
x
* m_size.
y
*
4
);
#
ifdef
SFML_OPENGL_ES
//
OpenGL ES doesn't have the glGetTexImage function, the only way to read
//
from a texture is to bind it to a FBO and use glReadPixels
GLuint frameBuffer =
0
;
glCheck
(
GLEXT_glGenFramebuffers
(
1
, &frameBuffer));
if
(frameBuffer)
{
GLint previousFrameBuffer =
0
;
glCheck
(
glGetIntegerv
(
GLEXT_GL_FRAMEBUFFER_BINDING
, &previousFrameBuffer));
glCheck
(
GLEXT_glBindFramebuffer
(
GLEXT_GL_FRAMEBUFFER
, frameBuffer));
glCheck
(
GLEXT_glFramebufferTexture2D
(
GLEXT_GL_FRAMEBUFFER
,
GLEXT_GL_COLOR_ATTACHMENT0
,
GL_TEXTURE_2D
, m_texture,
0
));
glCheck
(
glReadPixels
(
0
,
0
,
static_cast
<GLsizei>(m_size.
x
),
static_cast
<GLsizei>(m_size.
y
),
GL_RGBA
,
GL_UNSIGNED_BYTE
,
pixels.
data
()));
glCheck
(
GLEXT_glDeleteFramebuffers
(
1
, &frameBuffer));
glCheck
(
GLEXT_glBindFramebuffer
(
GLEXT_GL_FRAMEBUFFER
,
static_cast
<GLuint>(previousFrameBuffer)));
if
(m_pixelsFlipped)
{
//
Flip the texture vertically
const
auto
stride =
static_cast
<std::
ptrdiff_t
>(m_size.
x
*
4
);
auto
currentRowIterator = pixels.
begin
();
auto
nextRowIterator = pixels.
begin
() + stride;
auto
reverseRowIterator = pixels.
begin
() + (stride *
static_cast
<std::
ptrdiff_t
>(m_size.
y
-
1
));
for
(
unsigned
int
i =
0
; i < m_size.
y
/
2
; ++i)
{
std::swap_ranges
(currentRowIterator, nextRowIterator, reverseRowIterator);
currentRowIterator = nextRowIterator;
nextRowIterator += stride;
reverseRowIterator -= stride;
}
}
}
#
else
if
((m_size == m_actualSize) && !m_pixelsFlipped)
{
//
Texture is not padded nor flipped, we can use a direct copy
glCheck
(
glBindTexture
(
GL_TEXTURE_2D
, m_texture));
glCheck
(
glGetTexImage
(
GL_TEXTURE_2D
,
0
,
GL_RGBA
,
GL_UNSIGNED_BYTE
, pixels.
data
()));
}
else
{
//
Texture is either padded or flipped, we have to use a slower algorithm
//
All the pixels will first be copied to a temporary array
std::vector<std::
uint8_t
>
allPixels
(m_actualSize.
x
* m_actualSize.
y
*
4
);
glCheck
(
glBindTexture
(
GL_TEXTURE_2D
, m_texture));
glCheck
(
glGetTexImage
(
GL_TEXTURE_2D
,
0
,
GL_RGBA
,
GL_UNSIGNED_BYTE
, allPixels.
data
()));
//
Then we copy the useful pixels from the temporary array to the final one
const
std::
uint8_t
* src = allPixels.
data
();
std::
uint8_t
* dst = pixels.
data
();
int
srcPitch =
static_cast
<
int
>(m_actualSize.
x
*
4
);
const
unsigned
int
dstPitch = m_size.
x
*
4
;
//
Handle the case where source pixels are flipped vertically
if
(m_pixelsFlipped)
{
src +=
static_cast
<
unsigned
int
>(srcPitch *
static_cast
<
int
>(m_size.
y
-
1
));
srcPitch = -srcPitch;
}
for
(
unsigned
int
i =
0
; i < m_size.
y
; ++i)
{
std::memcpy
(dst, src, dstPitch);
src += srcPitch;
dst += dstPitch;
}
}
#
endif
//
SFML_OPENGL_ES
return
{m_size, pixels.
data
()};
}
//
//////////////////////////////////////////////////////////
void
Texture::update
(
const
std::
uint8_t
* pixels)
{
//
Update the whole texture
update
(pixels, m_size, {
0
,
0
});
}
//
//////////////////////////////////////////////////////////
void
Texture::update
(
const
std::
uint8_t
* pixels,
const
Vector2u& size,
const
Vector2u& dest)
{
assert
(dest.
x
+ size.
x
<= m_size.
x
&&
"
Destination x coordinate is outside of texture
"
);
assert
(dest.
y
+ size.
y
<= m_size.
y
&&
"
Destination y coordinate is outside of texture
"
);
if
(pixels && m_texture)
{
const
TransientContextLock lock;
//
Make sure that the current texture binding will be preserved
const
priv::TextureSaver save;
//
Copy pixels from the given array to the texture
glCheck
(
glBindTexture
(
GL_TEXTURE_2D
, m_texture));
glCheck
(
glTexSubImage2D
(
GL_TEXTURE_2D
,
0
,
static_cast
<GLint>(dest.
x
),
static_cast
<GLint>(dest.
y
),
static_cast
<GLsizei>(size.
x
),
static_cast
<GLsizei>(size.
y
),
GL_RGBA
,
GL_UNSIGNED_BYTE
,
pixels));
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MIN_FILTER
, m_isSmooth ?
GL_LINEAR
:
GL_NEAREST
));
m_hasMipmap =
false
;
m_pixelsFlipped =
false
;
m_cacheId =
TextureImpl::getUniqueId
();
//
Force an OpenGL flush, so that the texture data will appear updated
//
in all contexts immediately (solves problems in multi-threaded apps)
glCheck
(
glFlush
());
}
}
//
//////////////////////////////////////////////////////////
void
Texture::update
(
const
Texture& texture)
{
//
Update the whole texture
update
(texture, {
0
,
0
});
}
//
//////////////////////////////////////////////////////////
void
Texture::update
(
const
Texture& texture,
const
Vector2u& dest)
{
assert
(dest.
x
+ texture.
m_size
.
x
<= m_size.
x
&&
"
Destination x coordinate is outside of texture
"
);
assert
(dest.
y
+ texture.
m_size
.
y
<= m_size.
y
&&
"
Destination y coordinate is outside of texture
"
);
if
(!m_texture || !texture.
m_texture
)
return
;
#
ifndef
SFML_OPENGL_ES
{
const
TransientContextLock lock;
//
Make sure that extensions are initialized
priv::ensureExtensionsInit
();
}
if
(GLEXT_framebuffer_object && GLEXT_framebuffer_blit)
{
const
TransientContextLock lock;
//
Save the current bindings so we can restore them after we are done
GLint readFramebuffer =
0
;
GLint drawFramebuffer =
0
;
glCheck
(
glGetIntegerv
(
GLEXT_GL_READ_FRAMEBUFFER_BINDING
, &readFramebuffer));
glCheck
(
glGetIntegerv
(
GLEXT_GL_DRAW_FRAMEBUFFER_BINDING
, &drawFramebuffer));
//
Create the framebuffers
GLuint sourceFrameBuffer =
0
;
GLuint destFrameBuffer =
0
;
glCheck
(
GLEXT_glGenFramebuffers
(
1
, &sourceFrameBuffer));
glCheck
(
GLEXT_glGenFramebuffers
(
1
, &destFrameBuffer));
if
(!sourceFrameBuffer || !destFrameBuffer)
{
err
() <<
"
Cannot copy texture, failed to create a frame buffer object
"
<< std::endl;
return
;
}
//
Link the source texture to the source frame buffer
glCheck
(
GLEXT_glBindFramebuffer
(
GLEXT_GL_READ_FRAMEBUFFER
, sourceFrameBuffer));
glCheck
(
GLEXT_glFramebufferTexture2D
(
GLEXT_GL_READ_FRAMEBUFFER
,
GLEXT_GL_COLOR_ATTACHMENT0
,
GL_TEXTURE_2D
,
texture.
m_texture
,
0
));
//
Link the destination texture to the destination frame buffer
glCheck
(
GLEXT_glBindFramebuffer
(
GLEXT_GL_DRAW_FRAMEBUFFER
, destFrameBuffer));
glCheck
(
GLEXT_glFramebufferTexture2D
(
GLEXT_GL_DRAW_FRAMEBUFFER
,
GLEXT_GL_COLOR_ATTACHMENT0
,
GL_TEXTURE_2D
, m_texture,
0
));
//
A final check, just to be sure...
GLenum sourceStatus =
0
;
glCheck
(sourceStatus =
GLEXT_glCheckFramebufferStatus
(
GLEXT_GL_READ_FRAMEBUFFER
));
GLenum destStatus =
0
;
glCheck
(destStatus =
GLEXT_glCheckFramebufferStatus
(
GLEXT_GL_DRAW_FRAMEBUFFER
));
if
((sourceStatus ==
GLEXT_GL_FRAMEBUFFER_COMPLETE
) && (destStatus ==
GLEXT_GL_FRAMEBUFFER_COMPLETE
))
{
//
Scissor testing affects framebuffer blits as well
//
Since we don't want scissor testing to interfere with our copying, we temporarily disable it for the blit if it is enabled
GLboolean scissorEnabled =
GL_FALSE
;
glCheck
(
glGetBooleanv
(
GL_SCISSOR_TEST
, &scissorEnabled));
if
(scissorEnabled ==
GL_TRUE
)
glCheck
(
glDisable
(
GL_SCISSOR_TEST
));
//
Blit the texture contents from the source to the destination texture
glCheck
(
GLEXT_glBlitFramebuffer
(
0
,
texture.
m_pixelsFlipped
?
static_cast
<GLint>(texture.
m_size
.
y
) :
0
,
static_cast
<GLint>(texture.
m_size
.
x
),
texture.
m_pixelsFlipped
?
0
:
static_cast
<GLint>(texture.
m_size
.
y
),
//
Source rectangle, flip y if source is flipped
static_cast
<GLint>(dest.
x
),
static_cast
<GLint>(dest.
y
),
static_cast
<GLint>(dest.
x
+ texture.
m_size
.
x
),
static_cast
<GLint>(dest.
y
+ texture.
m_size
.
y
),
//
Destination rectangle
GL_COLOR_BUFFER_BIT
,
GL_NEAREST
));
//
Re-enable scissor testing if it was previously enabled
if
(scissorEnabled ==
GL_TRUE
)
glCheck
(
glEnable
(
GL_SCISSOR_TEST
));
}
else
{
err
() <<
"
Cannot copy texture, failed to link texture to frame buffer
"
<< std::endl;
}
//
Restore previously bound framebuffers
glCheck
(
GLEXT_glBindFramebuffer
(
GLEXT_GL_READ_FRAMEBUFFER
,
static_cast
<GLuint>(readFramebuffer)));
glCheck
(
GLEXT_glBindFramebuffer
(
GLEXT_GL_DRAW_FRAMEBUFFER
,
static_cast
<GLuint>(drawFramebuffer)));
//
Delete the framebuffers
glCheck
(
GLEXT_glDeleteFramebuffers
(
1
, &sourceFrameBuffer));
glCheck
(
GLEXT_glDeleteFramebuffers
(
1
, &destFrameBuffer));
//
Make sure that the current texture binding will be preserved
const
priv::TextureSaver save;
//
Set the parameters of this texture
glCheck
(
glBindTexture
(
GL_TEXTURE_2D
, m_texture));
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MIN_FILTER
, m_isSmooth ?
GL_LINEAR
:
GL_NEAREST
));
m_hasMipmap =
false
;
m_pixelsFlipped =
false
;
m_cacheId =
TextureImpl::getUniqueId
();
//
Force an OpenGL flush, so that the texture data will appear updated
//
in all contexts immediately (solves problems in multi-threaded apps)
glCheck
(
glFlush
());
return
;
}
#
endif
//
SFML_OPENGL_ES
update
(texture.
copyToImage
(), dest);
}
//
//////////////////////////////////////////////////////////
void
Texture::update
(
const
Image& image)
{
//
Update the whole texture
update
(image.
getPixelsPtr
(), image.
getSize
(), {
0
,
0
});
}
//
//////////////////////////////////////////////////////////
void
Texture::update
(
const
Image& image,
const
Vector2u& dest)
{
update
(image.
getPixelsPtr
(), image.
getSize
(), dest);
}
//
//////////////////////////////////////////////////////////
void
Texture::update
(
const
Window& window)
{
update
(window, {
0
,
0
});
}
//
//////////////////////////////////////////////////////////
void
Texture::update
(
const
Window& window,
const
Vector2u& dest)
{
assert
(dest.
x
+ window.
getSize
().
x
<= m_size.
x
&&
"
Destination x coordinate is outside of texture
"
);
assert
(dest.
y
+ window.
getSize
().
y
<= m_size.
y
&&
"
Destination y coordinate is outside of texture
"
);
if
(m_texture && window.
setActive
(
true
))
{
const
TransientContextLock lock;
//
Make sure that the current texture binding will be preserved
const
priv::TextureSaver save;
//
Copy pixels from the back-buffer to the texture
glCheck
(
glBindTexture
(
GL_TEXTURE_2D
, m_texture));
glCheck
(
glCopyTexSubImage2D
(
GL_TEXTURE_2D
,
0
,
static_cast
<GLint>(dest.
x
),
static_cast
<GLint>(dest.
y
),
0
,
0
,
static_cast
<GLsizei>(window.
getSize
().
x
),
static_cast
<GLsizei>(window.
getSize
().
y
)));
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MIN_FILTER
, m_isSmooth ?
GL_LINEAR
:
GL_NEAREST
));
m_hasMipmap =
false
;
m_pixelsFlipped =
true
;
m_cacheId =
TextureImpl::getUniqueId
();
//
Force an OpenGL flush, so that the texture will appear updated
//
in all contexts immediately (solves problems in multi-threaded apps)
glCheck
(
glFlush
());
}
}
//
//////////////////////////////////////////////////////////
void
Texture::setSmooth
(
bool
smooth)
{
if
(smooth != m_isSmooth)
{
m_isSmooth = smooth;
if
(m_texture)
{
const
TransientContextLock lock;
//
Make sure that the current texture binding will be preserved
const
priv::TextureSaver save;
glCheck
(
glBindTexture
(
GL_TEXTURE_2D
, m_texture));
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MAG_FILTER
, m_isSmooth ?
GL_LINEAR
:
GL_NEAREST
));
if
(m_hasMipmap)
{
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MIN_FILTER
,
m_isSmooth ?
GL_LINEAR_MIPMAP_LINEAR
:
GL_NEAREST_MIPMAP_LINEAR
));
}
else
{
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MIN_FILTER
, m_isSmooth ?
GL_LINEAR
:
GL_NEAREST
));
}
}
}
}
//
//////////////////////////////////////////////////////////
bool
Texture::isSmooth
()
const
{
return
m_isSmooth;
}
//
//////////////////////////////////////////////////////////
bool
Texture::isSrgb
()
const
{
return
m_sRgb;
}
//
//////////////////////////////////////////////////////////
void
Texture::setRepeated
(
bool
repeated)
{
if
(repeated != m_isRepeated)
{
m_isRepeated = repeated;
if
(m_texture)
{
const
TransientContextLock lock;
//
Make sure that the current texture binding will be preserved
const
priv::TextureSaver save;
static
const
bool
textureEdgeClamp = GLEXT_texture_edge_clamp;
if
(!m_isRepeated && !textureEdgeClamp)
{
static
bool
warned =
false
;
if
(!warned)
{
err
() <<
"
OpenGL extension SGIS_texture_edge_clamp unavailable
"
<<
'
\n
'
<<
"
Artifacts may occur along texture edges
"
<<
'
\n
'
<<
"
Ensure that hardware acceleration is enabled if available
"
<< std::endl;
warned =
true
;
}
}
#
ifndef
SFML_OPENGL_ES
const
GLint textureWrapParam = m_isRepeated ?
GL_REPEAT
: (textureEdgeClamp ?
GLEXT_GL_CLAMP_TO_EDGE
:
GLEXT_GL_CLAMP
);
#
else
const
GLint textureWrapParam = m_isRepeated ?
GL_REPEAT
:
GLEXT_GL_CLAMP_TO_EDGE
;
#
endif
glCheck
(
glBindTexture
(
GL_TEXTURE_2D
, m_texture));
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_WRAP_S
, textureWrapParam));
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_WRAP_T
, textureWrapParam));
}
}
}
//
//////////////////////////////////////////////////////////
bool
Texture::isRepeated
()
const
{
return
m_isRepeated;
}
//
//////////////////////////////////////////////////////////
bool
Texture::generateMipmap
()
{
if
(!m_texture)
return
false
;
const
TransientContextLock lock;
//
Make sure that extensions are initialized
priv::ensureExtensionsInit
();
if
(!GLEXT_framebuffer_object)
return
false
;
//
Make sure that the current texture binding will be preserved
const
priv::TextureSaver save;
glCheck
(
glBindTexture
(
GL_TEXTURE_2D
, m_texture));
glCheck
(
GLEXT_glGenerateMipmap
(
GL_TEXTURE_2D
));
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MIN_FILTER
,
m_isSmooth ?
GL_LINEAR_MIPMAP_LINEAR
:
GL_NEAREST_MIPMAP_LINEAR
));
m_hasMipmap =
true
;
return
true
;
}
//
//////////////////////////////////////////////////////////
void
Texture::invalidateMipmap
()
{
if
(!m_hasMipmap)
return
;
const
TransientContextLock lock;
//
Make sure that the current texture binding will be preserved
const
priv::TextureSaver save;
glCheck
(
glBindTexture
(
GL_TEXTURE_2D
, m_texture));
glCheck
(
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MIN_FILTER
, m_isSmooth ?
GL_LINEAR
:
GL_NEAREST
));
m_hasMipmap =
false
;
}
//
//////////////////////////////////////////////////////////
void
Texture::bind
(
const
Texture* texture, CoordinateType coordinateType)
{
const
TransientContextLock lock;
if
(texture && texture->
m_texture
)
{
//
Bind the texture
glCheck
(
glBindTexture
(
GL_TEXTURE_2D
, texture->
m_texture
));
//
Check if we need to define a special texture matrix
if
((coordinateType == CoordinateType::Pixels) || texture->
m_pixelsFlipped
)
{
//
clang-format off
std::array matrix = {
1
.
f
,
0
.
f
,
0
.
f
,
0
.
f
,
0
.
f
,
1
.
f
,
0
.
f
,
0
.
f
,
0
.
f
,
0
.
f
,
1
.
f
,
0
.
f
,
0
.
f
,
0
.
f
,
0
.
f
,
1
.
f
};
//
clang-format on
//
If non-normalized coordinates (= pixels) are requested, we need to
//
setup scale factors that convert the range [0 .. size] to [0 .. 1]
if
(coordinateType == CoordinateType::Pixels)
{
matrix[
0
] =
1
.
f
/
static_cast
<
float
>(texture->
m_actualSize
.
x
);
matrix[
5
] =
1
.
f
/
static_cast
<
float
>(texture->
m_actualSize
.
y
);
}
//
If pixels are flipped we must invert the Y axis
if
(texture->
m_pixelsFlipped
)
{
matrix[
5
] = -matrix[
5
];
matrix[
13
] =
static_cast
<
float
>(texture->
m_size
.
y
) /
static_cast
<
float
>(texture->
m_actualSize
.
y
);
}
//
Load the matrix
glCheck
(
glMatrixMode
(
GL_TEXTURE
));
glCheck
(
glLoadMatrixf
(matrix.
data
()));
}
else
{
//
Reset the texture matrix
glCheck
(
glMatrixMode
(
GL_TEXTURE
));
glCheck
(
glLoadIdentity
());
}
//
Go back to model-view mode (sf::RenderTarget relies on it)
glCheck
(
glMatrixMode
(
GL_MODELVIEW
));
}
else
{
//
Bind no texture
glCheck
(
glBindTexture
(
GL_TEXTURE_2D
,
0
));
//
Reset the texture matrix
glCheck
(
glMatrixMode
(
GL_TEXTURE
));
glCheck
(
glLoadIdentity
());
//
Go back to model-view mode (sf::RenderTarget relies on it)
glCheck
(
glMatrixMode
(
GL_MODELVIEW
));
}
}
//
//////////////////////////////////////////////////////////
unsigned
int
Texture::getMaximumSize
()
{
static
const
unsigned
int
size = []
{
const
TransientContextLock transientLock;
GLint value =
0
;
//
Make sure that extensions are initialized
priv::ensureExtensionsInit
();
glCheck
(
glGetIntegerv
(
GL_MAX_TEXTURE_SIZE
, &value));
return
static_cast
<
unsigned
int
>(value);
}();
return
size;
}
//
//////////////////////////////////////////////////////////
Texture& Texture::
operator
=(
const
Texture& right)
{
Texture
temp
(right);
swap
(temp);
return
*
this
;
}
//
//////////////////////////////////////////////////////////
void
Texture::swap
(Texture& right)
noexcept
{
std::swap
(m_size, right.
m_size
);
std::swap
(m_actualSize, right.
m_actualSize
);
std::swap
(m_texture, right.
m_texture
);
std::swap
(m_isSmooth, right.
m_isSmooth
);
std::swap
(m_sRgb, right.
m_sRgb
);
std::swap
(m_isRepeated, right.
m_isRepeated
);
std::swap
(m_pixelsFlipped, right.
m_pixelsFlipped
);
std::swap
(m_fboAttachment, right.
m_fboAttachment
);
std::swap
(m_hasMipmap, right.
m_hasMipmap
);
std::swap
(m_cacheId, right.
m_cacheId
);
}
//
//////////////////////////////////////////////////////////
unsigned
int
Texture::getNativeHandle
()
const
{
return
m_texture;
}
//
//////////////////////////////////////////////////////////
unsigned
int
Texture::getValidSize
(
unsigned
int
size)
{
if
(GLEXT_texture_non_power_of_two)
{
//
If hardware supports NPOT textures, then just return the unmodified size
return
size;
}
else
{
//
If hardware doesn't support NPOT textures, we calculate the nearest power of two
unsigned
int
powerOfTwo =
1
;
while
(powerOfTwo < size)
powerOfTwo *=
2
;
return
powerOfTwo;
}
}
//
//////////////////////////////////////////////////////////
void
swap
(Texture& left, Texture& right)
noexcept
{
left.
swap
(right);
}
}
//
namespace sf
Back
|
FazBrowse Home
|
New Git URL