The open source OpenXR runtime

c/main: Use high-level render helper

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2493>

+67 -218
+67 -218
src/xrt/compositor/main/comp_renderer.c
··· 31 31 #include "util/u_frame_times_widget.h" 32 32 33 33 #include "util/comp_render.h" 34 + #include "util/comp_high_level_render.h" 34 35 35 36 #include "main/comp_frame.h" 36 37 #include "main/comp_mirror_to_debug_gui.h" ··· 142 143 //! @} 143 144 }; 144 145 145 - struct comp_scratch_view_state 146 - { 147 - uint32_t index; 148 - 149 - bool used; 150 - }; 151 - 152 - /// Holds an array of @ref comp_scratch_view_state to match the number of views 153 - struct comp_render_scratch_state 154 - { 155 - struct comp_scratch_view_state views[2]; 156 - }; 157 - 158 - 159 - /* 160 - * 161 - * Scratch helpers. 162 - * 163 - */ 164 - 165 - /// Zeroes the object pointed to by @p crss then populates it with the image indices. 166 - static void 167 - scratch_get_init(struct comp_render_scratch_state *crss, struct comp_renderer *r, uint32_t view_count) 168 - { 169 - struct comp_compositor *c = r->c; 170 - U_ZERO(crss); 171 - 172 - for (uint32_t i = 0; i < view_count; i++) { 173 - comp_scratch_single_images_get(&c->scratch.views[i].cssi, &crss->views[i].index); 174 - } 175 - } 176 - 177 - /// Calls done or discard on each view in @p crss, depending on whether "used" is set. 178 - static void 179 - scratch_get_fini(struct comp_render_scratch_state *crss, struct comp_renderer *r, uint32_t view_count) 180 - { 181 - struct comp_compositor *c = r->c; 182 - 183 - for (uint32_t i = 0; i < view_count; i++) { 184 - if (crss->views[i].used) { 185 - comp_scratch_single_images_done(&c->scratch.views[i].cssi); 186 - } else { 187 - comp_scratch_single_images_discard(&c->scratch.views[i].cssi); 188 - } 189 - } 190 - } 191 146 192 147 /* 193 148 * ··· 882 837 static XRT_CHECK_RESULT VkResult 883 838 dispatch_graphics(struct comp_renderer *r, 884 839 struct render_gfx *render, 885 - struct comp_render_scratch_state *crss, 840 + struct chl_frame_state *frame_state, 886 841 enum comp_target_fov_source fov_source) 887 842 { 888 843 COMP_TRACE_MARKER(); ··· 894 849 // Basics 895 850 const struct comp_layer *layers = c->base.layer_accum.layers; 896 851 uint32_t layer_count = c->base.layer_accum.layer_count; 897 - bool fast_path = c->base.frame_params.one_projection_layer_fast_path; 898 - bool do_timewarp = !c->debug.atw_off; 899 852 900 853 // Resources for the distortion render target. 901 854 struct render_gfx_target_resources *rtr = &r->rtr_array[r->acquired_buffer]; 902 855 903 - // Consistency check. 904 - assert(!fast_path || c->base.layer_accum.layer_count >= 1); 905 - 906 856 // Viewport information. 907 857 struct render_viewport_data viewport_datas[XRT_MAX_VIEWS]; 908 858 calc_viewport_data(r, viewport_datas, render->r->view_count); ··· 923 873 eye_poses, // 924 874 render->r->view_count); // 925 875 926 - 927 - // The arguments for the dispatch function. 928 - struct comp_render_dispatch_data data; 929 - comp_render_initial_init( // 930 - &data, // data 931 - fast_path, // fast_path 932 - do_timewarp); // do_timewarp 933 - 934 - for (uint32_t i = 0; i < render->r->view_count; i++) { 935 - // Which image of the scratch images for this view are we using. 936 - uint32_t scratch_index = crss->views[i].index; 937 - 938 - // The set of scratch images we are using for this view. 939 - struct comp_scratch_single_images *scratch_view = &c->scratch.views[i].cssi; 940 - 941 - // The render target resources for the scratch images. 942 - struct render_gfx_target_resources *rsci_rtr = &c->scratch.views[i].targets[scratch_index]; 943 - 944 - // Scratch color image. 945 - struct render_scratch_color_image *rsci = &scratch_view->images[scratch_index]; 946 - 947 - // Use the whole scratch image. 948 - struct render_viewport_data layer_viewport_data = { 949 - .x = 0, 950 - .y = 0, 951 - .w = scratch_view->info.width, 952 - .h = scratch_view->info.height, 953 - }; 954 - 955 - // Scratch image covers the whole image. 956 - struct xrt_normalized_rect layer_norm_rect = {.x = 0.0f, .y = 0.0f, .w = 1.0f, .h = 1.0f}; 957 - 958 - VkImageView sample_view = comp_scratch_single_images_get_sample_view(scratch_view, scratch_index); 959 - 960 - comp_render_gfx_add_squash_view( // 961 - &data, // 962 - &world_poses[i], // 963 - &eye_poses[i], // 964 - &fovs[i], // 965 - rsci->image, // squash_image 966 - rsci_rtr, // squash_rtr 967 - &layer_viewport_data); // squash_viewport_data 968 - 969 - comp_render_gfx_add_target_view( // 970 - &data, // 971 - sample_view, // squash_as_src_sample_view 972 - &layer_norm_rect, // squash_as_src_norm_rect 973 - &vertex_rots[i], // target_vertex_rot 974 - &viewport_datas[i]); // target_viewport_data 975 - 976 - if (layer_count == 0) { 977 - crss->views[i].used = false; 978 - } else { 979 - crss->views[i].used = !fast_path; 980 - } 981 - } 982 - 983 - // Add the target info. 984 - comp_render_gfx_add_target(&data, rtr); 985 - 986 - // Start the graphics pipeline. 987 - render_gfx_begin(render); 988 - 989 - // Build the command buffer. 990 - comp_render_gfx_dispatch( // 991 - render, // 992 - layers, // 993 - layer_count, // 994 - &data); // 995 - 996 - // Make the command buffer submittable. 997 - render_gfx_end(render); 876 + // Does everything. 877 + chl_frame_state_gfx_default_pipeline( // 878 + frame_state, // 879 + render, // 880 + layers, // 881 + layer_count, // 882 + world_poses, // 883 + eye_poses, // 884 + fovs, // 885 + rtr, // 886 + viewport_datas, // 887 + vertex_rots); // 998 888 999 889 // Everything is ready, submit to the queue. 1000 890 ret = renderer_submit_queue(r, render->r->cmd, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT); ··· 1016 906 static XRT_CHECK_RESULT VkResult 1017 907 dispatch_compute(struct comp_renderer *r, 1018 908 struct render_compute *render, 1019 - struct comp_render_scratch_state *crss, 909 + struct chl_frame_state *frame_state, 1020 910 enum comp_target_fov_source fov_source) 1021 911 { 1022 912 COMP_TRACE_MARKER(); ··· 1028 918 // Basics 1029 919 const struct comp_layer *layers = c->base.layer_accum.layers; 1030 920 uint32_t layer_count = c->base.layer_accum.layer_count; 1031 - bool fast_path = c->base.frame_params.one_projection_layer_fast_path; 1032 - bool do_timewarp = !c->debug.atw_off; 1033 921 1034 922 // Device view information. 1035 923 struct xrt_fov fovs[XRT_MAX_VIEWS]; ··· 1045 933 1046 934 // Target Vulkan resources.. 1047 935 VkImage target_image = r->c->target->images[r->acquired_buffer].handle; 1048 - VkImageView target_image_view = r->c->target->images[r->acquired_buffer].view; 936 + VkImageView target_storage_view = r->c->target->images[r->acquired_buffer].view; 1049 937 1050 938 // Target view information. 1051 - struct render_viewport_data views[XRT_MAX_VIEWS]; 1052 - calc_viewport_data(r, views, render->r->view_count); 1053 - 1054 - // The arguments for the dispatch function. 1055 - struct comp_render_dispatch_data data; 1056 - comp_render_initial_init( // 1057 - &data, // data 1058 - fast_path, // fast_path 1059 - do_timewarp); // do_timewarp 1060 - 1061 - for (uint32_t i = 0; i < render->r->view_count; i++) { 1062 - // Which image of the scratch images for this view are we using. 1063 - uint32_t scratch_index = crss->views[i].index; 1064 - 1065 - // The set of scratch images we are using for this view. 1066 - struct comp_scratch_single_images *scratch_view = &c->scratch.views[i].cssi; 1067 - 1068 - // Scratch color image. 1069 - struct render_scratch_color_image *rsci = &scratch_view->images[scratch_index]; 1070 - 1071 - // Use the whole scratch image. 1072 - struct render_viewport_data layer_viewport_data = { 1073 - .x = 0, 1074 - .y = 0, 1075 - .w = scratch_view->info.width, 1076 - .h = scratch_view->info.height, 1077 - }; 1078 - 1079 - // Scratch image covers the whole image. 1080 - struct xrt_normalized_rect layer_norm_rect = {.x = 0.0f, .y = 0.0f, .w = 1.0f, .h = 1.0f}; 1081 - 1082 - VkImageView sample_view = comp_scratch_single_images_get_sample_view(scratch_view, scratch_index); 1083 - VkImageView storage_view = comp_scratch_single_images_get_storage_view(scratch_view, scratch_index); 1084 - 1085 - comp_render_cs_add_squash_view( // 1086 - &data, // 1087 - &world_poses[i], // 1088 - &eye_poses[i], // 1089 - &fovs[i], // 1090 - rsci->image, // squash_image 1091 - storage_view, // squash_storage_view 1092 - &layer_viewport_data); // squash_viewport_data 1093 - 1094 - comp_render_cs_add_target_view( // 1095 - &data, // 1096 - sample_view, // squash_as_src_sample_view 1097 - &layer_norm_rect, // squash_as_src_norm_rect 1098 - &views[i]); // target_viewport_data 1099 - 1100 - if (layer_count == 0) { 1101 - crss->views[i].used = false; 1102 - } else { 1103 - crss->views[i].used = !fast_path; 1104 - } 1105 - } 939 + struct render_viewport_data target_viewport_datas[XRT_MAX_VIEWS]; 940 + calc_viewport_data(r, target_viewport_datas, render->r->view_count); 1106 941 1107 - // Add the target info. 1108 - comp_render_cs_add_target( // 1109 - &data, // data 1110 - target_image, // target_image 1111 - target_image_view); // target_unorm_view 1112 - 1113 - // Start the compute pipeline. 1114 - render_compute_begin(render); 1115 - 1116 - // Build the command buffer. 1117 - comp_render_cs_dispatch( // 1118 - render, // 1119 - layers, // 1120 - layer_count, // 1121 - &data); // 1122 - 1123 - // Make the command buffer submittable. 1124 - render_compute_end(render); 942 + // Does everything. 943 + chl_frame_state_cs_default_pipeline( // 944 + frame_state, // 945 + render, // 946 + layers, // 947 + layer_count, // 948 + world_poses, // 949 + eye_poses, // 950 + fovs, // 951 + target_image, // 952 + target_storage_view, // 953 + target_viewport_datas); // 1125 954 1126 955 // Everything is ready, submit to the queue. 1127 956 ret = renderer_submit_queue(r, render->r->cmd, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); ··· 1182 1011 const uint32_t view_count = c->nr.view_count; 1183 1012 enum comp_target_fov_source fov_source = COMP_TARGET_FOV_SOURCE_DISTORTION; 1184 1013 1014 + bool fast_path = c->base.frame_params.one_projection_layer_fast_path; 1015 + bool do_timewarp = !c->debug.atw_off; 1016 + 1017 + // Consistency check. 1018 + assert(!fast_path || c->base.layer_accum.layer_count >= 1); 1019 + 1185 1020 // For scratch image debugging. 1186 - struct comp_render_scratch_state crss; 1187 - scratch_get_init(&crss, r, view_count); 1021 + struct chl_frame_state frame_state; 1022 + chl_frame_state_init( // 1023 + &frame_state, // 1024 + &c->nr, // 1025 + view_count, // 1026 + do_timewarp, // 1027 + fast_path, // 1028 + &c->scratch); // 1188 1029 1189 1030 bool use_compute = r->settings->use_compute; 1190 1031 struct render_gfx render_g = {0}; ··· 1193 1034 VkResult res = VK_SUCCESS; 1194 1035 if (use_compute) { 1195 1036 render_compute_init(&render_c, &c->nr); 1196 - res = dispatch_compute(r, &render_c, &crss, fov_source); 1037 + res = dispatch_compute(r, &render_c, &frame_state, fov_source); 1197 1038 } else { 1198 1039 render_gfx_init(&render_g, &c->nr); 1199 - res = dispatch_graphics(r, &render_g, &crss, fov_source); 1040 + res = dispatch_graphics(r, &render_g, &frame_state, fov_source); 1200 1041 } 1201 1042 if (res != VK_SUCCESS) { 1202 1043 return XRT_ERROR_VULKAN; ··· 1206 1047 if (c->peek) { 1207 1048 switch (comp_window_peek_get_eye(c->peek)) { 1208 1049 case COMP_WINDOW_PEEK_EYE_LEFT: { 1050 + uint32_t scratch_index = frame_state.scratch_state.views[0].index; 1209 1051 struct comp_scratch_single_images *view = &c->scratch.views[0].cssi; 1210 - comp_window_peek_blit( // 1211 - c->peek, // 1212 - view->images[crss.views[0].index].image, // 1213 - view->info.width, // 1214 - view->info.height); // 1052 + 1053 + comp_window_peek_blit( // 1054 + c->peek, // 1055 + view->images[scratch_index].image, // 1056 + view->info.width, // 1057 + view->info.height); // 1215 1058 } break; 1216 1059 case COMP_WINDOW_PEEK_EYE_RIGHT: { 1060 + uint32_t scratch_index = frame_state.scratch_state.views[1].index; 1217 1061 struct comp_scratch_single_images *view = &c->scratch.views[1].cssi; 1218 - comp_window_peek_blit( // 1219 - c->peek, // 1220 - view->images[crss.views[1].index].image, // 1221 - view->info.width, // 1222 - view->info.height); // 1062 + 1063 + comp_window_peek_blit( // 1064 + c->peek, // 1065 + view->images[scratch_index].image, // 1066 + view->info.width, // 1067 + view->info.height); // 1223 1068 } break; 1224 1069 case COMP_WINDOW_PEEK_EYE_BOTH: 1225 1070 /* TODO: display the undistorted image */ ··· 1245 1090 comp_mirror_fixup_ui_state(&r->mirror_to_debug_gui, c); 1246 1091 if (comp_mirror_is_ready_and_active(&r->mirror_to_debug_gui, c, predicted_display_time_ns)) { 1247 1092 1093 + uint32_t scratch_index = frame_state.scratch_state.views[0].index; 1248 1094 struct comp_scratch_single_images *view = &c->scratch.views[0].cssi; 1249 - struct render_scratch_color_image *rsci = &view->images[crss.views[0].index]; 1095 + struct render_scratch_color_image *rsci = &view->images[scratch_index]; 1250 1096 VkExtent2D extent = {view->info.width, view->info.width}; 1251 1097 1252 1098 // Used for both, want clamp to edge to no bring in black. ··· 1276 1122 */ 1277 1123 renderer_wait_queue_idle(r); 1278 1124 1279 - // Finalize the scratch images, send to debug UI if active. 1280 - scratch_get_fini(&crss, r, view_count); 1125 + /* 1126 + * Free any resources and finalize the scratch images, 1127 + * which sends them send to debug UI if it is active. 1128 + */ 1129 + chl_frame_state_fini(&frame_state); 1281 1130 1282 1131 // Check timestamps. 1283 1132 if (xret == XRT_SUCCESS) {