@@ -159,13 +159,20 @@ static bool map_color_space(VkColorSpaceKHR space, struct pl_color_space *out)
159159 }
160160}
161161
162+ static inline int quant_score (int depth , int requested )
163+ {
164+ pl_assert (depth >= 0 && depth <= 16 );
165+ pl_assert (requested >= 0 && requested <= 16 );
166+ return requested <= depth ? 0 : 17 * (16 - depth ) + requested - depth ;
167+ }
168+
162169static bool pick_surf_format (pl_swapchain sw , const struct pl_color_space * hint )
163170{
164171 struct priv * p = PL_PRIV (sw );
165172 struct vk_ctx * vk = p -> vk ;
166173 pl_gpu gpu = sw -> gpu ;
167174
168- int best_score = 0 , best_id ;
175+ int best_score = -1000 , best_id ;
169176 bool wide_gamut = pl_color_primaries_is_wide_gamut (hint -> primaries );
170177 bool prefer_hdr = pl_color_transfer_is_hdr (hint -> transfer );
171178
@@ -227,6 +234,12 @@ static bool pick_surf_format(pl_swapchain sw, const struct pl_color_space *hint)
227234 default : // skip any other format
228235 continue ;
229236 }
237+ int alpha_depth = plfmt -> num_components < 4 ? 0 : PL_MIN (plfmt -> component_depth [3 ], 16 );
238+ int err = quant_score (plfmt -> component_depth [0 ], PL_MIN (p -> params .color_bits , 16 )) +
239+ quant_score (alpha_depth , PL_MIN (p -> params .alpha_bits , 16 ));
240+ // Reset score if we don't meet the requested bit depth
241+ if (err )
242+ score = - err ;
230243#ifdef __APPLE__
231244 // On Apple hardware, only these formats allow direct-to-display
232245 // rendering, so give them a slight score boost to tie-break against
@@ -274,7 +287,7 @@ static bool pick_surf_format(pl_swapchain sw, const struct pl_color_space *hint)
274287 }
275288 }
276289
277- if (! best_score ) {
290+ if (best_score == -1000 ) {
278291 PL_ERR (vk , "Failed picking any valid, renderable surface format!" );
279292 return false;
280293 }
0 commit comments