comment and add additional checks for the previous commit

This commit is contained in:
Ivan Avdeev 2023-01-30 10:21:18 -08:00
parent 3cb9ca0579
commit 71f7449a8e
4 changed files with 44 additions and 24 deletions

View File

@ -174,6 +174,9 @@ class TypeInfo:
return True
def __repr__(self):
return 'Type(type=%x is_image=%d image_format=%x count=%d)' % (self.type, self.is_image, self.image_format, self.count)
def serialize(self, out):
out.writeU32(self.type)
out.writeU32(self.count)
@ -422,7 +425,7 @@ class Resources:
if index >= 0:
res = self.__storage.getByIndex(index)
res.checkSameType(node)
res.checkSameTypeNode(node)
return index
return self.__storage.put(name, self.Resource(name, node, dependency))
@ -431,21 +434,34 @@ class Resources:
return self.__map.get(index, index)
def __sortDependencies(self):
# We should sort only once at export time
assert(not self.__map)
self.__map = dict()
# We need to make sure that all the images that are referenced by their prev_ counterparts
# have been created (i.e. listed in resources) earlier than all the referencees
for i, r in enumerate(self.__storage):
dep = r.dependency
if not dep:
continue
# Cannot R/W the same resource
assert(dep != i)
# Check that their formats are congruent
depr = self.__storage.getByIndex(dep)
if depr.type != r.type:
raise Exception('Conflicting types for resource %s (%s) and %s (%s)' % (depr.name, depr.type, r.name, r.type))
if dep < i:
continue
# It is an error to have multiple entries for the same pair
assert(i not in self.__map)
assert(dep not in self.__map)
# Just swap their externally-referenced indexes, don't swap entries in the array itself
# This should be enough so that the writer index is less than the reader one
self.__map[i] = dep
self.__map[dep] = i
r.dependency = i
@ -456,22 +472,22 @@ class Resources:
class Resource:
def __init__(self, name, node, dependency = None):
self.__name = name
self.__type = node.getType() if node else None
self.name = name
self.type = node.getType() if node else None
self.dependency = dependency
def checkSameType(self, node):
if not self.__type:
self.__type = node.getType()
def checkSameTypeNode(self, node):
if not self.type:
self.type = node.getType()
return
if self.__type != node.getType():
raise Exception('Conflicting types for resource "%s": %s != %s' % (self.__name, self.__type, type))
if self.type != node.getType():
raise Exception('Conflicting types for resource "%s": %s != %s' % (self.name, self.type, type))
def serialize(self, out):
out.writeString(self.__name)
self.__type.serialize(out)
if self.__type.is_image:
out.writeString(self.name)
self.type.serialize(out)
if self.type.is_image:
out.writeU32((self.dependency + 1) if self.dependency is not None else 0)
resources = Resources()

View File

@ -304,7 +304,7 @@ static qboolean readResources(load_context_t *ctx) {
if (is_image) {
res->image_format = READ_U32("Couldn't read image format for res %d:%s", i, res->name);
res->prev_frame_index = READ_U32("Couldn't read resource %d:%s previous frame index", i, res->name);
res->prev_frame_index_plus_1 = READ_U32("Couldn't read resource %d:%s previous frame index", i, res->name);
}
gEngine.Con_Reportf("Resource %d:%s = %08x is_image=%d image_format=%08x count=%d\n",

View File

@ -17,8 +17,9 @@ typedef struct {
uint32_t image_format;
};
// If this image is supposed to be read from previous frame
int prev_frame_index;
// Index+1 of resource image to read data from if this resource is a "previous frame" contents of another one.
// Value of zero means that it is a standalone resource. The real index is the value - 1.
int prev_frame_index_plus_1;
} vk_meatpipe_resource_t;
struct vk_meatpipe_pass_s;

View File

@ -64,7 +64,7 @@ typedef struct {
vk_resource_t resource;
xvk_image_t image;
int refcount;
int source_index;
int source_index_plus_1;
} rt_resource_t;
static struct {
@ -232,11 +232,11 @@ static void performTracing(VkCommandBuffer cmdbuf, const perform_tracing_args_t*
// Transfer previous frames before they had a chance of their resource-barrier metadata overwritten (as there's no guaranteed order for them)
for (int i = ExternalResource_COUNT; i < MAX_RESOURCES; ++i) {
rt_resource_t* const res = g_rtx.res + i;
if (!res->name[0] || !res->image.image || res->source_index <= 0)
if (!res->name[0] || !res->image.image || res->source_index_plus_1 <= 0)
continue;
ASSERT(res->source_index <= COUNTOF(g_rtx.res));
rt_resource_t *const src = g_rtx.res + res->source_index - 1;
ASSERT(res->source_index_plus_1 <= COUNTOF(g_rtx.res));
rt_resource_t *const src = g_rtx.res + res->source_index_plus_1 - 1;
// Swap resources
const vk_resource_t tmp_res = res->resource;
@ -262,7 +262,7 @@ static void performTracing(VkCommandBuffer cmdbuf, const perform_tracing_args_t*
// Clear intra-frame resources
for (int i = ExternalResource_COUNT; i < MAX_RESOURCES; ++i) {
rt_resource_t* const res = g_rtx.res + i;
if (!res->name[0] || !res->image.image || res->source_index > 0)
if (!res->name[0] || !res->image.image || res->source_index_plus_1 > 0)
continue;
res->resource.read = res->resource.write = (ray_resource_state_t){0};
@ -307,6 +307,7 @@ static void performTracing(VkCommandBuffer cmdbuf, const perform_tracing_args_t*
// Update image resource links after the prev_-related swap above
// TODO Preserve the indexes somewhere to avoid searching
// FIXME I don't really get why we need this, the pointers should have been preserved ?!
for (int i = 0; i < g_rtx.mainpipe->resources_count; ++i) {
const vk_meatpipe_resource_t *mr = g_rtx.mainpipe->resources + i;
const int index = findResource(mr->name);
@ -315,7 +316,7 @@ static void performTracing(VkCommandBuffer cmdbuf, const perform_tracing_args_t*
rt_resource_t *const res = g_rtx.res + index;
const qboolean create = !!(mr->flags & MEATPIPE_RES_CREATE);
if (create && mr->descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE)
//ASSERT(g_rtx.mainpipe_resources[i]->value.image_object == &res->image);
// THIS FAILS WHY?! ASSERT(g_rtx.mainpipe_resources[i]->value.image_object == &res->image);
g_rtx.mainpipe_resources[i]->value.image_object = &res->image;
}
@ -347,6 +348,8 @@ static void performTracing(VkCommandBuffer cmdbuf, const perform_tracing_args_t*
R_VkImageBlit( cmdbuf, &blit_args );
// TODO this is to make sure we remember image layout after image_blit
// The proper way to do this would be to teach R_VkImageBlit to properly track the image metadata (i.e. vk_resource_t state)
g_rtx.mainpipe_out->resource.write.image_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
}
DEBUG_END(cmdbuf);
@ -471,15 +474,15 @@ static void reloadMainpipe(void) {
// Resolve prev_ frame resources
for (int i = 0; i < newpipe->resources_count; ++i) {
const vk_meatpipe_resource_t *mr = newpipe->resources + i;
if (mr->prev_frame_index <= 0)
if (mr->prev_frame_index_plus_1 <= 0)
continue;
ASSERT(mr->prev_frame_index < newpipe->resources_count);
ASSERT(mr->prev_frame_index_plus_1 < newpipe->resources_count);
const int index = findResource(mr->name);
ASSERT(index >= 0);
const vk_meatpipe_resource_t *pr = newpipe->resources + (mr->prev_frame_index - 1);
const vk_meatpipe_resource_t *pr = newpipe->resources + (mr->prev_frame_index_plus_1 - 1);
const int dest_index = findResource(pr->name);
if (dest_index < 0) {
@ -487,7 +490,7 @@ static void reloadMainpipe(void) {
goto fail;
}
g_rtx.res[index].source_index = dest_index + 1;
g_rtx.res[index].source_index_plus_1 = dest_index + 1;
}
// Loading successful