xref: /reactos/dll/directx/wine/d3d8/shader.c (revision 9987f029)
1 /*
2  * Copyright 2002-2003 Jason Edmeades
3  *                     Raphael Junqueira
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19 
20 #include "config.h"
21 #include "d3d8_private.h"
22 
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
24 
d3d8_vertexshader_wined3d_object_destroyed(void * parent)25 static void STDMETHODCALLTYPE d3d8_vertexshader_wined3d_object_destroyed(void *parent)
26 {
27     struct d3d8_vertex_shader *shader = parent;
28     d3d8_vertex_declaration_destroy(shader->vertex_declaration);
29     heap_free(shader);
30 }
31 
d3d8_vertex_shader_destroy(struct d3d8_vertex_shader * shader)32 void d3d8_vertex_shader_destroy(struct d3d8_vertex_shader *shader)
33 {
34     TRACE("shader %p.\n", shader);
35 
36     if (shader->wined3d_shader)
37     {
38         wined3d_mutex_lock();
39         wined3d_shader_decref(shader->wined3d_shader);
40         wined3d_mutex_unlock();
41     }
42     else
43     {
44         d3d8_vertexshader_wined3d_object_destroyed(shader);
45     }
46 }
47 
48 static const struct wined3d_parent_ops d3d8_vertexshader_wined3d_parent_ops =
49 {
50     d3d8_vertexshader_wined3d_object_destroyed,
51 };
52 
d3d8_vertexshader_create_vertexdeclaration(struct d3d8_device * device,const DWORD * declaration,DWORD shader_handle,struct d3d8_vertex_declaration ** decl_ptr)53 static HRESULT d3d8_vertexshader_create_vertexdeclaration(struct d3d8_device *device,
54         const DWORD *declaration, DWORD shader_handle, struct d3d8_vertex_declaration **decl_ptr)
55 {
56     struct d3d8_vertex_declaration *object;
57     HRESULT hr;
58 
59     TRACE("device %p, declaration %p, shader_handle %#x, decl_ptr %p.\n",
60             device, declaration, shader_handle, decl_ptr);
61 
62     if (!(object = heap_alloc_zero(sizeof(*object))))
63         return E_OUTOFMEMORY;
64 
65     hr = d3d8_vertex_declaration_init(object, device, declaration, shader_handle);
66     if (FAILED(hr))
67     {
68         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
69         heap_free(object);
70         return hr;
71     }
72 
73     TRACE("Created vertex declaration %p.\n", object);
74     *decl_ptr = object;
75 
76     return D3D_OK;
77 }
78 
d3d8_vertex_shader_init(struct d3d8_vertex_shader * shader,struct d3d8_device * device,const DWORD * declaration,const DWORD * byte_code,DWORD shader_handle,DWORD usage)79 HRESULT d3d8_vertex_shader_init(struct d3d8_vertex_shader *shader, struct d3d8_device *device,
80         const DWORD *declaration, const DWORD *byte_code, DWORD shader_handle, DWORD usage)
81 {
82     const DWORD *token = declaration;
83     HRESULT hr;
84 
85     /* Test if the vertex declaration is valid. */
86     while (D3DVSD_END() != *token)
87     {
88         D3DVSD_TOKENTYPE token_type = ((*token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
89 
90         if (token_type == D3DVSD_TOKEN_STREAMDATA && !(token_type & 0x10000000))
91         {
92             DWORD type = ((*token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
93             DWORD reg  = ((*token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
94 
95             if (reg == D3DVSDE_NORMAL && type != D3DVSDT_FLOAT3 && !byte_code)
96             {
97                 WARN("Attempt to use a non-FLOAT3 normal with the fixed-function function\n");
98                 return D3DERR_INVALIDCALL;
99             }
100         }
101         token += parse_token(token);
102     }
103 
104     hr = d3d8_vertexshader_create_vertexdeclaration(device, declaration, shader_handle, &shader->vertex_declaration);
105     if (FAILED(hr))
106     {
107         WARN("Failed to create vertex declaration, hr %#x.\n", hr);
108         return hr;
109     }
110 
111     if (byte_code)
112     {
113         struct wined3d_shader_desc desc;
114 
115         if (usage)
116             FIXME("Usage %#x not implemented.\n", usage);
117 
118         desc.byte_code = byte_code;
119         desc.byte_code_size = ~(size_t)0;
120         desc.format = WINED3D_SHADER_BYTE_CODE_FORMAT_SM1;
121         desc.input_signature.element_count = 0;
122         desc.output_signature.element_count = 0;
123         desc.patch_constant_signature.element_count = 0;
124         desc.max_version = 1;
125 
126         wined3d_mutex_lock();
127         hr = wined3d_shader_create_vs(device->wined3d_device, &desc, shader,
128                 &d3d8_vertexshader_wined3d_parent_ops, &shader->wined3d_shader);
129         wined3d_mutex_unlock();
130         if (FAILED(hr))
131         {
132             WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
133             d3d8_vertex_declaration_destroy(shader->vertex_declaration);
134             return hr;
135         }
136 
137         load_local_constants(declaration, shader->wined3d_shader);
138     }
139 
140     return D3D_OK;
141 }
142 
d3d8_pixelshader_wined3d_object_destroyed(void * parent)143 static void STDMETHODCALLTYPE d3d8_pixelshader_wined3d_object_destroyed(void *parent)
144 {
145     heap_free(parent);
146 }
147 
d3d8_pixel_shader_destroy(struct d3d8_pixel_shader * shader)148 void d3d8_pixel_shader_destroy(struct d3d8_pixel_shader *shader)
149 {
150     TRACE("shader %p.\n", shader);
151 
152     wined3d_mutex_lock();
153     wined3d_shader_decref(shader->wined3d_shader);
154     wined3d_mutex_unlock();
155 }
156 
157 static const struct wined3d_parent_ops d3d8_pixelshader_wined3d_parent_ops =
158 {
159     d3d8_pixelshader_wined3d_object_destroyed,
160 };
161 
d3d8_pixel_shader_init(struct d3d8_pixel_shader * shader,struct d3d8_device * device,const DWORD * byte_code,DWORD shader_handle)162 HRESULT d3d8_pixel_shader_init(struct d3d8_pixel_shader *shader, struct d3d8_device *device,
163         const DWORD *byte_code, DWORD shader_handle)
164 {
165     struct wined3d_shader_desc desc;
166     HRESULT hr;
167 
168     shader->handle = shader_handle;
169 
170     desc.byte_code = byte_code;
171     desc.byte_code_size = ~(size_t)0;
172     desc.format = WINED3D_SHADER_BYTE_CODE_FORMAT_SM1;
173     desc.input_signature.element_count = 0;
174     desc.output_signature.element_count = 0;
175     desc.patch_constant_signature.element_count = 0;
176     desc.max_version = 1;
177 
178     wined3d_mutex_lock();
179     hr = wined3d_shader_create_ps(device->wined3d_device, &desc, shader,
180             &d3d8_pixelshader_wined3d_parent_ops, &shader->wined3d_shader);
181     wined3d_mutex_unlock();
182     if (FAILED(hr))
183     {
184         WARN("Failed to create wined3d pixel shader, hr %#x.\n", hr);
185         return hr;
186     }
187 
188     return D3D_OK;
189 }
190