summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'drivers')
-rw-r--r--drivers/coreaudio/audio_driver_coreaudio.cpp304
-rw-r--r--drivers/coreaudio/audio_driver_coreaudio.h36
-rw-r--r--drivers/dummy/rasterizer_dummy.h14
-rw-r--r--drivers/gles2/rasterizer_canvas_gles2.cpp16
-rw-r--r--drivers/gles2/rasterizer_gles2.cpp2
-rw-r--r--drivers/gles2/rasterizer_scene_gles2.cpp375
-rw-r--r--drivers/gles2/rasterizer_scene_gles2.h18
-rw-r--r--drivers/gles2/rasterizer_storage_gles2.cpp191
-rw-r--r--drivers/gles2/rasterizer_storage_gles2.h51
-rw-r--r--drivers/gles2/shader_compiler_gles2.cpp4
-rw-r--r--drivers/gles2/shader_gles2.cpp59
-rw-r--r--drivers/gles2/shader_gles2.h6
-rw-r--r--drivers/gles2/shaders/scene.glsl108
-rw-r--r--drivers/gles3/rasterizer_gles3.cpp4
-rw-r--r--drivers/gles3/rasterizer_scene_gles3.cpp82
-rw-r--r--drivers/gles3/rasterizer_storage_gles3.cpp239
-rw-r--r--drivers/gles3/rasterizer_storage_gles3.h23
-rw-r--r--drivers/gles3/shader_compiler_gles3.cpp7
-rw-r--r--drivers/gles3/shader_compiler_gles3.h1
-rw-r--r--drivers/gles3/shaders/scene.glsl14
-rw-r--r--drivers/pulseaudio/audio_driver_pulseaudio.cpp350
-rw-r--r--drivers/pulseaudio/audio_driver_pulseaudio.h24
-rw-r--r--drivers/wasapi/audio_driver_wasapi.cpp529
-rw-r--r--drivers/wasapi/audio_driver_wasapi.h64
24 files changed, 1904 insertions, 617 deletions
diff --git a/drivers/coreaudio/audio_driver_coreaudio.cpp b/drivers/coreaudio/audio_driver_coreaudio.cpp
index ac21de91e4..e1f47cb8c2 100644
--- a/drivers/coreaudio/audio_driver_coreaudio.cpp
+++ b/drivers/coreaudio/audio_driver_coreaudio.cpp
@@ -35,8 +35,23 @@
#include "os/os.h"
#define kOutputBus 0
+#define kInputBus 1
#ifdef OSX_ENABLED
+OSStatus AudioDriverCoreAudio::input_device_address_cb(AudioObjectID inObjectID,
+ UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,
+ void *inClientData) {
+ AudioDriverCoreAudio *driver = (AudioDriverCoreAudio *)inClientData;
+
+ // If our selected device is the Default call set_device to update the
+ // kAudioOutputUnitProperty_CurrentDevice property
+ if (driver->capture_device_name == "Default") {
+ driver->capture_set_device("Default");
+ }
+
+ return noErr;
+}
+
OSStatus AudioDriverCoreAudio::output_device_address_cb(AudioObjectID inObjectID,
UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,
void *inClientData) {
@@ -79,6 +94,11 @@ Error AudioDriverCoreAudio::init() {
result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &prop, &output_device_address_cb, this);
ERR_FAIL_COND_V(result != noErr, FAILED);
+
+ prop.mSelector = kAudioHardwarePropertyDefaultInputDevice;
+
+ result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &prop, &input_device_address_cb, this);
+ ERR_FAIL_COND_V(result != noErr, FAILED);
#endif
AudioStreamBasicDescription strdesc;
@@ -102,6 +122,26 @@ Error AudioDriverCoreAudio::init() {
break;
}
+ zeromem(&strdesc, sizeof(strdesc));
+ size = sizeof(strdesc);
+ result = AudioUnitGetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &strdesc, &size);
+ ERR_FAIL_COND_V(result != noErr, FAILED);
+
+ switch (strdesc.mChannelsPerFrame) {
+ case 1: // Mono
+ capture_channels = 1;
+ break;
+
+ case 2: // Stereo
+ capture_channels = 2;
+ break;
+
+ default:
+ // Unknown number of channels, default to stereo
+ capture_channels = 2;
+ break;
+ }
+
mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE);
zeromem(&strdesc, sizeof(strdesc));
@@ -117,6 +157,11 @@ Error AudioDriverCoreAudio::init() {
result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &strdesc, sizeof(strdesc));
ERR_FAIL_COND_V(result != noErr, FAILED);
+ strdesc.mChannelsPerFrame = capture_channels;
+
+ result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &strdesc, sizeof(strdesc));
+ ERR_FAIL_COND_V(result != noErr, FAILED);
+
int latency = GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY);
// Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels)
buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
@@ -126,8 +171,12 @@ Error AudioDriverCoreAudio::init() {
ERR_FAIL_COND_V(result != noErr, FAILED);
#endif
- buffer_size = buffer_frames * channels;
+ unsigned int buffer_size = buffer_frames * channels;
samples_in.resize(buffer_size);
+ input_buf.resize(buffer_size);
+ input_buffer.resize(buffer_size * 8);
+ input_position = 0;
+ input_size = 0;
if (OS::get_singleton()->is_stdout_verbose()) {
print_line("CoreAudio: detected " + itos(channels) + " channels");
@@ -141,6 +190,12 @@ Error AudioDriverCoreAudio::init() {
result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback));
ERR_FAIL_COND_V(result != noErr, FAILED);
+ zeromem(&callback, sizeof(AURenderCallbackStruct));
+ callback.inputProc = &AudioDriverCoreAudio::input_callback;
+ callback.inputProcRefCon = this;
+ result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &callback, sizeof(callback));
+ ERR_FAIL_COND_V(result != noErr, FAILED);
+
result = AudioUnitInitialize(audio_unit);
ERR_FAIL_COND_V(result != noErr, FAILED);
@@ -192,6 +247,45 @@ OSStatus AudioDriverCoreAudio::output_callback(void *inRefCon,
return 0;
};
+OSStatus AudioDriverCoreAudio::input_callback(void *inRefCon,
+ AudioUnitRenderActionFlags *ioActionFlags,
+ const AudioTimeStamp *inTimeStamp,
+ UInt32 inBusNumber, UInt32 inNumberFrames,
+ AudioBufferList *ioData) {
+
+ AudioDriverCoreAudio *ad = (AudioDriverCoreAudio *)inRefCon;
+ if (!ad->active) {
+ return 0;
+ }
+
+ ad->lock();
+
+ AudioBufferList bufferList;
+ bufferList.mNumberBuffers = 1;
+ bufferList.mBuffers[0].mData = ad->input_buf.ptrw();
+ bufferList.mBuffers[0].mNumberChannels = ad->capture_channels;
+ bufferList.mBuffers[0].mDataByteSize = ad->input_buf.size() * sizeof(int16_t);
+
+ OSStatus result = AudioUnitRender(ad->audio_unit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList);
+ if (result == noErr) {
+ for (int i = 0; i < inNumberFrames * ad->capture_channels; i++) {
+ int32_t sample = ad->input_buf[i] << 16;
+ ad->input_buffer_write(sample);
+
+ if (ad->capture_channels == 1) {
+ // In case input device is single channel convert it to Stereo
+ ad->input_buffer_write(sample);
+ }
+ }
+ } else {
+ ERR_PRINT(("AudioUnitRender failed, code: " + itos(result)).utf8().get_data());
+ }
+
+ ad->unlock();
+
+ return result;
+}
+
void AudioDriverCoreAudio::start() {
if (!active) {
OSStatus result = AudioOutputUnitStart(audio_unit);
@@ -222,9 +316,94 @@ AudioDriver::SpeakerMode AudioDriverCoreAudio::get_speaker_mode() const {
return get_speaker_mode_by_total_channels(channels);
};
+void AudioDriverCoreAudio::lock() {
+ if (mutex)
+ mutex->lock();
+};
+
+void AudioDriverCoreAudio::unlock() {
+ if (mutex)
+ mutex->unlock();
+};
+
+bool AudioDriverCoreAudio::try_lock() {
+ if (mutex)
+ return mutex->try_lock() == OK;
+ return true;
+}
+
+void AudioDriverCoreAudio::finish() {
+ OSStatus result;
+
+ lock();
+
+ AURenderCallbackStruct callback;
+ zeromem(&callback, sizeof(AURenderCallbackStruct));
+ result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback));
+ if (result != noErr) {
+ ERR_PRINT("AudioUnitSetProperty failed");
+ }
+
+ if (active) {
+ result = AudioOutputUnitStop(audio_unit);
+ if (result != noErr) {
+ ERR_PRINT("AudioOutputUnitStop failed");
+ }
+
+ active = false;
+ }
+
+ result = AudioUnitUninitialize(audio_unit);
+ if (result != noErr) {
+ ERR_PRINT("AudioUnitUninitialize failed");
+ }
+
#ifdef OSX_ENABLED
+ AudioObjectPropertyAddress prop;
+ prop.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
+ prop.mScope = kAudioObjectPropertyScopeGlobal;
+ prop.mElement = kAudioObjectPropertyElementMaster;
-Array AudioDriverCoreAudio::get_device_list() {
+ result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop, &output_device_address_cb, this);
+ if (result != noErr) {
+ ERR_PRINT("AudioObjectRemovePropertyListener failed");
+ }
+#endif
+
+ result = AudioComponentInstanceDispose(audio_unit);
+ if (result != noErr) {
+ ERR_PRINT("AudioComponentInstanceDispose failed");
+ }
+
+ unlock();
+
+ if (mutex) {
+ memdelete(mutex);
+ mutex = NULL;
+ }
+};
+
+Error AudioDriverCoreAudio::capture_start() {
+
+ UInt32 flag = 1;
+ OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag));
+ ERR_FAIL_COND_V(result != noErr, FAILED);
+
+ return OK;
+}
+
+Error AudioDriverCoreAudio::capture_stop() {
+
+ UInt32 flag = 0;
+ OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag));
+ ERR_FAIL_COND_V(result != noErr, FAILED);
+
+ return OK;
+}
+
+#ifdef OSX_ENABLED
+
+Array AudioDriverCoreAudio::_get_device_list(bool capture) {
Array list;
@@ -243,20 +422,20 @@ Array AudioDriverCoreAudio::get_device_list() {
UInt32 deviceCount = size / sizeof(AudioDeviceID);
for (UInt32 i = 0; i < deviceCount; i++) {
- prop.mScope = kAudioDevicePropertyScopeOutput;
+ prop.mScope = capture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput;
prop.mSelector = kAudioDevicePropertyStreamConfiguration;
AudioObjectGetPropertyDataSize(audioDevices[i], &prop, 0, NULL, &size);
AudioBufferList *bufferList = (AudioBufferList *)malloc(size);
AudioObjectGetPropertyData(audioDevices[i], &prop, 0, NULL, &size, bufferList);
- UInt32 outputChannelCount = 0;
+ UInt32 channelCount = 0;
for (UInt32 j = 0; j < bufferList->mNumberBuffers; j++)
- outputChannelCount += bufferList->mBuffers[j].mNumberChannels;
+ channelCount += bufferList->mBuffers[j].mNumberChannels;
free(bufferList);
- if (outputChannelCount >= 1) {
+ if (channelCount >= 1) {
CFStringRef cfname;
size = sizeof(CFStringRef);
@@ -281,21 +460,11 @@ Array AudioDriverCoreAudio::get_device_list() {
return list;
}
-String AudioDriverCoreAudio::get_device() {
-
- return device_name;
-}
-
-void AudioDriverCoreAudio::set_device(String device) {
-
- device_name = device;
- if (!active) {
- return;
- }
+void AudioDriverCoreAudio::_set_device(const String &device, bool capture) {
AudioDeviceID deviceId;
bool found = false;
- if (device_name != "Default") {
+ if (device != "Default") {
AudioObjectPropertyAddress prop;
prop.mSelector = kAudioHardwarePropertyDevices;
@@ -309,20 +478,20 @@ void AudioDriverCoreAudio::set_device(String device) {
UInt32 deviceCount = size / sizeof(AudioDeviceID);
for (UInt32 i = 0; i < deviceCount && !found; i++) {
- prop.mScope = kAudioDevicePropertyScopeOutput;
+ prop.mScope = capture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput;
prop.mSelector = kAudioDevicePropertyStreamConfiguration;
AudioObjectGetPropertyDataSize(audioDevices[i], &prop, 0, NULL, &size);
AudioBufferList *bufferList = (AudioBufferList *)malloc(size);
AudioObjectGetPropertyData(audioDevices[i], &prop, 0, NULL, &size, bufferList);
- UInt32 outputChannelCount = 0;
+ UInt32 channelCount = 0;
for (UInt32 j = 0; j < bufferList->mNumberBuffers; j++)
- outputChannelCount += bufferList->mBuffers[j].mNumberChannels;
+ channelCount += bufferList->mBuffers[j].mNumberChannels;
free(bufferList);
- if (outputChannelCount >= 1) {
+ if (channelCount >= 1) {
CFStringRef cfname;
size = sizeof(CFStringRef);
@@ -335,7 +504,7 @@ void AudioDriverCoreAudio::set_device(String device) {
char *buffer = (char *)malloc(maxSize);
if (CFStringGetCString(cfname, buffer, maxSize, kCFStringEncodingUTF8)) {
String name = String(buffer) + " (" + itos(audioDevices[i]) + ")";
- if (name == device_name) {
+ if (name == device) {
deviceId = audioDevices[i];
found = true;
}
@@ -351,7 +520,8 @@ void AudioDriverCoreAudio::set_device(String device) {
if (!found) {
// If we haven't found the desired device get the system default one
UInt32 size = sizeof(AudioDeviceID);
- AudioObjectPropertyAddress property = { kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
+ UInt32 elem = capture ? kAudioHardwarePropertyDefaultInputDevice : kAudioHardwarePropertyDefaultOutputDevice;
+ AudioObjectPropertyAddress property = { elem, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &property, 0, NULL, &size, &deviceId);
ERR_FAIL_COND(result != noErr);
@@ -360,79 +530,52 @@ void AudioDriverCoreAudio::set_device(String device) {
}
if (found) {
- OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &deviceId, sizeof(AudioDeviceID));
+ OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, capture ? kInputBus : kOutputBus, &deviceId, sizeof(AudioDeviceID));
ERR_FAIL_COND(result != noErr);
+
+ // Reset audio input to keep synchronisation.
+ input_position = 0;
+ input_size = 0;
}
}
-#endif
-
-void AudioDriverCoreAudio::lock() {
- if (mutex)
- mutex->lock();
-};
-
-void AudioDriverCoreAudio::unlock() {
- if (mutex)
- mutex->unlock();
-};
+Array AudioDriverCoreAudio::get_device_list() {
-bool AudioDriverCoreAudio::try_lock() {
- if (mutex)
- return mutex->try_lock() == OK;
- return true;
+ return _get_device_list();
}
-void AudioDriverCoreAudio::finish() {
- OSStatus result;
+String AudioDriverCoreAudio::get_device() {
- lock();
+ return device_name;
+}
- AURenderCallbackStruct callback;
- zeromem(&callback, sizeof(AURenderCallbackStruct));
- result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback));
- if (result != noErr) {
- ERR_PRINT("AudioUnitSetProperty failed");
- }
+void AudioDriverCoreAudio::set_device(String device) {
+ device_name = device;
if (active) {
- result = AudioOutputUnitStop(audio_unit);
- if (result != noErr) {
- ERR_PRINT("AudioOutputUnitStop failed");
- }
-
- active = false;
+ _set_device(device_name);
}
+}
- result = AudioUnitUninitialize(audio_unit);
- if (result != noErr) {
- ERR_PRINT("AudioUnitUninitialize failed");
+void AudioDriverCoreAudio::capture_set_device(const String &p_name) {
+
+ capture_device_name = p_name;
+ if (active) {
+ _set_device(capture_device_name, true);
}
+}
-#ifdef OSX_ENABLED
- AudioObjectPropertyAddress prop;
- prop.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
- prop.mScope = kAudioObjectPropertyScopeGlobal;
- prop.mElement = kAudioObjectPropertyElementMaster;
+Array AudioDriverCoreAudio::capture_get_device_list() {
- result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop, &output_device_address_cb, this);
- if (result != noErr) {
- ERR_PRINT("AudioObjectRemovePropertyListener failed");
- }
-#endif
+ return _get_device_list(true);
+}
- result = AudioComponentInstanceDispose(audio_unit);
- if (result != noErr) {
- ERR_PRINT("AudioComponentInstanceDispose failed");
- }
+String AudioDriverCoreAudio::capture_get_device() {
- unlock();
+ return capture_device_name;
+}
- if (mutex) {
- memdelete(mutex);
- mutex = NULL;
- }
-};
+#endif
AudioDriverCoreAudio::AudioDriverCoreAudio() {
active = false;
@@ -440,14 +583,15 @@ AudioDriverCoreAudio::AudioDriverCoreAudio() {
mix_rate = 0;
channels = 2;
+ capture_channels = 2;
- buffer_size = 0;
buffer_frames = 0;
samples_in.clear();
device_name = "Default";
-};
+ capture_device_name = "Default";
+}
AudioDriverCoreAudio::~AudioDriverCoreAudio(){};
diff --git a/drivers/coreaudio/audio_driver_coreaudio.h b/drivers/coreaudio/audio_driver_coreaudio.h
index 99c910498e..d3f7c8d596 100644
--- a/drivers/coreaudio/audio_driver_coreaudio.h
+++ b/drivers/coreaudio/audio_driver_coreaudio.h
@@ -48,15 +48,24 @@ class AudioDriverCoreAudio : public AudioDriver {
Mutex *mutex;
String device_name;
+ String capture_device_name;
int mix_rate;
unsigned int channels;
+ unsigned int capture_channels;
unsigned int buffer_frames;
- unsigned int buffer_size;
Vector<int32_t> samples_in;
+ Vector<int16_t> input_buf;
#ifdef OSX_ENABLED
+ Array _get_device_list(bool capture = false);
+ void _set_device(const String &device, bool capture = false);
+
+ static OSStatus input_device_address_cb(AudioObjectID inObjectID,
+ UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,
+ void *inClientData);
+
static OSStatus output_device_address_cb(AudioObjectID inObjectID,
UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,
void *inClientData);
@@ -68,6 +77,12 @@ class AudioDriverCoreAudio : public AudioDriver {
UInt32 inBusNumber, UInt32 inNumberFrames,
AudioBufferList *ioData);
+ static OSStatus input_callback(void *inRefCon,
+ AudioUnitRenderActionFlags *ioActionFlags,
+ const AudioTimeStamp *inTimeStamp,
+ UInt32 inBusNumber, UInt32 inNumberFrames,
+ AudioBufferList *ioData);
+
public:
const char *get_name() const {
return "CoreAudio";
@@ -77,18 +92,27 @@ public:
virtual void start();
virtual int get_mix_rate() const;
virtual SpeakerMode get_speaker_mode() const;
-#ifdef OSX_ENABLED
- virtual Array get_device_list();
- virtual String get_device();
- virtual void set_device(String device);
-#endif
+
virtual void lock();
virtual void unlock();
virtual void finish();
+ virtual Error capture_start();
+ virtual Error capture_stop();
+
bool try_lock();
void stop();
+#ifdef OSX_ENABLED
+ virtual Array get_device_list();
+ virtual String get_device();
+ virtual void set_device(String device);
+
+ virtual Array capture_get_device_list();
+ virtual void capture_set_device(const String &p_name);
+ virtual String capture_get_device();
+#endif
+
AudioDriverCoreAudio();
~AudioDriverCoreAudio();
};
diff --git a/drivers/dummy/rasterizer_dummy.h b/drivers/dummy/rasterizer_dummy.h
index e045d4cd39..e39ec915fc 100644
--- a/drivers/dummy/rasterizer_dummy.h
+++ b/drivers/dummy/rasterizer_dummy.h
@@ -154,7 +154,8 @@ public:
ERR_FAIL_COND_V(!texture, RID());
return texture_owner.make_rid(texture);
}
- void texture_allocate(RID p_texture, int p_width, int p_height, Image::Format p_format, uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT) {
+
+ void texture_allocate(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, VisualServer::TextureType p_type = VS::TEXTURE_TYPE_2D, uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT) {
DummyTexture *t = texture_owner.getornull(p_texture);
ERR_FAIL_COND(!t);
t->width = p_width;
@@ -164,7 +165,7 @@ public:
t->image = Ref<Image>(memnew(Image));
t->image->create(p_width, p_height, false, p_format);
}
- void texture_set_data(RID p_texture, const Ref<Image> &p_image, VS::CubeMapSide p_cube_side = VS::CUBEMAP_LEFT) {
+ void texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_level) {
DummyTexture *t = texture_owner.getornull(p_texture);
ERR_FAIL_COND(!t);
t->width = p_image->get_width();
@@ -173,7 +174,7 @@ public:
t->image->create(t->width, t->height, false, t->format, p_image->get_data());
}
- void texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, VS::CubeMapSide p_cube_side) {
+ void texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, int p_level) {
DummyTexture *t = texture_owner.get(p_texture);
ERR_FAIL_COND(!t);
@@ -186,7 +187,7 @@ public:
t->image->blit_rect(p_image, Rect2(src_x, src_y, src_w, src_h), Vector2(dst_x, dst_y));
}
- Ref<Image> texture_get_data(RID p_texture, VS::CubeMapSide p_cube_side = VS::CUBEMAP_LEFT) const {
+ Ref<Image> texture_get_data(RID p_texture, int p_level) const {
DummyTexture *t = texture_owner.getornull(p_texture);
ERR_FAIL_COND_V(!t, Ref<Image>());
return t->image;
@@ -206,10 +207,13 @@ public:
ERR_FAIL_COND_V(!t, Image::FORMAT_RGB8);
return t->format;
}
+
+ VisualServer::TextureType texture_get_type(RID p_texture) const { return VS::TEXTURE_TYPE_2D; }
uint32_t texture_get_texid(RID p_texture) const { return 0; }
uint32_t texture_get_width(RID p_texture) const { return 0; }
uint32_t texture_get_height(RID p_texture) const { return 0; }
- void texture_set_size_override(RID p_texture, int p_width, int p_height) {}
+ uint32_t texture_get_depth(RID p_texture) const { return 0; }
+ void texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth_3d) {}
void texture_set_path(RID p_texture, const String &p_path) {
DummyTexture *t = texture_owner.getornull(p_texture);
diff --git a/drivers/gles2/rasterizer_canvas_gles2.cpp b/drivers/gles2/rasterizer_canvas_gles2.cpp
index 256d37186d..3d388c031a 100644
--- a/drivers/gles2/rasterizer_canvas_gles2.cpp
+++ b/drivers/gles2/rasterizer_canvas_gles2.cpp
@@ -349,7 +349,7 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur
state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_UV_ATTRIBUTE, false);
if (state.canvas_shader.bind()) {
_set_uniforms();
- state.canvas_shader.use_material((void *)p_material, 2);
+ state.canvas_shader.use_material((void *)p_material);
}
_bind_canvas_texture(RID(), RID());
@@ -393,7 +393,7 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur
state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_UV_ATTRIBUTE, false);
if (state.canvas_shader.bind()) {
_set_uniforms();
- state.canvas_shader.use_material((void *)p_material, 2);
+ state.canvas_shader.use_material((void *)p_material);
}
RasterizerStorageGLES2::Texture *tex = _bind_canvas_texture(r->texture, r->normal_map);
@@ -476,7 +476,7 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur
state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_UV_ATTRIBUTE, true);
if (state.canvas_shader.bind()) {
_set_uniforms();
- state.canvas_shader.use_material((void *)p_material, 2);
+ state.canvas_shader.use_material((void *)p_material);
}
glDisableVertexAttribArray(VS::ARRAY_COLOR);
@@ -642,7 +642,7 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur
if (state.canvas_shader.bind()) {
_set_uniforms();
- state.canvas_shader.use_material((void *)p_material, 2);
+ state.canvas_shader.use_material((void *)p_material);
}
static const int num_points = 32;
@@ -673,7 +673,7 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur
if (state.canvas_shader.bind()) {
_set_uniforms();
- state.canvas_shader.use_material((void *)p_material, 2);
+ state.canvas_shader.use_material((void *)p_material);
}
RasterizerStorageGLES2::Texture *texture = _bind_canvas_texture(polygon->texture, polygon->normal_map);
@@ -694,7 +694,7 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur
if (state.canvas_shader.bind()) {
_set_uniforms();
- state.canvas_shader.use_material((void *)p_material, 2);
+ state.canvas_shader.use_material((void *)p_material);
}
_bind_canvas_texture(RID(), RID());
@@ -727,7 +727,7 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur
if (state.canvas_shader.bind()) {
_set_uniforms();
- state.canvas_shader.use_material((void *)p_material, 2);
+ state.canvas_shader.use_material((void *)p_material);
}
ERR_CONTINUE(primitive->points.size() < 1);
@@ -926,7 +926,7 @@ void RasterizerCanvasGLES2::canvas_render_items(Item *p_item_list, int p_z, cons
state.canvas_shader.set_custom_shader(0);
state.canvas_shader.bind();
}
- state.canvas_shader.use_material((void *)material_ptr, 2);
+ state.canvas_shader.use_material((void *)material_ptr);
shader_cache = shader_ptr;
diff --git a/drivers/gles2/rasterizer_gles2.cpp b/drivers/gles2/rasterizer_gles2.cpp
index 335ad28670..a1a0b9e2c6 100644
--- a/drivers/gles2/rasterizer_gles2.cpp
+++ b/drivers/gles2/rasterizer_gles2.cpp
@@ -317,7 +317,7 @@ void RasterizerGLES2::set_boot_image(const Ref<Image> &p_image, const Color &p_c
canvas->canvas_begin();
RID texture = storage->texture_create();
- storage->texture_allocate(texture, p_image->get_width(), p_image->get_height(), p_image->get_format(), VS::TEXTURE_FLAG_FILTER);
+ storage->texture_allocate(texture, p_image->get_width(), p_image->get_height(), 0, p_image->get_format(), VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAG_FILTER);
storage->texture_set_data(texture, p_image);
Rect2 imgrect(0, 0, p_image->get_width(), p_image->get_height());
diff --git a/drivers/gles2/rasterizer_scene_gles2.cpp b/drivers/gles2/rasterizer_scene_gles2.cpp
index 00a79db347..288a144b32 100644
--- a/drivers/gles2/rasterizer_scene_gles2.cpp
+++ b/drivers/gles2/rasterizer_scene_gles2.cpp
@@ -35,6 +35,8 @@
#include "rasterizer_canvas_gles2.h"
#include "servers/visual/visual_server_raster.h"
+#include "vmap.h"
+
#ifndef GLES_OVER_GL
#define glClearDepth glClearDepthf
#endif
@@ -810,6 +812,14 @@ void RasterizerSceneGLES2::_fill_render_list(InstanceBase **p_cull_result, int p
}
} break;
+ case VS::INSTANCE_IMMEDIATE: {
+ RasterizerStorageGLES2::Immediate *im = storage->immediate_owner.getptr(instance->base);
+ ERR_CONTINUE(!im);
+
+ _add_geometry(im, instance, NULL, -1, p_depth_pass, p_shadow_pass);
+
+ } break;
+
default: {
} break;
@@ -827,7 +837,7 @@ static const GLenum gl_primitive[] = {
GL_TRIANGLE_FAN
};
-void RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_material, bool p_use_radiance_map, bool p_reverse_cull, bool p_shadow_atlas, bool p_skeleton_tex, Size2i p_skeleton_tex_size) {
+void RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_material, bool p_reverse_cull, Size2i p_skeleton_tex_size) {
// material parameters
@@ -864,25 +874,11 @@ void RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_m
ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = p_material->shader->texture_hints.ptrw();
- int num_default_tex = p_use_radiance_map ? 1 : 0;
-
- if (p_material->shader->spatial.uses_screen_texture) {
- num_default_tex = MIN(num_default_tex, 2);
- }
-
- if (p_shadow_atlas) {
- num_default_tex = MIN(num_default_tex, 3);
- }
-
- if (p_skeleton_tex) {
- num_default_tex = MIN(num_default_tex, 4);
-
- state.scene_shader.set_uniform(SceneShaderGLES2::SKELETON_TEXTURE_SIZE, p_skeleton_tex_size);
- }
+ state.scene_shader.set_uniform(SceneShaderGLES2::SKELETON_TEXTURE_SIZE, p_skeleton_tex_size);
for (int i = 0; i < tc; i++) {
- glActiveTexture(GL_TEXTURE0 + num_default_tex + i);
+ glActiveTexture(GL_TEXTURE0 + i);
RasterizerStorageGLES2::Texture *t = storage->texture_owner.getornull(textures[i].second);
@@ -911,7 +907,7 @@ void RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_m
glBindTexture(t->target, t->tex_id);
}
- state.scene_shader.use_material((void *)p_material, num_default_tex);
+ state.scene_shader.use_material((void *)p_material);
}
void RasterizerSceneGLES2::_setup_geometry(RenderList::Element *p_element, RasterizerStorageGLES2::Skeleton *p_skeleton) {
@@ -943,6 +939,13 @@ void RasterizerSceneGLES2::_setup_geometry(RenderList::Element *p_element, Raste
state.scene_shader.set_conditional(SceneShaderGLES2::ENABLE_UV2_INTERP, s->attribs[VS::ARRAY_TEX_UV2].enabled);
} break;
+ case VS::INSTANCE_IMMEDIATE: {
+ state.scene_shader.set_conditional(SceneShaderGLES2::USE_INSTANCING, false);
+ state.scene_shader.set_conditional(SceneShaderGLES2::ENABLE_COLOR_INTERP, true);
+ state.scene_shader.set_conditional(SceneShaderGLES2::ENABLE_UV_INTERP, true);
+ state.scene_shader.set_conditional(SceneShaderGLES2::ENABLE_UV2_INTERP, true);
+ } break;
+
default: {
} break;
@@ -1276,10 +1279,122 @@ void RasterizerSceneGLES2::_render_geometry(RenderList::Element *p_element) {
glBindBuffer(GL_ARRAY_BUFFER, 0);
} break;
+
+ case VS::INSTANCE_IMMEDIATE: {
+ const RasterizerStorageGLES2::Immediate *im = static_cast<const RasterizerStorageGLES2::Immediate *>(p_element->geometry);
+
+ if (im->building) {
+ return;
+ }
+
+ bool restore_tex = false;
+
+ glBindBuffer(GL_ARRAY_BUFFER, state.immediate_buffer);
+
+ for (const List<RasterizerStorageGLES2::Immediate::Chunk>::Element *E = im->chunks.front(); E; E = E->next()) {
+ const RasterizerStorageGLES2::Immediate::Chunk &c = E->get();
+
+ if (c.vertices.empty()) {
+ continue;
+ }
+
+ int vertices = c.vertices.size();
+
+ uint32_t buf_ofs = 0;
+
+ storage->info.render.vertices_count += vertices;
+
+ if (c.texture.is_valid() && storage->texture_owner.owns(c.texture)) {
+ RasterizerStorageGLES2::Texture *t = storage->texture_owner.get(c.texture);
+
+ t = t->get_ptr();
+
+ if (t->redraw_if_visible) {
+ VisualServerRaster::redraw_request();
+ }
+
+#ifdef TOOLS_ENABLED
+ if (t->detect_3d) {
+ t->detect_3d(t->detect_3d_ud);
+ }
+#endif
+ if (t->render_target) {
+ t->render_target->used_in_frame = true;
+ }
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(t->target, t->tex_id);
+ restore_tex = true;
+ } else if (restore_tex) {
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, state.current_main_tex);
+ restore_tex = false;
+ }
+
+ if (!c.normals.empty()) {
+ glEnableVertexAttribArray(VS::ARRAY_NORMAL);
+ glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector3) * vertices, c.normals.ptr());
+ glVertexAttribPointer(VS::ARRAY_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3), ((uint8_t *)NULL) + buf_ofs);
+ buf_ofs += sizeof(Vector3) * vertices;
+ } else {
+ glDisableVertexAttribArray(VS::ARRAY_NORMAL);
+ }
+
+ if (!c.tangents.empty()) {
+ glEnableVertexAttribArray(VS::ARRAY_TANGENT);
+ glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Plane) * vertices, c.tangents.ptr());
+ glVertexAttribPointer(VS::ARRAY_TANGENT, 4, GL_FLOAT, GL_FALSE, sizeof(Plane), ((uint8_t *)NULL) + buf_ofs);
+ buf_ofs += sizeof(Plane) * vertices;
+ } else {
+ glDisableVertexAttribArray(VS::ARRAY_TANGENT);
+ }
+
+ if (!c.colors.empty()) {
+ glEnableVertexAttribArray(VS::ARRAY_COLOR);
+ glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Color) * vertices, c.colors.ptr());
+ glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(Color), ((uint8_t *)NULL) + buf_ofs);
+ buf_ofs += sizeof(Color) * vertices;
+ } else {
+ glDisableVertexAttribArray(VS::ARRAY_COLOR);
+ }
+
+ if (!c.uvs.empty()) {
+ glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
+ glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector2) * vertices, c.uvs.ptr());
+ glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), ((uint8_t *)NULL) + buf_ofs);
+ buf_ofs += sizeof(Vector2) * vertices;
+ } else {
+ glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
+ }
+
+ if (!c.uv2s.empty()) {
+ glEnableVertexAttribArray(VS::ARRAY_TEX_UV2);
+ glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector2) * vertices, c.uv2s.ptr());
+ glVertexAttribPointer(VS::ARRAY_TEX_UV2, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), ((uint8_t *)NULL) + buf_ofs);
+ buf_ofs += sizeof(Vector2) * vertices;
+ } else {
+ glDisableVertexAttribArray(VS::ARRAY_TEX_UV2);
+ }
+
+ glEnableVertexAttribArray(VS::ARRAY_VERTEX);
+ glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector3) * vertices, c.vertices.ptr());
+ glVertexAttribPointer(VS::ARRAY_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3), ((uint8_t *)NULL) + buf_ofs);
+
+ glDrawArrays(gl_primitive[c.primitive], 0, c.vertices.size());
+ }
+
+ if (restore_tex) {
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, state.current_main_tex);
+ restore_tex = false;
+ }
+
+ } break;
}
}
-void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements, int p_element_count, const RID *p_light_cull_result, int p_light_cull_count, const Transform &p_view_transform, const CameraMatrix &p_projection, RID p_shadow_atlas, Environment *p_env, GLuint p_base_env, float p_shadow_bias, float p_shadow_normal_bias, bool p_reverse_cull, bool p_alpha_pass, bool p_shadow, bool p_directional_add, bool p_directional_shadows) {
+void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements, int p_element_count, const RID *p_directional_lights, int p_directional_light_count, const Transform &p_view_transform, const CameraMatrix &p_projection, RID p_shadow_atlas, Environment *p_env, GLuint p_base_env, float p_shadow_bias, float p_shadow_normal_bias, bool p_reverse_cull, bool p_alpha_pass, bool p_shadow, bool p_directional_add) {
ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
@@ -1289,6 +1404,8 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements,
bool use_radiance_map = false;
+ VMap<RID, Vector<RenderList::Element *> > lit_objects;
+
for (int i = 0; i < p_element_count; i++) {
RenderList::Element *e = p_elements[i];
@@ -1297,7 +1414,7 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements,
RasterizerStorageGLES2::Skeleton *skeleton = storage->skeleton_owner.getornull(e->instance->skeleton);
if (p_base_env) {
- glActiveTexture(GL_TEXTURE0);
+ glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 2);
glBindTexture(GL_TEXTURE_CUBE_MAP, p_base_env);
use_radiance_map = true;
}
@@ -1315,7 +1432,7 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements,
_setup_geometry(e, skeleton);
- _setup_material(material, use_radiance_map, p_reverse_cull, false, skeleton ? (skeleton->tex_id != 0) : 0, Size2i(skeleton ? skeleton->size * 3 : 0, 0));
+ _setup_material(material, p_reverse_cull, Size2i(skeleton ? skeleton->size * 3 : 0, 0));
if (use_radiance_map) {
state.scene_shader.set_uniform(SceneShaderGLES2::RADIANCE_INVERSE_XFORM, p_view_transform);
@@ -1404,66 +1521,88 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements,
_render_geometry(e);
- // render lights
-
if (material->shader->spatial.unshaded)
continue;
if (p_shadow)
continue;
- state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_PASS, true);
+ for (int light = 0; light < e->instance->light_instances.size(); light++) {
- state.scene_shader.bind();
+ RID light_instance = e->instance->light_instances[light];
- glBlendEquation(GL_FUNC_ADD);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+ lit_objects[light_instance].push_back(e);
+ }
+ }
- {
- bool has_shadow_atlas = shadow_atlas != NULL;
- _setup_material(material, false, p_reverse_cull, has_shadow_atlas, skeleton ? (skeleton->tex_id != 0) : 0, Size2i(skeleton ? skeleton->size * 3 : 0, 0));
+ if (p_shadow) {
+ state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, false);
+ state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM4, false);
+ state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM2, false);
+ state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, false);
+ return;
+ }
- if (has_shadow_atlas) {
- glActiveTexture(GL_TEXTURE3);
- glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth);
- }
+ state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_PASS, true);
- state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_MATRIX, p_view_transform.inverse());
- state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_INVERSE_MATRIX, p_view_transform);
- state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_MATRIX, p_projection);
- state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_INVERSE_MATRIX, p_projection.inverse());
+ glEnable(GL_BLEND);
+ glBlendEquation(GL_FUNC_ADD);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE);
- state.scene_shader.set_uniform(SceneShaderGLES2::TIME, storage->frame.time[0]);
+ for (int lo = 0; lo < lit_objects.size(); lo++) {
- state.scene_shader.set_uniform(SceneShaderGLES2::SCREEN_PIXEL_SIZE, screen_pixel_size);
- state.scene_shader.set_uniform(SceneShaderGLES2::NORMAL_MULT, 1.0); // TODO mirror?
- state.scene_shader.set_uniform(SceneShaderGLES2::WORLD_TRANSFORM, e->instance->transform);
- }
+ RID key = lit_objects.getk(lo);
- for (int j = 0; j < e->instance->light_instances.size(); j++) {
- RID light_rid = e->instance->light_instances[j];
- LightInstance *light = light_instance_owner.get(light_rid);
+ LightInstance *light = light_instance_owner.getornull(key);
+ RasterizerStorageGLES2::Light *light_ptr = light->light_ptr;
- switch (light->light_ptr->type) {
- case VS::LIGHT_DIRECTIONAL: {
- continue;
- } break;
+ const Vector<RenderList::Element *> &list = lit_objects.getv(lo);
+
+ for (int i = 0; i < list.size(); i++) {
+
+ RenderList::Element *e = list[i];
+ RasterizerStorageGLES2::Material *material = e->material;
+ RasterizerStorageGLES2::Skeleton *skeleton = storage->skeleton_owner.getornull(e->instance->skeleton);
+
+ {
+ _setup_geometry(e, skeleton);
+
+ _setup_material(material, p_reverse_cull, Size2i(skeleton ? skeleton->size * 3 : 0, 0));
+ if (shadow_atlas != NULL) {
+ glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
+ glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth);
+ }
+
+ state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_MATRIX, p_view_transform.inverse());
+ state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_INVERSE_MATRIX, p_view_transform);
+ state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_MATRIX, p_projection);
+ state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_INVERSE_MATRIX, p_projection.inverse());
+
+ state.scene_shader.set_uniform(SceneShaderGLES2::TIME, storage->frame.time[0]);
+
+ state.scene_shader.set_uniform(SceneShaderGLES2::SCREEN_PIXEL_SIZE, screen_pixel_size);
+ state.scene_shader.set_uniform(SceneShaderGLES2::NORMAL_MULT, 1.0); // TODO mirror?
+ state.scene_shader.set_uniform(SceneShaderGLES2::WORLD_TRANSFORM, e->instance->transform);
+ }
+
+ switch (light_ptr->type) {
case VS::LIGHT_OMNI: {
+
state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_TYPE, (int)1);
Vector3 position = p_view_transform.inverse().xform(light->transform.origin);
state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_POSITION, position);
- float range = light->light_ptr->param[VS::LIGHT_PARAM_RANGE];
+ float range = light_ptr->param[VS::LIGHT_PARAM_RANGE];
state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_RANGE, range);
Color attenuation = Color(0.0, 0.0, 0.0, 0.0);
- attenuation.a = light->light_ptr->param[VS::LIGHT_PARAM_ATTENUATION];
+ attenuation.a = light_ptr->param[VS::LIGHT_PARAM_ATTENUATION];
state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_ATTENUATION, attenuation);
- if (light->light_ptr->shadow && shadow_atlas->shadow_owners.has(light->self)) {
+ if (light_ptr->shadow && shadow_atlas->shadow_owners.has(light->self)) {
uint32_t key = shadow_atlas->shadow_owners[light->self];
@@ -1516,10 +1655,10 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements,
Vector3 direction = p_view_transform.inverse().basis.xform(light->transform.basis.xform(Vector3(0, 0, -1))).normalized();
state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_DIRECTION, direction);
Color attenuation = Color(0.0, 0.0, 0.0, 0.0);
- attenuation.a = light->light_ptr->param[VS::LIGHT_PARAM_ATTENUATION];
- float range = light->light_ptr->param[VS::LIGHT_PARAM_RANGE];
- float spot_attenuation = light->light_ptr->param[VS::LIGHT_PARAM_SPOT_ATTENUATION];
- float angle = light->light_ptr->param[VS::LIGHT_PARAM_SPOT_ANGLE];
+ attenuation.a = light_ptr->param[VS::LIGHT_PARAM_ATTENUATION];
+ float range = light_ptr->param[VS::LIGHT_PARAM_RANGE];
+ float spot_attenuation = light_ptr->param[VS::LIGHT_PARAM_SPOT_ATTENUATION];
+ float angle = light_ptr->param[VS::LIGHT_PARAM_SPOT_ANGLE];
angle = Math::cos(Math::deg2rad(angle));
state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_ATTENUATION, attenuation);
state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPOT_ATTENUATION, spot_attenuation);
@@ -1576,9 +1715,7 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements,
} break;
- default: {
- print_line("wat.");
- } break;
+ default: break;
}
float energy = light->light_ptr->param[VS::LIGHT_PARAM_ENERGY];
@@ -1590,62 +1727,57 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements,
_render_geometry(e);
}
+ }
- for (int j = 0; j < p_light_cull_count; j++) {
- RID light_rid = p_light_cull_result[j];
-
- LightInstance *light = light_instance_owner.getornull(light_rid);
+ for (int dl = 0; dl < p_directional_light_count; dl++) {
+ RID light_rid = p_directional_lights[dl];
+ LightInstance *light = light_instance_owner.getornull(light_rid);
+ RasterizerStorageGLES2::Light *light_ptr = light->light_ptr;
- RasterizerStorageGLES2::Light *light_ptr = light->light_ptr;
+ switch (light_ptr->directional_shadow_mode) {
+ case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: {
+ } break;
+ case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: {
+ state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM2, true);
+ state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, light_ptr->directional_blend_splits);
+ } break;
- switch (light_ptr->type) {
- case VS::LIGHT_DIRECTIONAL: {
-
- switch (light_ptr->directional_shadow_mode) {
- case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: {
- } break;
- case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: {
- state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM2, true);
- state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, light_ptr->directional_blend_splits);
- } break;
-
- case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: {
- state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM4, true);
- state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, light_ptr->directional_blend_splits);
- } break;
- default:
- break;
- }
+ case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: {
+ state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM4, true);
+ state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, light_ptr->directional_blend_splits);
+ } break;
+ default:
+ break;
+ }
- {
- _setup_material(material, false, p_reverse_cull, false, skeleton ? (skeleton->tex_id != 0) : 0, Size2i(skeleton ? skeleton->size * 3 : 0, 0));
+ for (int i = 0; i < p_element_count; i++) {
- if (directional_shadow.depth) {
- glActiveTexture(GL_TEXTURE3);
- glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
- }
+ RenderList::Element *e = p_elements[i];
+ RasterizerStorageGLES2::Material *material = e->material;
+ RasterizerStorageGLES2::Skeleton *skeleton = storage->skeleton_owner.getornull(e->instance->skeleton);
- state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_MATRIX, p_view_transform.inverse());
- state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_INVERSE_MATRIX, p_view_transform);
- state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_MATRIX, p_projection);
- state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_INVERSE_MATRIX, p_projection.inverse());
+ {
+ _setup_material(material, p_reverse_cull, Size2i(skeleton ? skeleton->size * 3 : 0, 0));
- state.scene_shader.set_uniform(SceneShaderGLES2::TIME, storage->frame.time[0]);
+ if (directional_shadow.depth) {
+ glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4); // TODO move into base pass
+ glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
+ }
- state.scene_shader.set_uniform(SceneShaderGLES2::SCREEN_PIXEL_SIZE, screen_pixel_size);
- state.scene_shader.set_uniform(SceneShaderGLES2::NORMAL_MULT, 1.0); // TODO mirror?
- state.scene_shader.set_uniform(SceneShaderGLES2::WORLD_TRANSFORM, e->instance->transform);
- }
- state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_TYPE, (int)0);
- Vector3 direction = p_view_transform.inverse().basis.xform(light->transform.basis.xform(Vector3(0, 0, -1))).normalized();
- state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_DIRECTION, direction);
+ state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_MATRIX, p_view_transform.inverse());
+ state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_INVERSE_MATRIX, p_view_transform);
+ state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_MATRIX, p_projection);
+ state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_INVERSE_MATRIX, p_projection.inverse());
- } break;
+ state.scene_shader.set_uniform(SceneShaderGLES2::TIME, storage->frame.time[0]);
- default: {
- continue;
- } break;
+ state.scene_shader.set_uniform(SceneShaderGLES2::SCREEN_PIXEL_SIZE, screen_pixel_size);
+ state.scene_shader.set_uniform(SceneShaderGLES2::NORMAL_MULT, 1.0); // TODO mirror?
+ state.scene_shader.set_uniform(SceneShaderGLES2::WORLD_TRANSFORM, e->instance->transform);
}
+ state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_TYPE, (int)0);
+ Vector3 direction = p_view_transform.inverse().basis.xform(light->transform.basis.xform(Vector3(0, 0, -1))).normalized();
+ state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_DIRECTION, direction);
float energy = light_ptr->param[VS::LIGHT_PARAM_ENERGY];
float specular = light_ptr->param[VS::LIGHT_PARAM_SPECULAR];
@@ -1753,10 +1885,10 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements,
_render_geometry(e);
}
-
- state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_PASS, false);
}
+ state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_PASS, false);
+
state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, false);
state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM4, false);
state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM2, false);
@@ -1899,7 +2031,7 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const
} break;
default: {
- print_line("uhm");
+ // FIXME: implement other background modes
} break;
}
}
@@ -1911,9 +2043,21 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const
}
}
+ Vector<RID> directional_lights;
+
+ for (int i = 0; i < p_light_cull_count; i++) {
+ RID light_rid = p_light_cull_result[i];
+
+ LightInstance *light = light_instance_owner.getornull(light_rid);
+
+ if (light->light_ptr->type == VS::LIGHT_DIRECTIONAL) {
+ directional_lights.push_back(light_rid);
+ }
+ }
+
// render opaque things first
render_list.sort_by_key(false);
- _render_render_list(render_list.elements, render_list.element_count, p_light_cull_result, p_light_cull_count, p_cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, false, false, false, false, false);
+ _render_render_list(render_list.elements, render_list.element_count, directional_lights.ptr(), directional_lights.size(), p_cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, false, false, false, false);
// alpha pass
@@ -1921,7 +2065,7 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
render_list.sort_by_key(true);
- _render_render_list(&render_list.elements[render_list.max_elements - render_list.alpha_element_count], render_list.alpha_element_count, p_light_cull_result, p_light_cull_count, p_cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, false, true, false, false, false);
+ _render_render_list(&render_list.elements[render_list.max_elements - render_list.alpha_element_count], render_list.alpha_element_count, directional_lights.ptr(), directional_lights.size(), p_cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, false, true, false, false);
glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);
@@ -2136,7 +2280,7 @@ void RasterizerSceneGLES2::render_shadow(RID p_light, RID p_shadow_atlas, int p_
state.scene_shader.set_conditional(SceneShaderGLES2::RENDER_DEPTH, true);
- _render_render_list(render_list.elements, render_list.element_count, NULL, 0, light_transform, light_projection, RID(), NULL, 0, bias, normal_bias, false, false, true, false, false);
+ _render_render_list(render_list.elements, render_list.element_count, NULL, 0, light_transform, light_projection, RID(), NULL, 0, bias, normal_bias, false, false, true, false);
state.scene_shader.set_conditional(SceneShaderGLES2::RENDER_DEPTH, false);
@@ -2230,6 +2374,15 @@ void RasterizerSceneGLES2::initialize() {
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
+ {
+ uint32_t immediate_buffer_size = GLOBAL_DEF("rendering/limits/buffers/immediate_buffer_size_kb", 2048);
+
+ glGenBuffers(1, &state.immediate_buffer);
+ glBindBuffer(GL_ARRAY_BUFFER, state.immediate_buffer);
+ glBufferData(GL_ARRAY_BUFFER, immediate_buffer_size * 1024, NULL, GL_DYNAMIC_DRAW);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ }
+
// cubemaps for shadows
{
int max_shadow_cubemap_sampler_size = 512;
diff --git a/drivers/gles2/rasterizer_scene_gles2.h b/drivers/gles2/rasterizer_scene_gles2.h
index 0ce7e9ae97..e153080e15 100644
--- a/drivers/gles2/rasterizer_scene_gles2.h
+++ b/drivers/gles2/rasterizer_scene_gles2.h
@@ -74,6 +74,8 @@ public:
GLuint sky_verts;
+ GLuint immediate_buffer;
+
// ResolveShaderGLES3 resolve_shader;
// ScreenSpaceReflectionShaderGLES3 ssr_shader;
// EffectBlurShaderGLES3 effect_blur_shader;
@@ -545,11 +547,23 @@ public:
void _add_geometry_with_material(RasterizerStorageGLES2::Geometry *p_geometry, InstanceBase *p_instance, RasterizerStorageGLES2::GeometryOwner *p_owner, RasterizerStorageGLES2::Material *p_material, bool p_depth_pass, bool p_shadow_pass);
void _fill_render_list(InstanceBase **p_cull_result, int p_cull_count, bool p_depth_pass, bool p_shadow_pass);
- void _render_render_list(RenderList::Element **p_elements, int p_element_count, const RID *p_light_cull_result, int p_light_cull_count, const Transform &p_view_transform, const CameraMatrix &p_projection, RID p_shadow_atlas, Environment *p_env, GLuint p_base_env, float p_shadow_bias, float p_shadow_normal_bias, bool p_reverse_cull, bool p_alpha_pass, bool p_shadow, bool p_directional_add, bool p_directional_shadows);
+ void _render_render_list(RenderList::Element **p_elements, int p_element_count,
+ const RID *p_directional_lights, int p_directional_light_count,
+ const Transform &p_view_transform,
+ const CameraMatrix &p_projection,
+ RID p_shadow_atlas,
+ Environment *p_env,
+ GLuint p_base_env,
+ float p_shadow_bias,
+ float p_shadow_normal_bias,
+ bool p_reverse_cull,
+ bool p_alpha_pass,
+ bool p_shadow,
+ bool p_directional_add);
void _draw_sky(RasterizerStorageGLES2::Sky *p_sky, const CameraMatrix &p_projection, const Transform &p_transform, bool p_vflip, float p_custom_fov, float p_energy);
- void _setup_material(RasterizerStorageGLES2::Material *p_material, bool p_use_radiance_map, bool p_reverse_cull, bool p_shadow_atlas = false, bool p_skeleton_tex = false, Size2i p_skeleton_tex_size = Size2i(0, 0));
+ void _setup_material(RasterizerStorageGLES2::Material *p_material, bool p_reverse_cull, Size2i p_skeleton_tex_size = Size2i(0, 0));
void _setup_geometry(RenderList::Element *p_element, RasterizerStorageGLES2::Skeleton *p_skeleton);
void _render_geometry(RenderList::Element *p_element);
diff --git a/drivers/gles2/rasterizer_storage_gles2.cpp b/drivers/gles2/rasterizer_storage_gles2.cpp
index 468659ed80..1bd3c0a935 100644
--- a/drivers/gles2/rasterizer_storage_gles2.cpp
+++ b/drivers/gles2/rasterizer_storage_gles2.cpp
@@ -346,7 +346,7 @@ RID RasterizerStorageGLES2::texture_create() {
return texture_owner.make_rid(texture);
}
-void RasterizerStorageGLES2::texture_allocate(RID p_texture, int p_width, int p_height, Image::Format p_format, uint32_t p_flags) {
+void RasterizerStorageGLES2::texture_allocate(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, VisualServer::TextureType p_type, uint32_t p_flags) {
GLenum format;
GLenum internal_format;
GLenum type;
@@ -365,7 +365,24 @@ void RasterizerStorageGLES2::texture_allocate(RID p_texture, int p_width, int p_
texture->format = p_format;
texture->flags = p_flags;
texture->stored_cube_sides = 0;
- texture->target = (p_flags & VS::TEXTURE_FLAG_CUBEMAP) ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
+ texture->type = p_type;
+
+ switch (p_type) {
+ case VS::TEXTURE_TYPE_2D: {
+ texture->target = GL_TEXTURE_2D;
+ texture->images.resize(1);
+ } break;
+ case VS::TEXTURE_TYPE_CUBEMAP: {
+ texture->target = GL_TEXTURE_CUBE_MAP;
+ texture->images.resize(6);
+ } break;
+ case VS::TEXTURE_TYPE_2D_ARRAY: {
+ texture->images.resize(p_depth_3d);
+ } break;
+ case VS::TEXTURE_TYPE_3D: {
+ texture->images.resize(p_depth_3d);
+ } break;
+ }
_get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, format, internal_format, type, compressed);
@@ -391,7 +408,7 @@ void RasterizerStorageGLES2::texture_allocate(RID p_texture, int p_width, int p_
texture->active = true;
}
-void RasterizerStorageGLES2::texture_set_data(RID p_texture, const Ref<Image> &p_image, VS::CubeMapSide p_cube_side) {
+void RasterizerStorageGLES2::texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer) {
Texture *texture = texture_owner.getornull(p_texture);
ERR_FAIL_COND(!texture);
@@ -406,7 +423,7 @@ void RasterizerStorageGLES2::texture_set_data(RID p_texture, const Ref<Image> &p
bool compressed = false;
if (config.keep_original_textures && !(texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING)) {
- texture->images[p_cube_side] = p_image;
+ texture->images.write[p_layer] = p_image;
}
Ref<Image> img = _get_gl_image_and_format(p_image, p_image->get_format(), texture->flags, format, internal_format, type, compressed);
@@ -425,7 +442,7 @@ void RasterizerStorageGLES2::texture_set_data(RID p_texture, const Ref<Image> &p
}
};
- GLenum blit_target = (texture->target == GL_TEXTURE_CUBE_MAP) ? _cube_side_enum[p_cube_side] : GL_TEXTURE_2D;
+ GLenum blit_target = (texture->target == GL_TEXTURE_CUBE_MAP) ? _cube_side_enum[p_layer] : GL_TEXTURE_2D;
texture->data_size = img->get_data().size();
PoolVector<uint8_t>::Read read = img->get_data().read();
@@ -527,9 +544,9 @@ void RasterizerStorageGLES2::texture_set_data(RID p_texture, const Ref<Image> &p
// printf("texture: %i x %i - size: %i - total: %i\n", texture->width, texture->height, tsize, info.texture_mem);
- texture->stored_cube_sides |= (1 << p_cube_side);
+ texture->stored_cube_sides |= (1 << p_layer);
- if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && mipmaps == 1 && !texture->ignore_mipmaps && (!(texture->flags & VS::TEXTURE_FLAG_CUBEMAP) || texture->stored_cube_sides == (1 << 6) - 1)) {
+ if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && mipmaps == 1 && !texture->ignore_mipmaps && (texture->type != VS::TEXTURE_TYPE_CUBEMAP || texture->stored_cube_sides == (1 << 6) - 1)) {
//generate mipmaps if they were requested and the image does not contain them
glGenerateMipmap(texture->target);
}
@@ -537,12 +554,12 @@ void RasterizerStorageGLES2::texture_set_data(RID p_texture, const Ref<Image> &p
texture->mipmaps = mipmaps;
}
-void RasterizerStorageGLES2::texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, VS::CubeMapSide p_cube_side) {
+void RasterizerStorageGLES2::texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, int p_layer) {
// TODO
ERR_PRINT("Not implemented (ask Karroffel to do it :p)");
}
-Ref<Image> RasterizerStorageGLES2::texture_get_data(RID p_texture, VS::CubeMapSide p_cube_side) const {
+Ref<Image> RasterizerStorageGLES2::texture_get_data(RID p_texture, int p_layer) const {
Texture *texture = texture_owner.getornull(p_texture);
@@ -550,8 +567,8 @@ Ref<Image> RasterizerStorageGLES2::texture_get_data(RID p_texture, VS::CubeMapSi
ERR_FAIL_COND_V(!texture->active, Ref<Image>());
ERR_FAIL_COND_V(texture->data_size == 0 && !texture->render_target, Ref<Image>());
- if (!texture->images[p_cube_side].is_null()) {
- return texture->images[p_cube_side];
+ if (texture->type == VS::TEXTURE_TYPE_CUBEMAP && p_layer < 6 && p_layer >= 0 && !texture->images[p_layer].is_null()) {
+ return texture->images[p_layer];
}
#ifdef GLES_OVER_GL
@@ -577,9 +594,13 @@ Ref<Image> RasterizerStorageGLES2::texture_get_data(RID p_texture, VS::CubeMapSi
ofs = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, texture->format, i - 1);
}
- glPixelStorei(GL_PACK_ALIGNMENT, 1);
-
- glGetTexImage(texture->target, i, texture->gl_format_cache, texture->gl_type_cache, &wb[ofs]);
+ if (texture->compressed) {
+ glPixelStorei(GL_PACK_ALIGNMENT, 4);
+ glGetCompressedTexImage(texture->target, i, &wb[ofs]);
+ } else {
+ glPixelStorei(GL_PACK_ALIGNMENT, 1);
+ glGetTexImage(texture->target, i, texture->gl_format_cache, texture->gl_type_cache, &wb[ofs]);
+ }
}
wb = PoolVector<uint8_t>::Write();
@@ -605,8 +626,6 @@ void RasterizerStorageGLES2::texture_set_flags(RID p_texture, uint32_t p_flags)
glActiveTexture(GL_TEXTURE0);
glBindTexture(texture->target, texture->tex_id);
- uint32_t cube = texture->flags & VS::TEXTURE_FLAG_CUBEMAP;
- texture->flags = p_flags | cube; // can't remove a cube from being a cube
if (((texture->flags & VS::TEXTURE_FLAG_REPEAT) || (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT)) && texture->target != GL_TEXTURE_CUBE_MAP) {
@@ -663,6 +682,14 @@ Image::Format RasterizerStorageGLES2::texture_get_format(RID p_texture) const {
return texture->format;
}
+VisualServer::TextureType RasterizerStorageGLES2::texture_get_type(RID p_texture) const {
+ Texture *texture = texture_owner.getornull(p_texture);
+
+ ERR_FAIL_COND_V(!texture, VS::TEXTURE_TYPE_2D);
+
+ return texture->type;
+}
+
uint32_t RasterizerStorageGLES2::texture_get_texid(RID p_texture) const {
Texture *texture = texture_owner.getornull(p_texture);
@@ -687,7 +714,15 @@ uint32_t RasterizerStorageGLES2::texture_get_height(RID p_texture) const {
return texture->height;
}
-void RasterizerStorageGLES2::texture_set_size_override(RID p_texture, int p_width, int p_height) {
+uint32_t RasterizerStorageGLES2::texture_get_depth(RID p_texture) const {
+ Texture *texture = texture_owner.getornull(p_texture);
+
+ ERR_FAIL_COND_V(!texture, 0);
+
+ return texture->depth;
+}
+
+void RasterizerStorageGLES2::texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth) {
Texture *texture = texture_owner.getornull(p_texture);
ERR_FAIL_COND(!texture);
@@ -726,8 +761,9 @@ void RasterizerStorageGLES2::texture_debug_usage(List<VS::TextureInfo> *r_info)
VS::TextureInfo tinfo;
tinfo.path = t->path;
tinfo.format = t->format;
- tinfo.size.x = t->alloc_width;
- tinfo.size.y = t->alloc_height;
+ tinfo.width = t->alloc_width;
+ tinfo.height = t->alloc_height;
+ tinfo.depth = 0;
tinfo.bytes = t->total_data_size;
r_info->push_back(tinfo);
}
@@ -2643,45 +2679,132 @@ void RasterizerStorageGLES2::update_dirty_multimeshes() {
/* IMMEDIATE API */
RID RasterizerStorageGLES2::immediate_create() {
- return RID();
+ Immediate *im = memnew(Immediate);
+ return immediate_owner.make_rid(im);
}
-void RasterizerStorageGLES2::immediate_begin(RID p_immediate, VS::PrimitiveType p_rimitive, RID p_texture) {
+void RasterizerStorageGLES2::immediate_begin(RID p_immediate, VS::PrimitiveType p_primitive, RID p_texture) {
+ Immediate *im = immediate_owner.get(p_immediate);
+ ERR_FAIL_COND(!im);
+ ERR_FAIL_COND(im->building);
+
+ Immediate::Chunk ic;
+ ic.texture = p_texture;
+ ic.primitive = p_primitive;
+ im->chunks.push_back(ic);
+ im->mask = 0;
+ im->building = true;
}
void RasterizerStorageGLES2::immediate_vertex(RID p_immediate, const Vector3 &p_vertex) {
+ Immediate *im = immediate_owner.get(p_immediate);
+ ERR_FAIL_COND(!im);
+ ERR_FAIL_COND(!im->building);
+
+ Immediate::Chunk *c = &im->chunks.back()->get();
+
+ if (c->vertices.empty() && im->chunks.size() == 1) {
+ im->aabb.position = p_vertex;
+ im->aabb.size = Vector3();
+ } else {
+ im->aabb.expand_to(p_vertex);
+ }
+
+ if (im->mask & VS::ARRAY_FORMAT_NORMAL)
+ c->normals.push_back(chunk_normal);
+ if (im->mask & VS::ARRAY_FORMAT_TANGENT)
+ c->tangents.push_back(chunk_tangent);
+ if (im->mask & VS::ARRAY_FORMAT_COLOR)
+ c->colors.push_back(chunk_color);
+ if (im->mask & VS::ARRAY_FORMAT_TEX_UV)
+ c->uvs.push_back(chunk_uv);
+ if (im->mask & VS::ARRAY_FORMAT_TEX_UV2)
+ c->uv2s.push_back(chunk_uv2);
+ im->mask |= VS::ARRAY_FORMAT_VERTEX;
+ c->vertices.push_back(p_vertex);
}
void RasterizerStorageGLES2::immediate_normal(RID p_immediate, const Vector3 &p_normal) {
+ Immediate *im = immediate_owner.get(p_immediate);
+ ERR_FAIL_COND(!im);
+ ERR_FAIL_COND(!im->building);
+
+ im->mask |= VS::ARRAY_FORMAT_NORMAL;
+ chunk_normal = p_normal;
}
void RasterizerStorageGLES2::immediate_tangent(RID p_immediate, const Plane &p_tangent) {
+ Immediate *im = immediate_owner.get(p_immediate);
+ ERR_FAIL_COND(!im);
+ ERR_FAIL_COND(!im->building);
+
+ im->mask |= VS::ARRAY_FORMAT_TANGENT;
+ chunk_tangent = p_tangent;
}
void RasterizerStorageGLES2::immediate_color(RID p_immediate, const Color &p_color) {
+ Immediate *im = immediate_owner.get(p_immediate);
+ ERR_FAIL_COND(!im);
+ ERR_FAIL_COND(!im->building);
+
+ im->mask |= VS::ARRAY_FORMAT_COLOR;
+ chunk_color = p_color;
}
void RasterizerStorageGLES2::immediate_uv(RID p_immediate, const Vector2 &tex_uv) {
+ Immediate *im = immediate_owner.get(p_immediate);
+ ERR_FAIL_COND(!im);
+ ERR_FAIL_COND(!im->building);
+
+ im->mask |= VS::ARRAY_FORMAT_TEX_UV;
+ chunk_uv = tex_uv;
}
void RasterizerStorageGLES2::immediate_uv2(RID p_immediate, const Vector2 &tex_uv) {
+ Immediate *im = immediate_owner.get(p_immediate);
+ ERR_FAIL_COND(!im);
+ ERR_FAIL_COND(!im->building);
+
+ im->mask |= VS::ARRAY_FORMAT_TEX_UV2;
+ chunk_uv2 = tex_uv;
}
void RasterizerStorageGLES2::immediate_end(RID p_immediate) {
+ Immediate *im = immediate_owner.get(p_immediate);
+ ERR_FAIL_COND(!im);
+ ERR_FAIL_COND(!im->building);
+
+ im->building = false;
+ im->instance_change_notify();
}
void RasterizerStorageGLES2::immediate_clear(RID p_immediate) {
+ Immediate *im = immediate_owner.get(p_immediate);
+ ERR_FAIL_COND(!im);
+ ERR_FAIL_COND(im->building);
+
+ im->chunks.clear();
+ im->instance_change_notify();
}
AABB RasterizerStorageGLES2::immediate_get_aabb(RID p_immediate) const {
- return AABB();
+ Immediate *im = immediate_owner.get(p_immediate);
+ ERR_FAIL_COND_V(!im, AABB());
+ return im->aabb;
}
void RasterizerStorageGLES2::immediate_set_material(RID p_immediate, RID p_material) {
+ Immediate *im = immediate_owner.get(p_immediate);
+ ERR_FAIL_COND(!im);
+
+ im->material = p_material;
+ im->instance_material_change_notify();
}
RID RasterizerStorageGLES2::immediate_get_material(RID p_immediate) const {
- return RID();
+ const Immediate *im = immediate_owner.get(p_immediate);
+ ERR_FAIL_COND_V(!im, RID());
+ return im->material;
}
/* SKELETON API */
@@ -3693,15 +3816,15 @@ VS::InstanceType RasterizerStorageGLES2::get_base_type(RID p_rid) const {
if (mesh_owner.owns(p_rid)) {
return VS::INSTANCE_MESH;
- }
- if (light_owner.owns(p_rid)) {
+ } else if (light_owner.owns(p_rid)) {
return VS::INSTANCE_LIGHT;
- }
- if (multimesh_owner.owns(p_rid)) {
+ } else if (multimesh_owner.owns(p_rid)) {
return VS::INSTANCE_MULTIMESH;
+ } else if (immediate_owner.owns(p_rid)) {
+ return VS::INSTANCE_IMMEDIATE;
+ } else {
+ return VS::INSTANCE_NONE;
}
-
- return VS::INSTANCE_NONE;
}
bool RasterizerStorageGLES2::free(RID p_rid) {
@@ -3859,6 +3982,14 @@ bool RasterizerStorageGLES2::free(RID p_rid) {
memdelete(multimesh);
return true;
+ } else if (immediate_owner.owns(p_rid)) {
+ Immediate *im = immediate_owner.get(p_rid);
+ im->instance_remove_deps();
+
+ immediate_owner.free(p_rid);
+ memdelete(im);
+
+ return true;
} else if (light_owner.owns(p_rid)) {
Light *light = light_owner.get(p_rid);
@@ -3929,7 +4060,7 @@ void RasterizerStorageGLES2::initialize() {
frame.clear_request = false;
// config.keep_original_textures = false;
- glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &config.max_texture_image_units);
+ glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &config.max_texture_image_units);
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &config.max_texture_size);
shaders.copy.init();
diff --git a/drivers/gles2/rasterizer_storage_gles2.h b/drivers/gles2/rasterizer_storage_gles2.h
index fe5d4af952..c1fbf73254 100644
--- a/drivers/gles2/rasterizer_storage_gles2.h
+++ b/drivers/gles2/rasterizer_storage_gles2.h
@@ -231,9 +231,10 @@ public:
String path;
uint32_t flags;
- int width, height;
+ int width, height, depth;
int alloc_width, alloc_height;
Image::Format format;
+ VS::TextureType type;
GLenum target;
GLenum gl_format_cache;
@@ -257,7 +258,7 @@ public:
RenderTarget *render_target;
- Ref<Image> images[6];
+ Vector<Ref<Image> > images;
bool redraw_if_visible;
@@ -327,17 +328,19 @@ public:
Ref<Image> _get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed);
virtual RID texture_create();
- virtual void texture_allocate(RID p_texture, int p_width, int p_height, Image::Format p_format, uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT);
- virtual void texture_set_data(RID p_texture, const Ref<Image> &p_image, VS::CubeMapSide p_cube_side = VS::CUBEMAP_LEFT);
- virtual void texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, VS::CubeMapSide p_cube_side = VS::CUBEMAP_LEFT);
- virtual Ref<Image> texture_get_data(RID p_texture, VS::CubeMapSide p_cube_side = VS::CUBEMAP_LEFT) const;
+ virtual void texture_allocate(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, VS::TextureType p_type, uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT);
+ virtual void texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer = 0);
+ virtual void texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, int p_layer = 0);
+ virtual Ref<Image> texture_get_data(RID p_texture, int p_layer = 0) const;
virtual void texture_set_flags(RID p_texture, uint32_t p_flags);
virtual uint32_t texture_get_flags(RID p_texture) const;
virtual Image::Format texture_get_format(RID p_texture) const;
+ virtual VS::TextureType texture_get_type(RID p_texture) const;
virtual uint32_t texture_get_texid(RID p_texture) const;
virtual uint32_t texture_get_width(RID p_texture) const;
virtual uint32_t texture_get_height(RID p_texture) const;
- virtual void texture_set_size_override(RID p_texture, int p_width, int p_height);
+ virtual uint32_t texture_get_depth(RID p_texture) const;
+ virtual void texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth);
virtual void texture_set_path(RID p_texture, const String &p_path);
virtual String texture_get_path(RID p_texture) const;
@@ -794,8 +797,40 @@ public:
/* IMMEDIATE API */
+ struct Immediate : public Geometry {
+
+ struct Chunk {
+ RID texture;
+ VS::PrimitiveType primitive;
+ Vector<Vector3> vertices;
+ Vector<Vector3> normals;
+ Vector<Plane> tangents;
+ Vector<Color> colors;
+ Vector<Vector2> uvs;
+ Vector<Vector2> uv2s;
+ };
+
+ List<Chunk> chunks;
+ bool building;
+ int mask;
+ AABB aabb;
+
+ Immediate() {
+ type = GEOMETRY_IMMEDIATE;
+ building = false;
+ }
+ };
+
+ Vector3 chunk_normal;
+ Plane chunk_tangent;
+ Color chunk_color;
+ Vector2 chunk_uv;
+ Vector2 chunk_uv2;
+
+ mutable RID_Owner<Immediate> immediate_owner;
+
virtual RID immediate_create();
- virtual void immediate_begin(RID p_immediate, VS::PrimitiveType p_rimitive, RID p_texture = RID());
+ virtual void immediate_begin(RID p_immediate, VS::PrimitiveType p_primitive, RID p_texture = RID());
virtual void immediate_vertex(RID p_immediate, const Vector3 &p_vertex);
virtual void immediate_normal(RID p_immediate, const Vector3 &p_normal);
virtual void immediate_tangent(RID p_immediate, const Plane &p_tangent);
diff --git a/drivers/gles2/shader_compiler_gles2.cpp b/drivers/gles2/shader_compiler_gles2.cpp
index 6c7f767733..5ac2af6e5c 100644
--- a/drivers/gles2/shader_compiler_gles2.cpp
+++ b/drivers/gles2/shader_compiler_gles2.cpp
@@ -704,6 +704,10 @@ String ShaderCompilerGLES2::_dump_node_code(SL::Node *p_node, int p_level, Gener
}
code += ";\n";
} else if (cf_node->flow_op == SL::FLOW_OP_DISCARD) {
+ if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) {
+ *p_actions.usage_flag_pointers["DISCARD"] = true;
+ used_flag_pointers.insert("DISCARD");
+ }
code += "discard;";
} else if (cf_node->flow_op == SL::FLOW_OP_CONTINUE) {
code += "continue;";
diff --git a/drivers/gles2/shader_gles2.cpp b/drivers/gles2/shader_gles2.cpp
index 146575973f..e9b58cb272 100644
--- a/drivers/gles2/shader_gles2.cpp
+++ b/drivers/gles2/shader_gles2.cpp
@@ -527,8 +527,13 @@ ShaderGLES2::Version *ShaderGLES2::get_current_version() {
for (int i = 0; i < texunit_pair_count; i++) {
GLint loc = glGetUniformLocation(v.id, texunit_pairs[i].name);
- if (loc >= 0)
- glUniform1i(loc, texunit_pairs[i].index);
+ if (loc >= 0) {
+ if (texunit_pairs[i].index < 0) {
+ glUniform1i(loc, max_image_units + texunit_pairs[i].index);
+ } else {
+ glUniform1i(loc, texunit_pairs[i].index);
+ }
+ }
}
if (cc) {
@@ -643,6 +648,8 @@ void ShaderGLES2::setup(
}
}
}
+
+ glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_image_units);
}
void ShaderGLES2::finish() {
@@ -717,7 +724,7 @@ void ShaderGLES2::free_custom_shader(uint32_t p_code_id) {
custom_code_map.erase(p_code_id);
}
-void ShaderGLES2::use_material(void *p_material, int p_num_predef_textures) {
+void ShaderGLES2::use_material(void *p_material) {
RasterizerStorageGLES2::Material *material = (RasterizerStorageGLES2::Material *)p_material;
if (!material) {
@@ -906,20 +913,58 @@ void ShaderGLES2::use_material(void *p_material, int p_num_predef_textures) {
case ShaderLanguage::TYPE_MAT2: {
Transform2D val = V->get();
- // TODO
+ if (value.second.size() < 4) {
+ value.second.resize(4);
+ }
+
+ value.second.write[0].real = val.elements[0][0];
+ value.second.write[1].real = val.elements[0][1];
+ value.second.write[2].real = val.elements[1][0];
+ value.second.write[3].real = val.elements[1][1];
} break;
case ShaderLanguage::TYPE_MAT3: {
Basis val = V->get();
- // TODO
+ if (value.second.size() < 9) {
+ value.second.resize(9);
+ }
+
+ value.second.write[0].real = val.elements[0][0];
+ value.second.write[1].real = val.elements[0][1];
+ value.second.write[2].real = val.elements[0][2];
+ value.second.write[3].real = val.elements[1][0];
+ value.second.write[4].real = val.elements[1][1];
+ value.second.write[5].real = val.elements[1][2];
+ value.second.write[6].real = val.elements[2][0];
+ value.second.write[7].real = val.elements[2][1];
+ value.second.write[8].real = val.elements[2][2];
} break;
case ShaderLanguage::TYPE_MAT4: {
Transform val = V->get();
- // TODO
+ if (value.second.size() < 16) {
+ value.second.resize(16);
+ }
+
+ value.second.write[0].real = val.basis.elements[0][0];
+ value.second.write[1].real = val.basis.elements[0][1];
+ value.second.write[2].real = val.basis.elements[0][2];
+ value.second.write[3].real = 0;
+ value.second.write[4].real = val.basis.elements[1][0];
+ value.second.write[5].real = val.basis.elements[1][1];
+ value.second.write[6].real = val.basis.elements[1][2];
+ value.second.write[7].real = 0;
+ value.second.write[8].real = val.basis.elements[2][0];
+ value.second.write[9].real = val.basis.elements[2][1];
+ value.second.write[10].real = val.basis.elements[2][2];
+ value.second.write[11].real = 0;
+ value.second.write[12].real = val.origin[0];
+ value.second.write[13].real = val.origin[1];
+ value.second.write[14].real = val.origin[2];
+ value.second.write[15].real = 1;
} break;
case ShaderLanguage::TYPE_SAMPLER2D: {
@@ -1034,7 +1079,7 @@ void ShaderGLES2::use_material(void *p_material, int p_num_predef_textures) {
Pair<ShaderLanguage::DataType, Vector<ShaderLanguage::ConstantNode::Value> > value;
value.first = ShaderLanguage::TYPE_INT;
value.second.resize(1);
- value.second.write[0].sint = p_num_predef_textures + i;
+ value.second.write[0].sint = i;
// GLint location = get_uniform_location(textures[i].first);
diff --git a/drivers/gles2/shader_gles2.h b/drivers/gles2/shader_gles2.h
index 0c53c4ba72..cb515c199c 100644
--- a/drivers/gles2/shader_gles2.h
+++ b/drivers/gles2/shader_gles2.h
@@ -300,7 +300,7 @@ public:
case ShaderLanguage::TYPE_MAT3: {
GLfloat mat[9];
- for (int i = 0; i < 0; i++) {
+ for (int i = 0; i < 9; i++) {
mat[i] = values[i].real;
}
@@ -311,7 +311,7 @@ public:
case ShaderLanguage::TYPE_MAT4: {
GLfloat mat[16];
- for (int i = 0; i < 0; i++) {
+ for (int i = 0; i < 16; i++) {
mat[i] = values[i].real;
}
@@ -465,7 +465,7 @@ public:
// this void* is actually a RasterizerStorageGLES2::Material, but C++ doesn't
// like forward declared nested classes.
- void use_material(void *p_material, int p_num_predef_textures);
+ void use_material(void *p_material);
uint32_t get_version() const { return new_conditional_version.version; }
diff --git a/drivers/gles2/shaders/scene.glsl b/drivers/gles2/shaders/scene.glsl
index e08e9d1117..9251e21080 100644
--- a/drivers/gles2/shaders/scene.glsl
+++ b/drivers/gles2/shaders/scene.glsl
@@ -48,7 +48,7 @@ attribute highp vec4 bone_transform_row_2; // attrib:11
attribute vec4 bone_ids; // attrib:6
attribute highp vec4 bone_weights; // attrib:7
-uniform highp sampler2D bone_transforms; // texunit:4
+uniform highp sampler2D bone_transforms; // texunit:-1
uniform ivec2 skeleton_texture_size;
#endif
@@ -181,7 +181,7 @@ void main() {
#else
// look up transform from the "pose texture"
{
-
+
for (int i = 0; i < 4; i++) {
ivec2 tex_ofs = ivec2(int(bone_ids[i]) * 3, 0);
@@ -252,7 +252,7 @@ VERTEX_SHADER_CODE
float z_ofs = light_bias;
z_ofs += (1.0 - abs(normal_interp.z)) * light_normal_bias;
-
+
vertex_interp.z -= z_ofs;
#endif
@@ -294,17 +294,17 @@ uniform highp float time;
uniform vec2 screen_pixel_size;
#endif
-uniform highp sampler2D depth_buffer; //texunit:1
+uniform highp sampler2D depth_buffer; //texunit:-5
#if defined(SCREEN_TEXTURE_USED)
-uniform highp sampler2D screen_texture; //texunit:2
+uniform highp sampler2D screen_texture; //texunit:-6
#endif
#ifdef USE_RADIANCE_MAP
#define RADIANCE_MAX_LOD 6.0
-uniform samplerCube radiance_map; // texunit:0
+uniform samplerCube radiance_map; // texunit:-2
uniform mat4 radiance_inverse_xform;
@@ -345,7 +345,7 @@ uniform float light_spot_angle;
// shadows
-uniform highp sampler2D light_shadow_atlas; //texunit:3
+uniform highp sampler2D light_shadow_atlas; //texunit:-4
uniform float light_has_shadow;
uniform mat4 light_shadow_matrix;
@@ -353,7 +353,7 @@ uniform vec4 light_clamp;
// directional shadow
-uniform highp sampler2D light_directional_shadow; // texunit:3
+uniform highp sampler2D light_directional_shadow; // texunit:-4
uniform vec4 light_split_offsets;
uniform mat4 light_shadow_matrix1;
@@ -439,11 +439,10 @@ void light_compute(vec3 N,
{
// calculate specular reflection
- vec3 R = normalize(-reflect(L,N));
- float cRdotV = max(dot(R, V), 0.0);
- float blob_intensity = pow(cRdotV, (1.0 - roughness) * 256.0);
- specular_light += light_color * attenuation * blob_intensity * specular_blob_intensity;
-
+ vec3 R = normalize(-reflect(L,N));
+ float cRdotV = max(dot(R, V), 0.0);
+ float blob_intensity = pow(cRdotV, (1.0 - roughness) * 256.0);
+ specular_light += light_color * attenuation * blob_intensity * specular_blob_intensity;
}
}
@@ -460,7 +459,7 @@ float sample_shadow(highp sampler2D shadow,
vec4 clamp_rect)
{
// vec4 depth_value = texture2D(shadow, pos);
-
+
// return depth_value.z;
return texture2DProj(shadow, vec4(pos, depth, 1.0)).r;
// return (depth_value.x + depth_value.y + depth_value.z + depth_value.w) / 4.0;
@@ -469,22 +468,22 @@ float sample_shadow(highp sampler2D shadow,
#endif
-void main()
+void main()
{
highp vec3 vertex = vertex_interp;
- vec3 albedo = vec3(0.8, 0.8, 0.8);
+ vec3 albedo = vec3(1.0);
vec3 transmission = vec3(0.0);
float metallic = 0.0;
float specular = 0.5;
- vec3 emission = vec3(0.0, 0.0, 0.0);
+ vec3 emission = vec3(0.0);
float roughness = 1.0;
float rim = 0.0;
float rim_tint = 0.0;
float clearcoat = 0.0;
float clearcoat_gloss = 0.0;
- float anisotropy = 1.0;
- vec2 anisotropy_flow = vec2(1.0,0.0);
+ float anisotropy = 0.0;
+ vec2 anisotropy_flow = vec2(1.0, 0.0);
float alpha = 1.0;
float side = 1.0;
@@ -536,7 +535,7 @@ FRAGMENT_SHADER_CODE
normal = normalize(normal);
vec3 N = normal;
-
+
vec3 specular_light = vec3(0.0, 0.0, 0.0);
vec3 diffuse_light = vec3(0.0, 0.0, 0.0);
@@ -551,7 +550,7 @@ FRAGMENT_SHADER_CODE
discard;
}
#endif
-
+
//
// Lighting
//
@@ -621,11 +620,11 @@ FRAGMENT_SHADER_CODE
vec3 light_vec = -light_direction;
vec3 attenuation = vec3(1.0, 1.0, 1.0);
-
+
float depth_z = -vertex.z;
-
+
if (light_has_shadow > 0.5) {
-
+
#ifdef LIGHT_USE_PSSM4
if (depth_z < light_split_offsets.w) {
#elif defined(LIGHT_USE_PSSM2)
@@ -633,36 +632,36 @@ FRAGMENT_SHADER_CODE
#else
if (depth_z < light_split_offsets.x) {
#endif
-
+
vec3 pssm_coord;
float pssm_fade = 0.0;
-
+
#ifdef LIGHT_USE_PSSM_BLEND
float pssm_blend;
vec3 pssm_coord2;
bool use_blend = true;
#endif
-
+
#ifdef LIGHT_USE_PSSM4
if (depth_z < light_split_offsets.y) {
if (depth_z < light_split_offsets.x) {
highp vec4 splane = (light_shadow_matrix1 * vec4(vertex, 1.0));
pssm_coord = splane.xyz / splane.w;
-
+
#ifdef LIGHT_USE_PSSM_BLEND
splane = (light_shadow_matrix2 * vec4(vertex, 1.0));
pssm_coord2 = splane.xyz / splane.w;
-
+
pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
#endif
} else {
highp vec4 splane = (light_shadow_matrix2 * vec4(vertex, 1.0));
pssm_coord = splane.xyz / splane.w;
-
+
#ifdef LIGHT_USE_PSSM_BLEND
splane = (light_shadow_matrix3 * vec4(vertex, 1.0));
pssm_coord2 = splane.xyz / splane.w;
-
+
pssm_blend = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z);
#endif
}
@@ -689,15 +688,15 @@ FRAGMENT_SHADER_CODE
#endif
}
}
-
+
#endif // LIGHT_USE_PSSM4
-
+
#ifdef LIGHT_USE_PSSM2
if (depth_z < light_split_offsets.x) {
-
+
highp vec4 splane = (light_shadow_matrix1 * vec4(vertex, 1.0));
pssm_coord = splane.xyz / splane.w;
-
+
#ifdef LIGHT_USE_PSSM_BLEND
splane = (light_shadow_matrix2 * vec4(vertex, 1.0));
pssm_coord2 = splane.xyz / splane.w;
@@ -711,29 +710,29 @@ FRAGMENT_SHADER_CODE
use_blend = false;
#endif
}
-
+
#endif // LIGHT_USE_PSSM2
-
+
#if !defined(LIGHT_USE_PSSM4) && !defined(LIGHT_USE_PSSM2)
{
highp vec4 splane = (light_shadow_matrix1 * vec4(vertex, 1.0));
pssm_coord = splane.xyz / splane.w;
}
#endif
-
+
float shadow = sample_shadow(light_shadow_atlas, vec2(0.0), pssm_coord.xy, pssm_coord.z, light_clamp);
-
+
#ifdef LIGHT_USE_PSSM_BLEND
if (use_blend) {
shadow = mix(shadow, sample_shadow(light_shadow_atlas, vec2(0.0), pssm_coord2.xy, pssm_coord2.z, light_clamp), pssm_blend);
}
#endif
-
+
attenuation *= shadow;
-
-
+
+
}
-
+
}
light_compute(normal,
@@ -758,19 +757,19 @@ FRAGMENT_SHADER_CODE
} else if (light_type == LIGHT_TYPE_SPOT) {
vec3 light_att = vec3(1.0);
-
+
if (light_has_shadow > 0.5) {
highp vec4 splane = (light_shadow_matrix * vec4(vertex, 1.0));
splane.xyz /= splane.w;
-
+
float shadow = sample_shadow(light_shadow_atlas, vec2(0.0), splane.xy, splane.z, light_clamp);
-
+
if (shadow > splane.z) {
} else {
light_att = vec3(0.0);
}
-
-
+
+
}
vec3 light_rel_vec = light_position - vertex;
@@ -788,7 +787,7 @@ FRAGMENT_SHADER_CODE
spot_attenuation *= 1.0 - pow(spot_rim, light_spot_attenuation);
light_att *= vec3(spot_attenuation);
-
+
light_compute(normal,
normalize(light_rel_vec),
eye_position,
@@ -808,7 +807,6 @@ FRAGMENT_SHADER_CODE
anisotropy,
diffuse_light,
specular_light);
-
}
gl_FragColor = vec4(ambient_light + diffuse_light + specular_light, alpha);
@@ -837,9 +835,9 @@ FRAGMENT_SHADER_CODE
}
ambient_light *= ambient_energy;
-
+
specular_light += env_reflection_light;
-
+
ambient_light *= albedo;
#if defined(ENABLE_AO)
@@ -848,12 +846,12 @@ FRAGMENT_SHADER_CODE
specular_light *= ao_light_affect;
diffuse_light *= ao_light_affect;
#endif
-
+
diffuse_light *= 1.0 - metallic;
ambient_light *= 1.0 - metallic;
-
+
// environment BRDF approximation
-
+
// TODO shadeless
{
const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
diff --git a/drivers/gles3/rasterizer_gles3.cpp b/drivers/gles3/rasterizer_gles3.cpp
index 97c4a98aab..cb17695c5f 100644
--- a/drivers/gles3/rasterizer_gles3.cpp
+++ b/drivers/gles3/rasterizer_gles3.cpp
@@ -225,7 +225,7 @@ void RasterizerGLES3::set_current_render_target(RID p_render_target) {
if (!p_render_target.is_valid() && storage->frame.current_rt && storage->frame.clear_request) {
//handle pending clear request, if the framebuffer was not cleared
glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
- print_line("unbind clear of: " + storage->frame.clear_request_color);
+
glClearColor(
storage->frame.clear_request_color.r,
storage->frame.clear_request_color.g,
@@ -290,7 +290,7 @@ void RasterizerGLES3::set_boot_image(const Ref<Image> &p_image, const Color &p_c
canvas->canvas_begin();
RID texture = storage->texture_create();
- storage->texture_allocate(texture, p_image->get_width(), p_image->get_height(), p_image->get_format(), VS::TEXTURE_FLAG_FILTER);
+ storage->texture_allocate(texture, p_image->get_width(), p_image->get_height(), 0, p_image->get_format(), VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAG_FILTER);
storage->texture_set_data(texture, p_image);
Rect2 imgrect(0, 0, p_image->get_width(), p_image->get_height());
diff --git a/drivers/gles3/rasterizer_scene_gles3.cpp b/drivers/gles3/rasterizer_scene_gles3.cpp
index d01ba2ddcc..eebdbe9493 100644
--- a/drivers/gles3/rasterizer_scene_gles3.cpp
+++ b/drivers/gles3/rasterizer_scene_gles3.cpp
@@ -1190,6 +1190,7 @@ bool RasterizerSceneGLES3::_setup_material(RasterizerStorageGLES3::Material *p_m
int tc = p_material->textures.size();
RID *textures = p_material->textures.ptrw();
ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = p_material->shader->texture_hints.ptrw();
+ const ShaderLanguage::DataType *texture_types = p_material->shader->texture_types.ptr();
state.current_main_tex = 0;
@@ -1198,39 +1199,17 @@ bool RasterizerSceneGLES3::_setup_material(RasterizerStorageGLES3::Material *p_m
glActiveTexture(GL_TEXTURE0 + i);
GLenum target;
- GLuint tex;
+ GLuint tex = 0;
- RasterizerStorageGLES3::Texture *t = storage->texture_owner.getornull(textures[i]);
+ RasterizerStorageGLES3::Texture *t = storage->texture_owner.getptr(textures[i]);
- if (!t) {
- //check hints
- target = GL_TEXTURE_2D;
-
- switch (texture_hints[i]) {
- case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
- case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
- tex = storage->resources.black_tex;
- } break;
- case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
- tex = storage->resources.aniso_tex;
- } break;
- case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
- tex = storage->resources.normal_tex;
-
- } break;
- default: {
- tex = storage->resources.white_tex;
- } break;
- }
-
- } else {
+ if (t) {
if (t->redraw_if_visible) { //must check before proxy because this is often used with proxies
VisualServerRaster::redraw_request();
}
t = t->get_ptr(); //resolve for proxies
-
#ifdef TOOLS_ENABLED
if (t->detect_3d) {
t->detect_3d(t->detect_3d_ud);
@@ -1247,6 +1226,59 @@ bool RasterizerSceneGLES3::_setup_material(RasterizerStorageGLES3::Material *p_m
target = t->target;
tex = t->tex_id;
+ } else {
+
+ switch (texture_types[i]) {
+ case ShaderLanguage::TYPE_ISAMPLER2D:
+ case ShaderLanguage::TYPE_USAMPLER2D:
+ case ShaderLanguage::TYPE_SAMPLER2D: {
+ target = GL_TEXTURE_2D;
+
+ switch (texture_hints[i]) {
+ case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
+ case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
+ tex = storage->resources.black_tex;
+ } break;
+ case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
+ tex = storage->resources.aniso_tex;
+ } break;
+ case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
+ tex = storage->resources.normal_tex;
+
+ } break;
+ default: {
+ tex = storage->resources.white_tex;
+ } break;
+ }
+
+ } break;
+
+ case ShaderLanguage::TYPE_SAMPLERCUBE: {
+ // TODO
+ } break;
+
+ case ShaderLanguage::TYPE_ISAMPLER3D:
+ case ShaderLanguage::TYPE_USAMPLER3D:
+ case ShaderLanguage::TYPE_SAMPLER3D: {
+
+ target = GL_TEXTURE_3D;
+
+ switch (texture_hints[i]) {
+
+ // TODO
+ default: {
+ tex = storage->resources.white_tex_3d;
+ } break;
+ }
+
+ } break;
+
+ case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
+ case ShaderLanguage::TYPE_USAMPLER2DARRAY:
+ case ShaderLanguage::TYPE_SAMPLER2DARRAY: {
+ // TODO
+ } break;
+ }
}
glBindTexture(target, tex);
diff --git a/drivers/gles3/rasterizer_storage_gles3.cpp b/drivers/gles3/rasterizer_storage_gles3.cpp
index c1c1b2a009..a5c81d6c4d 100644
--- a/drivers/gles3/rasterizer_storage_gles3.cpp
+++ b/drivers/gles3/rasterizer_storage_gles3.cpp
@@ -595,7 +595,7 @@ RID RasterizerStorageGLES3::texture_create() {
return texture_owner.make_rid(texture);
}
-void RasterizerStorageGLES3::texture_allocate(RID p_texture, int p_width, int p_height, Image::Format p_format, uint32_t p_flags) {
+void RasterizerStorageGLES3::texture_allocate(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, VisualServer::TextureType p_type, uint32_t p_flags) {
GLenum format;
GLenum internal_format;
@@ -612,15 +612,37 @@ void RasterizerStorageGLES3::texture_allocate(RID p_texture, int p_width, int p_
ERR_FAIL_COND(!texture);
texture->width = p_width;
texture->height = p_height;
+ texture->depth = p_depth_3d;
texture->format = p_format;
texture->flags = p_flags;
texture->stored_cube_sides = 0;
- texture->target = (p_flags & VS::TEXTURE_FLAG_CUBEMAP) ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
+
+ texture->type = p_type;
+
+ switch (p_type) {
+ case VS::TEXTURE_TYPE_2D: {
+ texture->target = GL_TEXTURE_2D;
+ texture->images.resize(1);
+ } break;
+ case VS::TEXTURE_TYPE_CUBEMAP: {
+ texture->target = GL_TEXTURE_CUBE_MAP;
+ texture->images.resize(6);
+ } break;
+ case VS::TEXTURE_TYPE_2D_ARRAY: {
+ texture->target = GL_TEXTURE_2D_ARRAY;
+ texture->images.resize(p_depth_3d);
+ } break;
+ case VS::TEXTURE_TYPE_3D: {
+ texture->target = GL_TEXTURE_3D;
+ texture->images.resize(p_depth_3d);
+ } break;
+ }
_get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, format, internal_format, type, compressed, srgb);
texture->alloc_width = texture->width;
texture->alloc_height = texture->height;
+ texture->alloc_depth = texture->depth;
texture->gl_format_cache = format;
texture->gl_type_cache = type;
@@ -633,7 +655,34 @@ void RasterizerStorageGLES3::texture_allocate(RID p_texture, int p_width, int p_
glActiveTexture(GL_TEXTURE0);
glBindTexture(texture->target, texture->tex_id);
- if (p_flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
+ if (p_type == VS::TEXTURE_TYPE_3D || p_type == VS::TEXTURE_TYPE_2D_ARRAY) {
+
+ int width = p_width;
+ int height = p_height;
+ int depth = p_depth_3d;
+
+ int mipmaps = 0;
+
+ while (width != 1 && height != 1) {
+ glTexImage3D(texture->target, 0, internal_format, width, height, depth, 0, format, type, NULL);
+
+ width = MAX(1, width / 2);
+ height = MAX(1, height / 2);
+
+ if (p_type == VS::TEXTURE_TYPE_3D) {
+ depth = MAX(1, depth / 2);
+ }
+
+ mipmaps++;
+
+ if (!(p_flags & VS::TEXTURE_FLAG_MIPMAPS))
+ break;
+ }
+
+ glTexParameteri(texture->target, GL_TEXTURE_BASE_LEVEL, 0);
+ glTexParameteri(texture->target, GL_TEXTURE_MAX_LEVEL, mipmaps - 1);
+
+ } else if (p_flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
//prealloc if video
glTexImage2D(texture->target, 0, internal_format, p_width, p_height, 0, format, type, NULL);
}
@@ -641,7 +690,7 @@ void RasterizerStorageGLES3::texture_allocate(RID p_texture, int p_width, int p_
texture->active = true;
}
-void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p_image, VS::CubeMapSide p_cube_side) {
+void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer) {
Texture *texture = texture_owner.get(p_texture);
@@ -658,7 +707,7 @@ void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p
bool srgb;
if (config.keep_original_textures && !(texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING)) {
- texture->images[p_cube_side] = p_image;
+ texture->images.write[p_layer] = p_image;
}
Ref<Image> img = _get_gl_image_and_format(p_image, p_image->get_format(), texture->flags, format, internal_format, type, compressed, srgb);
@@ -677,7 +726,23 @@ void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p
}
};
- GLenum blit_target = (texture->target == GL_TEXTURE_CUBE_MAP) ? _cube_side_enum[p_cube_side] : GL_TEXTURE_2D;
+ GLenum blit_target;
+
+ switch (texture->type) {
+ case VS::TEXTURE_TYPE_2D: {
+ blit_target = GL_TEXTURE_2D;
+ } break;
+ case VS::TEXTURE_TYPE_CUBEMAP: {
+ ERR_FAIL_INDEX(p_layer, 6);
+ blit_target = _cube_side_enum[p_layer];
+ } break;
+ case VS::TEXTURE_TYPE_2D_ARRAY: {
+ blit_target = GL_TEXTURE_2D_ARRAY;
+ } break;
+ case VS::TEXTURE_TYPE_3D: {
+ blit_target = GL_TEXTURE_3D;
+ } break;
+ }
texture->data_size = img->get_data().size();
PoolVector<uint8_t>::Read read = img->get_data().read();
@@ -785,20 +850,36 @@ void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p
//print_line("mipmap: "+itos(i)+" size: "+itos(size)+" w: "+itos(mm_w)+", h: "+itos(mm_h));
- if (texture->compressed) {
- glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+ if (texture->type == VS::TEXTURE_TYPE_2D || texture->type == VS::TEXTURE_TYPE_CUBEMAP) {
+
+ if (texture->compressed) {
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
- int bw = w;
- int bh = h;
+ int bw = w;
+ int bh = h;
- glCompressedTexImage2D(blit_target, i, internal_format, bw, bh, 0, size, &read[ofs]);
+ glCompressedTexImage2D(blit_target, i, internal_format, bw, bh, 0, size, &read[ofs]);
+ } else {
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ if (texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
+ glTexSubImage2D(blit_target, i, 0, 0, w, h, format, type, &read[ofs]);
+ } else {
+ glTexImage2D(blit_target, i, internal_format, w, h, 0, format, type, &read[ofs]);
+ }
+ }
} else {
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- if (texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
- glTexSubImage2D(blit_target, i, 0, 0, w, h, format, type, &read[ofs]);
+ if (texture->compressed) {
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+
+ int bw = w;
+ int bh = h;
+
+ glCompressedTexSubImage3D(blit_target, i, 0, 0, p_layer, bw, bh, 1, internal_format, size, &read[ofs]);
} else {
- glTexImage2D(blit_target, i, internal_format, w, h, 0, format, type, &read[ofs]);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+
+ glTexSubImage3D(blit_target, i, 0, 0, p_layer, w, h, 1, format, type, &read[ofs]);
}
}
tsize += size;
@@ -813,14 +894,17 @@ void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p
//printf("texture: %i x %i - size: %i - total: %i\n",texture->width,texture->height,tsize,_rinfo.texture_mem);
- texture->stored_cube_sides |= (1 << p_cube_side);
+ texture->stored_cube_sides |= (1 << p_layer);
- if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && mipmaps == 1 && !texture->ignore_mipmaps && (!(texture->flags & VS::TEXTURE_FLAG_CUBEMAP) || texture->stored_cube_sides == (1 << 6) - 1)) {
+ if ((texture->type == VS::TEXTURE_TYPE_2D || texture->type == VS::TEXTURE_TYPE_CUBEMAP) && (texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && mipmaps == 1 && !texture->ignore_mipmaps && (texture->type != VS::TEXTURE_TYPE_CUBEMAP || texture->stored_cube_sides == (1 << 6) - 1)) {
//generate mipmaps if they were requested and the image does not contain them
glGenerateMipmap(texture->target);
} else if (mipmaps > 1) {
glTexParameteri(texture->target, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(texture->target, GL_TEXTURE_MAX_LEVEL, mipmaps - 1);
+ } else {
+ glTexParameteri(texture->target, GL_TEXTURE_BASE_LEVEL, 0);
+ glTexParameteri(texture->target, GL_TEXTURE_MAX_LEVEL, 0);
}
texture->mipmaps = mipmaps;
@@ -831,7 +915,7 @@ void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p
// Uploads pixel data to a sub-region of a texture, for the specified mipmap.
// The texture pixels must have been allocated before, because most features seen in texture_set_data() make no sense in a partial update.
// TODO If we want this to be usable without pre-filling pixels with a full image, we have to call glTexImage2D() with null data.
-void RasterizerStorageGLES3::texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, VS::CubeMapSide p_cube_side) {
+void RasterizerStorageGLES3::texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, int p_layer) {
Texture *texture = texture_owner.get(p_texture);
@@ -859,7 +943,23 @@ void RasterizerStorageGLES3::texture_set_data_partial(RID p_texture, const Ref<I
Ref<Image> img = _get_gl_image_and_format(p_sub_img, p_sub_img->get_format(), texture->flags, format, internal_format, type, compressed, srgb);
- GLenum blit_target = (texture->target == GL_TEXTURE_CUBE_MAP) ? _cube_side_enum[p_cube_side] : GL_TEXTURE_2D;
+ GLenum blit_target;
+
+ switch (texture->type) {
+ case VS::TEXTURE_TYPE_2D: {
+ blit_target = GL_TEXTURE_2D;
+ } break;
+ case VS::TEXTURE_TYPE_CUBEMAP: {
+ ERR_FAIL_INDEX(p_layer, 6);
+ blit_target = _cube_side_enum[p_layer];
+ } break;
+ case VS::TEXTURE_TYPE_2D_ARRAY: {
+ blit_target = GL_TEXTURE_2D_ARRAY;
+ } break;
+ case VS::TEXTURE_TYPE_3D: {
+ blit_target = GL_TEXTURE_3D;
+ } break;
+ }
PoolVector<uint8_t>::Read read = img->get_data().read();
@@ -869,18 +969,38 @@ void RasterizerStorageGLES3::texture_set_data_partial(RID p_texture, const Ref<I
int src_data_size = img->get_data().size();
int src_ofs = 0;
- if (texture->compressed) {
- glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
- glCompressedTexSubImage2D(blit_target, p_dst_mip, dst_x, dst_y, src_w, src_h, internal_format, src_data_size, &read[src_ofs]);
+ if (texture->type == VS::TEXTURE_TYPE_2D || texture->type == VS::TEXTURE_TYPE_CUBEMAP) {
+ if (texture->compressed) {
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+ glCompressedTexSubImage2D(blit_target, p_dst_mip, dst_x, dst_y, src_w, src_h, internal_format, src_data_size, &read[src_ofs]);
+ } else {
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ // `format` has to match the internal_format used when the texture was created
+ glTexSubImage2D(blit_target, p_dst_mip, dst_x, dst_y, src_w, src_h, format, type, &read[src_ofs]);
+ }
} else {
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- // `format` has to match the internal_format used when the texture was created
- glTexSubImage2D(blit_target, p_dst_mip, dst_x, dst_y, src_w, src_h, format, type, &read[src_ofs]);
+ if (texture->compressed) {
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+ glCompressedTexSubImage3D(blit_target, p_dst_mip, dst_x, dst_y, p_layer, src_w, src_h, 1, format, src_data_size, &read[src_ofs]);
+ } else {
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ // `format` has to match the internal_format used when the texture was created
+ glTexSubImage3D(blit_target, p_dst_mip, dst_x, dst_y, p_layer, src_w, src_h, 1, format, type, &read[src_ofs]);
+ }
+ }
+
+ if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
+
+ glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtering
+
+ } else {
+
+ glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // raw Filtering
}
}
-Ref<Image> RasterizerStorageGLES3::texture_get_data(RID p_texture, VS::CubeMapSide p_cube_side) const {
+Ref<Image> RasterizerStorageGLES3::texture_get_data(RID p_texture, int p_layer) const {
Texture *texture = texture_owner.get(p_texture);
@@ -888,8 +1008,8 @@ Ref<Image> RasterizerStorageGLES3::texture_get_data(RID p_texture, VS::CubeMapSi
ERR_FAIL_COND_V(!texture->active, Ref<Image>());
ERR_FAIL_COND_V(texture->data_size == 0 && !texture->render_target, Ref<Image>());
- if (!texture->images[p_cube_side].is_null()) {
- return texture->images[p_cube_side];
+ if (texture->type == VS::TEXTURE_TYPE_CUBEMAP && p_layer < 6 && !texture->images[p_layer].is_null()) {
+ return texture->images[p_layer];
}
#ifdef GLES_OVER_GL
@@ -977,10 +1097,10 @@ void RasterizerStorageGLES3::texture_set_flags(RID p_texture, uint32_t p_flags)
bool had_mipmaps = texture->flags & VS::TEXTURE_FLAG_MIPMAPS;
+ texture->flags = p_flags;
+
glActiveTexture(GL_TEXTURE0);
glBindTexture(texture->target, texture->tex_id);
- uint32_t cube = texture->flags & VS::TEXTURE_FLAG_CUBEMAP;
- texture->flags = p_flags | cube; // can't remove a cube from being a cube
if (((texture->flags & VS::TEXTURE_FLAG_REPEAT) || (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT)) && texture->target != GL_TEXTURE_CUBE_MAP) {
@@ -1058,6 +1178,14 @@ Image::Format RasterizerStorageGLES3::texture_get_format(RID p_texture) const {
return texture->format;
}
+
+VisualServer::TextureType RasterizerStorageGLES3::texture_get_type(RID p_texture) const {
+ Texture *texture = texture_owner.get(p_texture);
+
+ ERR_FAIL_COND_V(!texture, VS::TEXTURE_TYPE_2D);
+
+ return texture->type;
+}
uint32_t RasterizerStorageGLES3::texture_get_texid(RID p_texture) const {
Texture *texture = texture_owner.get(p_texture);
@@ -1083,7 +1211,16 @@ uint32_t RasterizerStorageGLES3::texture_get_height(RID p_texture) const {
return texture->height;
}
-void RasterizerStorageGLES3::texture_set_size_override(RID p_texture, int p_width, int p_height) {
+uint32_t RasterizerStorageGLES3::texture_get_depth(RID p_texture) const {
+
+ Texture *texture = texture_owner.get(p_texture);
+
+ ERR_FAIL_COND_V(!texture, 0);
+
+ return texture->depth;
+}
+
+void RasterizerStorageGLES3::texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth) {
Texture *texture = texture_owner.get(p_texture);
@@ -1123,8 +1260,9 @@ void RasterizerStorageGLES3::texture_debug_usage(List<VS::TextureInfo> *r_info)
VS::TextureInfo tinfo;
tinfo.path = t->path;
tinfo.format = t->format;
- tinfo.size.x = t->alloc_width;
- tinfo.size.y = t->alloc_height;
+ tinfo.width = t->alloc_width;
+ tinfo.height = t->alloc_height;
+ tinfo.depth = 0;
tinfo.bytes = t->total_data_size;
r_info->push_back(tinfo);
}
@@ -1169,7 +1307,7 @@ RID RasterizerStorageGLES3::texture_create_radiance_cubemap(RID p_source, int p_
Texture *texture = texture_owner.get(p_source);
ERR_FAIL_COND_V(!texture, RID());
- ERR_FAIL_COND_V(!(texture->flags & VS::TEXTURE_FLAG_CUBEMAP), RID());
+ ERR_FAIL_COND_V(texture->type != VS::TEXTURE_TYPE_CUBEMAP, RID());
bool use_float = config.hdr_supported;
@@ -1285,7 +1423,8 @@ RID RasterizerStorageGLES3::texture_create_radiance_cubemap(RID p_source, int p_
Texture *ctex = memnew(Texture);
- ctex->flags = VS::TEXTURE_FLAG_CUBEMAP | VS::TEXTURE_FLAG_MIPMAPS | VS::TEXTURE_FLAG_FILTER;
+ ctex->type = VS::TEXTURE_TYPE_CUBEMAP;
+ ctex->flags = VS::TEXTURE_FLAG_MIPMAPS | VS::TEXTURE_FLAG_FILTER;
ctex->width = p_resolution;
ctex->height = p_resolution;
ctex->alloc_width = p_resolution;
@@ -1765,6 +1904,7 @@ void RasterizerStorageGLES3::_update_shader(Shader *p_shader) const {
p_shader->ubo_offsets = gen_code.uniform_offsets;
p_shader->texture_count = gen_code.texture_uniforms.size();
p_shader->texture_hints = gen_code.texture_hints;
+ p_shader->texture_types = gen_code.texture_types;
p_shader->uses_vertex_time = gen_code.uses_vertex_time;
p_shader->uses_fragment_time = gen_code.uses_fragment_time;
@@ -1875,6 +2015,13 @@ void RasterizerStorageGLES3::shader_get_param_list(RID p_shader, List<PropertyIn
pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
pi.hint_string = "Texture";
} break;
+ case ShaderLanguage::TYPE_SAMPLER3D:
+ case ShaderLanguage::TYPE_ISAMPLER3D:
+ case ShaderLanguage::TYPE_USAMPLER3D: {
+ pi.type = Variant::OBJECT;
+ pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
+ pi.hint_string = "Texture3D";
+ } break;
case ShaderLanguage::TYPE_SAMPLERCUBE: {
pi.type = Variant::OBJECT;
@@ -2649,6 +2796,7 @@ void RasterizerStorageGLES3::_update_material(Material *material) {
//set up the texture array, for easy access when it needs to be drawn
if (material->shader && material->shader->texture_count) {
+ material->texture_is_3d.resize(material->shader->texture_count);
material->textures.resize(material->shader->texture_count);
for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = material->shader->uniforms.front(); E; E = E->next()) {
@@ -2658,6 +2806,16 @@ void RasterizerStorageGLES3::_update_material(Material *material) {
RID texture;
+ switch (E->get().type) {
+ case ShaderLanguage::TYPE_SAMPLER3D:
+ case ShaderLanguage::TYPE_SAMPLER2DARRAY: {
+ material->texture_is_3d.write[E->get().texture_order] = true;
+ } break;
+ default: {
+ material->texture_is_3d.write[E->get().texture_order] = false;
+ } break;
+ }
+
Map<StringName, Variant>::Element *V = material->params.find(E->key());
if (V) {
texture = V->get();
@@ -2675,6 +2833,7 @@ void RasterizerStorageGLES3::_update_material(Material *material) {
} else {
material->textures.clear();
+ material->texture_is_3d.clear();
}
}
@@ -6999,6 +7158,7 @@ bool RasterizerStorageGLES3::free(RID p_rid) {
info.texture_mem -= texture->total_data_size;
texture_owner.free(p_rid);
memdelete(texture);
+
} else if (sky_owner.owns(p_rid)) {
// delete the sky
Sky *sky = sky_owner.get(p_rid);
@@ -7408,6 +7568,15 @@ void RasterizerStorageGLES3::initialize() {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, anisotexdata);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
+
+ glGenTextures(1, &resources.white_tex_3d);
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_3D, resources.white_tex_3d);
+ glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, 2, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, whitetexdata);
+
+ glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 0);
+ glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 0);
}
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &config.max_texture_image_units);
diff --git a/drivers/gles3/rasterizer_storage_gles3.h b/drivers/gles3/rasterizer_storage_gles3.h
index d9c770d1b7..f55c8026ea 100644
--- a/drivers/gles3/rasterizer_storage_gles3.h
+++ b/drivers/gles3/rasterizer_storage_gles3.h
@@ -123,6 +123,8 @@ public:
GLuint normal_tex;
GLuint aniso_tex;
+ GLuint white_tex_3d;
+
GLuint quadie;
GLuint quadie_array;
@@ -248,9 +250,10 @@ public:
String path;
uint32_t flags;
- int width, height;
- int alloc_width, alloc_height;
+ int width, height, depth;
+ int alloc_width, alloc_height, alloc_depth;
Image::Format format;
+ VS::TextureType type;
GLenum target;
GLenum gl_format_cache;
@@ -274,7 +277,7 @@ public:
RenderTarget *render_target;
- Ref<Image> images[6];
+ Vector<Ref<Image> > images;
VisualServer::TextureDetectCallback detect_3d;
void *detect_3d_ud;
@@ -340,17 +343,19 @@ public:
Ref<Image> _get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed, bool &srgb);
virtual RID texture_create();
- virtual void texture_allocate(RID p_texture, int p_width, int p_height, Image::Format p_format, uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT);
- virtual void texture_set_data(RID p_texture, const Ref<Image> &p_image, VS::CubeMapSide p_cube_side = VS::CUBEMAP_LEFT);
- virtual void texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, VS::CubeMapSide p_cube_side = VS::CUBEMAP_LEFT);
- virtual Ref<Image> texture_get_data(RID p_texture, VS::CubeMapSide p_cube_side = VS::CUBEMAP_LEFT) const;
+ virtual void texture_allocate(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, VS::TextureType p_type, uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT);
+ virtual void texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer = 0);
+ virtual void texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, int p_layer = 0);
+ virtual Ref<Image> texture_get_data(RID p_texture, int p_layer = 0) const;
virtual void texture_set_flags(RID p_texture, uint32_t p_flags);
virtual uint32_t texture_get_flags(RID p_texture) const;
virtual Image::Format texture_get_format(RID p_texture) const;
+ virtual VS::TextureType texture_get_type(RID p_texture) const;
virtual uint32_t texture_get_texid(RID p_texture) const;
virtual uint32_t texture_get_width(RID p_texture) const;
virtual uint32_t texture_get_height(RID p_texture) const;
- virtual void texture_set_size_override(RID p_texture, int p_width, int p_height);
+ virtual uint32_t texture_get_depth(RID p_texture) const;
+ virtual void texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth);
virtual void texture_set_path(RID p_texture, const String &p_path);
virtual String texture_get_path(RID p_texture) const;
@@ -410,6 +415,7 @@ public:
Map<StringName, RID> default_textures;
+ Vector<ShaderLanguage::DataType> texture_types;
Vector<ShaderLanguage::ShaderNode::Uniform::Hint> texture_hints;
bool valid;
@@ -532,6 +538,7 @@ public:
Map<StringName, Variant> params;
SelfList<Material> list;
SelfList<Material> dirty_list;
+ Vector<bool> texture_is_3d;
Vector<RID> textures;
float line_width;
int render_priority;
diff --git a/drivers/gles3/shader_compiler_gles3.cpp b/drivers/gles3/shader_compiler_gles3.cpp
index 4ff8c72e13..0c353d42bb 100644
--- a/drivers/gles3/shader_compiler_gles3.cpp
+++ b/drivers/gles3/shader_compiler_gles3.cpp
@@ -341,6 +341,7 @@ String ShaderCompilerGLES3::_dump_node_code(SL::Node *p_node, int p_level, Gener
r_gen_code.texture_uniforms.resize(max_texture_uniforms);
r_gen_code.texture_hints.resize(max_texture_uniforms);
+ r_gen_code.texture_types.resize(max_texture_uniforms);
Vector<int> uniform_sizes;
Vector<int> uniform_alignments;
@@ -367,6 +368,7 @@ String ShaderCompilerGLES3::_dump_node_code(SL::Node *p_node, int p_level, Gener
r_gen_code.fragment_global += ucode;
r_gen_code.texture_uniforms.write[E->get().texture_order] = _mkid(E->key());
r_gen_code.texture_hints.write[E->get().texture_order] = E->get().hint;
+ r_gen_code.texture_types.write[E->get().texture_order] = E->get().type;
} else {
if (!uses_uniforms) {
@@ -700,6 +702,11 @@ String ShaderCompilerGLES3::_dump_node_code(SL::Node *p_node, int p_level, Gener
}
} else if (cfnode->flow_op == SL::FLOW_OP_DISCARD) {
+ if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) {
+ *p_actions.usage_flag_pointers["DISCARD"] = true;
+ used_flag_pointers.insert("DISCARD");
+ }
+
code = "discard;";
} else if (cfnode->flow_op == SL::FLOW_OP_CONTINUE) {
diff --git a/drivers/gles3/shader_compiler_gles3.h b/drivers/gles3/shader_compiler_gles3.h
index bf776ee062..7a32057741 100644
--- a/drivers/gles3/shader_compiler_gles3.h
+++ b/drivers/gles3/shader_compiler_gles3.h
@@ -52,6 +52,7 @@ public:
Vector<CharString> defines;
Vector<StringName> texture_uniforms;
+ Vector<ShaderLanguage::DataType> texture_types;
Vector<ShaderLanguage::ShaderNode::Uniform::Hint> texture_hints;
Vector<uint32_t> uniform_offsets;
diff --git a/drivers/gles3/shaders/scene.glsl b/drivers/gles3/shaders/scene.glsl
index 6fd85cc1dd..dee8bcbc58 100644
--- a/drivers/gles3/shaders/scene.glsl
+++ b/drivers/gles3/shaders/scene.glsl
@@ -339,7 +339,7 @@ void main() {
#endif
#endif
- float roughness=0.0;
+ float roughness = 1.0;
//defines that make writing custom shaders easier
#define projection_matrix local_projection
@@ -1608,18 +1608,18 @@ void main() {
//lay out everything, whathever is unused is optimized away anyway
highp vec3 vertex = vertex_interp;
- vec3 albedo = vec3(0.8,0.8,0.8);
+ vec3 albedo = vec3(1.0);
vec3 transmission = vec3(0.0);
float metallic = 0.0;
float specular = 0.5;
- vec3 emission = vec3(0.0,0.0,0.0);
+ vec3 emission = vec3(0.0);
float roughness = 1.0;
float rim = 0.0;
float rim_tint = 0.0;
- float clearcoat=0.0;
- float clearcoat_gloss=0.0;
- float anisotropy = 1.0;
- vec2 anisotropy_flow = vec2(1.0,0.0);
+ float clearcoat = 0.0;
+ float clearcoat_gloss = 0.0;
+ float anisotropy = 0.0;
+ vec2 anisotropy_flow = vec2(1.0, 0.0);
#if defined(ENABLE_AO)
float ao=1.0;
diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.cpp b/drivers/pulseaudio/audio_driver_pulseaudio.cpp
index 6db0e58737..d1104eec18 100644
--- a/drivers/pulseaudio/audio_driver_pulseaudio.cpp
+++ b/drivers/pulseaudio/audio_driver_pulseaudio.cpp
@@ -64,18 +64,32 @@ void AudioDriverPulseAudio::pa_sink_info_cb(pa_context *c, const pa_sink_info *l
ad->pa_status++;
}
+void AudioDriverPulseAudio::pa_source_info_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata) {
+ AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata;
+
+ // If eol is set to a positive number, you're at the end of the list
+ if (eol > 0) {
+ return;
+ }
+
+ ad->pa_rec_map = l->channel_map;
+ ad->pa_status++;
+}
+
void AudioDriverPulseAudio::pa_server_info_cb(pa_context *c, const pa_server_info *i, void *userdata) {
AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata;
+ ad->capture_default_device = i->default_source_name;
ad->default_device = i->default_sink_name;
ad->pa_status++;
}
-void AudioDriverPulseAudio::detect_channels() {
+void AudioDriverPulseAudio::detect_channels(bool capture) {
- pa_channel_map_init_stereo(&pa_map);
+ pa_channel_map_init_stereo(capture ? &pa_rec_map : &pa_map);
- if (device_name == "Default") {
+ String device = capture ? capture_device_name : device_name;
+ if (device == "Default") {
// Get the default output device name
pa_status = 0;
pa_operation *pa_op = pa_context_get_server_info(pa_ctx, &AudioDriverPulseAudio::pa_server_info_cb, (void *)this);
@@ -93,16 +107,22 @@ void AudioDriverPulseAudio::detect_channels() {
}
}
- char device[1024];
- if (device_name == "Default") {
- strcpy(device, default_device.utf8().get_data());
+ char dev[1024];
+ if (device == "Default") {
+ strcpy(dev, capture ? capture_default_device.utf8().get_data() : default_device.utf8().get_data());
} else {
- strcpy(device, device_name.utf8().get_data());
+ strcpy(dev, device.utf8().get_data());
}
// Now using the device name get the amount of channels
pa_status = 0;
- pa_operation *pa_op = pa_context_get_sink_info_by_name(pa_ctx, device, &AudioDriverPulseAudio::pa_sink_info_cb, (void *)this);
+ pa_operation *pa_op;
+ if (capture) {
+ pa_op = pa_context_get_source_info_by_name(pa_ctx, dev, &AudioDriverPulseAudio::pa_source_info_cb, (void *)this);
+ } else {
+ pa_op = pa_context_get_sink_info_by_name(pa_ctx, dev, &AudioDriverPulseAudio::pa_sink_info_cb, (void *)this);
+ }
+
if (pa_op) {
while (pa_status == 0) {
int ret = pa_mainloop_iterate(pa_ml, 1, NULL);
@@ -113,7 +133,11 @@ void AudioDriverPulseAudio::detect_channels() {
pa_operation_unref(pa_op);
} else {
- ERR_PRINT("pa_context_get_sink_info_by_name error");
+ if (capture) {
+ ERR_PRINT("pa_context_get_source_info_by_name error");
+ } else {
+ ERR_PRINT("pa_context_get_sink_info_by_name error");
+ }
}
}
@@ -195,6 +219,10 @@ Error AudioDriverPulseAudio::init_device() {
samples_in.resize(buffer_frames * channels);
samples_out.resize(pa_buffer_size);
+ // Reset audio input to keep synchronisation.
+ input_position = 0;
+ input_size = 0;
+
return OK;
}
@@ -287,74 +315,71 @@ float AudioDriverPulseAudio::get_latency() {
void AudioDriverPulseAudio::thread_func(void *p_udata) {
AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)p_udata;
+ unsigned int write_ofs = 0;
+ size_t avail_bytes = 0;
while (!ad->exit_thread) {
- ad->lock();
- ad->start_counting_ticks();
-
- if (!ad->active) {
- for (unsigned int i = 0; i < ad->pa_buffer_size; i++) {
- ad->samples_out.write[i] = 0;
- }
+ size_t read_bytes = 0;
+ size_t written_bytes = 0;
- } else {
- ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw());
+ if (avail_bytes == 0) {
+ ad->lock();
+ ad->start_counting_ticks();
- if (ad->channels == ad->pa_map.channels) {
+ if (!ad->active) {
for (unsigned int i = 0; i < ad->pa_buffer_size; i++) {
- ad->samples_out.write[i] = ad->samples_in[i] >> 16;
+ ad->samples_out.write[i] = 0;
}
} else {
- // Uneven amount of channels
- unsigned int in_idx = 0;
- unsigned int out_idx = 0;
+ ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw());
- for (unsigned int i = 0; i < ad->buffer_frames; i++) {
- for (unsigned int j = 0; j < ad->pa_map.channels - 1; j++) {
- ad->samples_out.write[out_idx++] = ad->samples_in[in_idx++] >> 16;
+ if (ad->channels == ad->pa_map.channels) {
+ for (unsigned int i = 0; i < ad->pa_buffer_size; i++) {
+ ad->samples_out.write[i] = ad->samples_in[i] >> 16;
+ }
+ } else {
+ // Uneven amount of channels
+ unsigned int in_idx = 0;
+ unsigned int out_idx = 0;
+
+ for (unsigned int i = 0; i < ad->buffer_frames; i++) {
+ for (unsigned int j = 0; j < ad->pa_map.channels - 1; j++) {
+ ad->samples_out.write[out_idx++] = ad->samples_in[in_idx++] >> 16;
+ }
+ uint32_t l = ad->samples_in[in_idx++];
+ uint32_t r = ad->samples_in[in_idx++];
+ ad->samples_out.write[out_idx++] = ((l >> 1) + (r >> 1)) >> 16;
}
- uint32_t l = ad->samples_in[in_idx++];
- uint32_t r = ad->samples_in[in_idx++];
- ad->samples_out.write[out_idx++] = (l >> 1 + r >> 1) >> 16;
}
}
+
+ avail_bytes = ad->pa_buffer_size * sizeof(int16_t);
+ write_ofs = 0;
+ ad->stop_counting_ticks();
+ ad->unlock();
}
- int error_code;
- int byte_size = ad->pa_buffer_size * sizeof(int16_t);
+ ad->lock();
+ ad->start_counting_ticks();
+
int ret;
do {
ret = pa_mainloop_iterate(ad->pa_ml, 0, NULL);
} while (ret > 0);
- if (pa_stream_get_state(ad->pa_str) == PA_STREAM_READY) {
- const void *ptr = ad->samples_out.ptr();
- while (byte_size > 0) {
- size_t bytes = pa_stream_writable_size(ad->pa_str);
- if (bytes > 0) {
- if (bytes > byte_size) {
- bytes = byte_size;
- }
-
- ret = pa_stream_write(ad->pa_str, ptr, bytes, NULL, 0LL, PA_SEEK_RELATIVE);
- if (ret >= 0) {
- byte_size -= bytes;
- ptr = (const char *)ptr + bytes;
- }
+ if (avail_bytes > 0 && pa_stream_get_state(ad->pa_str) == PA_STREAM_READY) {
+ size_t bytes = pa_stream_writable_size(ad->pa_str);
+ if (bytes > 0) {
+ size_t bytes_to_write = MIN(bytes, avail_bytes);
+ const void *ptr = ad->samples_out.ptr();
+ ret = pa_stream_write(ad->pa_str, (char *)ptr + write_ofs, bytes_to_write, NULL, 0LL, PA_SEEK_RELATIVE);
+ if (ret != 0) {
+ ERR_PRINT("pa_stream_write error");
} else {
- ret = pa_mainloop_iterate(ad->pa_ml, 0, NULL);
- if (ret == 0) {
- // If pa_mainloop_iterate returns 0 sleep for 1 msec to wait
- // for the stream to be able to process more bytes
- ad->stop_counting_ticks();
- ad->unlock();
-
- OS::get_singleton()->delay_usec(1000);
-
- ad->lock();
- ad->start_counting_ticks();
- }
+ avail_bytes -= bytes_to_write;
+ write_ofs += bytes_to_write;
+ written_bytes += bytes_to_write;
}
}
}
@@ -379,8 +404,64 @@ void AudioDriverPulseAudio::thread_func(void *p_udata) {
}
}
+ if (ad->pa_rec_str && pa_stream_get_state(ad->pa_rec_str) == PA_STREAM_READY) {
+ size_t bytes = pa_stream_readable_size(ad->pa_rec_str);
+ if (bytes > 0) {
+ const void *ptr = NULL;
+ size_t maxbytes = ad->input_buffer.size() * sizeof(int16_t);
+
+ bytes = MIN(bytes, maxbytes);
+ ret = pa_stream_peek(ad->pa_rec_str, &ptr, &bytes);
+ if (ret != 0) {
+ ERR_PRINT("pa_stream_peek error");
+ } else {
+ int16_t *srcptr = (int16_t *)ptr;
+ for (size_t i = bytes >> 1; i > 0; i--) {
+ int32_t sample = int32_t(*srcptr++) << 16;
+ ad->input_buffer_write(sample);
+
+ if (ad->pa_rec_map.channels == 1) {
+ // In case input device is single channel convert it to Stereo
+ ad->input_buffer_write(sample);
+ }
+ }
+
+ read_bytes += bytes;
+ ret = pa_stream_drop(ad->pa_rec_str);
+ if (ret != 0) {
+ ERR_PRINT("pa_stream_drop error");
+ }
+ }
+ }
+
+ // User selected a new device, finish the current one so we'll init the new device
+ if (ad->capture_device_name != ad->capture_new_device) {
+ ad->capture_device_name = ad->capture_new_device;
+ ad->capture_finish_device();
+
+ Error err = ad->capture_init_device();
+ if (err != OK) {
+ ERR_PRINT("PulseAudio: capture_init_device error");
+ ad->capture_device_name = "Default";
+ ad->capture_new_device = "Default";
+
+ err = ad->capture_init_device();
+ if (err != OK) {
+ ad->active = false;
+ ad->exit_thread = true;
+ break;
+ }
+ }
+ }
+ }
+
ad->stop_counting_ticks();
ad->unlock();
+
+ // Let the thread rest a while if we haven't read or write anything
+ if (written_bytes == 0 && read_bytes == 0) {
+ OS::get_singleton()->delay_usec(1000);
+ }
}
ad->thread_exited = true;
@@ -510,11 +591,165 @@ void AudioDriverPulseAudio::finish() {
thread = NULL;
}
+Error AudioDriverPulseAudio::capture_init_device() {
+
+ // If there is a specified device check that it is really present
+ if (capture_device_name != "Default") {
+ Array list = capture_get_device_list();
+ if (list.find(capture_device_name) == -1) {
+ capture_device_name = "Default";
+ capture_new_device = "Default";
+ }
+ }
+
+ detect_channels(true);
+ switch (pa_rec_map.channels) {
+ case 1: // Mono
+ case 2: // Stereo
+ break;
+
+ default:
+ WARN_PRINTS("PulseAudio: Unsupported number of input channels: " + itos(pa_rec_map.channels));
+ pa_channel_map_init_stereo(&pa_rec_map);
+ break;
+ }
+
+ if (OS::get_singleton()->is_stdout_verbose()) {
+ print_line("PulseAudio: detected " + itos(pa_rec_map.channels) + " input channels");
+ }
+
+ pa_sample_spec spec;
+
+ spec.format = PA_SAMPLE_S16LE;
+ spec.channels = pa_rec_map.channels;
+ spec.rate = mix_rate;
+
+ int latency = 30;
+ input_buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
+ int buffer_size = input_buffer_frames * spec.channels;
+
+ pa_buffer_attr attr;
+ attr.fragsize = buffer_size * sizeof(int16_t);
+
+ pa_rec_str = pa_stream_new(pa_ctx, "Record", &spec, &pa_rec_map);
+ if (pa_rec_str == NULL) {
+ ERR_PRINTS("PulseAudio: pa_stream_new error: " + String(pa_strerror(pa_context_errno(pa_ctx))));
+ ERR_FAIL_V(ERR_CANT_OPEN);
+ }
+
+ const char *dev = capture_device_name == "Default" ? NULL : capture_device_name.utf8().get_data();
+ pa_stream_flags flags = pa_stream_flags(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE);
+ int error_code = pa_stream_connect_record(pa_rec_str, dev, &attr, flags);
+ if (error_code < 0) {
+ ERR_PRINTS("PulseAudio: pa_stream_connect_record error: " + String(pa_strerror(error_code)));
+ ERR_FAIL_V(ERR_CANT_OPEN);
+ }
+
+ input_buffer.resize(input_buffer_frames * 8);
+ input_position = 0;
+ input_size = 0;
+
+ return OK;
+}
+
+void AudioDriverPulseAudio::capture_finish_device() {
+
+ if (pa_rec_str) {
+ int ret = pa_stream_disconnect(pa_rec_str);
+ if (ret != 0) {
+ ERR_PRINTS("PulseAudio: pa_stream_disconnect error: " + String(pa_strerror(ret)));
+ }
+ pa_stream_unref(pa_rec_str);
+ pa_rec_str = NULL;
+ }
+}
+
+Error AudioDriverPulseAudio::capture_start() {
+
+ lock();
+ Error err = capture_init_device();
+ unlock();
+
+ return err;
+}
+
+Error AudioDriverPulseAudio::capture_stop() {
+ lock();
+ capture_finish_device();
+ unlock();
+
+ return OK;
+}
+
+void AudioDriverPulseAudio::capture_set_device(const String &p_name) {
+
+ lock();
+ capture_new_device = p_name;
+ unlock();
+}
+
+void AudioDriverPulseAudio::pa_sourcelist_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata) {
+ AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata;
+
+ // If eol is set to a positive number, you're at the end of the list
+ if (eol > 0) {
+ return;
+ }
+
+ if (l->monitor_of_sink == PA_INVALID_INDEX) {
+ ad->pa_rec_devices.push_back(l->name);
+ }
+
+ ad->pa_status++;
+}
+
+Array AudioDriverPulseAudio::capture_get_device_list() {
+
+ pa_rec_devices.clear();
+ pa_rec_devices.push_back("Default");
+
+ if (pa_ctx == NULL) {
+ return pa_rec_devices;
+ }
+
+ lock();
+
+ // Get the device list
+ pa_status = 0;
+ pa_operation *pa_op = pa_context_get_source_info_list(pa_ctx, pa_sourcelist_cb, (void *)this);
+ if (pa_op) {
+ while (pa_status == 0) {
+ int ret = pa_mainloop_iterate(pa_ml, 1, NULL);
+ if (ret < 0) {
+ ERR_PRINT("pa_mainloop_iterate error");
+ }
+ }
+
+ pa_operation_unref(pa_op);
+ } else {
+ ERR_PRINT("pa_context_get_server_info error");
+ }
+
+ unlock();
+
+ return pa_rec_devices;
+}
+
+String AudioDriverPulseAudio::capture_get_device() {
+
+ lock();
+ String name = capture_device_name;
+ unlock();
+
+ return name;
+}
+
AudioDriverPulseAudio::AudioDriverPulseAudio() {
pa_ml = NULL;
pa_ctx = NULL;
pa_str = NULL;
+ pa_rec_str = NULL;
mutex = NULL;
thread = NULL;
@@ -528,6 +763,7 @@ AudioDriverPulseAudio::AudioDriverPulseAudio() {
mix_rate = 0;
buffer_frames = 0;
+ input_buffer_frames = 0;
pa_buffer_size = 0;
channels = 0;
pa_ready = 0;
diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.h b/drivers/pulseaudio/audio_driver_pulseaudio.h
index b471f5f9d5..f8358a452b 100644
--- a/drivers/pulseaudio/audio_driver_pulseaudio.h
+++ b/drivers/pulseaudio/audio_driver_pulseaudio.h
@@ -47,22 +47,30 @@ class AudioDriverPulseAudio : public AudioDriver {
pa_mainloop *pa_ml;
pa_context *pa_ctx;
pa_stream *pa_str;
+ pa_stream *pa_rec_str;
pa_channel_map pa_map;
+ pa_channel_map pa_rec_map;
String device_name;
String new_device;
String default_device;
+ String capture_device_name;
+ String capture_new_device;
+ String capture_default_device;
+
Vector<int32_t> samples_in;
Vector<int16_t> samples_out;
unsigned int mix_rate;
unsigned int buffer_frames;
+ unsigned int input_buffer_frames;
unsigned int pa_buffer_size;
int channels;
int pa_ready;
int pa_status;
Array pa_devices;
+ Array pa_rec_devices;
bool active;
bool thread_exited;
@@ -72,13 +80,18 @@ class AudioDriverPulseAudio : public AudioDriver {
static void pa_state_cb(pa_context *c, void *userdata);
static void pa_sink_info_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata);
+ static void pa_source_info_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata);
static void pa_server_info_cb(pa_context *c, const pa_server_info *i, void *userdata);
static void pa_sinklist_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata);
+ static void pa_sourcelist_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata);
Error init_device();
void finish_device();
- void detect_channels();
+ Error capture_init_device();
+ void capture_finish_device();
+
+ void detect_channels(bool capture = false);
static void thread_func(void *p_udata);
@@ -91,15 +104,24 @@ public:
virtual void start();
virtual int get_mix_rate() const;
virtual SpeakerMode get_speaker_mode() const;
+
virtual Array get_device_list();
virtual String get_device();
virtual void set_device(String device);
+
+ virtual Array capture_get_device_list();
+ virtual void capture_set_device(const String &p_name);
+ virtual String capture_get_device();
+
virtual void lock();
virtual void unlock();
virtual void finish();
virtual float get_latency();
+ virtual Error capture_start();
+ virtual Error capture_stop();
+
AudioDriverPulseAudio();
~AudioDriverPulseAudio();
};
diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp
index 5982955c4f..2dcb4ff3d8 100644
--- a/drivers/wasapi/audio_driver_wasapi.cpp
+++ b/drivers/wasapi/audio_driver_wasapi.cpp
@@ -52,8 +52,22 @@ const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
const IID IID_IAudioClient = __uuidof(IAudioClient);
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
+const IID IID_IAudioCaptureClient = __uuidof(IAudioCaptureClient);
-static bool default_device_changed = false;
+#define SAFE_RELEASE(memory) \
+ if ((memory) != NULL) { \
+ (memory)->Release(); \
+ (memory) = NULL; \
+ }
+
+#define REFTIMES_PER_SEC 10000000
+#define REFTIMES_PER_MILLISEC 10000
+
+#define CAPTURE_BUFFER_CHANNELS 2
+
+static StringName capture_device_id;
+static bool default_render_device_changed = false;
+static bool default_capture_device_changed = false;
class CMMNotificationClient : public IMMNotificationClient {
LONG _cRef;
@@ -109,8 +123,13 @@ public:
}
HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) {
- if (flow == eRender && role == eConsole) {
- default_device_changed = true;
+ if (role == eConsole) {
+ if (flow == eRender) {
+ default_render_device_changed = true;
+ } else if (flow == eCapture) {
+ default_capture_device_changed = true;
+ capture_device_id = String(pwstrDeviceId);
+ }
}
return S_OK;
@@ -123,7 +142,7 @@ public:
static CMMNotificationClient notif_client;
-Error AudioDriverWASAPI::init_device(bool reinit) {
+Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_capture, bool reinit) {
WAVEFORMATEX *pwfex;
IMMDeviceEnumerator *enumerator = NULL;
@@ -134,12 +153,12 @@ Error AudioDriverWASAPI::init_device(bool reinit) {
HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
- if (device_name == "Default") {
- hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
+ if (p_device->device_name == "Default") {
+ hr = enumerator->GetDefaultAudioEndpoint(p_capture ? eCapture : eRender, eConsole, &device);
} else {
IMMDeviceCollection *devices = NULL;
- hr = enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &devices);
+ hr = enumerator->EnumAudioEndpoints(p_capture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
LPWSTR strId = NULL;
@@ -165,7 +184,7 @@ Error AudioDriverWASAPI::init_device(bool reinit) {
hr = props->GetValue(PKEY_Device_FriendlyName, &propvar);
ERR_BREAK(hr != S_OK);
- if (device_name == String(propvar.pwszVal)) {
+ if (p_device->device_name == String(propvar.pwszVal)) {
hr = device->GetId(&strId);
ERR_BREAK(hr != S_OK);
@@ -186,9 +205,10 @@ Error AudioDriverWASAPI::init_device(bool reinit) {
}
if (device == NULL) {
- hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
+ hr = enumerator->GetDefaultAudioEndpoint(p_capture ? eCapture : eRender, eConsole, &device);
}
}
+
if (reinit) {
// In case we're trying to re-initialize the device prevent throwing this error on the console,
// otherwise if there is currently no device available this will spam the console.
@@ -200,11 +220,15 @@ Error AudioDriverWASAPI::init_device(bool reinit) {
}
hr = enumerator->RegisterEndpointNotificationCallback(&notif_client);
+ SAFE_RELEASE(enumerator)
+
if (hr != S_OK) {
ERR_PRINT("WASAPI: RegisterEndpointNotificationCallback error");
}
- hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)&audio_client);
+ hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)&p_device->audio_client);
+ SAFE_RELEASE(device)
+
if (reinit) {
if (hr != S_OK) {
return ERR_CANT_OPEN;
@@ -213,75 +237,89 @@ Error AudioDriverWASAPI::init_device(bool reinit) {
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
}
- hr = audio_client->GetMixFormat(&pwfex);
+ hr = p_device->audio_client->GetMixFormat(&pwfex);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
// Since we're using WASAPI Shared Mode we can't control any of these, we just tag along
- wasapi_channels = pwfex->nChannels;
- format_tag = pwfex->wFormatTag;
- bits_per_sample = pwfex->wBitsPerSample;
+ p_device->channels = pwfex->nChannels;
+ p_device->format_tag = pwfex->wFormatTag;
+ p_device->bits_per_sample = pwfex->wBitsPerSample;
+ p_device->frame_size = (p_device->bits_per_sample / 8) * p_device->channels;
- switch (wasapi_channels) {
- case 2: // Stereo
- case 4: // Surround 3.1
- case 6: // Surround 5.1
- case 8: // Surround 7.1
- channels = wasapi_channels;
- break;
-
- default:
- WARN_PRINTS("WASAPI: Unsupported number of channels: " + itos(wasapi_channels));
- channels = 2;
- break;
- }
-
- if (format_tag == WAVE_FORMAT_EXTENSIBLE) {
+ if (p_device->format_tag == WAVE_FORMAT_EXTENSIBLE) {
WAVEFORMATEXTENSIBLE *wfex = (WAVEFORMATEXTENSIBLE *)pwfex;
if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) {
- format_tag = WAVE_FORMAT_PCM;
+ p_device->format_tag = WAVE_FORMAT_PCM;
} else if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) {
- format_tag = WAVE_FORMAT_IEEE_FLOAT;
+ p_device->format_tag = WAVE_FORMAT_IEEE_FLOAT;
} else {
ERR_PRINT("WASAPI: Format not supported");
ERR_FAIL_V(ERR_CANT_OPEN);
}
} else {
- if (format_tag != WAVE_FORMAT_PCM && format_tag != WAVE_FORMAT_IEEE_FLOAT) {
+ if (p_device->format_tag != WAVE_FORMAT_PCM && p_device->format_tag != WAVE_FORMAT_IEEE_FLOAT) {
ERR_PRINT("WASAPI: Format not supported");
ERR_FAIL_V(ERR_CANT_OPEN);
}
}
- DWORD streamflags = AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
+ DWORD streamflags = 0;
if (mix_rate != pwfex->nSamplesPerSec) {
streamflags |= AUDCLNT_STREAMFLAGS_RATEADJUST;
pwfex->nSamplesPerSec = mix_rate;
pwfex->nAvgBytesPerSec = pwfex->nSamplesPerSec * pwfex->nChannels * (pwfex->wBitsPerSample / 8);
}
- hr = audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, streamflags, 0, 0, pwfex, NULL);
+ hr = p_device->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, streamflags, p_capture ? REFTIMES_PER_SEC : 0, 0, pwfex, NULL);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
- event = CreateEvent(NULL, FALSE, FALSE, NULL);
- ERR_FAIL_COND_V(event == NULL, ERR_CANT_OPEN);
-
- hr = audio_client->SetEventHandle(event);
+ if (p_capture) {
+ hr = p_device->audio_client->GetService(IID_IAudioCaptureClient, (void **)&p_device->capture_client);
+ } else {
+ hr = p_device->audio_client->GetService(IID_IAudioRenderClient, (void **)&p_device->render_client);
+ }
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
- hr = audio_client->GetService(IID_IAudioRenderClient, (void **)&render_client);
- ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
+ // Free memory
+ CoTaskMemFree(pwfex);
+ SAFE_RELEASE(device)
+
+ return OK;
+}
+
+Error AudioDriverWASAPI::init_render_device(bool reinit) {
+
+ Error err = audio_device_init(&audio_output, false, reinit);
+ if (err != OK)
+ return err;
+
+ switch (audio_output.channels) {
+ case 2: // Stereo
+ case 4: // Surround 3.1
+ case 6: // Surround 5.1
+ case 8: // Surround 7.1
+ channels = audio_output.channels;
+ break;
+
+ default:
+ WARN_PRINTS("WASAPI: Unsupported number of channels: " + itos(audio_output.channels));
+ channels = 2;
+ break;
+ }
UINT32 max_frames;
- hr = audio_client->GetBufferSize(&max_frames);
+ HRESULT hr = audio_output.audio_client->GetBufferSize(&max_frames);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
// Due to WASAPI Shared Mode we have no control of the buffer size
buffer_frames = max_frames;
// Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels)
- buffer_size = buffer_frames * channels;
- samples_in.resize(buffer_size);
+ samples_in.resize(buffer_frames * channels);
+
+ input_position = 0;
+ input_size = 0;
if (OS::get_singleton()->is_stdout_verbose()) {
print_line("WASAPI: detected " + itos(channels) + " channels");
@@ -291,41 +329,61 @@ Error AudioDriverWASAPI::init_device(bool reinit) {
return OK;
}
-Error AudioDriverWASAPI::finish_device() {
+Error AudioDriverWASAPI::init_capture_device(bool reinit) {
- if (audio_client) {
- if (active) {
- audio_client->Stop();
- active = false;
- }
+ Error err = audio_device_init(&audio_input, true, reinit);
+ if (err != OK)
+ return err;
- audio_client->Release();
- audio_client = NULL;
- }
+ // Get the max frames
+ UINT32 max_frames;
+ HRESULT hr = audio_input.audio_client->GetBufferSize(&max_frames);
+ ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
- if (render_client) {
- render_client->Release();
- render_client = NULL;
- }
+ // Set the buffer size
+ input_buffer.resize(max_frames * CAPTURE_BUFFER_CHANNELS);
+ input_position = 0;
+ input_size = 0;
- if (audio_client) {
- audio_client->Release();
- audio_client = NULL;
+ return OK;
+}
+
+Error AudioDriverWASAPI::audio_device_finish(AudioDeviceWASAPI *p_device) {
+
+ if (p_device->active) {
+ if (p_device->audio_client) {
+ p_device->audio_client->Stop();
+ }
+
+ p_device->active = false;
}
+ SAFE_RELEASE(p_device->audio_client)
+ SAFE_RELEASE(p_device->render_client)
+ SAFE_RELEASE(p_device->capture_client)
+
return OK;
}
+Error AudioDriverWASAPI::finish_render_device() {
+
+ return audio_device_finish(&audio_output);
+}
+
+Error AudioDriverWASAPI::finish_capture_device() {
+
+ return audio_device_finish(&audio_input);
+}
+
Error AudioDriverWASAPI::init() {
mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE);
- Error err = init_device();
+ Error err = init_render_device();
if (err != OK) {
- ERR_PRINT("WASAPI: init_device error");
+ ERR_PRINT("WASAPI: init_render_device error");
}
- active = false;
exit_thread = false;
thread_exited = false;
@@ -345,7 +403,7 @@ AudioDriver::SpeakerMode AudioDriverWASAPI::get_speaker_mode() const {
return get_speaker_mode_by_total_channels(channels);
}
-Array AudioDriverWASAPI::get_device_list() {
+Array AudioDriverWASAPI::audio_device_get_list(bool p_capture) {
Array list;
IMMDeviceCollection *devices = NULL;
@@ -358,7 +416,7 @@ Array AudioDriverWASAPI::get_device_list() {
HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
ERR_FAIL_COND_V(hr != S_OK, Array());
- hr = enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &devices);
+ hr = enumerator->EnumAudioEndpoints(p_capture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices);
ERR_FAIL_COND_V(hr != S_OK, Array());
UINT count = 0;
@@ -393,21 +451,63 @@ Array AudioDriverWASAPI::get_device_list() {
return list;
}
+Array AudioDriverWASAPI::get_device_list() {
+
+ return audio_device_get_list(false);
+}
+
String AudioDriverWASAPI::get_device() {
- return device_name;
+ lock();
+ String name = audio_output.device_name;
+ unlock();
+
+ return name;
}
void AudioDriverWASAPI::set_device(String device) {
lock();
- new_device = device;
+ audio_output.new_device = device;
unlock();
}
-void AudioDriverWASAPI::write_sample(AudioDriverWASAPI *ad, BYTE *buffer, int i, int32_t sample) {
- if (ad->format_tag == WAVE_FORMAT_PCM) {
- switch (ad->bits_per_sample) {
+int32_t AudioDriverWASAPI::read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i) {
+ if (format_tag == WAVE_FORMAT_PCM) {
+ int32_t sample = 0;
+ switch (bits_per_sample) {
+ case 8:
+ sample = int32_t(((int8_t *)buffer)[i]) << 24;
+ break;
+
+ case 16:
+ sample = int32_t(((int16_t *)buffer)[i]) << 16;
+ break;
+
+ case 24:
+ sample |= int32_t(((int8_t *)buffer)[i * 3 + 2]) << 24;
+ sample |= int32_t(((int8_t *)buffer)[i * 3 + 1]) << 16;
+ sample |= int32_t(((int8_t *)buffer)[i * 3 + 0]) << 8;
+ break;
+
+ case 32:
+ sample = ((int32_t *)buffer)[i];
+ break;
+ }
+
+ return sample;
+ } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) {
+ return int32_t(((float *)buffer)[i] * 32768.0) << 16;
+ } else {
+ ERR_PRINT("WASAPI: Unknown format tag");
+ }
+
+ return 0;
+}
+
+void AudioDriverWASAPI::write_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i, int32_t sample) {
+ if (format_tag == WAVE_FORMAT_PCM) {
+ switch (bits_per_sample) {
case 8:
((int8_t *)buffer)[i] = sample >> 24;
break;
@@ -426,83 +526,99 @@ void AudioDriverWASAPI::write_sample(AudioDriverWASAPI *ad, BYTE *buffer, int i,
((int32_t *)buffer)[i] = sample;
break;
}
- } else if (ad->format_tag == WAVE_FORMAT_IEEE_FLOAT) {
+ } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) {
((float *)buffer)[i] = (sample >> 16) / 32768.f;
} else {
ERR_PRINT("WASAPI: Unknown format tag");
- ad->exit_thread = true;
}
}
void AudioDriverWASAPI::thread_func(void *p_udata) {
AudioDriverWASAPI *ad = (AudioDriverWASAPI *)p_udata;
+ uint32_t avail_frames = 0;
+ uint32_t write_ofs = 0;
while (!ad->exit_thread) {
- ad->lock();
- ad->start_counting_ticks();
+ uint32_t read_frames = 0;
+ uint32_t written_frames = 0;
- if (ad->active) {
- ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw());
- } else {
- for (unsigned int i = 0; i < ad->buffer_size; i++) {
- ad->samples_in.write[i] = 0;
+ if (avail_frames == 0) {
+ ad->lock();
+ ad->start_counting_ticks();
+
+ if (ad->audio_output.active) {
+ ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw());
+ } else {
+ for (unsigned int i = 0; i < ad->samples_in.size(); i++) {
+ ad->samples_in.write[i] = 0;
+ }
}
- }
- ad->stop_counting_ticks();
- ad->unlock();
+ avail_frames = ad->buffer_frames;
+ write_ofs = 0;
- unsigned int left_frames = ad->buffer_frames;
- unsigned int buffer_idx = 0;
- while (left_frames > 0 && ad->audio_client) {
- WaitForSingleObject(ad->event, 1000);
+ ad->stop_counting_ticks();
+ ad->unlock();
+ }
- ad->lock();
- ad->start_counting_ticks();
+ ad->lock();
+ ad->start_counting_ticks();
+
+ if (avail_frames > 0 && ad->audio_output.audio_client) {
UINT32 cur_frames;
bool invalidated = false;
- HRESULT hr = ad->audio_client->GetCurrentPadding(&cur_frames);
+ HRESULT hr = ad->audio_output.audio_client->GetCurrentPadding(&cur_frames);
if (hr == S_OK) {
- // Check how much frames are available on the WASAPI buffer
- UINT32 avail_frames = ad->buffer_frames - cur_frames;
- UINT32 write_frames = avail_frames > left_frames ? left_frames : avail_frames;
- BYTE *buffer = NULL;
- hr = ad->render_client->GetBuffer(write_frames, &buffer);
- if (hr == S_OK) {
- // We're using WASAPI Shared Mode so we must convert the buffer
-
- if (ad->channels == ad->wasapi_channels) {
- for (unsigned int i = 0; i < write_frames * ad->channels; i++) {
- ad->write_sample(ad, buffer, i, ad->samples_in[buffer_idx++]);
- }
- } else {
- for (unsigned int i = 0; i < write_frames; i++) {
- for (unsigned int j = 0; j < MIN(ad->channels, ad->wasapi_channels); j++) {
- ad->write_sample(ad, buffer, i * ad->wasapi_channels + j, ad->samples_in[buffer_idx++]);
+ // Check how much frames are available on the WASAPI buffer
+ UINT32 write_frames = MIN(ad->buffer_frames - cur_frames, avail_frames);
+ if (write_frames > 0) {
+ BYTE *buffer = NULL;
+ hr = ad->audio_output.render_client->GetBuffer(write_frames, &buffer);
+ if (hr == S_OK) {
+
+ // We're using WASAPI Shared Mode so we must convert the buffer
+ if (ad->channels == ad->audio_output.channels) {
+ for (unsigned int i = 0; i < write_frames * ad->channels; i++) {
+ ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i, ad->samples_in.write[write_ofs++]);
}
- if (ad->wasapi_channels > ad->channels) {
- for (unsigned int j = ad->channels; j < ad->wasapi_channels; j++) {
- ad->write_sample(ad, buffer, i * ad->wasapi_channels + j, 0);
+ } else {
+ for (unsigned int i = 0; i < write_frames; i++) {
+ for (unsigned int j = 0; j < MIN(ad->channels, ad->audio_output.channels); j++) {
+ ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + j, ad->samples_in.write[write_ofs++]);
+ }
+ if (ad->audio_output.channels > ad->channels) {
+ for (unsigned int j = ad->channels; j < ad->audio_output.channels; j++) {
+ ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + j, 0);
+ }
}
}
}
- }
- hr = ad->render_client->ReleaseBuffer(write_frames, 0);
- if (hr != S_OK) {
- ERR_PRINT("WASAPI: Release buffer error");
- }
+ hr = ad->audio_output.render_client->ReleaseBuffer(write_frames, 0);
+ if (hr != S_OK) {
+ ERR_PRINT("WASAPI: Release buffer error");
+ }
- left_frames -= write_frames;
- } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
- invalidated = true;
- } else {
- ERR_PRINT("WASAPI: Get buffer error");
- ad->exit_thread = true;
+ avail_frames -= write_frames;
+ written_frames += write_frames;
+ } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
+ // Device is not valid anymore, reopen it
+
+ Error err = ad->finish_render_device();
+ if (err != OK) {
+ ERR_PRINT("WASAPI: finish_render_device error");
+ } else {
+ // We reopened the device and samples_in may have resized, so invalidate the current avail_frames
+ avail_frames = 0;
+ }
+ } else {
+ ERR_PRINT("WASAPI: Get buffer error");
+ ad->exit_thread = true;
+ }
}
} else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
invalidated = true;
@@ -514,47 +630,117 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
// Device is not valid anymore
WARN_PRINT("WASAPI: Current device invalidated, closing device");
- Error err = ad->finish_device();
+ Error err = ad->finish_render_device();
if (err != OK) {
- ERR_PRINT("WASAPI: finish_device error");
+ ERR_PRINT("WASAPI: finish_render_device error");
}
}
-
- ad->stop_counting_ticks();
- ad->unlock();
}
- ad->lock();
- ad->start_counting_ticks();
-
// If we're using the Default device and it changed finish it so we'll re-init the device
- if (ad->device_name == "Default" && default_device_changed) {
- Error err = ad->finish_device();
+ if (ad->audio_output.device_name == "Default" && default_render_device_changed) {
+ Error err = ad->finish_render_device();
if (err != OK) {
- ERR_PRINT("WASAPI: finish_device error");
+ ERR_PRINT("WASAPI: finish_render_device error");
}
- default_device_changed = false;
+ default_render_device_changed = false;
}
// User selected a new device, finish the current one so we'll init the new device
- if (ad->device_name != ad->new_device) {
- ad->device_name = ad->new_device;
- Error err = ad->finish_device();
+ if (ad->audio_output.device_name != ad->audio_output.new_device) {
+ ad->audio_output.device_name = ad->audio_output.new_device;
+ Error err = ad->finish_render_device();
if (err != OK) {
- ERR_PRINT("WASAPI: finish_device error");
+ ERR_PRINT("WASAPI: finish_render_device error");
}
}
- if (!ad->audio_client) {
- Error err = ad->init_device(true);
+ if (!ad->audio_output.audio_client) {
+ Error err = ad->init_render_device(true);
if (err == OK) {
ad->start();
}
}
+ if (ad->audio_input.active) {
+ UINT32 packet_length = 0;
+ BYTE *data;
+ UINT32 num_frames_available;
+ DWORD flags;
+
+ HRESULT hr = ad->audio_input.capture_client->GetNextPacketSize(&packet_length);
+ if (hr == S_OK) {
+ while (packet_length != 0) {
+ hr = ad->audio_input.capture_client->GetBuffer(&data, &num_frames_available, &flags, NULL, NULL);
+ ERR_BREAK(hr != S_OK);
+
+ // fixme: Only works for floating point atm
+ for (int j = 0; j < num_frames_available; j++) {
+ int32_t l, r;
+
+ if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
+ l = r = 0;
+ } else {
+ if (ad->audio_input.channels == 2) {
+ l = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2);
+ r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2 + 1);
+ } else if (ad->audio_input.channels == 1) {
+ l = r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j);
+ } else {
+ l = r = 0;
+ ERR_PRINT("WASAPI: unsupported channel count in microphone!");
+ }
+ }
+
+ ad->input_buffer_write(l);
+ ad->input_buffer_write(r);
+ }
+
+ read_frames += num_frames_available;
+
+ hr = ad->audio_input.capture_client->ReleaseBuffer(num_frames_available);
+ ERR_BREAK(hr != S_OK);
+
+ hr = ad->audio_input.capture_client->GetNextPacketSize(&packet_length);
+ ERR_BREAK(hr != S_OK);
+ }
+ }
+
+ // If we're using the Default device and it changed finish it so we'll re-init the device
+ if (ad->audio_input.device_name == "Default" && default_capture_device_changed) {
+ Error err = ad->finish_capture_device();
+ if (err != OK) {
+ ERR_PRINT("WASAPI: finish_capture_device error");
+ }
+
+ default_capture_device_changed = false;
+ }
+
+ // User selected a new device, finish the current one so we'll init the new device
+ if (ad->audio_input.device_name != ad->audio_input.new_device) {
+ ad->audio_input.device_name = ad->audio_input.new_device;
+ Error err = ad->finish_capture_device();
+ if (err != OK) {
+ ERR_PRINT("WASAPI: finish_capture_device error");
+ }
+ }
+
+ if (!ad->audio_input.audio_client) {
+ Error err = ad->init_capture_device(true);
+ if (err == OK) {
+ ad->capture_start();
+ }
+ }
+ }
+
ad->stop_counting_ticks();
ad->unlock();
+
+ // Let the thread rest a while if we haven't read or write anything
+ if (written_frames == 0 && read_frames == 0) {
+ OS::get_singleton()->delay_usec(1000);
+ }
}
ad->thread_exited = true;
@@ -562,12 +748,12 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
void AudioDriverWASAPI::start() {
- if (audio_client) {
- HRESULT hr = audio_client->Start();
+ if (audio_output.audio_client) {
+ HRESULT hr = audio_output.audio_client->Start();
if (hr != S_OK) {
ERR_PRINT("WASAPI: Start failed");
} else {
- active = true;
+ audio_output.active = true;
}
}
}
@@ -594,7 +780,8 @@ void AudioDriverWASAPI::finish() {
thread = NULL;
}
- finish_device();
+ finish_capture_device();
+ finish_render_device();
if (mutex) {
memdelete(mutex);
@@ -602,30 +789,70 @@ void AudioDriverWASAPI::finish() {
}
}
+Error AudioDriverWASAPI::capture_start() {
+
+ Error err = init_capture_device();
+ if (err != OK) {
+ ERR_PRINT("WASAPI: init_capture_device error");
+ return err;
+ }
+
+ if (audio_input.active == false) {
+ audio_input.audio_client->Start();
+ audio_input.active = true;
+
+ return OK;
+ }
+
+ return FAILED;
+}
+
+Error AudioDriverWASAPI::capture_stop() {
+
+ if (audio_input.active == true) {
+ audio_input.audio_client->Stop();
+ audio_input.active = false;
+
+ return OK;
+ }
+
+ return FAILED;
+}
+
+void AudioDriverWASAPI::capture_set_device(const String &p_name) {
+
+ lock();
+ audio_input.new_device = p_name;
+ unlock();
+}
+
+Array AudioDriverWASAPI::capture_get_device_list() {
+
+ return audio_device_get_list(true);
+}
+
+String AudioDriverWASAPI::capture_get_device() {
+
+ lock();
+ String name = audio_input.device_name;
+ unlock();
+
+ return name;
+}
+
AudioDriverWASAPI::AudioDriverWASAPI() {
- audio_client = NULL;
- render_client = NULL;
mutex = NULL;
thread = NULL;
- format_tag = 0;
- bits_per_sample = 0;
-
samples_in.clear();
- buffer_size = 0;
channels = 0;
- wasapi_channels = 0;
mix_rate = 0;
buffer_frames = 0;
thread_exited = false;
exit_thread = false;
- active = false;
-
- device_name = "Default";
- new_device = "Default";
}
#endif
diff --git a/drivers/wasapi/audio_driver_wasapi.h b/drivers/wasapi/audio_driver_wasapi.h
index f3ee5976eb..3d94f3ba49 100644
--- a/drivers/wasapi/audio_driver_wasapi.h
+++ b/drivers/wasapi/audio_driver_wasapi.h
@@ -43,35 +43,63 @@
class AudioDriverWASAPI : public AudioDriver {
- HANDLE event;
- IAudioClient *audio_client;
- IAudioRenderClient *render_client;
+ class AudioDeviceWASAPI {
+ public:
+ IAudioClient *audio_client;
+ IAudioRenderClient *render_client;
+ IAudioCaptureClient *capture_client;
+ bool active;
+
+ WORD format_tag;
+ WORD bits_per_sample;
+ unsigned int channels;
+ unsigned int frame_size;
+
+ String device_name;
+ String new_device;
+
+ AudioDeviceWASAPI() {
+ audio_client = NULL;
+ render_client = NULL;
+ capture_client = NULL;
+ active = false;
+ format_tag = 0;
+ bits_per_sample = 0;
+ channels = 0;
+ frame_size = 0;
+ device_name = "Default";
+ new_device = "Default";
+ }
+ };
+
+ AudioDeviceWASAPI audio_input;
+ AudioDeviceWASAPI audio_output;
+
Mutex *mutex;
Thread *thread;
- String device_name;
- String new_device;
-
- WORD format_tag;
- WORD bits_per_sample;
-
Vector<int32_t> samples_in;
- unsigned int buffer_size;
unsigned int channels;
- unsigned int wasapi_channels;
int mix_rate;
int buffer_frames;
bool thread_exited;
mutable bool exit_thread;
- bool active;
- _FORCE_INLINE_ void write_sample(AudioDriverWASAPI *ad, BYTE *buffer, int i, int32_t sample);
+ static _FORCE_INLINE_ void write_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i, int32_t sample);
+ static _FORCE_INLINE_ int32_t read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i);
static void thread_func(void *p_udata);
- Error init_device(bool reinit = false);
- Error finish_device();
+ Error init_render_device(bool reinit = false);
+ Error init_capture_device(bool reinit = false);
+
+ Error finish_render_device();
+ Error finish_capture_device();
+
+ Error audio_device_init(AudioDeviceWASAPI *p_device, bool p_capture, bool reinit);
+ Error audio_device_finish(AudioDeviceWASAPI *p_device);
+ Array audio_device_get_list(bool p_capture);
public:
virtual const char *get_name() const {
@@ -89,6 +117,12 @@ public:
virtual void unlock();
virtual void finish();
+ virtual Error capture_start();
+ virtual Error capture_stop();
+ virtual Array capture_get_device_list();
+ virtual void capture_set_device(const String &p_name);
+ virtual String capture_get_device();
+
AudioDriverWASAPI();
};