1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3bf215546Sopenharmony_ci * Copyright 2015 Patrick Rudolph <siro@das-labor.org>
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
9bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
10bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef _NINE_BUFFER9_H_
25bf215546Sopenharmony_ci#define _NINE_BUFFER9_H_
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "device9.h"
28bf215546Sopenharmony_ci#include "nine_buffer_upload.h"
29bf215546Sopenharmony_ci#include "nine_state.h"
30bf215546Sopenharmony_ci#include "resource9.h"
31bf215546Sopenharmony_ci#include "pipe/p_context.h"
32bf215546Sopenharmony_ci#include "pipe/p_defines.h"
33bf215546Sopenharmony_ci#include "pipe/p_state.h"
34bf215546Sopenharmony_ci#include "util/list.h"
35bf215546Sopenharmony_ci#include "util/u_box.h"
36bf215546Sopenharmony_ci#include "util/u_upload_mgr.h"
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistruct pipe_screen;
39bf215546Sopenharmony_cistruct pipe_context;
40bf215546Sopenharmony_cistruct pipe_transfer;
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_cistruct NineTransfer {
43bf215546Sopenharmony_ci    struct pipe_transfer *transfer;
44bf215546Sopenharmony_ci    bool is_pipe_secondary;
45bf215546Sopenharmony_ci    struct nine_subbuffer *buf; /* NULL unless subbuffer are used */
46bf215546Sopenharmony_ci    bool should_destroy_buf; /* If the subbuffer should be destroyed */
47bf215546Sopenharmony_ci};
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_cistruct NineBuffer9
50bf215546Sopenharmony_ci{
51bf215546Sopenharmony_ci    struct NineResource9 base;
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci    /* G3D */
54bf215546Sopenharmony_ci    struct NineTransfer *maps;
55bf215546Sopenharmony_ci    int nlocks, nmaps, maxmaps;
56bf215546Sopenharmony_ci    UINT size;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci    int16_t bind_count; /* to Device9->state.stream */
59bf215546Sopenharmony_ci    /* Whether only discard and nooverwrite were used so far
60bf215546Sopenharmony_ci     * for this buffer. Allows some optimization. */
61bf215546Sopenharmony_ci    boolean discard_nooverwrite_only;
62bf215546Sopenharmony_ci    boolean need_sync_if_nooverwrite;
63bf215546Sopenharmony_ci    struct nine_subbuffer *buf;
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci    /* Specific to managed buffers */
66bf215546Sopenharmony_ci    struct {
67bf215546Sopenharmony_ci        void *data;
68bf215546Sopenharmony_ci        boolean dirty;
69bf215546Sopenharmony_ci        struct pipe_box dirty_box; /* region in the resource to update */
70bf215546Sopenharmony_ci        struct pipe_box upload_pending_regions; /* region with uploads pending */
71bf215546Sopenharmony_ci        struct list_head list; /* for update_buffers */
72bf215546Sopenharmony_ci        struct list_head list2; /* for managed_buffers */
73bf215546Sopenharmony_ci        unsigned pending_upload; /* for uploads */
74bf215546Sopenharmony_ci        /* SYSTEMMEM DYNAMIC */
75bf215546Sopenharmony_ci        bool can_unsynchronized; /* Whether the upload can use nooverwrite */
76bf215546Sopenharmony_ci        struct pipe_box valid_region; /* Region in the GPU buffer with valid content */
77bf215546Sopenharmony_ci        struct pipe_box required_valid_region; /* Region that needs to be valid right now. */
78bf215546Sopenharmony_ci        struct pipe_box filled_region; /* Region in the GPU buffer filled since last discard */
79bf215546Sopenharmony_ci        unsigned num_worker_thread_syncs;
80bf215546Sopenharmony_ci        unsigned frame_count_last_discard;
81bf215546Sopenharmony_ci    } managed;
82bf215546Sopenharmony_ci};
83bf215546Sopenharmony_cistatic inline struct NineBuffer9 *
84bf215546Sopenharmony_ciNineBuffer9( void *data )
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci    return (struct NineBuffer9 *)data;
87bf215546Sopenharmony_ci}
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ciHRESULT
90bf215546Sopenharmony_ciNineBuffer9_ctor( struct NineBuffer9 *This,
91bf215546Sopenharmony_ci                        struct NineUnknownParams *pParams,
92bf215546Sopenharmony_ci                        D3DRESOURCETYPE Type,
93bf215546Sopenharmony_ci                        DWORD Usage,
94bf215546Sopenharmony_ci                        UINT Size,
95bf215546Sopenharmony_ci                        D3DPOOL Pool );
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_civoid
98bf215546Sopenharmony_ciNineBuffer9_dtor( struct NineBuffer9 *This );
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_cistruct pipe_resource *
101bf215546Sopenharmony_ciNineBuffer9_GetResource( struct NineBuffer9 *This, unsigned *offset );
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ciHRESULT NINE_WINAPI
104bf215546Sopenharmony_ciNineBuffer9_Lock( struct NineBuffer9 *This,
105bf215546Sopenharmony_ci                        UINT OffsetToLock,
106bf215546Sopenharmony_ci                        UINT SizeToLock,
107bf215546Sopenharmony_ci                        void **ppbData,
108bf215546Sopenharmony_ci                        DWORD Flags );
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ciHRESULT NINE_WINAPI
111bf215546Sopenharmony_ciNineBuffer9_Unlock( struct NineBuffer9 *This );
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_civoid
114bf215546Sopenharmony_ciNineBuffer9_Upload( struct NineBuffer9 *This );
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_cistatic void inline
117bf215546Sopenharmony_ciNineBindBufferToDevice( struct NineDevice9 *device,
118bf215546Sopenharmony_ci                        struct NineBuffer9 **slot,
119bf215546Sopenharmony_ci                        struct NineBuffer9 *buf )
120bf215546Sopenharmony_ci{
121bf215546Sopenharmony_ci    struct NineBuffer9 *old = *slot;
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci    if (buf) {
124bf215546Sopenharmony_ci        if ((buf->managed.dirty) && list_is_empty(&buf->managed.list))
125bf215546Sopenharmony_ci            list_add(&buf->managed.list, &device->update_buffers);
126bf215546Sopenharmony_ci        buf->bind_count++;
127bf215546Sopenharmony_ci    }
128bf215546Sopenharmony_ci    if (old) {
129bf215546Sopenharmony_ci        old->bind_count--;
130bf215546Sopenharmony_ci        if (!old->bind_count && old->managed.dirty)
131bf215546Sopenharmony_ci            list_delinit(&old->managed.list);
132bf215546Sopenharmony_ci    }
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci    nine_bind(slot, buf);
135bf215546Sopenharmony_ci}
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_civoid
138bf215546Sopenharmony_ciNineBuffer9_SetDirty( struct NineBuffer9 *This );
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci#define BASEBUF_REGISTER_UPDATE(b) { \
141bf215546Sopenharmony_ci    if ((b)->managed.dirty && (b)->bind_count) \
142bf215546Sopenharmony_ci        if (list_is_empty(&(b)->managed.list)) \
143bf215546Sopenharmony_ci            list_add(&(b)->managed.list, &(b)->base.base.device->update_buffers); \
144bf215546Sopenharmony_ci    }
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci#endif /* _NINE_BUFFER9_H_ */
147