1// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "Direct3DVertexShader9.hpp"
16
17#include "Direct3DDevice9.hpp"
18#include "Debug.hpp"
19
20namespace D3D9
21{
22	Direct3DVertexShader9::Direct3DVertexShader9(Direct3DDevice9 *device, const unsigned long *shaderToken) : device(device), vertexShader(shaderToken)
23	{
24		tokenCount = 0;
25
26		while(shaderToken[tokenCount] != 0x0000FFFF)
27		{
28			tokenCount += sw::Shader::size(shaderToken[tokenCount], (unsigned short)(shaderToken[0] & 0xFFFF)) + 1;
29		}
30
31		tokenCount += 1;
32
33		this->shaderToken = new unsigned long[tokenCount];
34		memcpy(this->shaderToken, shaderToken, tokenCount * sizeof(unsigned long));
35	}
36
37	Direct3DVertexShader9::~Direct3DVertexShader9()
38	{
39		delete[] shaderToken;
40		shaderToken = 0;
41	}
42
43	long Direct3DVertexShader9::QueryInterface(const IID &iid, void **object)
44	{
45		CriticalSection cs(device);
46
47		TRACE("");
48
49		if(iid == IID_IDirect3DVertexShader9 ||
50		   iid == IID_IUnknown)
51		{
52			AddRef();
53			*object = this;
54
55			return S_OK;
56		}
57
58		*object = 0;
59
60		return NOINTERFACE(iid);
61	}
62
63	unsigned long Direct3DVertexShader9::AddRef()
64	{
65		TRACE("");
66
67		return Unknown::AddRef();
68	}
69
70	unsigned long Direct3DVertexShader9::Release()
71	{
72		TRACE("");
73
74		return Unknown::Release();
75	}
76
77	long Direct3DVertexShader9::GetDevice(IDirect3DDevice9 **device)
78	{
79		CriticalSection cs(this->device);
80
81		TRACE("");
82
83		if(!device)
84		{
85			return INVALIDCALL();
86		}
87
88		this->device->AddRef();
89		*device = this->device;
90
91		return D3D_OK;
92	}
93
94	long Direct3DVertexShader9::GetFunction(void *data, unsigned int *size)
95	{
96		CriticalSection cs(device);
97
98		TRACE("");
99
100		if(!size)
101		{
102			return INVALIDCALL();
103		}
104
105		if(data)
106		{
107			memcpy(data, shaderToken, tokenCount * 4);
108		}
109
110		*size = tokenCount * 4;
111
112		return D3D_OK;
113	}
114
115	const sw::VertexShader *Direct3DVertexShader9::getVertexShader() const
116	{
117		return &vertexShader;
118	}
119}
120