summarylogtreecommitdiffstats
path: root/wine-staging-pba.patch
blob: 050a7c3c4071de76e799dc842b753cdba63136a5 (plain)
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
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
diff -r -u --new-file a/dlls/wined3d/buffer.c b/dlls/wined3d/buffer.c
--- a/dlls/wined3d/buffer.c	2018-03-28 23:57:50.342915285 +0200
+++ b/dlls/wined3d/buffer.c	2018-03-28 23:27:46.013998252 +0200
@@ -28,12 +28,14 @@
 #include "wined3d_private.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
+WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
 
 #define WINED3D_BUFFER_HASDESC      0x01    /* A vertex description has been found. */
 #define WINED3D_BUFFER_USE_BO       0x02    /* Use a buffer object for this buffer. */
 #define WINED3D_BUFFER_PIN_SYSMEM   0x04    /* Keep a system memory copy for this buffer. */
 #define WINED3D_BUFFER_DISCARD      0x08    /* A DISCARD lock has occurred since the last preload. */
 #define WINED3D_BUFFER_APPLESYNC    0x10    /* Using sync as in GL_APPLE_flush_buffer_range. */
+#define WINED3D_BUFFER_PERSISTENT   0x20    /* Uses a persistent-mapped buffer via ARB_buffer_storage. */
 
 #define VB_MAXDECLCHANGES     100     /* After that number of decl changes we stop converting */
 #define VB_RESETDECLCHANGE    1000    /* Reset the decl changecount after that number of draws */
@@ -269,6 +271,53 @@
     return FALSE;
 }
 
+/* Context activation is done by the caller. */
+static BOOL buffer_alloc_persistent_map(struct wined3d_buffer *buffer)
+{
+    struct wined3d_device *device = buffer->resource.device;
+    struct wined3d_buffer_heap *heap;
+    struct wined3d_buffer_heap_element *elem;
+    HRESULT hr;
+
+    if (buffer->bind_flags & WINED3D_BIND_CONSTANT_BUFFER)
+    {
+        // Use a heap aligned to constant buffer offset requirements.
+        heap = device->cb_buffer_heap;
+    }
+    else
+    {
+        if (!(buffer->resource.usage & WINED3DUSAGE_WRITEONLY))
+            FIXME("Using a write-only persistent buffer for %p without WINED3DUSAGE_WRITEONLY.\n", buffer);
+        heap = device->wo_buffer_heap;
+    }
+
+    buffer->buffer_heap = heap;
+    if (FAILED(hr = wined3d_buffer_heap_alloc(heap, buffer->resource.size, &elem)))
+    {
+        goto fail;
+    }
+    buffer->cs_persistent_map = elem;
+    buffer->mt_persistent_map = elem;
+    return TRUE;
+
+fail:
+    // FIXME(acomminos): fall back to standalone BO here?
+    ERR("Failed to create persistent map for buffer %p, hr=%x\n", buffer, hr);
+    buffer->buffer_heap = NULL;
+    return FALSE;
+}
+
+static void buffer_free_persistent_map(struct wined3d_buffer *buffer)
+{
+    if (!buffer->buffer_heap)
+        return;
+
+    // TODO(acomminos): get the CS thread to free pending main thread buffers.
+    wined3d_buffer_heap_free(buffer->buffer_heap, buffer->cs_persistent_map);
+    buffer->buffer_heap = NULL;
+}
+
+
 static BOOL buffer_process_converted_attribute(struct wined3d_buffer *buffer,
         const enum wined3d_buffer_conversion_type conversion_type,
         const struct wined3d_stream_info_element *attrib, DWORD *stride_this_run)
@@ -632,6 +681,17 @@
             }
             return buffer_create_buffer_object(buffer, context);
 
+        case WINED3D_LOCATION_PERSISTENT_MAP:
+            if (buffer->buffer_heap)
+                return TRUE;
+
+            if (!(buffer->flags & WINED3D_BUFFER_PERSISTENT))
+            {
+                WARN("Trying to map a persistent region for buffer %p without WINED3D_BUFFER_PERSISTENT.\n", buffer);
+                return FALSE;
+            }
+            return buffer_alloc_persistent_map(buffer);
+ 
         default:
             ERR("Invalid location %s.\n", wined3d_debug_location(location));
             return FALSE;
@@ -689,16 +749,32 @@
                 buffer_conversion_upload(buffer, context);
             break;
 
+        case WINED3D_LOCATION_PERSISTENT_MAP:
+            // TODO(acomminos): are we guaranteed location_sysmem to be kept?
+            // no.
+            if (buffer->conversion_map)
+                FIXME("Attempting to use conversion map with persistent mapping.\n");
+            memcpy(buffer->buffer_heap->map_ptr +
+                   buffer->cs_persistent_map->range.offset,
+                   buffer->resource.heap_memory, buffer->resource.size);
+            break;
+
         default:
             ERR("Invalid location %s.\n", wined3d_debug_location(location));
             return FALSE;
     }
 
     wined3d_buffer_validate_location(buffer, location);
-    if (buffer->resource.heap_memory && location == WINED3D_LOCATION_BUFFER
+    if (buffer->resource.heap_memory
+            && location & WINED3D_LOCATION_BUFFER
             && !(buffer->resource.usage & WINED3DUSAGE_DYNAMIC))
         wined3d_buffer_evict_sysmem(buffer);
 
+    // FIXME(acomminos)
+    if (buffer->resource.heap_memory
+           && location & WINED3D_LOCATION_PERSISTENT_MAP)
+        wined3d_buffer_evict_sysmem(buffer);
+
     return TRUE;
 }
 
@@ -720,12 +796,26 @@
     {
         data->buffer_object = buffer->buffer_object;
         data->addr = NULL;
+        data->length = buffer->resource.size;
         return WINED3D_LOCATION_BUFFER;
     }
+    if (locations & WINED3D_LOCATION_PERSISTENT_MAP)
+    {
+        // FIXME(acomminos): should we expose a buffer object we don't wholly own here?
+        data->buffer_object = buffer->buffer_heap->buffer_object;
+        data->addr = buffer->cs_persistent_map->range.offset;
+
+        // Note that the size of the underlying buffer allocation may be larger
+        // than the buffer knows about. In this case, we've rounded it up to be
+        // aligned (e.g. for uniform buffer offsets).
+        data->length = buffer->cs_persistent_map->range.size;
+        return WINED3D_LOCATION_PERSISTENT_MAP;
+    }
     if (locations & WINED3D_LOCATION_SYSMEM)
     {
         data->buffer_object = 0;
         data->addr = buffer->resource.heap_memory;
+        data->length = buffer->resource.size;
         return WINED3D_LOCATION_SYSMEM;
     }
 
@@ -761,6 +851,8 @@
         buffer->flags &= ~WINED3D_BUFFER_HASDESC;
     }
 
+    buffer_free_persistent_map(buffer);
+
     resource_unload(resource);
 }
 
@@ -784,6 +876,8 @@
         heap_free(buffer->conversion_map);
     }
 
+    buffer_free_persistent_map(buffer);
+
     heap_free(buffer->maps);
     heap_free(buffer);
 }
@@ -900,6 +994,16 @@
 
     buffer_mark_used(buffer);
 
+    if (buffer->flags & WINED3D_BUFFER_PERSISTENT)
+    {
+        if (wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_PERSISTENT_MAP))
+            return;
+
+        ERR("Failed to preload persistent mapping for %p, falling back to BO.\n", buffer);
+        buffer->flags |= WINED3D_BUFFER_USE_BO;
+        buffer->flags &= ~WINED3D_BUFFER_PERSISTENT;
+    }
+
     /* TODO: Make converting independent from VBOs */
     if (!(buffer->flags & WINED3D_BUFFER_USE_BO))
     {
@@ -1010,6 +1114,25 @@
 
     count = ++buffer->resource.map_count;
 
+    if (buffer->locations & WINED3D_LOCATION_PERSISTENT_MAP)
+    {
+        const struct wined3d_gl_info *gl_info;
+        context = context_acquire(device, NULL, 0);
+
+        FIXME_(d3d_perf)("Fences not used for persistent buffer maps on CS thread, using glFinish (flags: %x)\n", flags);
+
+        gl_info = context->gl_info;
+        gl_info->gl_ops.gl.p_glFinish();
+
+        base = buffer->buffer_heap->map_ptr
+             + buffer->cs_persistent_map->range.offset;
+        *data = base + offset;
+
+        context_release(context);
+
+        return WINED3D_OK;
+    }
+
     if (buffer->buffer_object)
     {
         unsigned int dirty_offset = offset, dirty_size = size;
@@ -1152,6 +1275,12 @@
         return;
     }
 
+    if (buffer->flags & WINED3D_BUFFER_PERSISTENT)
+    {
+        TRACE("Persistent buffer, ignore unmap.\n");
+        return;
+    }
+
     if (buffer->map_ptr)
     {
         struct wined3d_device *device = buffer->resource.device;
@@ -1256,6 +1385,73 @@
         struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
 {
     struct wined3d_buffer *buffer = buffer_from_resource(resource);
+    UINT offset = box ? box->left : 0;
+
+    if (sub_resource_idx)
+    {
+        WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
+        return E_INVALIDARG;
+    }
+
+    // Support immediate mapping of persistent buffers off the command thread,
+    // which require no GL calls to interface with.
+    if (buffer->flags & WINED3D_BUFFER_PERSISTENT)
+     {
+        // Attempt to load a persistent map without syncing, if possible.
+        if (!(buffer->locations & WINED3D_LOCATION_PERSISTENT_MAP))
+        {
+            wined3d_resource_wait_idle(resource);
+            if (!buffer_alloc_persistent_map(buffer))
+            {
+                ERR_(d3d_perf)("Failed to allocate persistent buffer, falling back to sync path.");
+                return E_FAIL;
+            }
+            wined3d_buffer_validate_location(buffer, WINED3D_LOCATION_PERSISTENT_MAP);
+        }
+
+        map_desc->row_pitch = map_desc->slice_pitch = buffer->desc.byte_width;
+        if (flags & WINED3D_MAP_DISCARD)
+        {
+            HRESULT hr;
+            struct wined3d_buffer_heap_element *mt_elem;
+            if (FAILED(hr = wined3d_buffer_heap_alloc(buffer->buffer_heap, resource->size, &mt_elem)))
+            {
+                FIXME_(d3d_perf)("Failed to allocate new buffer, falling back to sync path.\n");
+                return hr;
+            }
+            map_desc->data = buffer->buffer_heap->map_ptr + mt_elem->range.offset + offset;
+            resource->map_count++;
+
+            buffer->mt_persistent_map = mt_elem;
+
+            // Discard handler on CSMT thread is responsible for returning the
+            // currently used buffer to the free pool, along with the fence that
+            // must be called before the buffer can be reused.
+            wined3d_cs_emit_discard_buffer(resource->device->cs, buffer, mt_elem);
+
+            return WINED3D_OK;
+        }
+        else if (flags & WINED3D_MAP_NOOVERWRITE)
+        {
+            // Allow immediate access for persistent buffers without a fence.
+            // Always use the latest buffer in this case in case the latest
+            // DISCARDed one hasn't reached the command stream yet.
+            struct wined3d_map_range map_range = buffer->mt_persistent_map->range;
+            map_desc->data = buffer->buffer_heap->map_ptr + map_range.offset + offset;
+            resource->map_count++;
+            return WINED3D_OK;
+        }
+
+        WARN_(d3d_perf)("Mapping persistent buffer %p in sync with CS thread.\n", buffer);
+    }
+
+    return E_NOTIMPL;
+}
+
+static HRESULT buffer_resource_sub_resource_map_cs(struct wined3d_resource *resource, unsigned int sub_resource_idx,
+        struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
+{
+    struct wined3d_buffer *buffer = buffer_from_resource(resource);
     UINT offset, size;
 
     if (sub_resource_idx)
@@ -1298,6 +1494,18 @@
 
 static HRESULT buffer_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
 {
+    struct wined3d_buffer *buffer = buffer_from_resource(resource);
+    if (buffer->locations & WINED3D_LOCATION_PERSISTENT_MAP)
+    {
+        // Nothing to be done to unmap a region of a persistent buffer.
+        resource->map_count--;
+        return WINED3D_OK;
+    }
+    return E_NOTIMPL;
+}
+
+static HRESULT buffer_resource_sub_resource_unmap_cs(struct wined3d_resource *resource, unsigned int sub_resource_idx)
+{
     if (sub_resource_idx)
     {
         WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
@@ -1317,6 +1525,8 @@
     buffer_resource_sub_resource_map,
     buffer_resource_sub_resource_map_info,
     buffer_resource_sub_resource_unmap,
+    buffer_resource_sub_resource_map_cs,
+    buffer_resource_sub_resource_unmap_cs,
 };
 
 static GLenum buffer_type_hint_from_bind_flags(const struct wined3d_gl_info *gl_info,
@@ -1392,12 +1602,34 @@
         buffer->flags |= WINED3D_BUFFER_PIN_SYSMEM;
     }
 
+    if (buffer->resource.usage & WINED3DUSAGE_DYNAMIC)
+    {
+        if (!device->use_pba)
+        {
+            WARN_(d3d_perf)("Not creating a persistent mapping for dynamic buffer %p because the PBA is disabled.\n", buffer);
+        }
+        else if (bind_flags & WINED3D_BIND_SHADER_RESOURCE)
+        {
+            FIXME_(d3d_perf)("Not using a persistent mapping for shader resource buffer %p (unimplemented)\n", buffer);
+        }
+        else
+        {
+            // If supported, use persistent mapped buffers instead of a
+            // standalone BO for dynamic buffers.
+            buffer->flags |= WINED3D_BUFFER_PERSISTENT;
+        }
+    }
+
     /* Observations show that draw_primitive_immediate_mode() is faster on
      * dynamic vertex buffers than converting + draw_primitive_arrays().
      * (Half-Life 2 and others.) */
     dynamic_buffer_ok = gl_info->supported[APPLE_FLUSH_BUFFER_RANGE] || gl_info->supported[ARB_MAP_BUFFER_RANGE];
 
-    if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
+    if (buffer->flags & WINED3D_BUFFER_PERSISTENT)
+    {
+        TRACE("Not creating a BO because a persistent mapped buffer will be used.\n");
+    }
+    else if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
     {
         TRACE("Not creating a BO because GL_ARB_vertex_buffer is not supported.\n");
     }
diff -r -u --new-file a/dlls/wined3d/buffer_heap.c b/dlls/wined3d/buffer_heap.c
--- a/dlls/wined3d/buffer_heap.c	1970-01-01 01:00:00.000000000 +0100
+++ b/dlls/wined3d/buffer_heap.c	2018-03-28 23:35:39.595966143 +0200
@@ -0,0 +1,530 @@
+/*
+ * Copyright 2018 Andrew Comminos
+ *
+ * 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 "wine/rbtree.h"
+#include "wined3d_private.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(d3d);
+WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
+
+// Arbitrary binding to use when binding the persistent buffer.
+#define BIND_TARGET GL_ARRAY_BUFFER
+
+
+struct wined3d_buffer_heap_fenced_element
+{
+    struct wined3d_buffer_heap_bin_set free_list;
+    struct wined3d_fence *fence;
+
+    struct wined3d_buffer_heap_fenced_element *next;
+};
+
+static struct wined3d_buffer_heap_element* element_new(GLsizei offset, GLsizei size)
+{
+    struct wined3d_buffer_heap_element* elem;
+    elem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct wined3d_buffer_heap_element));
+    if (!elem)
+        return NULL;
+    elem->range.offset = offset;
+    elem->range.size = size;
+    return elem;
+}
+
+static inline int bitwise_log2_floor(GLsizei size)
+{
+    // XXX(acomminos): I hope this gets unrolled.
+    for (int i = 8 * sizeof(GLsizei) - 1; i >= 0; i--)
+    {
+        if ((size >> i) & 1) {
+            return i;
+        }
+    }
+    return 0;
+}
+
+static inline int bitwise_log2_ceil(GLsizei size)
+{
+    // Add one to the floor of size if size isn't a power of two.
+    return bitwise_log2_floor(size) + !!(size & (size - 1));
+}
+
+static int element_bin(struct wined3d_buffer_heap_element *elem)
+{
+    return min(WINED3D_BUFFER_HEAP_BINS - 1, bitwise_log2_floor(elem->range.size));
+}
+
+// Inserts an element into the appropriate free list bin.
+static void element_insert_free_bin(struct wined3d_buffer_heap *heap, struct wined3d_buffer_heap_element *elem)
+{
+    if (elem->prev || elem->next)
+    {
+        ERR("Element %p in already in a free list (for some reason).\n", elem);
+    }
+
+    int bin = element_bin(elem);
+
+    elem->prev = NULL;
+    elem->next = heap->free_list.bins[bin].head;
+    if (heap->free_list.bins[bin].head)
+        heap->free_list.bins[bin].head->prev = elem;
+    heap->free_list.bins[bin].head = elem;
+
+    if (!heap->free_list.bins[bin].tail)
+        heap->free_list.bins[bin].tail = elem;
+
+    TRACE("Inserted allocation at %p of size %lld into bin %d\n", elem->range.offset, elem->range.size, bin);
+}
+
+// Removes an element from the free tree, its bin, and the coalesce list.
+static void element_remove_free(struct wined3d_buffer_heap *heap, struct wined3d_buffer_heap_element *elem)
+{
+    int bin = element_bin(elem);
+
+    if (elem->prev)
+        elem->prev->next = elem->next;
+
+    if (elem->next)
+        elem->next->prev = elem->prev;
+
+    if (elem == heap->free_list.bins[bin].head)
+        heap->free_list.bins[bin].head = elem->next;
+
+    if (elem == heap->free_list.bins[bin].tail)
+        heap->free_list.bins[bin].tail = elem->prev;
+
+    elem->prev = NULL;
+    elem->next = NULL;
+
+    TRACE("Freed allocation at %p of size %lld from bin %d\n", elem->range.offset, elem->range.size, bin);
+}
+
+static struct wined3d_buffer_heap_fenced_element* fenced_element_new(struct wined3d_buffer_heap_bin_set bins, struct wined3d_fence* fence)
+{
+    struct wined3d_buffer_heap_fenced_element* elem;
+    elem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct wined3d_buffer_heap_fenced_element));
+    if (!elem)
+        return NULL;
+    elem->free_list = bins;
+    elem->fence = fence;
+    elem->next = NULL;
+    return elem;
+}
+
+static int free_tree_compare(const void *key, const struct wine_rb_entry *entry)
+{
+    const GLsizei offset = *(const GLsizei*) key;
+    struct wined3d_buffer_heap_element *elem = WINE_RB_ENTRY_VALUE(entry, struct wined3d_buffer_heap_element, entry);
+
+    if (offset < elem->range.offset)
+        return -1;
+    if (offset > elem->range.offset)
+        return 1;
+    return 0;
+}
+
+/* Context activation is done by the caller. */
+HRESULT wined3d_buffer_heap_create(struct wined3d_context *context, GLsizeiptr size, GLsizeiptr alignment, BOOL write_only, struct wined3d_buffer_heap **buffer_heap)
+{
+    const struct wined3d_gl_info *gl_info = context->gl_info;
+    GLbitfield access_flags;
+    GLbitfield storage_flags;
+    struct wined3d_buffer_heap_element *initial_elem;
+
+    struct wined3d_buffer_heap *object;
+
+    if ((alignment & (alignment - 1)) != 0)
+    {
+        return E_FAIL;
+    }
+
+    if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
+    {
+        return E_OUTOFMEMORY;
+    }
+
+    access_flags = GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_MAP_WRITE_BIT;
+    if (!write_only)
+    {
+        access_flags |= GL_MAP_READ_BIT;
+    }
+    storage_flags = access_flags;
+    // FIXME(acomminos): So, about GL_CLIENT_STORAGE_BIT:
+    // - On NVIDIA, DMA CACHED memory is used when this flag is set. SYSTEM HEAP
+    //   memory is used without it, which (in my testing) is much faster.
+    // - On Mesa, GTT is used when this flag is set. This is what we want- we
+    //   upload to VRAM occur otherwise, which is unusably slow (on radeon).
+    //
+    // Thus, we're only going to set this on mesa for now.
+    // Hints are awful anyway.
+    if (gl_info->quirks & WINED3D_QUIRK_USE_CLIENT_STORAGE_BIT)
+    {
+        FIXME_(d3d_perf)("PBA: using GL_CLIENT_STORAGE_BIT quirk");
+        storage_flags |= GL_CLIENT_STORAGE_BIT;
+    }
+
+    GL_EXTCALL(glGenBuffers(1, &object->buffer_object));
+    checkGLcall("glGenBuffers");
+
+    context_bind_bo(context, BIND_TARGET, object->buffer_object);
+
+    GL_EXTCALL(glBufferStorage(BIND_TARGET, size, NULL, storage_flags));
+    checkGLcall("glBufferStorage");
+
+    if (!(object->map_ptr = GL_EXTCALL(glMapBufferRange(BIND_TARGET, 0, size, access_flags))))
+    {
+        ERR("Couldn't map persistent buffer.\n");
+        return -1; // FIXME(acomminos): proper error code, cleanup
+    }
+    context_bind_bo(context, BIND_TARGET, 0);
+
+    object->fenced_head = object->fenced_tail = NULL;
+    object->alignment = alignment;
+    InitializeCriticalSection(&object->temp_lock);
+
+    initial_elem = element_new(0, size);
+    // Don't bother adding the initial allocation to the coalescing tree.
+    element_insert_free_bin(object, initial_elem);
+
+    *buffer_heap = object;
+
+    return WINED3D_OK;
+}
+
+/* Context activation is done by the caller. */
+HRESULT wined3d_buffer_heap_destroy(struct wined3d_buffer_heap *heap, struct wined3d_context *context)
+{
+    const struct wined3d_gl_info *gl_info = context->gl_info;
+
+    context_bind_bo(context, BIND_TARGET, heap->buffer_object);
+    GL_EXTCALL(glUnmapBuffer(BIND_TARGET));
+    checkGLcall("glUnmapBuffer");
+    context_bind_bo(context, BIND_TARGET, 0);
+
+    GL_EXTCALL(glDeleteBuffers(1, &heap->buffer_object));
+    checkGLcall("glDeleteBuffers");
+
+    DeleteCriticalSection(&heap->temp_lock);
+
+    // TODO(acomminos): cleanup free lists, fenced list, etc.
+
+    HeapFree(GetProcessHeap(), 0, heap);
+
+    return WINED3D_OK;
+}
+
+HRESULT wined3d_buffer_heap_alloc(struct wined3d_buffer_heap *heap, GLsizeiptr size, struct wined3d_buffer_heap_element **out_elem)
+{
+    int initial_bin;
+    int initial_size = size;
+
+    EnterCriticalSection(&heap->temp_lock);
+
+    // After alignment, reduce fragmentation by rounding to next power of two.
+    // If the alignment is a power of two (which it should be), this should be
+    // no problem.
+    size = 1 << bitwise_log2_ceil(size);
+
+    // Align size values where possible.
+    if (heap->alignment && (size % heap->alignment != 0))
+        size += heap->alignment - (size % heap->alignment);
+
+    initial_bin = min(WINED3D_BUFFER_HEAP_BINS - 1, bitwise_log2_ceil(size));
+
+    for (int i = initial_bin; i < WINED3D_BUFFER_HEAP_BINS; i++)
+    {
+        struct wined3d_buffer_heap_element *elem = heap->free_list.bins[i].head;
+        if (elem)
+        {
+            struct wined3d_map_range remaining_range;
+            remaining_range.offset = elem->range.offset + size;
+            remaining_range.size = elem->range.size - size;
+
+            // Take the element from the free list, transferring ownership to
+            // the caller.
+            element_remove_free(heap, elem);
+
+            // Resize the element so that we can free the remainder.
+            elem->range.size = size;
+            *out_elem = elem;
+
+            TRACE_(d3d_perf)("Allocated %d (requested %d) at %p from bin %d (initial %d)\n", size, initial_size, elem->range.offset, i, initial_bin);
+
+            if (remaining_range.size > 0)
+            {
+                struct wined3d_buffer_heap_element *remaining_elem;
+
+                TRACE_(d3d_perf)("Imperfect fit allocated, fragmenting remainder of %lld at %p.\n", remaining_range.size, remaining_range.offset);
+
+                remaining_elem = element_new(remaining_range.offset, remaining_range.size);
+                element_insert_free_bin(heap, remaining_elem);
+            }
+
+            LeaveCriticalSection(&heap->temp_lock);
+            return WINED3D_OK;
+        }
+    }
+
+    LeaveCriticalSection(&heap->temp_lock);
+
+    FIXME_(d3d_perf)("Forcing coalesce, not enough free space in buffer heap.\n");
+    int num_coalesced;
+    if (SUCCEEDED(wined3d_buffer_heap_deferred_coalesce(heap, &num_coalesced)))
+    {
+        if (num_coalesced > 0)
+            return wined3d_buffer_heap_alloc(heap, size, out_elem);
+    }
+
+    FIXME_(d3d_perf)("Coalescing did not create new blocks, failing.\n");
+
+    return WINED3DERR_OUTOFVIDEOMEMORY;
+}
+
+HRESULT wined3d_buffer_heap_free(struct wined3d_buffer_heap *heap, struct wined3d_buffer_heap_element *elem)
+{
+    EnterCriticalSection(&heap->temp_lock);
+
+    // Only insert the element into a free bin, coalescing will occur later.
+    //
+    // Note that the reason that we pass around wined3d_buffer_heap_element
+    // instead of a range is to avoid frequent HeapAlloc/HeapFree operations
+    // when we're reusing buffers.
+    element_insert_free_bin(heap, elem);
+
+    LeaveCriticalSection(&heap->temp_lock);
+
+    return WINED3D_OK;
+}
+
+HRESULT wined3d_buffer_heap_free_fenced(struct wined3d_buffer_heap *heap, struct wined3d_device *device, struct wined3d_buffer_heap_element *elem)
+{
+    int bin_index = element_bin(elem);
+    struct wined3d_buffer_heap_bin *bin = &heap->pending_fenced_bins.bins[bin_index];
+
+    if (bin->tail)
+    {
+        bin->tail->next = elem;
+        elem->prev = bin->tail;
+        bin->tail = elem;
+    }
+    else
+    {
+        bin->head = elem;
+        bin->tail = elem;
+    }
+
+    return WINED3D_OK;
+}
+
+HRESULT wined3d_buffer_heap_cs_fence_issue(struct wined3d_buffer_heap *heap, struct wined3d_device *device)
+{
+    struct wined3d_buffer_heap_fenced_element *fenced_elem;
+    struct wined3d_fence *fence;
+    HRESULT hr;
+
+    if (heap->fenced_head)
+    {
+        // XXX(acomminos): double or triple buffer this?
+        wined3d_buffer_heap_cs_fence_wait(heap, device);
+    }
+
+    if (FAILED(hr = wined3d_fence_create(device, &fence)))
+    {
+        ERR("Failed to create fence.\n");
+        return hr;
+    }
+
+    fenced_elem = fenced_element_new(heap->pending_fenced_bins, fence);
+    if (!fenced_elem)
+        return E_OUTOFMEMORY;
+
+    TRACE_(d3d_perf)("Dispatching fenced buffer set.\n");
+    memset(&heap->pending_fenced_bins, 0, sizeof(heap->pending_fenced_bins));
+
+    // Append to end of fenced list, which works well if you assume that buffers
+    // are freed in some ascending draw call ordering.
+    if (!heap->fenced_head)
+    {
+        heap->fenced_head = fenced_elem;
+        heap->fenced_tail = fenced_elem;
+    }
+    else
+    {
+        heap->fenced_tail->next = fenced_elem;
+        heap->fenced_tail = fenced_elem;
+    }
+
+    wined3d_fence_issue(fence, device);
+    return WINED3D_OK;
+}
+
+HRESULT wined3d_buffer_heap_cs_fence_wait(struct wined3d_buffer_heap *heap, struct wined3d_device *device)
+{
+    enum wined3d_fence_result res;
+    struct wined3d_buffer_heap_fenced_element *elem = heap->fenced_head;
+    if (!elem)
+        return WINED3D_OK;
+
+    res = wined3d_fence_wait(elem->fence, device);
+    switch (res)
+    {
+        case WINED3D_FENCE_OK:
+        case WINED3D_FENCE_NOT_STARTED:
+            {
+                TRACE_(d3d_perf)("Freed fence group.\n");
+
+                EnterCriticalSection(&heap->temp_lock);
+                for (int i = 0; i < WINED3D_BUFFER_HEAP_BINS; i++)
+                {
+                    struct wined3d_buffer_heap_bin *elem_bin = &elem->free_list.bins[i];
+                    if (!elem_bin->tail)
+                        continue;
+
+                    struct wined3d_buffer_heap_bin *heap_bin = &heap->free_list.bins[i];
+                    if (heap_bin->head)
+                    {
+                        // Insert to front.
+                        elem_bin->tail->next = heap_bin->head;
+                        heap_bin->head->prev = elem_bin->tail;
+
+                        elem_bin->head->prev = NULL;
+                        heap_bin->head = elem_bin->head;
+                    }
+                    else
+                    {
+                        elem_bin->head->prev = NULL;
+                        heap_bin->head = elem_bin->head;
+                        elem_bin->tail->next = NULL;
+                        heap_bin->tail = elem_bin->tail;
+                    }
+                }
+                LeaveCriticalSection(&heap->temp_lock);
+
+                wined3d_fence_destroy(elem->fence);
+
+                heap->fenced_head = elem->next;
+                HeapFree(GetProcessHeap(), 0, elem);
+                // TODO(acomminos): bother to null out fenced_tail?
+                break;
+            }
+        default:
+            return WINED3D_OK;
+    }
+
+    return WINED3D_OK;
+}
+
+HRESULT wined3d_buffer_heap_deferred_coalesce(struct wined3d_buffer_heap *heap, int *coalesced_count)
+{
+    struct wined3d_buffer_heap_element *elem = NULL;
+    struct wined3d_buffer_heap_element *next = NULL;
+    struct wine_rb_entry *entry;
+    struct wined3d_map_range coalesced_range;
+
+    struct wine_rb_tree free_tree;
+    int num_coalesced = 0;
+
+    wine_rb_init(&free_tree, free_tree_compare);
+
+    EnterCriticalSection(&heap->temp_lock);
+
+    // TODO(acomminos): on one hand, if there's a lot of elements in the list,
+    // it's highly fragmented. on the other, we can potentially waste a decent
+    // sum of time checking for uncoalesced bins.
+    for (int i = 0; i < WINED3D_BUFFER_HEAP_BINS; i++)
+    {
+        elem = heap->free_list.bins[i].head;
+        while (elem)
+        {
+            // Insert a sentry. FIXME(acomminos): can skip this with traversal.
+            if (wine_rb_put(&free_tree, &elem->range.offset, &elem->entry) == -1)
+            {
+                ERR("Failed to insert key %x in tree.\n", elem->range.offset);
+                elem = elem->next;
+                continue;
+            }
+
+            coalesced_range = elem->range;
+
+            // Coalesce right.
+            entry = wine_rb_next(&elem->entry);
+            if (entry)
+            {
+                TRACE("Coalesced right.\n");
+                struct wined3d_buffer_heap_element *right_elem = WINE_RB_ENTRY_VALUE(entry, struct wined3d_buffer_heap_element, entry);
+                if (elem->range.offset + elem->range.size == right_elem->range.offset)
+                {
+                    coalesced_range.size += right_elem->range.size;
+
+                    wine_rb_remove(&free_tree, entry);
+                    element_remove_free(heap, right_elem);
+                    HeapFree(GetProcessHeap(), 0, right_elem);
+
+                    num_coalesced++;
+                }
+            }
+
+            // Coalesce left.
+            entry = wine_rb_prev(&elem->entry);
+            if (entry)
+            {
+                TRACE("Coalesced left.\n");
+                struct wined3d_buffer_heap_element *left_elem = WINE_RB_ENTRY_VALUE(entry, struct wined3d_buffer_heap_element, entry);
+                if (left_elem->range.offset + left_elem->range.size == coalesced_range.offset)
+                {
+                    coalesced_range.offset = left_elem->range.offset;
+                    coalesced_range.size += left_elem->range.size;
+
+                    wine_rb_remove(&free_tree, entry);
+                    element_remove_free(heap, left_elem);
+                    HeapFree(GetProcessHeap(), 0, left_elem);
+
+                    num_coalesced++;
+                }
+            }
+
+            next = elem->next;
+
+            if (elem->range.size != coalesced_range.size)
+            {
+                FIXME_(d3d_perf)("Coalesced range from (%p, %ld) to (%p, %ld)\n", elem->range.offset, elem->range.size, coalesced_range.offset, coalesced_range.size);
+
+                wine_rb_remove(&free_tree, &elem->entry);
+
+                // Move to the correct free bin.
+                element_remove_free(heap, elem);
+                elem->range = coalesced_range;
+                element_insert_free_bin(heap, elem);
+
+                wine_rb_put(&free_tree, &elem->range.offset, &elem->entry);
+            }
+
+            elem = next;
+        }
+    }
+
+    LeaveCriticalSection(&heap->temp_lock);
+
+    FIXME_(d3d_perf)("Performed %d coalesces.\n", num_coalesced);
+    if (coalesced_count)
+        *coalesced_count = num_coalesced;
+
+    return WINED3D_OK;
+}
\ No newline at end of file
diff -r -u --new-file a/dlls/wined3d/context.c b/dlls/wined3d/context.c
--- a/dlls/wined3d/context.c	2018-03-28 23:57:59.979900010 +0200
+++ b/dlls/wined3d/context.c	2018-03-28 23:22:42.726661696 +0200
@@ -4956,7 +4956,11 @@
     if (parameters->indexed)
     {
         struct wined3d_buffer *index_buffer = state->index_buffer;
-        if (!index_buffer->buffer_object || !stream_info->all_vbo)
+        if (index_buffer->cs_persistent_map)
+        {
+            idx_data = index_buffer->cs_persistent_map->range.offset;
+        }
+        else if (!index_buffer->buffer_object || !stream_info->all_vbo)
         {
             idx_data = index_buffer->resource.heap_memory;
         }
diff -r -u --new-file a/dlls/wined3d/cs.c b/dlls/wined3d/cs.c
--- a/dlls/wined3d/cs.c	2018-03-28 23:57:59.780900325 +0200
+++ b/dlls/wined3d/cs.c	2018-03-28 23:23:49.308515873 +0200
@@ -73,6 +73,7 @@
     WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW,
     WINED3D_CS_OP_COPY_UAV_COUNTER,
     WINED3D_CS_OP_GENERATE_MIPMAPS,
+    WINED3D_CS_OP_DISCARD_BUFFER,
     WINED3D_CS_OP_STOP,
 };
 
@@ -437,6 +438,13 @@
     struct wined3d_shader_resource_view *view;
 };
 
+struct wined3d_cs_discard_buffer
+{
+    enum wined3d_cs_op opcode;
+    struct wined3d_buffer *buffer;
+    struct wined3d_buffer_heap_element *map_range;
+};
+
 struct wined3d_cs_stop
 {
     enum wined3d_cs_op opcode;
@@ -465,6 +473,15 @@
     }
 
     InterlockedDecrement(&cs->pending_presents);
+
+    // FIXME(acomminos): is this the right place to put double-buffered frame
+    //                   timing based logic?
+    // FIXME(acomminos): this conditional sucks, replace with fancier feature check
+    if (cs->device->wo_buffer_heap && cs->device->cb_buffer_heap)
+    {
+        wined3d_buffer_heap_cs_fence_issue(cs->device->wo_buffer_heap, cs->device);
+        wined3d_buffer_heap_cs_fence_issue(cs->device->cb_buffer_heap, cs->device);
+    }
 }
 
 void wined3d_cs_emit_present(struct wined3d_cs *cs, struct wined3d_swapchain *swapchain,
@@ -1984,7 +2001,7 @@
     const struct wined3d_cs_map *op = data;
     struct wined3d_resource *resource = op->resource;
 
-    *op->hr = resource->resource_ops->resource_sub_resource_map(resource,
+    *op->hr = resource->resource_ops->resource_sub_resource_map_cs(resource,
             op->sub_resource_idx, op->map_desc, op->box, op->flags);
 }
 
@@ -2018,7 +2035,7 @@
     const struct wined3d_cs_unmap *op = data;
     struct wined3d_resource *resource = op->resource;
 
-    *op->hr = resource->resource_ops->resource_sub_resource_unmap(resource, op->sub_resource_idx);
+    *op->hr = resource->resource_ops->resource_sub_resource_unmap_cs(resource, op->sub_resource_idx);
 }
 
 HRESULT wined3d_cs_unmap(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx)
@@ -2417,6 +2434,53 @@
     cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
 }
 
+static void wined3d_cs_exec_discard_buffer(struct wined3d_cs *cs, const void *data)
+{
+    const struct wined3d_cs_discard_buffer *op = data;
+    struct wined3d_buffer *buffer = op->buffer;
+    HRESULT hr;
+
+    // TODO(acomminos): should call into buffer.c here instead.
+    if (FAILED(hr = wined3d_buffer_heap_free_fenced(buffer->buffer_heap, cs->device, buffer->cs_persistent_map)))
+    {
+        ERR("Failed to do a fenced free on discarded buffer %p, hr %x\n. Freeing anyway.", buffer, hr);
+        wined3d_buffer_heap_free(buffer->buffer_heap, buffer->cs_persistent_map);
+    }
+
+    buffer->cs_persistent_map = op->map_range;
+
+    // TODO(acomminos): merge this logic with buffer.c functions for standalone BOs
+    if (buffer->bind_flags & WINED3D_BIND_VERTEX_BUFFER)
+        device_invalidate_state(cs->device, STATE_STREAMSRC);
+    if (buffer->bind_flags & WINED3D_BIND_INDEX_BUFFER)
+        device_invalidate_state(cs->device, STATE_INDEXBUFFER);
+    if (buffer->bind_flags & WINED3D_BIND_CONSTANT_BUFFER)
+    {
+        device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX));
+        device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_HULL));
+        device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_DOMAIN));
+        device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY));
+        device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL));
+        device_invalidate_state(cs->device, STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_COMPUTE));
+    }
+
+    wined3d_resource_release(&op->buffer->resource);
+}
+
+void wined3d_cs_emit_discard_buffer(struct wined3d_cs *cs, struct wined3d_buffer *buffer, struct wined3d_buffer_heap_element *elem)
+{
+    struct wined3d_cs_discard_buffer *op;
+
+    op = cs->ops->require_space(cs, sizeof(*op), WINED3D_CS_QUEUE_DEFAULT);
+    op->opcode = WINED3D_CS_OP_DISCARD_BUFFER;
+    op->buffer = buffer;
+    op->map_range = elem;
+
+    wined3d_resource_acquire(&buffer->resource);
+
+    cs->ops->submit(cs, WINED3D_CS_QUEUE_DEFAULT);
+}
+
 static void wined3d_cs_emit_stop(struct wined3d_cs *cs)
 {
     struct wined3d_cs_stop *op;
@@ -2477,6 +2541,7 @@
     /* WINED3D_CS_OP_CLEAR_UNORDERED_ACCESS_VIEW */ wined3d_cs_exec_clear_unordered_access_view,
     /* WINED3D_CS_OP_COPY_UAV_COUNTER            */ wined3d_cs_exec_copy_uav_counter,
     /* WINED3D_CS_OP_GENERATE_MIPMAPS            */ wined3d_cs_exec_generate_mipmaps,
+    /* WINED3D_CS_OP_DISCARD_BUFFER              */ wined3d_cs_exec_discard_buffer,
 };
 
 static BOOL wined3d_cs_st_check_space(struct wined3d_cs *cs, size_t size, enum wined3d_cs_queue_id queue_id)
diff -r -u --new-file a/dlls/wined3d/device.c b/dlls/wined3d/device.c
--- a/dlls/wined3d/device.c	2018-03-28 23:58:00.031899927 +0200
+++ b/dlls/wined3d/device.c	2018-03-28 23:33:09.909291883 +0200
@@ -840,6 +840,66 @@
     device->null_sampler = NULL;
 }
 
+/* Context activation is done by the caller. */
+static void create_buffer_heap(struct wined3d_device *device, struct wined3d_context *context)
+{
+    const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
+    BOOL use_pba = FALSE;
+    char *env_pba_disable;
+
+    if (!gl_info->supported[ARB_BUFFER_STORAGE])
+    {
+        FIXME("Not using PBA, ARB_buffer_storage unsupported.\n");
+    }
+    else if ((env_pba_disable = getenv("PBA_DISABLE")) && *env_pba_disable != '0')
+    {
+        FIXME("Not using PBA, envvar 'PBA_DISABLE' set.\n");
+    }
+    else
+    {
+        // TODO(acomminos): kill this magic number. perhaps base on vram.
+        GLsizeiptr geo_heap_size = 512 * 1024 * 1024;
+        // We choose a constant buffer size of 128MB, the same as NVIDIA claims to
+        // use in their Direct3D driver for discarded constant buffers.
+        GLsizeiptr cb_heap_size = 128 * 1024 * 1024;
+        GLint ub_alignment;
+        HRESULT hr;
+
+        gl_info->gl_ops.gl.p_glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &ub_alignment);
+
+        // Align constant buffer heap size, in case GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT isn't a power of two (for some reason).
+        cb_heap_size -= cb_heap_size % ub_alignment;
+
+        if (FAILED(hr = wined3d_buffer_heap_create(context, geo_heap_size, 0, TRUE, &device->wo_buffer_heap)))
+        {
+            ERR("Failed to create write-only persistent buffer heap, hr %#x.\n", hr);
+            goto fail;
+        }
+
+        if (FAILED(hr = wined3d_buffer_heap_create(context, cb_heap_size, ub_alignment, TRUE, &device->cb_buffer_heap)))
+        {
+            ERR("Failed to create persistent buffer heap for constant buffers, hr %#x.\n", hr);
+            goto fail;
+        }
+
+        FIXME("Initialized PBA (geo_heap_size: %ld, cb_heap_size: %ld, ub_align: %d)\n", geo_heap_size, cb_heap_size, ub_alignment);
+
+        use_pba = TRUE;
+    }
+fail:
+    device->use_pba = use_pba;
+}
+
+/* Context activation is done by the caller. */
+static void destroy_buffer_heap(struct wined3d_device *device, struct wined3d_context *context)
+{
+    if (device->wo_buffer_heap)
+        wined3d_buffer_heap_destroy(device->wo_buffer_heap, context);
+
+    if (device->cb_buffer_heap)
+        wined3d_buffer_heap_destroy(device->cb_buffer_heap, context);
+}
+
 static LONG fullscreen_style(LONG style)
 {
     /* Make sure the window is managed, otherwise we won't get keyboard input. */
@@ -1004,6 +1064,7 @@
     device->shader_backend->shader_free_private(device);
     destroy_dummy_textures(device, context);
     destroy_default_samplers(device, context);
+	destroy_buffer_heap(device, context);
     context_release(context);
 
     while (device->context_count)
@@ -1052,6 +1113,7 @@
     context = context_acquire(device, target, 0);
     create_dummy_textures(device, context);
     create_default_samplers(device, context);
+	create_buffer_heap(device, context);
     context_release(context);
 }
 
diff -r -u --new-file a/dlls/wined3d/directx.c b/dlls/wined3d/directx.c
--- a/dlls/wined3d/directx.c	2018-03-28 23:58:00.062899878 +0200
+++ b/dlls/wined3d/directx.c	2018-03-28 23:37:33.887751410 +0200
@@ -111,6 +111,7 @@
     /* ARB */
     {"GL_ARB_base_instance",                ARB_BASE_INSTANCE             },
     {"GL_ARB_blend_func_extended",          ARB_BLEND_FUNC_EXTENDED       },
+	{"GL_ARB_buffer_storage",               ARB_BUFFER_STORAGE            },
     {"GL_ARB_clear_buffer_object",          ARB_CLEAR_BUFFER_OBJECT       },
     {"GL_ARB_clear_texture",                ARB_CLEAR_TEXTURE             },
     {"GL_ARB_clip_control",                 ARB_CLIP_CONTROL              },
@@ -148,6 +149,7 @@
     {"GL_ARB_internalformat_query2",        ARB_INTERNALFORMAT_QUERY2     },
     {"GL_ARB_map_buffer_alignment",         ARB_MAP_BUFFER_ALIGNMENT      },
     {"GL_ARB_map_buffer_range",             ARB_MAP_BUFFER_RANGE          },
+	{"GL_ARB_multi_bind",                   ARB_MULTI_BIND                },
     {"GL_ARB_multisample",                  ARB_MULTISAMPLE               },
     {"GL_ARB_multitexture",                 ARB_MULTITEXTURE              },
     {"GL_ARB_occlusion_query",              ARB_OCCLUSION_QUERY           },
@@ -944,6 +946,13 @@
     return !wined3d_caps_gl_ctx_test_viewport_subpixel_bits(ctx);
 }
 
+static BOOL match_mesa(const struct wined3d_gl_info *gl_info, struct wined3d_caps_gl_ctx *ctx,
+        const char *gl_renderer, enum wined3d_gl_vendor gl_vendor,
+        enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
+{
+    return gl_vendor == GL_VENDOR_MESA;
+}
+
 static void quirk_apple_glsl_constants(struct wined3d_gl_info *gl_info)
 {
     /* MacOS needs uniforms for relative addressing offsets. This can accumulate to quite a few uniforms.
@@ -1081,6 +1090,13 @@
     }
 }
 
+static void quirk_use_client_storage_bit(struct wined3d_gl_info *gl_info)
+{
+    // Using ARB_buffer_storage on Mesa requires the GL_CLIENT_STORAGE_BIT to be
+    // set to use GTT for immutable buffers on radeon (see PIPE_USAGE_STREAM).
+    gl_info->quirks |= WINED3D_QUIRK_USE_CLIENT_STORAGE_BIT;
+}
+
 struct driver_quirk
 {
     BOOL (*match)(const struct wined3d_gl_info *gl_info, struct wined3d_caps_gl_ctx *ctx,
@@ -1177,6 +1193,11 @@
         quirk_broken_viewport_subpixel_bits,
         "Nvidia viewport subpixel bits bug"
     },
+    {
+        match_mesa,
+        quirk_use_client_storage_bit,
+        "Use GL_CLIENT_STORAGE_BIT for persistent buffers on mesa",
+    },
 };
 
 /* Certain applications (Steam) complain if we report an outdated driver version. In general,
@@ -2713,6 +2734,8 @@
     /* GL_ARB_blend_func_extended */
     USE_GL_FUNC(glBindFragDataLocationIndexed)
     USE_GL_FUNC(glGetFragDataIndex)
+    /* GL_ARB_buffer_storage */
+    USE_GL_FUNC(glBufferStorage)
     /* GL_ARB_clear_buffer_object */
     USE_GL_FUNC(glClearBufferData)
     USE_GL_FUNC(glClearBufferSubData)
@@ -2792,6 +2815,8 @@
     /* GL_ARB_map_buffer_range */
     USE_GL_FUNC(glFlushMappedBufferRange)
     USE_GL_FUNC(glMapBufferRange)
+    /* GL_ARB_multi_bind */
+    USE_GL_FUNC(glBindBuffersRange)
     /* GL_ARB_multisample */
     USE_GL_FUNC(glSampleCoverageARB)
     /* GL_ARB_multitexture */
@@ -3951,6 +3976,7 @@
         {ARB_TEXTURE_VIEW,                 MAKEDWORD_VERSION(4, 3)},
 
         {ARB_CLEAR_TEXTURE,                MAKEDWORD_VERSION(4, 4)},
+        {ARB_MULTI_BIND,                   MAKEDWORD_VERSION(4, 4)},
 
         {ARB_CLIP_CONTROL,                 MAKEDWORD_VERSION(4, 5)},
         {ARB_CULL_DISTANCE,                MAKEDWORD_VERSION(4, 5)},
diff -r -u --new-file a/dlls/wined3d/Makefile.in b/dlls/wined3d/Makefile.in
--- a/dlls/wined3d/Makefile.in	2018-03-28 23:57:51.270913815 +0200
+++ b/dlls/wined3d/Makefile.in	2018-03-28 21:23:02.867794542 +0200
@@ -6,6 +6,7 @@
 	arb_program_shader.c \
 	ati_fragment_shader.c \
 	buffer.c \
+	buffer_heap.c \
 	context.c \
 	cs.c \
 	device.c \
diff -r -u --new-file a/dlls/wined3d/resource.c b/dlls/wined3d/resource.c
--- a/dlls/wined3d/resource.c	2018-03-28 23:57:59.708900439 +0200
+++ b/dlls/wined3d/resource.c	2018-03-28 22:54:16.100016102 +0200
@@ -340,6 +340,7 @@
 HRESULT CDECL wined3d_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
         struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
 {
+    HRESULT hr;
     TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
             resource, sub_resource_idx, map_desc, debug_box(box), flags);
 
@@ -362,9 +363,14 @@
     }
 
     flags = wined3d_resource_sanitise_map_flags(resource, flags);
-    wined3d_resource_wait_idle(resource);
-
-    return wined3d_cs_map(resource->device->cs, resource, sub_resource_idx, map_desc, box, flags);
+    if (FAILED(hr = resource->resource_ops->resource_sub_resource_map(resource, sub_resource_idx, map_desc, box, flags)))
+    {
+        TRACE_(d3d_perf)("Mapping resource %p on the command stream.\n", resource);
+        wined3d_resource_wait_idle(resource);
+        hr = wined3d_cs_map(resource->device->cs, resource, sub_resource_idx, map_desc, box, flags);
+    }
+ 
+    return hr;
 }
 
 HRESULT CDECL wined3d_resource_map_info(struct wined3d_resource *resource, unsigned int sub_resource_idx,
@@ -379,7 +385,12 @@
 {
     TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
 
-    return wined3d_cs_unmap(resource->device->cs, resource, sub_resource_idx);
+    if (FAILED(hr = resource->resource_ops->resource_sub_resource_unmap(resource, sub_resource_idx)))
+    {
+        TRACE_(d3d_perf)("Unmapping resource %p on the command stream.\n", resource);
+        hr = wined3d_cs_unmap(resource->device->cs, resource, sub_resource_idx);
+    }
+    return hr;
 }
 
 UINT CDECL wined3d_resource_update_info(struct wined3d_resource *resource, unsigned int sub_resource_idx,
diff -r -u --new-file a/dlls/wined3d/state.c b/dlls/wined3d/state.c
--- a/dlls/wined3d/state.c	2018-03-28 23:57:59.808900281 +0200
+++ b/dlls/wined3d/state.c	2018-03-28 23:06:47.167455200 +0200
@@ -4797,7 +4797,11 @@
     else
     {
         struct wined3d_buffer *ib = state->index_buffer;
-        GL_EXTCALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib->buffer_object));
+        // FIXME(acomminos): disasterous.
+        if (ib->locations & WINED3D_LOCATION_PERSISTENT_MAP)
+            GL_EXTCALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib->buffer_heap->buffer_object));
+        else
+            GL_EXTCALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib->buffer_object));
     }
 }
 
@@ -4863,6 +4867,7 @@
     enum wined3d_shader_type shader_type;
     struct wined3d_buffer *buffer;
     unsigned int i, base, count;
+    struct wined3d_bo_address bo_addr;
 
     TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
 
@@ -4872,10 +4877,49 @@
         shader_type = WINED3D_SHADER_TYPE_COMPUTE;
 
     wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, shader_type, &base, &count);
-    for (i = 0; i < count; ++i)
+
+    if (gl_info->supported[ARB_MULTI_BIND])
+    {
+        GLuint buffer_objects[count];
+        GLsizeiptr buffer_offsets[count];
+        GLsizeiptr buffer_sizes[count];
+
+        for (i = 0; i < count; ++i)
+        {
+            buffer = state->cb[shader_type][i];
+            if (buffer)
+            {
+                wined3d_buffer_get_memory(buffer, &bo_addr, buffer->locations);
+                buffer_objects[i] = bo_addr.buffer_object;
+                buffer_offsets[i] = bo_addr.addr;
+                buffer_sizes[i] = bo_addr.length;
+            }
+            else
+            {
+                buffer_objects[i] = buffer_offsets[i] = 0;
+                // The ARB_multi_bind spec states that an error may be thrown if
+                // `size` is less than or equal to zero, Thus, we specify a size for
+                // unused buffers anyway.
+                buffer_sizes[i] = 1;
+            }
+        }
+        GL_EXTCALL(glBindBuffersRange(GL_UNIFORM_BUFFER, base, count, buffer_objects, buffer_offsets, buffer_sizes));
+    }
+    else
     {
-        buffer = state->cb[shader_type][i];
-        GL_EXTCALL(glBindBufferBase(GL_UNIFORM_BUFFER, base + i, buffer ? buffer->buffer_object : 0));
+        for (i = 0; i < count; ++i)
+        {
+            buffer = state->cb[shader_type][i];
+            if (buffer)
+            {
+                wined3d_buffer_get_memory(buffer, &bo_addr, buffer->locations);
+                GL_EXTCALL(glBindBufferRange(GL_UNIFORM_BUFFER, base + i, bo_addr.buffer_object, bo_addr.addr, bo_addr.length));
+            }
+            else
+            {
+                GL_EXTCALL(glBindBufferBase(GL_UNIFORM_BUFFER, base + i, 0));
+            }
+        }
     }
     checkGLcall("bind constant buffers");
 }
diff -r -u --new-file a/dlls/wined3d/texture.c b/dlls/wined3d/texture.c
--- a/dlls/wined3d/texture.c	2018-03-28 23:57:59.729900406 +0200
+++ b/dlls/wined3d/texture.c	2018-03-28 22:58:49.906449411 +0200
@@ -2096,6 +2096,12 @@
 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
         struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
 {
+    return E_NOTIMPL;
+}
+
+static HRESULT texture_resource_sub_resource_map_cs(struct wined3d_resource *resource, unsigned int sub_resource_idx,
+        struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
+{
     const struct wined3d_format *format = resource->format;
     struct wined3d_texture_sub_resource *sub_resource;
     struct wined3d_device *device = resource->device;
@@ -2256,6 +2262,11 @@
 
 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
 {
+    return E_NOTIMPL;
+}
+
+static HRESULT texture_resource_sub_resource_unmap_cs(struct wined3d_resource *resource, unsigned int sub_resource_idx)
+{
     struct wined3d_texture_sub_resource *sub_resource;
     struct wined3d_device *device = resource->device;
     struct wined3d_context *context = NULL;
@@ -2307,6 +2318,8 @@
     texture_resource_sub_resource_map,
     texture_resource_sub_resource_map_info,
     texture_resource_sub_resource_unmap,
+    texture_resource_sub_resource_map_cs,
+    texture_resource_sub_resource_unmap_cs,
 };
 
 /* Context activation is done by the caller. */
diff -r -u --new-file a/dlls/wined3d/utils.c b/dlls/wined3d/utils.c
--- a/dlls/wined3d/utils.c	2018-03-28 23:57:59.981900006 +0200
+++ b/dlls/wined3d/utils.c	2018-03-28 22:59:19.628387862 +0200
@@ -6368,6 +6368,7 @@
     LOCATION_TO_STR(WINED3D_LOCATION_DRAWABLE);
     LOCATION_TO_STR(WINED3D_LOCATION_RB_MULTISAMPLE);
     LOCATION_TO_STR(WINED3D_LOCATION_RB_RESOLVED);
+    LOCATION_TO_STR(WINED3D_LOCATION_PERSISTENT_MAP);
 #undef LOCATION_TO_STR
     if (location)
         FIXME("Unrecognized location flag(s) %#x.\n", location);
diff -r -u --new-file a/dlls/wined3d/wined3d_gl.h b/dlls/wined3d/wined3d_gl.h
--- a/dlls/wined3d/wined3d_gl.h	2018-03-28 23:57:59.555900682 +0200
+++ b/dlls/wined3d/wined3d_gl.h	2018-03-28 23:07:20.628395936 +0200
@@ -44,6 +44,7 @@
     /* ARB */
     ARB_BASE_INSTANCE,
     ARB_BLEND_FUNC_EXTENDED,
+	ARB_BUFFER_STORAGE,
     ARB_CLEAR_BUFFER_OBJECT,
     ARB_CLEAR_TEXTURE,
     ARB_CLIP_CONTROL,
@@ -81,6 +82,7 @@
     ARB_INTERNALFORMAT_QUERY2,
     ARB_MAP_BUFFER_ALIGNMENT,
     ARB_MAP_BUFFER_RANGE,
+    ARB_MULTI_BIND,
     ARB_MULTISAMPLE,
     ARB_MULTITEXTURE,
     ARB_OCCLUSION_QUERY,
diff -r -u --new-file a/dlls/wined3d/wined3d_private.h b/dlls/wined3d/wined3d_private.h
--- a/dlls/wined3d/wined3d_private.h	2018-03-28 23:58:00.048899900 +0200
+++ b/dlls/wined3d/wined3d_private.h	2018-03-28 23:37:57.861715709 +0200
@@ -75,6 +75,7 @@
 #define WINED3D_QUIRK_INFO_LOG_SPAM             0x00000080
 #define WINED3D_QUIRK_LIMITED_TEX_FILTERING     0x00000100
 #define WINED3D_QUIRK_BROKEN_ARB_FOG            0x00000200
+#define WINED3D_QUIRK_USE_CLIENT_STORAGE_BIT    0x00000400
 
 enum wined3d_ffp_idx
 {
@@ -1470,6 +1471,7 @@
 {
     GLuint buffer_object;
     BYTE *addr;
+    GLsizeiptr length;
 };
 
 struct wined3d_const_bo_address
@@ -2929,7 +2931,8 @@
     BYTE inScene : 1;                   /* A flag to check for proper BeginScene / EndScene call pairs */
     BYTE softwareVertexProcessing : 1;  /* process vertex shaders using software or hardware */
     BYTE filter_messages : 1;
-    BYTE padding : 3;
+    BYTE use_pba : 1;                   /* A flag to use the persistent buffer allocator for dynamic buffers. */
+    BYTE padding : 2;
 
     unsigned char           surface_alignment; /* Line Alignment of surfaces                      */
 
@@ -2980,6 +2983,10 @@
     /* Context management */
     struct wined3d_context **contexts;
     UINT context_count;
+
+    /* Dynamic buffer heap */
+    struct wined3d_buffer_heap *wo_buffer_heap;
+    struct wined3d_buffer_heap *cb_buffer_heap;
 };
 
 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
@@ -3021,6 +3028,9 @@
     HRESULT (*resource_map_info)(struct wined3d_resource *resource, unsigned int sub_resource_idx,
             struct wined3d_map_info *info, DWORD flags);
     HRESULT (*resource_sub_resource_unmap)(struct wined3d_resource *resource, unsigned int sub_resource_idx);
+    HRESULT (*resource_sub_resource_map_cs)(struct wined3d_resource *resource, unsigned int sub_resource_idx,
+            struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags);
+    HRESULT (*resource_sub_resource_unmap_cs)(struct wined3d_resource *resource, unsigned int sub_resource_idx);
 };
 
 struct wined3d_resource
@@ -3324,6 +3334,7 @@
 #define WINED3D_LOCATION_DRAWABLE       0x00000040
 #define WINED3D_LOCATION_RB_MULTISAMPLE 0x00000080
 #define WINED3D_LOCATION_RB_RESOLVED    0x00000100
+#define WINED3D_LOCATION_PERSISTENT_MAP 0x00000200
 
 const char *wined3d_debug_location(DWORD location) DECLSPEC_HIDDEN;
 
@@ -3480,6 +3491,25 @@
         DWORD flags) DECLSPEC_HIDDEN;
 void state_unbind_resources(struct wined3d_state *state) DECLSPEC_HIDDEN;
 
+struct wined3d_map_range
+{
+    GLintptr offset;
+    GLsizeiptr size;
+};
+
+struct wined3d_buffer_heap_element
+{
+    struct wined3d_map_range range;
+
+    // rbtree data
+    struct wine_rb_entry entry;
+
+    // Binned free list positions
+    struct wined3d_buffer_heap_element *next;
+    struct wined3d_buffer_heap_element *prev;
+};
+
+
 enum wined3d_cs_queue_id
 {
     WINED3D_CS_QUEUE_DEFAULT = 0,
@@ -3624,6 +3654,7 @@
 void wined3d_cs_emit_update_sub_resource(struct wined3d_cs *cs, struct wined3d_resource *resource,
         unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
         unsigned int slice_pitch) DECLSPEC_HIDDEN;
+void wined3d_cs_emit_discard_buffer(struct wined3d_cs *cs, struct wined3d_buffer *buffer, struct wined3d_buffer_heap_element *map_range) DECLSPEC_HIDDEN;
 void wined3d_cs_init_object(struct wined3d_cs *cs,
         void (*callback)(void *object), void *object) DECLSPEC_HIDDEN;
 HRESULT wined3d_cs_map(struct wined3d_cs *cs, struct wined3d_resource *resource, unsigned int sub_resource_idx,
@@ -3657,12 +3688,61 @@
     CONV_POSITIONT,
 };
 
-struct wined3d_map_range
-{
-    UINT offset;
-    UINT size;
+struct wined3d_buffer_heap_fenced_element;
+
+// Number of power-of-two buckets to populate.
+#define WINED3D_BUFFER_HEAP_BINS 32
+
+struct wined3d_buffer_heap_bin
+ {
+    struct wined3d_buffer_heap_element *head;
+    struct wined3d_buffer_heap_element *tail;
 };
 
+struct wined3d_buffer_heap_bin_set
+{
+    struct wined3d_buffer_heap_bin bins[WINED3D_BUFFER_HEAP_BINS];
+ };
+ 
+// A heap that manages allocations with a single GL buffer.
+struct wined3d_buffer_heap
+{
+    GLuint buffer_object;
+    void *map_ptr;
+    GLsizeiptr alignment;
+    CRITICAL_SECTION temp_lock; // Temporary lock while we implement the fenced free list.
+
+    struct wined3d_buffer_heap_bin_set free_list;
+
+    // Elements that need to be fenced, but haven't reached the required size.
+    struct wined3d_buffer_heap_bin_set pending_fenced_bins;
+
+    // List of sets of buffers behind a common fence, in FIFO order.
+    struct wined3d_buffer_heap_fenced_element *fenced_head;
+    struct wined3d_buffer_heap_fenced_element *fenced_tail;
+};
+
+HRESULT wined3d_buffer_heap_create(struct wined3d_context *context, GLsizeiptr size, GLsizeiptr alignment, BOOL write_only, struct wined3d_buffer_heap **heap) DECLSPEC_HIDDEN;
+HRESULT wined3d_buffer_heap_destroy(struct wined3d_buffer_heap *heap, struct wined3d_context *context) DECLSPEC_HIDDEN;
+// Fetches a buffer from the heap of at least the given size.
+// Attempts to coalesce blocks under memory pressure.
+HRESULT wined3d_buffer_heap_alloc(struct wined3d_buffer_heap *heap, GLsizeiptr size, struct wined3d_buffer_heap_element** out_elem) DECLSPEC_HIDDEN;
+// Immediately frees a heap-allocated buffer segment.
+HRESULT wined3d_buffer_heap_free(struct wined3d_buffer_heap *heap, struct wined3d_buffer_heap_element *elem) DECLSPEC_HIDDEN;
+// Enqueues a buffer segment to return to the heap once its fence has been signaled.
+HRESULT wined3d_buffer_heap_free_fenced(struct wined3d_buffer_heap *heap, struct wined3d_device *device, struct wined3d_buffer_heap_element *elem) DECLSPEC_HIDDEN;
+// Issues a fence for the current set of pending fenced buffers.
+// Double-buffered: if the last fence issued has not yet been triggered, waits
+// on it.
+HRESULT wined3d_buffer_heap_cs_fence_issue(struct wined3d_buffer_heap *heap, struct wined3d_device *device) DECLSPEC_HIDDEN;
+// Waits on the next issued fence in FIFO order. Frees the fenced buffers after
+// the fence has been triggered.
+HRESULT wined3d_buffer_heap_cs_fence_wait(struct wined3d_buffer_heap *heap, struct wined3d_device *device) DECLSPEC_HIDDEN;
+// Performs deferred coalescing of buffers. To be called under memory pressure.
+// Outputs the number of coalesced regions in `num_coalesced`.
+HRESULT wined3d_buffer_heap_deferred_coalesce(struct wined3d_buffer_heap *heap, int *num_coalesced) DECLSPEC_HIDDEN;
+
+
 struct wined3d_buffer
 {
     struct wined3d_resource resource;
@@ -3687,6 +3767,11 @@
     UINT stride;                                            /* 0 if no conversion */
     enum wined3d_buffer_conversion_type *conversion_map;    /* NULL if no conversion */
     UINT conversion_stride;                                 /* 0 if no shifted conversion */
+
+    /* persistent mapped buffer */
+    struct wined3d_buffer_heap *buffer_heap;
+    struct wined3d_buffer_heap_element *cs_persistent_map;
+    struct wined3d_buffer_heap_element *mt_persistent_map;
 };
 
 static inline struct wined3d_buffer *buffer_from_resource(struct wined3d_resource *resource)