aboutsummaryrefslogtreecommitdiff
path: root/src/shader.h
blob: b8aa88bce17f92db0c8f66fccb914bff89cfafb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2013 Kahrl <kahrl@gmx.net>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef SHADER_HEADER
#define SHADER_HEADER

#include "irrlichttypes_extrabloated.h"
#include "threads.h"
#include <string>

class IGameDef;

/*
	shader.{h,cpp}: Shader handling stuff.
*/

/*
	Gets the path to a shader by first checking if the file
	  name_of_shader/filename
	exists in shader_path and if not, using the data path.

	If not found, returns "".

	Utilizes a thread-safe cache.
*/
std::string getShaderPath(const std::string &name_of_shader,
		const std::string &filename);

struct ShaderInfo
{
	std::string name;
	video::E_MATERIAL_TYPE base_material;
	video::E_MATERIAL_TYPE material;
	u8 drawtype;
	u8 material_type;
	s32 user_data;

	ShaderInfo(): name(""), base_material(video::EMT_SOLID),
		material(video::EMT_SOLID),
		drawtype(0), material_type(0) {}
	virtual ~ShaderInfo() {}
};

/*
	Setter of constants for shaders
*/

namespace irr { namespace video {
	class IMaterialRendererServices;
} }

class IShaderConstantSetter
{
public:
	virtual ~IShaderConstantSetter(){};
	virtual void onSetConstants(video::IMaterialRendererServices *services,
			bool is_highlevel) = 0;
};

/*
	ShaderSource creates and caches shaders.
*/

class IShaderSource
{
public:
	IShaderSource(){}
	virtual ~IShaderSource(){}
	virtual u32 getShaderIdDirect(const std::string &name,
		const u8 material_type, const u8 drawtype){return 0;}
	virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
	virtual u32 getShader(const std::string &name,
		const u8 material_type, const u8 drawtype){return 0;}
};

class IWritableShaderSource : public IShaderSource
{
public:
	IWritableShaderSource(){}
	virtual ~IWritableShaderSource(){}
	virtual u32 getShaderIdDirect(const std::string &name,
		const u8 material_type, const u8 drawtype){return 0;}
	virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
	virtual u32 getShader(const std::string &name,
		const u8 material_type, const u8 drawtype){return 0;}

	virtual void processQueue()=0;
	virtual void insertSourceShader(const std::string &name_of_shader,
		const std::string &filename, const std::string &program)=0;
	virtual void rebuildShaders()=0;
	virtual void addGlobalConstantSetter(IShaderConstantSetter *setter)=0;
};

IWritableShaderSource* createShaderSource(IrrlichtDevice *device);

void dumpShaderProgram(std::ostream &output_stream,
	const std::string &program_type, const std::string &program);

#endif
n class="hl opt">(m_address)); setAddress(a, b, c, d); setPort(port); } Address::Address(const IPv6AddressBytes *ipv6_bytes, u16 port) { memset(&m_address, 0, sizeof(m_address)); setAddress(ipv6_bytes); setPort(port); } // Equality (address family, address and port must be equal) bool Address::operator==(const Address &address) { if (address.m_addr_family != m_addr_family || address.m_port != m_port) return false; if (m_addr_family == AF_INET) { return m_address.ipv4.sin_addr.s_addr == address.m_address.ipv4.sin_addr.s_addr; } if (m_addr_family == AF_INET6) { return memcmp(m_address.ipv6.sin6_addr.s6_addr, address.m_address.ipv6.sin6_addr.s6_addr, 16) == 0; } return false; } bool Address::operator!=(const Address &address) { return !(*this == address); } void Address::Resolve(const char *name) { if (!name || name[0] == 0) { if (m_addr_family == AF_INET) { setAddress((u32)0); } else if (m_addr_family == AF_INET6) { setAddress((IPv6AddressBytes *)0); } return; } struct addrinfo *resolved, hints; memset(&hints, 0, sizeof(hints)); // Setup hints hints.ai_socktype = 0; hints.ai_protocol = 0; hints.ai_flags = 0; if (g_settings->getBool("enable_ipv6")) { // AF_UNSPEC allows both IPv6 and IPv4 addresses to be returned hints.ai_family = AF_UNSPEC; } else { hints.ai_family = AF_INET; } // Do getaddrinfo() int e = getaddrinfo(name, NULL, &hints, &resolved); if (e != 0) throw ResolveError(gai_strerror(e)); // Copy data if (resolved->ai_family == AF_INET) { struct sockaddr_in *t = (struct sockaddr_in *)resolved->ai_addr; m_addr_family = AF_INET; m_address.ipv4 = *t; } else if (resolved->ai_family == AF_INET6) { struct sockaddr_in6 *t = (struct sockaddr_in6 *)resolved->ai_addr; m_addr_family = AF_INET6; m_address.ipv6 = *t; } else { freeaddrinfo(resolved); throw ResolveError(""); } freeaddrinfo(resolved); } // IP address -> textual representation std::string Address::serializeString() const { // windows XP doesnt have inet_ntop, maybe use better func #ifdef _WIN32 if (m_addr_family == AF_INET) { u8 a, b, c, d; u32 addr; addr = ntohl(m_address.ipv4.sin_addr.s_addr); a = (addr & 0xFF000000) >> 24; b = (addr & 0x00FF0000) >> 16; c = (addr & 0x0000FF00) >> 8; d = (addr & 0x000000FF); return itos(a) + "." + itos(b) + "." + itos(c) + "." + itos(d); } else if (m_addr_family == AF_INET6) { std::ostringstream os; for (int i = 0; i < 16; i += 2) { u16 section = (m_address.ipv6.sin6_addr.s6_addr[i] << 8) | (m_address.ipv6.sin6_addr.s6_addr[i + 1]); os << std::hex << section; if (i < 14) os << ":"; } return os.str(); } else return std::string(""); #else char str[INET6_ADDRSTRLEN]; if (inet_ntop(m_addr_family, (m_addr_family == AF_INET) ? (void *)&(m_address.ipv4.sin_addr) : (void *)&(m_address.ipv6.sin6_addr), str, INET6_ADDRSTRLEN) == NULL) { return std::string(""); } return std::string(str); #endif } struct sockaddr_in Address::getAddress() const { return m_address.ipv4; // NOTE: NO PORT INCLUDED, use getPort() } struct sockaddr_in6 Address::getAddress6() const { return m_address.ipv6; // NOTE: NO PORT INCLUDED, use getPort() } u16 Address::getPort() const { return m_port; } int Address::getFamily() const { return m_addr_family; } bool Address::isIPv6() const { return m_addr_family == AF_INET6; } bool Address::isZero() const { if (m_addr_family == AF_INET) { return m_address.ipv4.sin_addr.s_addr == 0; } if (m_addr_family == AF_INET6) { static const char zero[16] = {0}; return memcmp(m_address.ipv6.sin6_addr.s6_addr, zero, 16) == 0; } return false; } void Address::setAddress(u32 address) { m_addr_family = AF_INET; m_address.ipv4.sin_family = AF_INET; m_address.ipv4.sin_addr.s_addr = htonl(address); } void Address::setAddress(u8 a, u8 b, u8 c, u8 d) { m_addr_family = AF_INET; m_address.ipv4.sin_family = AF_INET; u32 addr = htonl((a << 24) | (b << 16) | (c << 8) | d); m_address.ipv4.sin_addr.s_addr = addr; } void Address::setAddress(const IPv6AddressBytes *ipv6_bytes) { m_addr_family = AF_INET6; m_address.ipv6.sin6_family = AF_INET6; if (ipv6_bytes) memcpy(m_address.ipv6.sin6_addr.s6_addr, ipv6_bytes->bytes, 16); else memset(m_address.ipv6.sin6_addr.s6_addr, 0, 16); } void Address::setPort(u16 port) { m_port = port; } void Address::print(std::ostream *s) const { if (m_addr_family == AF_INET6) *s << "[" << serializeString() << "]:" << m_port; else *s << serializeString() << ":" << m_port; } bool Address::isLocalhost() const { if (isIPv6()) { static const unsigned char localhost_bytes[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; static const unsigned char mapped_ipv4_localhost[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x7f, 0, 0, 0}; auto addr = m_address.ipv6.sin6_addr.s6_addr; return memcmp(addr, localhost_bytes, 16) == 0 || memcmp(addr, mapped_ipv4_localhost, 13) == 0; } return (m_address.ipv4.sin_addr.s_addr & 0xFF) == 0x7f; }