-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathdraw.c
769 lines (647 loc) · 21.1 KB
/
draw.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
/*
Copyright (C) 2018 Christoph Schied
Copyright (C) 2019, NVIDIA CORPORATION. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "shared/shared.h"
#include "refresh/refresh.h"
#include "client/client.h"
#include "refresh/images.h"
#include <assert.h>
#include "vkpt.h"
#include "shader/global_textures.h"
#define TEXNUM_WHITE (~0)
#define MAX_STRETCH_PICS (1<<14)
static drawStatic_t draw = {
.scale = 1.0f,
.alpha_scale = 1.0f
};
static int num_stretch_pics = 0;
typedef struct {
float x, y, w, h;
float s, t, w_s, h_t;
uint32_t color, tex_handle;
} StretchPic_t;
static clipRect_t clip_rect;
static qboolean clip_enable = qfalse;
static StretchPic_t stretch_pic_queue[MAX_STRETCH_PICS];
static VkPipelineLayout pipeline_layout_stretch_pic;
static VkPipelineLayout pipeline_layout_final_blit;
static VkRenderPass render_pass_stretch_pic;
static VkPipeline pipeline_stretch_pic;
static VkPipeline pipeline_final_blit;
static VkFramebuffer framebuffer_stretch_pic[MAX_FRAMES_IN_FLIGHT];
static BufferResource_t buf_stretch_pic_queue[MAX_FRAMES_IN_FLIGHT];
static VkDescriptorSetLayout desc_set_layout_sbo;
static VkDescriptorPool desc_pool_sbo;
static VkDescriptorSet desc_set_sbo[MAX_FRAMES_IN_FLIGHT];
VkExtent2D
vkpt_draw_get_extent()
{
return qvk.extent_unscaled;
}
static inline void enqueue_stretch_pic(
float x, float y, float w, float h,
float s1, float t1, float s2, float t2,
uint32_t color, int tex_handle)
{
if (draw.alpha_scale == 0.f)
return;
if(num_stretch_pics == MAX_STRETCH_PICS) {
Com_EPrintf("Error: stretch pic queue full!\n");
assert(0);
return;
}
assert(tex_handle);
StretchPic_t *sp = stretch_pic_queue + num_stretch_pics++;
if (clip_enable)
{
if (x >= clip_rect.right || x + w <= clip_rect.left || y >= clip_rect.bottom || y + h <= clip_rect.top)
return;
if (x < clip_rect.left)
{
float dw = clip_rect.left - x;
s1 += dw / w * (s2 - s1);
w -= dw;
x = clip_rect.left;
if (w <= 0) return;
}
if (x + w > clip_rect.right)
{
float dw = (x + w) - clip_rect.right;
s2 -= dw / w * (s2 - s1);
w -= dw;
if (w <= 0) return;
}
if (y < clip_rect.top)
{
float dh = clip_rect.top - y;
t1 += dh / h * (t2 - t1);
h -= dh;
y = clip_rect.top;
if (h <= 0) return;
}
if (y + h > clip_rect.bottom)
{
float dh = (y + h) - clip_rect.bottom;
t2 -= dh / h * (t2 - t1);
h -= dh;
if (h <= 0) return;
}
}
float width = r_config.width * draw.scale;
float height = r_config.height * draw.scale;
x = 2.0f * x / width - 1.0f;
y = 2.0f * y / height - 1.0f;
w = 2.0f * w / width;
h = 2.0f * h / height;
sp->x = x;
sp->y = y;
sp->w = w;
sp->h = h;
sp->s = s1;
sp->t = t1;
sp->w_s = s2 - s1;
sp->h_t = t2 - t1;
if (draw.alpha_scale < 1.f)
{
float alpha = (color >> 24) & 0xff;
alpha *= draw.alpha_scale;
alpha = max(0.f, min(255.f, alpha));
color = (color & 0xffffff) | ((int)(alpha) << 24);
}
sp->color = color;
sp->tex_handle = tex_handle;
if(tex_handle >= 0 && tex_handle < MAX_RIMAGES
&& !r_images[tex_handle].registration_sequence) {
sp->tex_handle = TEXNUM_WHITE;
}
}
static void
create_render_pass()
{
LOG_FUNC();
VkAttachmentDescription color_attachment = {
.format = qvk.surf_format.format,
.samples = VK_SAMPLE_COUNT_1_BIT,
.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
//.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
//.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE,
.initialLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
};
VkAttachmentReference color_attachment_ref = {
.attachment = 0, /* index in fragment shader */
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
};
VkSubpassDescription subpass = {
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
.colorAttachmentCount = 1,
.pColorAttachments = &color_attachment_ref,
};
VkSubpassDependency dependencies[] = {
{
.srcSubpass = VK_SUBPASS_EXTERNAL,
.dstSubpass = 0, /* index for own subpass */
.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
.srcAccessMask = 0, /* XXX verify */
.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT
| VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
},
};
VkRenderPassCreateInfo render_pass_info = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
.attachmentCount = 1,
.pAttachments = &color_attachment,
.subpassCount = 1,
.pSubpasses = &subpass,
.dependencyCount = LENGTH(dependencies),
.pDependencies = dependencies,
};
_VK(vkCreateRenderPass(qvk.device, &render_pass_info, NULL, &render_pass_stretch_pic));
ATTACH_LABEL_VARIABLE(render_pass_stretch_pic, RENDER_PASS);
}
VkResult
vkpt_draw_initialize()
{
num_stretch_pics = 0;
LOG_FUNC();
create_render_pass();
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
_VK(buffer_create(buf_stretch_pic_queue + i, sizeof(StretchPic_t) * MAX_STRETCH_PICS,
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT));
}
VkDescriptorSetLayoutBinding layout_bindings[] = {
{
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.descriptorCount = 1,
.binding = 0,
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
},
};
VkDescriptorSetLayoutCreateInfo layout_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.bindingCount = LENGTH(layout_bindings),
.pBindings = layout_bindings,
};
_VK(vkCreateDescriptorSetLayout(qvk.device, &layout_info, NULL, &desc_set_layout_sbo));
ATTACH_LABEL_VARIABLE(desc_set_layout_sbo, DESCRIPTOR_SET_LAYOUT);
VkDescriptorPoolSize pool_size = {
.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.descriptorCount = MAX_FRAMES_IN_FLIGHT,
};
VkDescriptorPoolCreateInfo pool_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.poolSizeCount = 1,
.pPoolSizes = &pool_size,
.maxSets = MAX_FRAMES_IN_FLIGHT,
};
_VK(vkCreateDescriptorPool(qvk.device, &pool_info, NULL, &desc_pool_sbo));
ATTACH_LABEL_VARIABLE(desc_pool_sbo, DESCRIPTOR_POOL);
VkDescriptorSetAllocateInfo descriptor_set_alloc_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.descriptorPool = desc_pool_sbo,
.descriptorSetCount = 1,
.pSetLayouts = &desc_set_layout_sbo,
};
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
_VK(vkAllocateDescriptorSets(qvk.device, &descriptor_set_alloc_info, desc_set_sbo + i));
BufferResource_t *sbo = buf_stretch_pic_queue + i;
VkDescriptorBufferInfo buf_info = {
.buffer = sbo->buffer,
.offset = 0,
.range = sizeof(stretch_pic_queue),
};
VkWriteDescriptorSet output_buf_write = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = desc_set_sbo[i],
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.descriptorCount = 1,
.pBufferInfo = &buf_info,
};
vkUpdateDescriptorSets(qvk.device, 1, &output_buf_write, 0, NULL);
}
return VK_SUCCESS;
}
VkResult
vkpt_draw_destroy()
{
LOG_FUNC();
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
buffer_destroy(buf_stretch_pic_queue + i);
}
vkDestroyRenderPass(qvk.device, render_pass_stretch_pic, NULL);
vkDestroyDescriptorPool(qvk.device, desc_pool_sbo, NULL);
vkDestroyDescriptorSetLayout(qvk.device, desc_set_layout_sbo, NULL);
return VK_SUCCESS;
}
VkResult
vkpt_draw_destroy_pipelines()
{
LOG_FUNC();
vkDestroyPipeline(qvk.device, pipeline_stretch_pic, NULL);
vkDestroyPipeline(qvk.device, pipeline_final_blit, NULL);
vkDestroyPipelineLayout(qvk.device, pipeline_layout_stretch_pic, NULL);
vkDestroyPipelineLayout(qvk.device, pipeline_layout_final_blit, NULL);
for(int i = 0; i < qvk.num_swap_chain_images; i++) {
vkDestroyFramebuffer(qvk.device, framebuffer_stretch_pic[i], NULL);
}
return VK_SUCCESS;
}
VkResult
vkpt_draw_create_pipelines()
{
LOG_FUNC();
assert(desc_set_layout_sbo);
VkDescriptorSetLayout desc_set_layouts[] = {
desc_set_layout_sbo, qvk.desc_set_layout_textures
};
CREATE_PIPELINE_LAYOUT(qvk.device, &pipeline_layout_stretch_pic,
.setLayoutCount = LENGTH(desc_set_layouts),
.pSetLayouts = desc_set_layouts
);
desc_set_layouts[0] = qvk.desc_set_layout_ubo;
CREATE_PIPELINE_LAYOUT(qvk.device, &pipeline_layout_final_blit,
.setLayoutCount = LENGTH(desc_set_layouts),
.pSetLayouts = desc_set_layouts
);
VkPipelineShaderStageCreateInfo shader_info[] = {
SHADER_STAGE(QVK_MOD_STRETCH_PIC_VERT, VK_SHADER_STAGE_VERTEX_BIT),
SHADER_STAGE(QVK_MOD_STRETCH_PIC_FRAG, VK_SHADER_STAGE_FRAGMENT_BIT)
};
VkPipelineVertexInputStateCreateInfo vertex_input_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.vertexBindingDescriptionCount = 0,
.pVertexBindingDescriptions = NULL,
.vertexAttributeDescriptionCount = 0,
.pVertexAttributeDescriptions = NULL,
};
VkPipelineInputAssemblyStateCreateInfo input_assembly_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
.primitiveRestartEnable = VK_FALSE,
};
VkViewport viewport = {
.x = 0.0f,
.y = 0.0f,
.width = (float) vkpt_draw_get_extent().width,
.height = (float) vkpt_draw_get_extent().height,
.minDepth = 0.0f,
.maxDepth = 1.0f,
};
VkRect2D scissor = {
.offset = { 0, 0 },
.extent = vkpt_draw_get_extent(),
};
VkPipelineViewportStateCreateInfo viewport_state = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
.viewportCount = 1,
.pViewports = &viewport,
.scissorCount = 1,
.pScissors = &scissor,
};
VkPipelineRasterizationStateCreateInfo rasterizer_state = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
.depthClampEnable = VK_FALSE,
.rasterizerDiscardEnable = VK_FALSE, /* skip rasterizer */
.polygonMode = VK_POLYGON_MODE_FILL,
.lineWidth = 1.0f,
.cullMode = VK_CULL_MODE_BACK_BIT,
.frontFace = VK_FRONT_FACE_CLOCKWISE,
.depthBiasEnable = VK_FALSE,
.depthBiasConstantFactor = 0.0f,
.depthBiasClamp = 0.0f,
.depthBiasSlopeFactor = 0.0f,
};
VkPipelineMultisampleStateCreateInfo multisample_state = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
.sampleShadingEnable = VK_FALSE,
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
.minSampleShading = 1.0f,
.pSampleMask = NULL,
.alphaToCoverageEnable = VK_FALSE,
.alphaToOneEnable = VK_FALSE,
};
VkPipelineColorBlendAttachmentState color_blend_attachment = {
.colorWriteMask = VK_COLOR_COMPONENT_R_BIT
| VK_COLOR_COMPONENT_G_BIT
| VK_COLOR_COMPONENT_B_BIT
| VK_COLOR_COMPONENT_A_BIT,
.blendEnable = VK_TRUE,
.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA,
.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
.colorBlendOp = VK_BLEND_OP_ADD,
.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE,
.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO,
.alphaBlendOp = VK_BLEND_OP_ADD,
};
VkPipelineColorBlendStateCreateInfo color_blend_state = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
.logicOpEnable = VK_FALSE,
.logicOp = VK_LOGIC_OP_COPY,
.attachmentCount = 1,
.pAttachments = &color_blend_attachment,
.blendConstants = { 0.0f, 0.0f, 0.0f, 0.0f },
};
VkGraphicsPipelineCreateInfo pipeline_info = {
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
.stageCount = LENGTH(shader_info),
.pStages = shader_info,
.pVertexInputState = &vertex_input_info,
.pInputAssemblyState = &input_assembly_info,
.pViewportState = &viewport_state,
.pRasterizationState = &rasterizer_state,
.pMultisampleState = &multisample_state,
.pDepthStencilState = NULL,
.pColorBlendState = &color_blend_state,
.pDynamicState = NULL,
.layout = pipeline_layout_stretch_pic,
.renderPass = render_pass_stretch_pic,
.subpass = 0,
.basePipelineHandle = VK_NULL_HANDLE,
.basePipelineIndex = -1,
};
_VK(vkCreateGraphicsPipelines(qvk.device, VK_NULL_HANDLE, 1, &pipeline_info, NULL, &pipeline_stretch_pic));
ATTACH_LABEL_VARIABLE(pipeline_stretch_pic, PIPELINE);
VkPipelineShaderStageCreateInfo shader_info_final_blit[] = {
SHADER_STAGE(QVK_MOD_FINAL_BLIT_VERT, VK_SHADER_STAGE_VERTEX_BIT),
SHADER_STAGE(QVK_MOD_FINAL_BLIT_LANCZOS_FRAG, VK_SHADER_STAGE_FRAGMENT_BIT)
};
pipeline_info.pStages = shader_info_final_blit;
pipeline_info.layout = pipeline_layout_final_blit;
_VK(vkCreateGraphicsPipelines(qvk.device, VK_NULL_HANDLE, 1, &pipeline_info, NULL, &pipeline_final_blit));
ATTACH_LABEL_VARIABLE(pipeline_final_blit, PIPELINE);
for(int i = 0; i < qvk.num_swap_chain_images; i++) {
VkImageView attachments[] = {
qvk.swap_chain_image_views[i]
};
VkFramebufferCreateInfo fb_create_info = {
.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
.renderPass = render_pass_stretch_pic,
.attachmentCount = 1,
.pAttachments = attachments,
.width = vkpt_draw_get_extent().width,
.height = vkpt_draw_get_extent().height,
.layers = 1,
};
_VK(vkCreateFramebuffer(qvk.device, &fb_create_info, NULL, framebuffer_stretch_pic + i));
ATTACH_LABEL_VARIABLE(framebuffer_stretch_pic[i], FRAMEBUFFER);
}
return VK_SUCCESS;
}
VkResult
vkpt_draw_clear_stretch_pics()
{
num_stretch_pics = 0;
return VK_SUCCESS;
}
VkResult
vkpt_draw_submit_stretch_pics(VkCommandBuffer cmd_buf)
{
if (num_stretch_pics == 0)
return VK_SUCCESS;
BufferResource_t *buf_spq = buf_stretch_pic_queue + qvk.current_frame_index;
StretchPic_t *spq_dev = (StretchPic_t *) buffer_map(buf_spq);
memcpy(spq_dev, stretch_pic_queue, sizeof(StretchPic_t) * num_stretch_pics);
buffer_unmap(buf_spq);
spq_dev = NULL;
VkRenderPassBeginInfo render_pass_info = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
.renderPass = render_pass_stretch_pic,
.framebuffer = framebuffer_stretch_pic[qvk.current_swap_chain_image_index],
.renderArea.offset = { 0, 0 },
.renderArea.extent = vkpt_draw_get_extent()
};
VkDescriptorSet desc_sets[] = {
desc_set_sbo[qvk.current_frame_index],
qvk_get_current_desc_set_textures()
};
vkCmdBeginRenderPass(cmd_buf, &render_pass_info, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
pipeline_layout_stretch_pic, 0, LENGTH(desc_sets), desc_sets, 0, 0);
vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_stretch_pic);
vkCmdDraw(cmd_buf, 4, num_stretch_pics, 0, 0);
vkCmdEndRenderPass(cmd_buf);
num_stretch_pics = 0;
return VK_SUCCESS;
}
VkResult
vkpt_final_blit_simple(VkCommandBuffer cmd_buf)
{
VkImageSubresourceRange subresource_range = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1
};
IMAGE_BARRIER(cmd_buf,
.image = qvk.swap_chain_images[qvk.current_swap_chain_image_index],
.subresourceRange = subresource_range,
.srcAccessMask = 0,
.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
);
int output_img = VKPT_IMG_TAA_OUTPUT;
IMAGE_BARRIER(cmd_buf,
.image = qvk.images[output_img],
.subresourceRange = subresource_range,
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
.oldLayout = VK_IMAGE_LAYOUT_GENERAL,
.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
);
VkOffset3D blit_size = {
.x = qvk.extent_taa_output.width,
.y = qvk.extent_taa_output.height,
.z = 1
};
VkOffset3D blit_size_unscaled = {
.x = qvk.extent_unscaled.width,.y = qvk.extent_unscaled.height,.z = 1
};
VkImageBlit img_blit = {
.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
.srcOffsets = { [1] = blit_size },
.dstOffsets = { [1] = blit_size_unscaled },
};
vkCmdBlitImage(cmd_buf,
qvk.images[output_img], VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
qvk.swap_chain_images[qvk.current_swap_chain_image_index], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1, &img_blit, VK_FILTER_NEAREST);
IMAGE_BARRIER(cmd_buf,
.image = qvk.swap_chain_images[qvk.current_swap_chain_image_index],
.subresourceRange = subresource_range,
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.dstAccessMask = 0,
.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
);
IMAGE_BARRIER(cmd_buf,
.image = qvk.images[output_img],
.subresourceRange = subresource_range,
.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_GENERAL
);
return VK_SUCCESS;
}
VkResult
vkpt_final_blit_filtered(VkCommandBuffer cmd_buf)
{
VkRenderPassBeginInfo render_pass_info = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
.renderPass = render_pass_stretch_pic,
.framebuffer = framebuffer_stretch_pic[qvk.current_swap_chain_image_index],
.renderArea.offset = { 0, 0 },
.renderArea.extent = vkpt_draw_get_extent()
};
VkDescriptorSet desc_sets[] = {
qvk.desc_set_ubo,
qvk_get_current_desc_set_textures()
};
vkCmdBeginRenderPass(cmd_buf, &render_pass_info, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
pipeline_layout_final_blit, 0, LENGTH(desc_sets), desc_sets, 0, 0);
vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_final_blit);
vkCmdDraw(cmd_buf, 4, 1, 0, 0);
vkCmdEndRenderPass(cmd_buf);
return VK_SUCCESS;
}
void R_SetClipRect_RTX(const clipRect_t *clip)
{
if (clip)
{
clip_enable = qtrue;
clip_rect = *clip;
}
else
{
clip_enable = qfalse;
}
}
void
R_ClearColor_RTX(void)
{
draw.colors[0].u32 = U32_WHITE;
draw.colors[1].u32 = U32_WHITE;
}
void
R_SetAlpha_RTX(float alpha)
{
alpha = powf(fabsf(alpha), 0.4545f); // un-sRGB the alpha
draw.colors[0].u8[3] = draw.colors[1].u8[3] = alpha * 255;
}
void
R_SetAlphaScale_RTX(float alpha)
{
draw.alpha_scale = alpha;
}
void
R_SetColor_RTX(uint32_t color)
{
draw.colors[0].u32 = color;
draw.colors[1].u8[3] = draw.colors[0].u8[3];
}
void
R_LightPoint_RTX(vec3_t origin, vec3_t light)
{
VectorSet(light, 1, 1, 1);
}
void
R_SetScale_RTX(float scale)
{
draw.scale = scale;
}
void
R_DrawStretchPic_RTX(int x, int y, int w, int h, qhandle_t pic)
{
enqueue_stretch_pic(
x, y, w, h,
0.0f, 0.0f, 1.0f, 1.0f,
draw.colors[0].u32, pic);
}
void
R_DrawPic_RTX(int x, int y, qhandle_t pic)
{
image_t *image = IMG_ForHandle(pic);
R_DrawStretchPic(x, y, image->width, image->height, pic);
}
#define DIV64 (1.0f / 64.0f)
void
R_TileClear_RTX(int x, int y, int w, int h, qhandle_t pic)
{
enqueue_stretch_pic(x, y, w, h,
x * DIV64, y * DIV64, (x + w) * DIV64, (y + h) * DIV64,
U32_WHITE, pic);
}
void
R_DrawFill8_RTX(int x, int y, int w, int h, int c)
{
if(!w || !h)
return;
enqueue_stretch_pic(x, y, w, h, 0.0f, 0.0f, 1.0f, 1.0f,
d_8to24table[c & 0xff], TEXNUM_WHITE);
}
void
R_DrawFill32_RTX(int x, int y, int w, int h, uint32_t color)
{
if(!w || !h)
return;
enqueue_stretch_pic(x, y, w, h, 0.0f, 0.0f, 1.0f, 1.0f,
color, TEXNUM_WHITE);
}
static inline void
draw_char(int x, int y, int flags, int c, qhandle_t font)
{
if ((c & 127) == 32) {
return;
}
if (flags & UI_ALTCOLOR) {
c |= 0x80;
}
if (flags & UI_XORCOLOR) {
c ^= 0x80;
}
float s = (c & 15) * 0.0625f;
float t = (c >> 4) * 0.0625f;
float eps = 1e-5f; /* fixes some ugly artifacts */
enqueue_stretch_pic(x, y, CHAR_WIDTH, CHAR_HEIGHT,
s + eps, t + eps, s + 0.0625f - eps, t + 0.0625f - eps,
draw.colors[c >> 7].u32, font);
}
void
R_DrawChar_RTX(int x, int y, int flags, int c, qhandle_t font)
{
draw_char(x, y, flags, c & 255, font);
}
int
R_DrawString_RTX(int x, int y, int flags, size_t maxlen, const char *s, qhandle_t font)
{
while(maxlen-- && *s) {
byte c = *s++;
draw_char(x, y, flags, c, font);
x += CHAR_WIDTH;
}
return x;
}
// vim: shiftwidth=4 noexpandtab tabstop=4 cindent