Signed-off-by: Jan Sikorski jsikorski@codeweavers.com --- This is meant to be followed by more patches that manage references to all the other objects that need to be kept alive for command lists.
That includes: all the ones handled similarly to samplers (i.e. the ones that we put into dedicated arrays); wined3d_resources, which are currently acquired on every draw/dispatch call; and views on these resources, which are not handled at all at this point.
Apart from simplifying the code to some degree, it will fix the size problem of wined3d_resources arrays, which can get pretty large when the application is doing many repeated draw/dispatch calls.
v2: naming & source structure changes --- dlls/wined3d/cs.c | 83 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 5 deletions(-)
diff --git a/dlls/wined3d/cs.c b/dlls/wined3d/cs.c index 2c94b7aed76..03a9cb74ffe 100644 --- a/dlls/wined3d/cs.c +++ b/dlls/wined3d/cs.c @@ -102,6 +102,10 @@ ULONG CDECL wined3d_command_list_incref(struct wined3d_command_list *list) return refcount; }
+struct wined3d_cs_packet; +static void wined3d_cs_packet_decref_objects(const struct wined3d_cs_packet *); +static struct wined3d_cs_packet *wined3d_next_cs_packet(const uint8_t *, SIZE_T *); + ULONG CDECL wined3d_command_list_decref(struct wined3d_command_list *list) { ULONG refcount = InterlockedDecrement(&list->refcount); @@ -111,7 +115,8 @@ ULONG CDECL wined3d_command_list_decref(struct wined3d_command_list *list)
if (!refcount) { - SIZE_T i; + const struct wined3d_cs_packet *packet; + SIZE_T i, start = 0;
for (i = 0; i < list->command_list_count; ++i) wined3d_command_list_decref(list->command_lists[i]); @@ -132,6 +137,12 @@ ULONG CDECL wined3d_command_list_decref(struct wined3d_command_list *list) for (i = 0; i < list->sampler_count; ++i) wined3d_sampler_decref(list->samplers[i]);
+ while (start < list->data_size) + { + packet = wined3d_next_cs_packet(list->data, &start); + wined3d_cs_packet_decref_objects(packet); + } + wined3d_mutex_lock(); wined3d_cs_destroy_object(device->cs, wined3d_command_list_destroy_object, list); wined3d_mutex_unlock(); @@ -207,6 +218,14 @@ struct wined3d_cs_packet BYTE data[1]; };
+static struct wined3d_cs_packet *wined3d_next_cs_packet(const uint8_t *data, SIZE_T *start) +{ + struct wined3d_cs_packet *packet; + packet = (struct wined3d_cs_packet *)&data[*start]; + *start += offsetof(struct wined3d_cs_packet, data[packet->size]); + return packet; +} + struct wined3d_cs_nop { enum wined3d_cs_op opcode; @@ -3626,6 +3645,52 @@ void wined3d_cs_destroy(struct wined3d_cs *cs) heap_free(cs); }
+static void wined3d_cs_packet_decref_objects(const struct wined3d_cs_packet *packet) +{ + enum wined3d_cs_op opcode = *(const enum wined3d_cs_op *)packet->data; + unsigned int i; + + switch (opcode) + { + case WINED3D_CS_OP_SET_SAMPLERS: + { + struct wined3d_cs_set_samplers *op + = (struct wined3d_cs_set_samplers *)packet->data; + for (i = 0; i < op->count; ++i) + { + if (op->samplers[i]) + wined3d_sampler_decref(op->samplers[i]); + } + break; + } + default: + break; + } +} + +static void wined3d_cs_packet_incref_objects(struct wined3d_cs_packet *packet) +{ + enum wined3d_cs_op opcode = *(const enum wined3d_cs_op *)packet->data; + unsigned int i; + + switch (opcode) + { + case WINED3D_CS_OP_SET_SAMPLERS: + { + struct wined3d_cs_set_samplers *op + = (struct wined3d_cs_set_samplers *)packet->data; + for (i = 0; i < op->count; ++i) + { + if (op->samplers[i]) + wined3d_sampler_incref(op->samplers[i]); + } + break; + } + default: + break; + } +} + struct wined3d_deferred_context { struct wined3d_device_context c; @@ -3688,16 +3753,18 @@ static void *wined3d_deferred_context_require_space(struct wined3d_device_contex
packet = (struct wined3d_cs_packet *)((BYTE *)deferred->data + deferred->data_size); TRACE("size was %zu, adding %zu\n", (size_t)deferred->data_size, packet_size); - deferred->data_size += packet_size; packet->size = packet_size - header_size; return &packet->data; }
static void wined3d_deferred_context_submit(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id) { - assert(queue_id == WINED3D_CS_QUEUE_DEFAULT); + struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context); + struct wined3d_cs_packet *packet;
- /* Nothing to do. */ + assert(queue_id == WINED3D_CS_QUEUE_DEFAULT); + packet = wined3d_next_cs_packet(deferred->data, &deferred->data_size); + wined3d_cs_packet_incref_objects(packet); }
static void wined3d_deferred_context_finish(struct wined3d_device_context *context, enum wined3d_cs_queue_id queue_id) @@ -3979,7 +4046,7 @@ HRESULT CDECL wined3d_deferred_context_create(struct wined3d_device *device, str void CDECL wined3d_deferred_context_destroy(struct wined3d_device_context *context) { struct wined3d_deferred_context *deferred = wined3d_deferred_context_from_context(context); - SIZE_T i; + SIZE_T i, start = 0;
TRACE("context %p.\n", context);
@@ -4022,6 +4089,12 @@ void CDECL wined3d_deferred_context_destroy(struct wined3d_device_context *conte wined3d_sampler_decref(deferred->samplers[i]); heap_free(deferred->samplers);
+ while (start < deferred->data_size) + { + const struct wined3d_cs_packet *packet = wined3d_next_cs_packet(deferred->data, &start); + wined3d_cs_packet_decref_objects(packet); + } + wined3d_state_destroy(deferred->c.state); heap_free(deferred->data); heap_free(deferred);