17ec681f3Smrg/*
27ec681f3Smrg * Copyright 2018 Collabora Ltd.
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
87ec681f3Smrg * license, and/or sell copies of the Software, and to permit persons to whom
97ec681f3Smrg * the Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
197ec681f3Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
207ec681f3Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
217ec681f3Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg #ifndef ZINK_SURFACE_H
257ec681f3Smrg #define ZINK_SURFACE_H
267ec681f3Smrg
277ec681f3Smrg#include "pipe/p_state.h"
287ec681f3Smrg#include "zink_batch.h"
297ec681f3Smrg#include <vulkan/vulkan.h>
307ec681f3Smrg
317ec681f3Smrgstruct pipe_context;
327ec681f3Smrg
337ec681f3Smrgstruct zink_surface_info {
347ec681f3Smrg   VkImageCreateFlags flags;
357ec681f3Smrg   VkImageUsageFlags usage;
367ec681f3Smrg   uint32_t width;
377ec681f3Smrg   uint32_t height;
387ec681f3Smrg   uint32_t layerCount;
397ec681f3Smrg   VkFormat format;
407ec681f3Smrg};
417ec681f3Smrg
427ec681f3Smrgstruct zink_surface {
437ec681f3Smrg   struct pipe_surface base;
447ec681f3Smrg   VkImageViewCreateInfo ivci;
457ec681f3Smrg   struct zink_surface_info info; //TODO: union with fb refs
467ec681f3Smrg   uint32_t info_hash;
477ec681f3Smrg   VkImageView image_view;
487ec681f3Smrg   VkImageView simage_view;//old iview after storage replacement/rebind
497ec681f3Smrg   void *obj; //backing resource object
507ec681f3Smrg   uint32_t hash;
517ec681f3Smrg   struct zink_batch_usage *batch_uses;
527ec681f3Smrg   struct util_dynarray framebuffer_refs;
537ec681f3Smrg   struct zink_descriptor_refs desc_set_refs;
547ec681f3Smrg};
557ec681f3Smrg
567ec681f3Smrg/* wrapper object that preserves the gallium expectation of having
577ec681f3Smrg * pipe_surface::context match the context used to create the surface
587ec681f3Smrg */
597ec681f3Smrgstruct zink_ctx_surface {
607ec681f3Smrg   struct pipe_surface base;
617ec681f3Smrg   struct zink_surface *surf;
627ec681f3Smrg   struct zink_ctx_surface *transient; //zink_ctx_surface
637ec681f3Smrg   /* TODO: need replicate EXT */
647ec681f3Smrg   bool transient_init;
657ec681f3Smrg};
667ec681f3Smrg
677ec681f3Smrg/* use this cast for framebuffer surfaces */
687ec681f3Smrgstatic inline struct zink_surface *
697ec681f3Smrgzink_csurface(struct pipe_surface *psurface)
707ec681f3Smrg{
717ec681f3Smrg   return psurface ? ((struct zink_ctx_surface *)psurface)->surf : NULL;
727ec681f3Smrg}
737ec681f3Smrg
747ec681f3Smrg/* use this cast for checking transient framebuffer surfaces */
757ec681f3Smrgstatic inline struct zink_surface *
767ec681f3Smrgzink_transient_surface(struct pipe_surface *psurface)
777ec681f3Smrg{
787ec681f3Smrg   return psurface ? ((struct zink_ctx_surface *)psurface)->transient ? ((struct zink_ctx_surface *)psurface)->transient->surf : NULL : NULL;
797ec681f3Smrg}
807ec681f3Smrg
817ec681f3Smrg/* use this cast for internal surfaces */
827ec681f3Smrgstatic inline struct zink_surface *
837ec681f3Smrgzink_surface(struct pipe_surface *psurface)
847ec681f3Smrg{
857ec681f3Smrg   return (struct zink_surface *)psurface;
867ec681f3Smrg}
877ec681f3Smrg
887ec681f3Smrgvoid
897ec681f3Smrgzink_destroy_surface(struct zink_screen *screen, struct pipe_surface *psurface);
907ec681f3Smrg
917ec681f3Smrgstatic inline void
927ec681f3Smrgzink_surface_reference(struct zink_screen *screen, struct zink_surface **dst, struct zink_surface *src)
937ec681f3Smrg{
947ec681f3Smrg   struct zink_surface *old_dst = *dst;
957ec681f3Smrg
967ec681f3Smrg   if (pipe_reference_described(old_dst ? &old_dst->base.reference : NULL,
977ec681f3Smrg                                src ? &src->base.reference : NULL,
987ec681f3Smrg                                (debug_reference_descriptor)
997ec681f3Smrg                                debug_describe_surface))
1007ec681f3Smrg      zink_destroy_surface(screen, &old_dst->base);
1017ec681f3Smrg   *dst = src;
1027ec681f3Smrg}
1037ec681f3Smrg
1047ec681f3Smrgvoid
1057ec681f3Smrgzink_context_surface_init(struct pipe_context *context);
1067ec681f3Smrg
1077ec681f3SmrgVkImageViewCreateInfo
1087ec681f3Smrgcreate_ivci(struct zink_screen *screen,
1097ec681f3Smrg            struct zink_resource *res,
1107ec681f3Smrg            const struct pipe_surface *templ,
1117ec681f3Smrg            enum pipe_texture_target target);
1127ec681f3Smrg
1137ec681f3Smrgstruct pipe_surface *
1147ec681f3Smrgzink_get_surface(struct zink_context *ctx,
1157ec681f3Smrg            struct pipe_resource *pres,
1167ec681f3Smrg            const struct pipe_surface *templ,
1177ec681f3Smrg            VkImageViewCreateInfo *ivci);
1187ec681f3Smrg
1197ec681f3Smrgstatic inline VkImageViewType
1207ec681f3Smrgzink_surface_clamp_viewtype(VkImageViewType viewType, unsigned first_layer, unsigned last_layer, unsigned array_size)
1217ec681f3Smrg{
1227ec681f3Smrg   unsigned layerCount = 1 + last_layer - first_layer;
1237ec681f3Smrg   if (viewType == VK_IMAGE_VIEW_TYPE_CUBE || viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
1247ec681f3Smrg      if (first_layer == last_layer)
1257ec681f3Smrg         return VK_IMAGE_VIEW_TYPE_2D;
1267ec681f3Smrg      if (layerCount % 6 != 0 && (first_layer || layerCount != array_size))
1277ec681f3Smrg         return VK_IMAGE_VIEW_TYPE_2D_ARRAY;
1287ec681f3Smrg   }
1297ec681f3Smrg   return viewType;
1307ec681f3Smrg}
1317ec681f3Smrg
1327ec681f3Smrgbool
1337ec681f3Smrgzink_rebind_surface(struct zink_context *ctx, struct pipe_surface **psurface);
1347ec681f3Smrg
1357ec681f3Smrgstatic inline bool
1367ec681f3Smrgzink_rebind_ctx_surface(struct zink_context *ctx, struct pipe_surface **psurface)
1377ec681f3Smrg{
1387ec681f3Smrg   struct zink_ctx_surface *csurf = (struct zink_ctx_surface*)*psurface;
1397ec681f3Smrg   return zink_rebind_surface(ctx, (struct pipe_surface**)&csurf->surf);
1407ec681f3Smrg}
1417ec681f3Smrg
1427ec681f3Smrgstruct pipe_surface *
1437ec681f3Smrgzink_surface_create_null(struct zink_context *ctx, enum pipe_texture_target target, unsigned width, unsigned height, unsigned samples);
1447ec681f3Smrg#endif
145