forked from wine-mirror/wine
/
texture.c
2928 lines (2446 loc) · 109 KB
/
texture.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
/*
* Copyright 2002-2005 Jason Edmeades
* Copyright 2002-2005 Raphael Junqueira
* Copyright 2005 Oliver Stieber
* Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
* Copyright 2009-2011 Henri Verbeet for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include "wined3d_private.h"
WINE_DEFAULT_DEBUG_CHANNEL(d3d);
WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
WINE_DECLARE_DEBUG_CHANNEL(winediag);
#define WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD 50
static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
{
return texture->resource.pool == WINED3D_POOL_DEFAULT
&& texture->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU
&& gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
&& !texture->resource.format->convert
&& !(texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED));
}
GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
{
const struct wined3d_swapchain *swapchain = texture->swapchain;
TRACE("texture %p.\n", texture);
if (!swapchain)
{
ERR("Texture %p is not part of a swapchain.\n", texture);
return GL_NONE;
}
if (swapchain->back_buffers && swapchain->back_buffers[0] == texture)
{
if (swapchain->render_to_fbo)
{
TRACE("Returning GL_COLOR_ATTACHMENT0.\n");
return GL_COLOR_ATTACHMENT0;
}
TRACE("Returning GL_BACK.\n");
return GL_BACK;
}
else if (texture == swapchain->front_buffer)
{
TRACE("Returning GL_FRONT.\n");
return GL_FRONT;
}
FIXME("Higher back buffer, returning GL_BACK.\n");
return GL_BACK;
}
static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
{
struct wined3d_texture_sub_resource *sub_resource;
unsigned int i, sub_count;
if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
|| texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
{
TRACE("Not evicting system memory for texture %p.\n", texture);
return;
}
TRACE("Evicting system memory for texture %p.\n", texture);
sub_count = texture->level_count * texture->layer_count;
for (i = 0; i < sub_count; ++i)
{
sub_resource = &texture->sub_resources[i];
if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
i, texture);
sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
}
wined3d_resource_free_sysmem(&texture->resource);
texture->resource.map_heap_memory = NULL;
}
void wined3d_texture_validate_location(struct wined3d_texture *texture,
unsigned int sub_resource_idx, DWORD location)
{
struct wined3d_texture_sub_resource *sub_resource;
DWORD previous_locations;
TRACE("texture %p, sub_resource_idx %u, location %s.\n",
texture, sub_resource_idx, wined3d_debug_location(location));
sub_resource = &texture->sub_resources[sub_resource_idx];
previous_locations = sub_resource->locations;
sub_resource->locations |= location;
if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
&& !--texture->sysmem_count)
wined3d_texture_evict_sysmem(texture);
TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
}
static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
{
texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
}
void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
unsigned int sub_resource_idx, DWORD location)
{
struct wined3d_texture_sub_resource *sub_resource;
TRACE("texture %p, sub_resource_idx %u, location %s.\n",
texture, sub_resource_idx, wined3d_debug_location(location));
if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
wined3d_texture_set_dirty(texture);
sub_resource = &texture->sub_resources[sub_resource_idx];
sub_resource->locations &= ~location;
if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
++texture->sysmem_count;
TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
if (!sub_resource->locations)
ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
sub_resource_idx, texture);
}
/* Context activation is done by the caller. */
void *wined3d_texture_map_bo_address(const struct wined3d_bo_address *data, size_t size,
const struct wined3d_gl_info *gl_info, GLenum binding, DWORD flags)
{
BYTE *memory;
if (!data->buffer_object)
return data->addr;
GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
{
GLbitfield map_flags = wined3d_resource_gl_map_flags(flags) & ~GL_MAP_FLUSH_EXPLICIT_BIT;
memory = GL_EXTCALL(glMapBufferRange(binding, (INT_PTR)data->addr, size, map_flags));
}
else
{
memory = GL_EXTCALL(glMapBuffer(binding, wined3d_resource_gl_legacy_map_flags(flags)));
memory += (INT_PTR)data->addr;
}
GL_EXTCALL(glBindBuffer(binding, 0));
checkGLcall("Map buffer object");
return memory;
}
/* Context activation is done by the caller. */
void wined3d_texture_unmap_bo_address(const struct wined3d_bo_address *data,
const struct wined3d_gl_info *gl_info, GLenum binding)
{
if (!data->buffer_object)
return;
GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
GL_EXTCALL(glUnmapBuffer(binding));
GL_EXTCALL(glBindBuffer(binding, 0));
checkGLcall("Unmap buffer object");
}
void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
struct wined3d_bo_address *data, DWORD locations, BOOL map)
{
struct wined3d_texture_sub_resource *sub_resource;
TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
texture, sub_resource_idx, data, wined3d_debug_location(locations));
sub_resource = &texture->sub_resources[sub_resource_idx];
if (locations & WINED3D_LOCATION_BUFFER)
{
data->addr = NULL;
if (map)
data->buffer_object = sub_resource->map_buffer->name;
else
data->buffer_object = sub_resource->buffer->name;
return;
}
if (locations & WINED3D_LOCATION_USER_MEMORY)
{
data->addr = texture->user_memory;
data->buffer_object = 0;
return;
}
if (locations & WINED3D_LOCATION_SYSMEM)
{
if (map)
data->addr = texture->resource.map_heap_memory;
else
data->addr = texture->resource.heap_memory;
data->addr += sub_resource->offset;
data->buffer_object = 0;
return;
}
ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
data->addr = NULL;
data->buffer_object = 0;
}
static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, DWORD flags,
struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops,
const struct wined3d_resource_ops *resource_ops)
{
const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
unsigned int i, j, size, offset = 0;
HRESULT hr;
TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
"multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
"flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
flags, device, parent, parent_ops, resource_ops);
if (!desc->width || !desc->height || !desc->depth)
return WINED3DERR_INVALIDCALL;
for (i = 0; i < layer_count; ++i)
{
for (j = 0; j < level_count; ++j)
{
unsigned int idx = i * level_count + j;
size = wined3d_format_calculate_size(format, device->surface_alignment,
max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
texture->sub_resources[idx].offset = offset;
texture->sub_resources[idx].size = size;
offset += size;
}
offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
}
if (!offset)
return WINED3DERR_INVALIDCALL;
if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
desc->width, desc->height, desc->depth, offset, parent, parent_ops, resource_ops)))
{
static unsigned int once;
/* DXTn 3D textures are not supported. Do not write the ERR for them. */
if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
|| desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
&& !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
&& desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
WARN("Failed to initialize resource, returning %#x\n", hr);
return hr;
}
wined3d_resource_update_draw_binding(&texture->resource);
if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
texture->resource.access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
texture->texture_ops = texture_ops;
texture->layer_count = layer_count;
texture->level_count = level_count;
texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
texture->lod = 0;
texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
texture->flags |= WINED3D_TEXTURE_DISCARD;
return WINED3D_OK;
}
/* Context activation is done by the caller. */
static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
unsigned int sub_resource_idx, struct wined3d_context *context)
{
struct wined3d_gl_bo *buffer = texture->sub_resources[sub_resource_idx].buffer;
GLuint name = buffer->name;
if (buffer != texture->sub_resources[sub_resource_idx].map_buffer)
ERR("Buffer is %p, map buffer is %p.\n", buffer,
texture->sub_resources[sub_resource_idx].map_buffer);
wined3d_device_release_bo(texture->resource.device, buffer, context);
texture->sub_resources[sub_resource_idx].buffer = NULL;
texture->sub_resources[sub_resource_idx].map_buffer = NULL;
wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
name, texture, sub_resource_idx);
}
static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
{
unsigned int sub_count = texture->level_count * texture->layer_count;
const struct wined3d_device *device = texture->resource.device;
DWORD map_binding = texture->update_map_binding;
struct wined3d_context *context = NULL;
unsigned int i;
if (device->d3d_initialized)
context = context_acquire(device, NULL);
for (i = 0; i < sub_count; ++i)
{
if (texture->sub_resources[i].locations == texture->resource.map_binding
&& !wined3d_texture_load_location(texture, i, context, map_binding))
ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
wined3d_texture_remove_buffer_object(texture, i, context);
}
if (context)
context_release(context);
texture->resource.map_binding = map_binding;
texture->update_map_binding = 0;
}
void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
{
texture->update_map_binding = map_binding;
if (!texture->resource.map_count)
wined3d_texture_update_map_binding(texture);
}
/* A GL context is provided by the caller */
static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
struct gl_texture *tex)
{
context_gl_resource_released(device, tex->name, FALSE);
gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
tex->name = 0;
}
static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
{
struct wined3d_device *device = texture->resource.device;
const struct wined3d_gl_info *gl_info = NULL;
struct wined3d_context *context = NULL;
if (texture->texture_rgb.name || texture->texture_srgb.name
|| texture->rb_multisample || texture->rb_resolved)
{
context = context_acquire(device, NULL);
gl_info = context->gl_info;
}
if (texture->texture_rgb.name)
gltexture_delete(device, context->gl_info, &texture->texture_rgb);
if (texture->texture_srgb.name)
gltexture_delete(device, context->gl_info, &texture->texture_srgb);
if (texture->rb_multisample)
{
TRACE("Deleting multisample renderbuffer %u.\n", texture->rb_multisample);
context_gl_resource_released(device, texture->rb_multisample, TRUE);
gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_multisample);
texture->rb_multisample = 0;
}
if (texture->rb_resolved)
{
TRACE("Deleting resolved renderbuffer %u.\n", texture->rb_resolved);
context_gl_resource_released(device, texture->rb_resolved, TRUE);
gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_resolved);
texture->rb_resolved = 0;
}
if (context) context_release(context);
wined3d_texture_set_dirty(texture);
resource_unload(&texture->resource);
}
void wined3d_texture_cleanup(struct wined3d_texture *texture)
{
unsigned int sub_count = texture->level_count * texture->layer_count;
struct wined3d_device *device = texture->resource.device;
struct wined3d_context *context = NULL;
struct wined3d_gl_bo *buffer;
unsigned int i;
TRACE("texture %p.\n", texture);
for (i = 0; i < sub_count; ++i)
{
if (texture->sub_resources[i].buffer != texture->sub_resources[i].map_buffer)
ERR("Buffer is %p, map buffer is %p.\n", texture->sub_resources[i].buffer,
texture->sub_resources[i].map_buffer);
if (!(buffer = texture->sub_resources[i].buffer))
continue;
TRACE("Deleting buffer object %u.\n", buffer->name);
/* We may not be able to get a context in wined3d_texture_cleanup() in
* general, but if a buffer object was previously created we can. */
if (!context)
context = context_acquire(device, NULL);
wined3d_device_release_bo(device, buffer, context);
texture->sub_resources[i].buffer = NULL;
texture->sub_resources[i].map_buffer = NULL;
}
if (context)
context_release(context);
texture->texture_ops->texture_cleanup_sub_resources(texture);
wined3d_texture_unload_gl_texture(texture);
HeapFree(GetProcessHeap(), 0, texture);
}
void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
{
texture->swapchain = swapchain;
wined3d_resource_update_draw_binding(&texture->resource);
}
/* Context activation is done by the caller. */
void wined3d_texture_bind(struct wined3d_texture *texture,
struct wined3d_context *context, BOOL srgb)
{
const struct wined3d_gl_info *gl_info = context->gl_info;
const struct wined3d_format *format = texture->resource.format;
const struct color_fixup_desc fixup = format->color_fixup;
struct gl_texture *gl_tex;
GLenum target;
TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
if (!needs_separate_srgb_gl_texture(context))
srgb = FALSE;
/* sRGB mode cache for preload() calls outside drawprim. */
if (srgb)
texture->flags |= WINED3D_TEXTURE_IS_SRGB;
else
texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
target = texture->target;
if (gl_tex->name)
{
context_bind_texture(context, target, gl_tex->name);
return;
}
gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
checkGLcall("glGenTextures");
TRACE("Generated texture %d.\n", gl_tex->name);
if (!gl_tex->name)
{
ERR("Failed to generate a texture name.\n");
return;
}
/* Initialise the state of the texture object to the OpenGL defaults, not
* the wined3d defaults. */
gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
gl_tex->sampler_desc.lod_bias = 0.0f;
gl_tex->sampler_desc.min_lod = -1000.0f;
gl_tex->sampler_desc.max_lod = 1000.0f;
gl_tex->sampler_desc.max_anisotropy = 1;
gl_tex->sampler_desc.compare = FALSE;
gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
gl_tex->sampler_desc.srgb_decode = TRUE;
else
gl_tex->sampler_desc.srgb_decode = srgb;
gl_tex->base_level = 0;
wined3d_texture_set_dirty(texture);
context_bind_texture(context, target, gl_tex->name);
if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
{
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
}
/* For a new texture we have to set the texture levels after binding the
* texture. Beware that texture rectangles do not support mipmapping, but
* set the maxmiplevel if we're relying on the partial
* GL_ARB_texture_non_power_of_two emulation with texture rectangles.
* (I.e., do not care about cond_np2 here, just look for
* GL_TEXTURE_RECTANGLE_ARB.) */
if (target != GL_TEXTURE_RECTANGLE_ARB)
{
TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
}
if (target == GL_TEXTURE_CUBE_MAP_ARB)
{
/* Cubemaps are always set to clamp, regardless of the sampler state. */
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
if (texture->flags & WINED3D_TEXTURE_COND_NP2)
{
/* Conditinal non power of two textures use a different clamping
* default. If we're using the GL_WINE_normalized_texrect partial
* driver emulation, we're dealing with a GL_TEXTURE_2D texture which
* has the address mode set to repeat - something that prevents us
* from hitting the accelerated codepath. Thus manually set the GL
* state. The same applies to filtering. Even if the texture has only
* one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
* fallback on macos. */
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
checkGLcall("glTexParameteri");
gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
}
if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
{
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
}
if (!is_identity_fixup(fixup) && can_use_texture_swizzle(gl_info, format))
{
static const GLenum swizzle_source[] =
{
GL_ZERO, /* CHANNEL_SOURCE_ZERO */
GL_ONE, /* CHANNEL_SOURCE_ONE */
GL_RED, /* CHANNEL_SOURCE_X */
GL_GREEN, /* CHANNEL_SOURCE_Y */
GL_BLUE, /* CHANNEL_SOURCE_Z */
GL_ALPHA, /* CHANNEL_SOURCE_W */
};
struct
{
GLint x, y, z, w;
}
swizzle;
swizzle.x = swizzle_source[fixup.x_source];
swizzle.y = swizzle_source[fixup.y_source];
swizzle.z = swizzle_source[fixup.z_source];
swizzle.w = swizzle_source[fixup.w_source];
gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, &swizzle.x);
checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
}
}
/* Context activation is done by the caller. */
void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
struct wined3d_context *context, BOOL srgb)
{
DWORD active_sampler;
/* We don't need a specific texture unit, but after binding the texture
* the current unit is dirty. Read the unit back instead of switching to
* 0, this avoids messing around with the state manager's GL states. The
* current texture unit should always be a valid one.
*
* To be more specific, this is tricky because we can implicitly be
* called from sampler() in state.c. This means we can't touch anything
* other than whatever happens to be the currently active texture, or we
* would risk marking already applied sampler states dirty again. */
active_sampler = context->rev_tex_unit_map[context->active_texture];
if (active_sampler != WINED3D_UNMAPPED_STAGE)
context_invalidate_state(context, STATE_SAMPLER(active_sampler));
/* FIXME: Ideally we'd only do this when touching a binding that's used by
* a shader. */
context_invalidate_state(context, STATE_SHADER_RESOURCE_BINDING);
wined3d_texture_bind(texture, context, srgb);
}
/* Context activation is done by the caller (state handler). */
/* This function relies on the correct texture being bound and loaded. */
void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
{
const struct wined3d_gl_info *gl_info = context->gl_info;
GLenum target = texture->target;
struct gl_texture *gl_tex;
DWORD state;
TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
state = sampler_desc->address_u;
if (state != gl_tex->sampler_desc.address_u)
{
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
gl_tex->sampler_desc.address_u = state;
}
state = sampler_desc->address_v;
if (state != gl_tex->sampler_desc.address_v)
{
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
gl_tex->sampler_desc.address_v = state;
}
state = sampler_desc->address_w;
if (state != gl_tex->sampler_desc.address_w)
{
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
gl_tex->sampler_desc.address_w = state;
}
if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
sizeof(gl_tex->sampler_desc.border_color)))
{
gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
sizeof(gl_tex->sampler_desc.border_color));
}
state = sampler_desc->mag_filter;
if (state != gl_tex->sampler_desc.mag_filter)
{
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
gl_tex->sampler_desc.mag_filter = state;
}
if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
|| sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
{
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
}
state = sampler_desc->max_anisotropy;
if (state != gl_tex->sampler_desc.max_anisotropy)
{
if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, state);
else
WARN("Anisotropic filtering not supported.\n");
gl_tex->sampler_desc.max_anisotropy = state;
}
if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
&& (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
&& gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
{
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
}
if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
{
if (sampler_desc->compare)
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
else
gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
gl_tex->sampler_desc.compare = sampler_desc->compare;
}
checkGLcall("Texture parameter application");
if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
{
gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
}
}
ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
{
ULONG refcount;
TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
if (texture->swapchain)
return wined3d_swapchain_incref(texture->swapchain);
refcount = InterlockedIncrement(&texture->resource.ref);
TRACE("%p increasing refcount to %u.\n", texture, refcount);
return refcount;
}
static void wined3d_texture_cleanup_main(struct wined3d_texture *texture)
{
struct wined3d_device *device = texture->resource.device;
unsigned int sub_count = texture->level_count * texture->layer_count;
struct wined3d_texture_sub_resource *sub_resource;
unsigned int i;
for (i = 0; i < sub_count; ++i) {
sub_resource = &texture->sub_resources[i];
if (sub_resource->parent_ops && sub_resource->parent)
sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
}
/* Wait for the CS to finish operations on this texture when user memory was in use.
* The application is allowed to free the memory after texture / surface destruction
* returns. */
if (texture->resource.map_binding == WINED3D_LOCATION_USER_MEMORY)
wined3d_resource_wait_fence(&texture->resource);
resource_cleanup(&texture->resource);
wined3d_cs_emit_texture_cleanup(device->cs, texture);
}
ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
{
ULONG refcount;
TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
if (texture->swapchain)
return wined3d_swapchain_decref(texture->swapchain);
refcount = InterlockedDecrement(&texture->resource.ref);
TRACE("%p decreasing refcount to %u.\n", texture, refcount);
if (!refcount)
{
texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
wined3d_texture_cleanup_main(texture);
}
return refcount;
}
struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
{
TRACE("texture %p.\n", texture);
return &texture->resource;
}
static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
{
return c1->color_space_low_value == c2->color_space_low_value
&& c1->color_space_high_value == c2->color_space_high_value;
}
/* Context activation is done by the caller */
void wined3d_texture_load(struct wined3d_texture *texture,
struct wined3d_context *context, BOOL srgb)
{
UINT sub_count = texture->level_count * texture->layer_count;
const struct wined3d_d3d_info *d3d_info = context->d3d_info;
DWORD flag;
UINT i;
TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
if (!needs_separate_srgb_gl_texture(context))
srgb = FALSE;
if (srgb)
flag = WINED3D_TEXTURE_SRGB_VALID;
else
flag = WINED3D_TEXTURE_RGB_VALID;
if (!d3d_info->shader_color_key
&& (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
!= !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
|| (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
&& !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
{
unsigned int sub_count = texture->level_count * texture->layer_count;
unsigned int i;
TRACE("Reloading because of color key value change.\n");
for (i = 0; i < sub_count; i++)
{
if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
else
wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
}
texture->async.gl_color_key = texture->async.src_blt_color_key;
}
if (texture->flags & flag)
{
TRACE("Texture %p not dirty, nothing to do.\n", texture);
return;
}
/* Reload the surfaces if the texture is marked dirty. */
for (i = 0; i < sub_count; ++i)
{
if (!wined3d_texture_load_location(texture, i, context,
srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
ERR("Failed to load location (srgb %#x).\n", srgb);
}
texture->flags |= flag;
}
void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
{
const struct wined3d_device *device = texture->resource.device;
wined3d_cs_emit_texture_preload(device->cs, texture);
}
void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
{
TRACE("texture %p.\n", texture);
return texture->resource.parent;
}
static BOOL wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
unsigned int level, const struct wined3d_box *box)
{
if (box->left >= box->right
|| box->top >= box->bottom
|| box->front >= box->back)
return FALSE;
if (box->right > wined3d_texture_get_level_width(texture, level)
|| box->bottom > wined3d_texture_get_level_height(texture, level)
|| box->back > wined3d_texture_get_level_depth(texture, level))
return FALSE;
return TRUE;
}
void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
{
const struct wined3d_resource *resource = &texture->resource;
unsigned int width = wined3d_texture_get_level_width(texture, level);
unsigned int height = wined3d_texture_get_level_height(texture, level);
if (texture->row_pitch)
{
*row_pitch = texture->row_pitch;
*slice_pitch = texture->slice_pitch;
return;
}
wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
width, height, row_pitch, slice_pitch);
}
DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
{
DWORD old = texture->lod;
TRACE("texture %p, lod %u.\n", texture, lod);
/* The d3d9:texture test shows that SetLOD is ignored on non-managed
* textures. The call always returns 0, and GetLOD always returns 0. */
if (texture->resource.pool != WINED3D_POOL_MANAGED)
{
TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
return 0;
}
if (lod >= texture->level_count)
lod = texture->level_count - 1;
if (texture->lod != lod)
{
if (wined3d_settings.cs_multithreaded)
{
struct wined3d_device *device = texture->resource.device;
FIXME("Waiting for cs.\n");
device->cs->ops->finish(device->cs);
}
texture->lod = lod;
texture->texture_rgb.base_level = ~0u;
texture->texture_srgb.base_level = ~0u;
if (texture->resource.bind_count)
device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
}
return old;
}
DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
{
TRACE("texture %p, returning %u.\n", texture, texture->lod);
return texture->lod;
}
DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
{
TRACE("texture %p, returning %u.\n", texture, texture->level_count);
return texture->level_count;
}
HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
enum wined3d_texture_filter_type filter_type)
{
FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
{
WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
return WINED3DERR_INVALIDCALL;
}
texture->filter_type = filter_type;
return WINED3D_OK;
}
enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
{
TRACE("texture %p.\n", texture);
return texture->filter_type;
}
HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
DWORD flags, const struct wined3d_color_key *color_key)
{
struct wined3d_device *device = texture->resource.device;
static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
| WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
if (flags & ~all_flags)
{
WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
return WINED3DERR_INVALIDCALL;
}
wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
return WINED3D_OK;
}
HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
UINT multisample_quality, void *mem, UINT pitch)
{
struct wined3d_device *device = texture->resource.device;
const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
struct wined3d_texture_sub_resource *sub_resource;
struct wined3d_surface *surface;
DWORD valid_location = 0;