On 7/15/22 18:31, Francisco Casas wrote:
But, that said, I don't think that either of these are prohibitive, or make things worse than the alternative.
So, just to confirm, I should replace compute_component_path() with a hlsl_deref_from_component_index() in v3. Right?
Without having seen it, I think I'd be in favor of it. In a sense it's the same function anyway, mostly, just with a few differences.
+struct hlsl_type *hlsl_type_get_component_type(struct hlsl_ctx *ctx, struct hlsl_type *type, + unsigned int index) +{ + while (!type_is_single_component(type)) + subtype_index_from_component_index(ctx, &type, &index);
+ return type; +}
+/* Returns the path of a given component within a type, given its index.
- *path_len will be set to the lenght of the path.
- Memory should be free afterwards.
- */
+static unsigned int *compute_component_path(struct hlsl_ctx *ctx, struct hlsl_type *type, + unsigned int index, unsigned int *path_len) +{ + struct hlsl_type *path_type; + unsigned int *path, path_index;
+ *path_len = 0; + path_type = type; + path_index = index; + while (!type_is_single_component(path_type)) + { + subtype_index_from_component_index(ctx, &path_type, &path_index); + ++*path_len; + } + if (!(path = hlsl_alloc(ctx, *path_len * sizeof(unsigned int) + 1))) + return NULL;
+ *path_len = 0; + path_type = type; + path_index = index; + while (!type_is_single_component(path_type)) + { + path[*path_len] = subtype_index_from_component_index(ctx, &path_type, &path_index); + ++*path_len; + }
+ return path; +}
Could we return path_type from this function, since we've already calculated it, and throw out hlsl_type_get_component_type() entirely?
We would be giving the caller the responsibility of freeing the returned path even if it is not used (which I think was a concern in v1). It also happens often that the path is required but not the component type.
So, given that the implementation of both functions is small and doesn't introduce too much overhead, I think that keeping them separate is better, but, as always, if you insist I will change it.
Oh, right, I forgot that add_cast() needed hlsl_type_get_component_type() but not this function. Never mind, then.