xref: /reactos/dll/directx/wine/d3d8/shader.c (revision 84ccccab)
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 "d3d8_private.h"
21 
22 static void STDMETHODCALLTYPE d3d8_vertexshader_wined3d_object_destroyed(void *parent)
23 {
24     struct d3d8_vertex_shader *shader = parent;
25     d3d8_vertex_declaration_destroy(shader->vertex_declaration);
26     HeapFree(GetProcessHeap(), 0, shader);
27 }
28 
29 void d3d8_vertex_shader_destroy(struct d3d8_vertex_shader *shader)
30 {
31     TRACE("shader %p.\n", shader);
32 
33     if (shader->wined3d_shader)
34     {
35         wined3d_mutex_lock();
36         wined3d_shader_decref(shader->wined3d_shader);
37         wined3d_mutex_unlock();
38     }
39     else
40     {
41         d3d8_vertexshader_wined3d_object_destroyed(shader);
42     }
43 }
44 
45 static const struct wined3d_parent_ops d3d8_vertexshader_wined3d_parent_ops =
46 {
47     d3d8_vertexshader_wined3d_object_destroyed,
48 };
49 
50 static HRESULT d3d8_vertexshader_create_vertexdeclaration(struct d3d8_device *device,
51         const DWORD *declaration, DWORD shader_handle, struct d3d8_vertex_declaration **decl_ptr)
52 {
53     struct d3d8_vertex_declaration *object;
54     HRESULT hr;
55 
56     TRACE("device %p, declaration %p, shader_handle %#x, decl_ptr %p.\n",
57             device, declaration, shader_handle, decl_ptr);
58 
59     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
60     if (!object)
61         return E_OUTOFMEMORY;
62 
63     hr = d3d8_vertex_declaration_init(object, device, declaration, shader_handle);
64     if (FAILED(hr))
65     {
66         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
67         HeapFree(GetProcessHeap(), 0, object);
68         return hr;
69     }
70 
71     TRACE("Created vertex declaration %p.\n", object);
72     *decl_ptr = object;
73 
74     return D3D_OK;
75 }
76 
77 HRESULT d3d8_vertex_shader_init(struct d3d8_vertex_shader *shader, struct d3d8_device *device,
78         const DWORD *declaration, const DWORD *byte_code, DWORD shader_handle, DWORD usage)
79 {
80     const DWORD *token = declaration;
81     HRESULT hr;
82 
83     /* Test if the vertex declaration is valid. */
84     while (D3DVSD_END() != *token)
85     {
86         D3DVSD_TOKENTYPE token_type = ((*token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
87 
88         if (token_type == D3DVSD_TOKEN_STREAMDATA && !(token_type & 0x10000000))
89         {
90             DWORD type = ((*token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
91             DWORD reg  = ((*token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
92 
93             if (reg == D3DVSDE_NORMAL && type != D3DVSDT_FLOAT3 && !byte_code)
94             {
95                 WARN("Attempt to use a non-FLOAT3 normal with the fixed-function function\n");
96                 return D3DERR_INVALIDCALL;
97             }
98         }
99         token += parse_token(token);
100     }
101 
102     hr = d3d8_vertexshader_create_vertexdeclaration(device, declaration, shader_handle, &shader->vertex_declaration);
103     if (FAILED(hr))
104     {
105         WARN("Failed to create vertex declaration, hr %#x.\n", hr);
106         return hr;
107     }
108 
109     if (byte_code)
110     {
111         struct wined3d_shader_desc desc;
112 
113         if (usage)
114             FIXME("Usage %#x not implemented.\n", usage);
115 
116         desc.byte_code = byte_code;
117         desc.byte_code_size = ~(size_t)0;
118         desc.format = WINED3D_SHADER_BYTE_CODE_FORMAT_SM1;
119         desc.input_signature.element_count = 0;
120         desc.output_signature.element_count = 0;
121         desc.patch_constant_signature.element_count = 0;
122         desc.max_version = 1;
123 
124         wined3d_mutex_lock();
125         hr = wined3d_shader_create_vs(device->wined3d_device, &desc, shader,
126                 &d3d8_vertexshader_wined3d_parent_ops, &shader->wined3d_shader);
127         wined3d_mutex_unlock();
128         if (FAILED(hr))
129         {
130             WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
131             d3d8_vertex_declaration_destroy(shader->vertex_declaration);
132             return hr;
133         }
134 
135         load_local_constants(declaration, shader->wined3d_shader);
136     }
137 
138     return D3D_OK;
139 }
140 
141 static void STDMETHODCALLTYPE d3d8_pixelshader_wined3d_object_destroyed(void *parent)
142 {
143     HeapFree(GetProcessHeap(), 0, parent);
144 }
145 
146 void d3d8_pixel_shader_destroy(struct d3d8_pixel_shader *shader)
147 {
148     TRACE("shader %p.\n", shader);
149 
150     wined3d_mutex_lock();
151     wined3d_shader_decref(shader->wined3d_shader);
152     wined3d_mutex_unlock();
153 }
154 
155 static const struct wined3d_parent_ops d3d8_pixelshader_wined3d_parent_ops =
156 {
157     d3d8_pixelshader_wined3d_object_destroyed,
158 };
159 
160 HRESULT d3d8_pixel_shader_init(struct d3d8_pixel_shader *shader, struct d3d8_device *device,
161         const DWORD *byte_code, DWORD shader_handle)
162 {
163     struct wined3d_shader_desc desc;
164     HRESULT hr;
165 
166     shader->handle = shader_handle;
167 
168     desc.byte_code = byte_code;
169     desc.byte_code_size = ~(size_t)0;
170     desc.format = WINED3D_SHADER_BYTE_CODE_FORMAT_SM1;
171     desc.input_signature.element_count = 0;
172     desc.output_signature.element_count = 0;
173     desc.patch_constant_signature.element_count = 0;
174     desc.max_version = 1;
175 
176     wined3d_mutex_lock();
177     hr = wined3d_shader_create_ps(device->wined3d_device, &desc, shader,
178             &d3d8_pixelshader_wined3d_parent_ops, &shader->wined3d_shader);
179     wined3d_mutex_unlock();
180     if (FAILED(hr))
181     {
182         WARN("Failed to create wined3d pixel shader, hr %#x.\n", hr);
183         return hr;
184     }
185 
186     return D3D_OK;
187 }
188